关键要点包括:
- 应用封装与管理:WordPress 和 MySQL 被封装成 Pod,运行所需环境变量保存在 ConfigMap 中,采用"声明式"管理,便于阅读和版本化管理。
- MySQL YAML 定义:需定义 MySQL configmap(mysql - cm.yaml),包含 4 个环境变量;还需定义 MySQL Pod(mysql - pod.yaml),并从 configmap 引用环境变量。
- WordPress YAML 定义:定义 WordPress Configmap(wp - cm.yaml),包含数据库相关信息;定义 WordPress Pod(wp - pod.yaml),同样从 configmap 引用环境变量。
- 部署操作:使用 kubectl apply - f 部署 MySQL 和 WordPress。
- 访问 WordPress:通过 kubectl get pods - o wide 查看 Pod IP 地址,使用 kubectl port - forward --address [0.0.0.0](0.0.0.0) wp - pod 8080:80 从外部访问 Pod。
- Configmap 引用优化:可使用 envFrom 从 configmap 批量引用环境变量,并对 configmap 的数据结构进行调整。
一、WordPress 网站搭建
1.定义MySQL YAML
[root@localhost 07-wordpress-deploy]# cat mysql-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysql-pod
labels:
app: wordpress
role: database
spec:
containers:
- name: wp-mysql
image: registry.cn-beijing.aliyuncs.com/xxhf/mysql:8.0
ports:
- containerPort: 3306
env:
- name: MYSQL_DATABASE
value: "db_wordpress"
- name: MYSQL_USER
value: "wordpress"
- name: MYSQL_PASSWORD
value: "123456"
- name: MYSQL_ROOT_PASSWORD
value: "123456"
[root@localhost 07-wordpress-deploy]# kubectl apply -f mysql-pod.yaml
pod/mysql-pod created
[root@localhost 07-wordpress-deploy]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-pod 1/1 Running 0 3m34s 172.17.219.83 master <none> <none>
注:mysql容器的ip地址172.17.219.83是要同步给wordpress项目的
2.定义 WordPress YAML
[root@localhost 07-wordpress-deploy]# cat wp-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: wp-pod
labels:
app: wordpress
role: website
spec:
containers:
- name: wp-pod
image: registry.cn-beijing.aliyuncs.com/xxhf/wordpress:php7.4
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
env:
- name: WORDPRESS_DB_HOST
value: "172.17.219.83"
- name: WORDPRESS_DB_USER
value: "wordpress"
- name: WORDPRESS_DB_PASSWORD
value: "123456"
- name: WORDPRESS_DB_NAME
value: "db_wordpress"
[root@localhost 07-wordpress-deploy]# kubectl apply -f wp-pod.yaml
pod/wp-pod created
3.访问 WordPress
从外部访问,需要临时开放一个映射端口:
[root@localhost 07-wordpress-deploy]# kubectl port-forward --address 0.0.0.0 wp-pod 8080:80

