K8S中部署wordpress

省略了storageclass的部署过程。详情参考 Storageclass 外挂NFS配置与应用

1.创建名称空间

kubectl create ns wordpress

2.部署数据库

2.1 创建pvc


使用pvc,申请一个pv;从storageClassName: managed-nfs-storage中获取。pv的名称为


cat 0-mysql-pvc.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
namespace: wordpress
spec:
storageClassName: managed-nfs-storage
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi

2.2 创建mysql POD

cat 1-mysql.yaml 
# 创建pod
apiVersion: v1
kind: Pod
metadata:
name: wp-mysql
namespace: wordpress
labels:
app: wp-mysql
spec:
containers:
- name: mysql
image: mysql:5.7
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
name: dbport
env:
- name: MYSQL_ROOT_PASSWORD
value: gong.123
- name: MYSQL_DATABASE
value: wordpress
- name: MYSQL_USER
value: wordpress
- name: MYSQL_PASSWORD
value: wordpress
volumeMounts:
- name: db
mountPath: /var/lib/mysql
volumes:
- name: db
persistentVolumeClaim:
claimName: mysql-pv-claim

2.3 暴露mysql的svc

cat 2-mysql-svc.yaml 
apiVersion: v1
kind: Service
metadata:
labels:
app: wp-mysql
name: wp-mysql-svc
namespace: wordpress
spec:
ports:
- port: 3306
protocol: TCP
targetPort: 3306
selector:
app: wp-mysql

3.部署wordpress

3.1 创建pvc

cat wp-storage.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wp-storage
namespace: wordpress
spec:
storageClassName: managed-nfs-storage
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi

3.2 创建deployment资源的wordpress

cat wordpress-dp.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
namespace: wordpress
spec:
selector:
matchLabels:
app: wordpress
replicas: 1
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: wordpress
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
env:
- name: WORDPRESS_DB_HOST
value: wp-mysql-svc:3306
- name: WORDPRESS_DB_USER
value: wordpress
- name: WORDPRESS_DB_PASSWORD
value: wordpress
volumeMounts:
- name: wp-storage
mountPath: /var/www/html
volumes:
- name: wp-storage
persistentVolumeClaim:
claimName: wp-storage

3.3 暴露wordpress端口

kubectl expose -n wordpress deployment wordpress --type=NodePort --port=8081 --target-port=80 --name=wp-svc