k8s部署wordpress+mysql博客平台
- 1、yaml文件准备
- 1.1 wordpress-db.yaml
- 1.2 wordpress.yaml
- 2、部署安装
- 2.1 先创建wordpress命名空间
- 2.2 部署wordpress-db
- 2.3部署wordpress
- 3、访问测试
1、yaml文件准备
1.1 wordpress-db.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: wordpress
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
name: dbport
env: #环境编排,进入数据库中创建Wordpress数据库和WordPress用户
- name: MYSQL_ROOT_PASSWORD
value: rootPassW0rd
- name: MYSQL_DATABASE
value: wordpress
- name: MYSQL_USER
value: wordpress
- name: MYSQL_PASSWORD
value: wordpress
volumeMounts:
- name: db
mountPath: /var/lib/mysql #mysql数据存放的容器目录
resources: {}
volumes:
- name: db
hostPath:
path: /var/lib/mysql # 容器目录中的数据映射到本地目录中
status: {}
---
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: wordpress
spec:
selector:
app: mysql
ports:
- name: mysqlport
protocol: TCP
port: 3306
targetPort: dbport
1.2 wordpress.yaml
注意需要修改文件中连接数据库的ip信息
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
namespace: wordpress
labels:
app: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: wordpress
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: wdport
env:
- name: WORDPRESS_DB_HOST
value: 10.108.244.58:3306 #连接数据库,这里的ip地址查看mysql启动时的ip
- name: WORDPRESS_DB_USER
value: wordpress
- name: WORDPRESS_DB_PASSWORD
value: wordpress
status: {}
---
apiVersion: v1
kind: Service
metadata:
name: wordpress
namespace: wordpress
spec:
type: NodePort
selector:
app: wordpress
ports:
- name: wordpressport
protocol: TCP
port: 80
targetPort: wdport
2、部署安装
2.1 先创建wordpress命名空间
kubectl create ns wordpress
2.2 部署wordpress-db
kubectl apply -f wordpress-db.yaml
kubectl get svc -n wordpress
2.3部署wordpress
先修改ordpress.yaml文件中的数据库连接信息,使用2.2中mysql服务的CLUSTER-IP地址
kubectl apply -f wordpress.yaml
kubectl get svc -n wordpress
可以看到wordpress对外暴露的端口号为30215,node节点是k8s-node1
3、访问测试
我这里用的node1节点http://192.168.2.102:30215/