二、ConfigMap 存储数据
1.定义MySQL configmap
[root@localhost 07-wordpress-deploy]# cat 01-mysql-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-cm
data:
MYSQL_DATABASE: 'db_wordpress'
MYSQL_USER: 'wordpress'
MYSQL_PASSWORD: '123456'
MYSQL_ROOT_PASSWORD: '123456'
挂载到mysql-pod容器当中:
[root@localhost 07-wordpress-deploy]# cat 01-mysql-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysql-pod
labels:
app: wordpress
role: database
spec:
containers:
- name: wp-mysql
image: registry.cn-beijing.aliyuncs.com/xxhf/mysql:8.0
#image: mysql:8.0
#imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
env:
- name: MYSQL_DATABASE
valueFrom:
configMapKeyRef:
name: mysql-cm
key: MYSQL_DATABASE
- name: MYSQL_USER
valueFrom:
configMapKeyRef:
name: mysql-cm
key: MYSQL_USER
- name: MYSQL_PASSWORD
valueFrom:
configMapKeyRef:
name: mysql-cm
key: MYSQL_PASSWORD
- name: MYSQL_ROOT_PASSWORD
valueFrom:
configMapKeyRef:
name: mysql-cm
key: MYSQL_ROOT_PASSWORD
启动容器服务:
[root@localhost 07-wordpress-deploy]# kubectl apply -f 01-mysql-cm.yaml
configmap/mysql-cm unchanged
[root@localhost 07-wordpress-deploy]# kubectl apply -f 01-mysql-pod.yaml
pod/mysql-pod created
[root@localhost 07-wordpress-deploy]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-pod 1/1 Running 0 8s 172.17.219.85 master <none> <none>
2.定义 WordPress Configmap
[root@localhost 07-wordpress-deploy]# cat 01-wp-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: wp-cm
data:
WORDPRESS_DB_HOST: '172.17.219.85'
WORDPRESS_DB_USER: 'wordpress'
WORDPRESS_DB_PASSWORD: '123456'
WORDPRESS_DB_NAME: 'db_wordpress'
挂载到wp-pod容器当中:
[root@localhost 07-wordpress-deploy]# cat 01-wp-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: wp-pod
labels:
app: wordpress
role: website
spec:
containers:
- name: wp-pod
image: registry.cn-beijing.aliyuncs.com/xxhf/wordpress:php7.4
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
env:
- name: WORDPRESS_DB_HOST
valueFrom:
configMapKeyRef:
name: wp-cm
key: WORDPRESS_DB_HOST
- name: WORDPRESS_DB_USER
valueFrom:
configMapKeyRef:
name: wp-cm
key: WORDPRESS_DB_USER
- name: WORDPRESS_DB_PASSWORD
valueFrom:
configMapKeyRef:
name: wp-cm
key: WORDPRESS_DB_PASSWORD
- name: WORDPRESS_DB_NAME
valueFrom:
configMapKeyRef:
name: wp-cm
key: WORDPRESS_DB_NAME
启动容器服务:
[root@localhost 07-wordpress-deploy]# kubectl apply -f 01-wp-cm.yaml
configmap/wp-cm configured
[root@localhost 07-wordpress-deploy]# kubectl apply -f 01-wp-pod.yaml
pod/wp-pod created
[root@localhost 07-wordpress-deploy]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-pod 1/1 Running 0 4m42s 172.17.219.85 master <none> <none>
wp-pod 1/1 Running 0 51s 172.17.219.86 master <none> <none>
3.访问 WordPress
[root@localhost 07-wordpress-deploy]# kubectl port-forward --address 0.0.0.0 wp-pod 8080:80

4.优化 configmap 引用
针对mysql-cm的优化:
[root@localhost 07-wordpress-deploy]# cat 02-mysql-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-cm
data:
DATABASE: 'db_wordpress'
USER: 'wordpress'
PASSWORD: '123456'
ROOT_PASSWORD: '123456'
[root@localhost 07-wordpress-deploy]# cat 02-mysql-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-cm
data:
DATABASE: 'db_wordpress'
USER: 'wordpress'
PASSWORD: '123456'
ROOT_PASSWORD: '123456'
[root@localhost 07-wordpress-deploy]# cat 02-mysql-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysql-pod
labels:
app: wordpress
role: database
spec:
containers:
- name: wp-mysql
image: registry.cn-beijing.aliyuncs.com/xxhf/mysql:8.0
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
envFrom:
- prefix: 'MYSQL_' # 前缀
configMapRef:
name: mysql-cm
针对wp-cm的优化:
[root@localhost 07-wordpress-deploy]# cat 02-wp-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: wp-cm
data:
HOST: '10.244.171.6'
USER: 'wordpress'
PASSWORD: '123456'
NAME: 'db_wordpress'
[root@localhost 07-wordpress-deploy]# cat 02-wp-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: wp-pod
labels:
app: wordpress
role: website
spec:
containers:
- name: wp-pod
image: registry.cn-beijing.aliyuncs.com/xxhf/wordpress:php7.4
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
envFrom:
- prefix: 'WORDPRESS_DB_'
configMapRef:
name: wp-cm
三、部署k8s集群服务

- 部署 MySQL:先用 ConfigMap 定义数据库环境变量,再将 MySQL 以 Deployment 方式部署,replicas 设为 1,最后创建 Service 对象映射端口 3306,可将这三个对象写在一个 YAML 文件中一次性创建。
- 部署 WordPress:ConfigMap 配置中"HOST"用 MySQL 的 Service 名字,Deployment 的 replicas 设为 2,创建 NodePort 类型的 Service 并指定端口 30080,可在集群各节点访问服务。
- 部署 Ingress Controller:部署 Nginx Ingress Controller,使用课上部署的 nginx ingress controller,为 WordPress 创建 Ingress 对象,给 ingress - nginx - controller deployment 添加 hostNetwork: true 属性让 Pod 使用宿主机网络。
1.部署MySQL
YAML配置文件集合:
# 先要用 ConfigMap 定义数据库的环境变量,有 DATABASE、USER、PASSWORD、ROOT_PASSWORD
[root@localhost 11-wordpress-deploy]# cat 01-mysql-dep.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-cm
data:
DATABASE: 'db_wordpress'
USER: 'wordpress'
PASSWORD: '123456'
ROOT_PASSWORD: '123456'
---
# 把 MySQL 由 Pod 改成 Deployment 的方式,replicas 设置成 1 个,template 里面的 Pod 部分没有任何变化,还是要用 envFrom把配置信息以环境变量的形式注入 Pod
apiVersion: apps/v1
kind: Deployment
metadata:
name: wp-mysql
labels:
app: wordpress
role: database
spec:
replicas: 1
minReadySeconds: 0
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
selector:
matchLabels:
app: wp-mysql
template:
metadata:
labels:
app: wp-mysql
spec:
containers:
- name: wp-mysql
image: registry.cn-beijing.aliyuncs.com/xxhf/mysql:8.0
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: 1
memory: 2048Mi
requests:
cpu: 500m
memory: 500Mi
startupProbe:
periodSeconds: 15
tcpSocket:
port: 3306
livenessProbe:
periodSeconds: 10
tcpSocket:
port: 3306
readinessProbe:
periodSeconds: 15
tcpSocket:
port: 3306
ports:
- containerPort: 3306
envFrom:
- prefix: 'MYSQL_'
configMapRef:
name: mysql-cm
---
# 为 MySQL 定义一个 Service 对象,映射端口 3306,让其他应用不再关心 IP 地址,直接用 Service 对象的名字来访问数据库服务
apiVersion: v1
kind: Service
metadata:
name: wp-mysql-svc
labels:
spec:
selector:
app: wp-mysql
ports:
- port: 3306
targetPort: 3306
protocol: TCP
name: mysql-svc
创建mysql服务:
[root@localhost 11-wordpress-deploy]# kubectl apply -f 01-mysql-dep.yaml
configmap/mysql-cm configured
deployment.apps/wp-mysql created
service/wp-mysql-svc created
[root@localhost 11-wordpress-deploy]# kubectl get cm
NAME DATA AGE
mysql-cm 4 4d14h
[root@localhost 11-wordpress-deploy]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wp-mysql-svc ClusterIP 192.168.111.220 <none> 3306/TCP 28s
[root@localhost 11-wordpress-deploy]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
wp-mysql 1/1 1 1 46s
[root@localhost 11-wordpress-deploy]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
wp-mysql-7566f55d5f-kkjgw 1/1 Running 0 58s 172.17.171.109 worker <none> <none>
2.部署 WordPress
YAML配置文件集合:
[root@localhost 11-wordpress-deploy]# cat 02-wp-dep.yaml
# 因为刚才创建了 MySQL 的 Service,所以在写 ConfigMap 配置的时候"HOST"就不应该是 IP 地址了,而应该是 DNS 域名,也就是 Service 的名字 wp-mysql-svc
apiVersion: v1
kind: ConfigMap
metadata:
name: wp-cm
data:
HOST: 'wp-mysql-svc.default.svc.cluster.local'
USER: 'wordpress'
PASSWORD: '123456'
NAME: 'db_wordpress'
---
# Pod 套一个 Deployment 的"外壳",replicas 设置成 2 个,用字段"envFrom"配置环境变量
apiVersion: apps/v1
kind: Deployment
metadata:
name: wp-dep
labels:
app: wp-dep
spec:
replicas: 2
minReadySeconds: 30
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
selector:
matchLabels:
app: wp-dep
template:
metadata:
labels:
app: wp-dep
spec:
containers:
- name: wordpress
image: registry.cn-beijing.aliyuncs.com/xxhf/wordpress:php7.4
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: 1
memory: 1024Mi
requests:
cpu: 200m
memory: 500Mi
startupProbe:
periodSeconds: 5
tcpSocket:
port: 80
livenessProbe:
periodSeconds: 10
tcpSocket:
port: 80
readinessProbe:
periodSeconds: 15
tcpSocket:
port: 80
ports:
- containerPort: 80
envFrom:
- prefix: 'WORDPRESS_DB_'
configMapRef:
name: wp-cm
---
# 为 WordPress 创建 Service 对象,这里使用了"NodePort"类型,并且手工指定了端口号"30080"(必须在 30000~32767 之间)
apiVersion: v1
kind: Service
metadata:
name: wp-dep-svc
labels:
spec:
selector:
app: wp-dep
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
nodePort: 30080
name: http
注:因为 WordPress 的 Service 对象是 NodePort 类型的,我们可以在集群的每个节点上访问 WordPress 服务
创建wordpress服务:
[root@localhost 11-wordpress-deploy]# kubectl apply -f 02-wp-dep.yaml
configmap/wp-cm configured
deployment.apps/wp-dep created
service/wp-dep-svc created
[root@localhost 11-wordpress-deploy]# kubectl get cm
NAME DATA AGE
mysql-cm 4 4d14h
wp-cm 4 4d14h
[root@localhost 11-wordpress-deploy]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wp-dep-svc NodePort 192.168.111.210 <none> 80:32720/TCP 6s
wp-mysql-svc ClusterIP 192.168.111.119 <none> 3306/TCP 3m20s
[root@localhost 11-wordpress-deploy]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
wp-dep 0/1 1 0 18s
wp-mysql 1/1 1 1 8m42s
3.部署 Ingress Controller
注:这里的控制器可以选择之前部署的ingress-nginx-controller,通过这个控制器处理我们接下来要配置的ingress规则
[root@localhost 11-wordpress-deploy]# kubectl -n ingress-nginx get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
ingress-nginx-controller 1/1 1 1 153m
配置ingress规则:
[root@localhost 11-wordpress-deploy]# cat 03-wp-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: wp-ingress
spec:
ingressClassName: nginx
rules:
- host: wp.xxhf.cc
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: wp-dep-svc
port:
number: 80
[root@localhost 11-wordpress-deploy]# kubectl apply -f 03-wp-ingress.yaml
ingress.networking.k8s.io/wp-ingress created

在本机hosts配置文件中写入:
192.168.5.120 wp.xxhf.cc
访问浏览器:

注意:给 ingress-nginx-controller deployment 添加 hostNetwork: true 属性,可以让 Pod 能够使用宿主机的网络
四、StatefulSet 数据持久化

1.持久化MySQL存储
(1)部署Provisioner 对象
部署RBAC权限:
[root@localhost 14-wordpress-deploy]# cat rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: kube-system
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-client-provisioner-runner
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "update"]
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["events"]
verbs: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: run-nfs-client-provisioner
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: kube-system
roleRef:
kind: ClusterRole
name: nfs-client-provisioner-runner
apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: kube-system
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: kube-system
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: kube-system
roleRef:
kind: Role
name: leader-locking-nfs-client-provisioner
apiGroup: rbac.authorization.k8s.io
[root@localhost 14-wordpress-deploy]# kubectl apply -f rbac.yaml
serviceaccount/nfs-client-provisioner created
clusterrole.rbac.authorization.k8s.io/nfs-client-provisioner-runner created
clusterrolebinding.rbac.authorization.k8s.io/run-nfs-client-provisioner created
role.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner created
rolebinding.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner created
部署Provisioner 对象:
[root@localhost 14-wordpress-deploy]# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-client-provisioner
labels:
app: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: kube-system
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: nfs-client-provisioner
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: registry.cn-beijing.aliyuncs.com/xxhf/nfs-subdir-external-provisioner:v4.0.2
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: k8s-sigs.io/nfs-subdir-external-provisioner
- name: NFS_SERVER
value: 192.168.5.120 # nfs server address
- name: NFS_PATH
value: /data/nfs # nfs share directory
volumes:
- name: nfs-client-root
nfs:
server: 192.168.5.120
path: /data/nfs
[root@localhost 14-wordpress-deploy]# kubectl apply -f deployment.yaml
[root@localhost 14-wordpress-deploy]# kubectl -n kube-system get pod
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-d7c6585cb-j5tbz 1/1 Running 0 3m
注:接下来部署StorageClass对象,绑定一个 Provisioner 对象,自动管理存储、创建 PV
(2)部署StorageClass对象
[root@localhost 14-wordpress-deploy]# cat nfs-client-retained-sc.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-client-retained
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner
parameters:
onDelete: "retain"
[root@localhost 14-wordpress-deploy]# kubectl apply -f nfs-client-retained-sc.yaml
storageclass.storage.k8s.io/nfs-client-retained created
[root@localhost 14-wordpress-deploy]# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs-client-retained k8s-sigs.io/nfs-subdir-external-provisioner Delete Immediate false 56s

注:这里我用的是网络存储,具体情况可以看我之前的博客持久化存储
(3)部署StatefulSet
注:因为 MySQL 由 Deployment 改成了 StatefulSet,所以我们要修改 YAML,添加"serviceName""volumeClaimTemplates"这两个字段,定义网络标识和 NFS 动态存储卷,然后在容器部分用"volumeMounts"挂载到容器里的数据目录"/var/lib/mysql"
[root@localhost 14-wordpress-deploy]# cat mysql-dep.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-cm
data:
DATABASE: 'db_wordpress'
USER: 'wordpress'
PASSWORD: '123456'
ROOT_PASSWORD: '123456'
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: wp-mysql
labels:
app: wordpress
role: database
spec:
serviceName: wp-mysql-svc
volumeClaimTemplates: # 这里可以直接创建pvc
- metadata:
name: wp-mysql-pvc
spec:
storageClassName: nfs-client-retained # 指定StorageClass
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Mi
replicas: 1
minReadySeconds: 30
selector:
matchLabels:
app: wp-mysql
template:
metadata:
labels:
app: wp-mysql
spec:
containers:
- name: wp-mysql
image: registry.cn-beijing.aliyuncs.com/xxhf/mysql:8.0
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: 1
memory: 2048Mi
requests:
cpu: 500m
memory: 500Mi
startupProbe:
periodSeconds: 15
tcpSocket:
port: 3306
livenessProbe:
periodSeconds: 10
tcpSocket:
port: 3306
readinessProbe:
periodSeconds: 15
tcpSocket:
port: 3306
ports:
- containerPort: 3306
envFrom:
- prefix: 'MYSQL_'
configMapRef:
name: mysql-cm
volumeMounts:
- name: wp-mysql-pvc
mountPath: /var/lib/mysql
---
apiVersion: v1
kind: Service
metadata:
name: wp-mysql-svc
labels:
spec:
selector:
app: wp-mysql
ports:
- port: 3306
targetPort: 3306
protocol: TCP
name: mysql-svc
[root@localhost 14-wordpress-deploy]# kubectl apply -f mysql-dep.yaml
configmap/mysql-cm created
statefulset.apps/wp-mysql created
service/wp-mysql-svc created
2.部署 WordPress
注:StatefulSet 管理的每个 Pod 都有自己的域名,所以要把 WordPress 的环境变量改成 MySQL 的新名字,也就是"wp-mysql-0.wp-mysql-svc"
[root@localhost 14-wordpress-deploy]# cat wp-dep.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: wp-cm
data:
HOST: 'wp-mysql-0.wp-mysql-svc'
USER: 'wordpress'
PASSWORD: '123456'
NAME: 'db_wordpress'
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wp-dep
labels:
app: wp-dep
spec:
replicas: 2
minReadySeconds: 30
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
selector:
matchLabels:
app: wp-dep
template:
metadata:
labels:
app: wp-dep
spec:
containers:
- name: wordpress
image: registry.cn-beijing.aliyuncs.com/xxhf/wordpress:php7.4
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: 1
memory: 1024Mi
requests:
cpu: 200m
memory: 500Mi
startupProbe:
periodSeconds: 5
tcpSocket:
port: 80
livenessProbe:
periodSeconds: 10
tcpSocket:
port: 80
readinessProbe:
periodSeconds: 15
tcpSocket:
port: 80
ports:
- containerPort: 80
envFrom:
- prefix: 'WORDPRESS_DB_'
configMapRef:
name: wp-cm
---
apiVersion: v1
kind: Service
metadata:
name: wp-dep-svc
labels:
spec:
selector:
app: wp-dep
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
name: http
[root@localhost 14-wordpress-deploy]# kubectl apply -f wp-dep.yaml
3.访问 WordPress
注:ingress七层负载直接用之前的就好了,因为ingress规则匹配的是svc服务向外暴露的域名wp-dep-svc,这个域名是固定不变的


注:因为启用了两个wordpress容器,svc负责负载均衡调度,如果两个容器(在不同的worker节点)间没有使用使用共享文件存储,则很大可能会出现登录循环的问题
4.容器错误演示
当前启动的容器:
[root@localhost 14-wordpress-deploy]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
wp-dep-7c48f5744-4wwfl 1/1 Running 0 68m 172.17.189.109 worker2 <none> <none>
wp-dep-7c48f5744-hkndc 1/1 Running 0 68m 172.17.171.120 worker <none> <none>
wp-mysql-0 1/1 Running 0 4m17s 172.17.171.64 worker <none> <none>
手动删除mysql容器,等待它再次重建:
[root@localhost 14-wordpress-deploy]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
wp-dep-7c48f5744-4wwfl 1/1 Running 0 68m 172.17.189.109 worker2 <none> <none>
wp-dep-7c48f5744-hkndc 1/1 Running 0 68m 172.17.171.120 worker <none> <none>
wp-mysql-0 1/1 Running 0 4m17s 172.17.171.64 worker <none> <none>
[root@localhost 14-wordpress-deploy]# kubectl delete pod wp-mysql-0
pod "wp-mysql-0" deleted
[root@localhost 14-wordpress-deploy]# kubectl get pod
NAME READY STATUS RESTARTS AGE
wp-dep-7c48f5744-4wwfl 1/1 Running 0 76m
wp-dep-7c48f5744-hkndc 1/1 Running 0 76m
wp-mysql-0 0/1 Running 0 4s
重启之后数据不丢失,依旧可以登录网页:

