一、WordPress简介

WordPress是使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站。也可以把WordPres当作一个内容管理系统(CMS)来使用。

WordPress是一款个人博客系统,并逐步演化成一款内容管理系统软件,它是使用PHP语言和MySQL数据库开发的,用户可以在支持PHP和MySQL数据库的服务器上使用自己的博客。

WordPress有许多第三方开发的免费模板,安装方式简单易用。不过要做一个自己的模板,则需要你有一定的专业知识。比如你至少要懂的标准通用标记语言下的一个应用HTML代码、CSS、PHP等相关知识。

WordPress官方支持中文版,同时有爱好者开发的第三方中文语言包,如wopus中文语言包。WordPress拥有成千上万个各式插件和不计其数的主题模板样式。

二、创建StorageClass、Namespace

1、创建StorageClass

[root@cmp-k8s-dev-master01 ~]# mkdir kube-wordpress && cd kube-wordpress

[root@cmp-k8s-dev-master01 kube-wordpress]# vim kube-wordpress-storageclass.yaml

  1. kind:StorageClass
  2. apiVersion: storage.k8s.io/v1
  3. metadata:
  4. name: nfs-wordpress
  5. provisioner: kubernetes.io/no-provisioner
  6. volumeBindingMode:WaitForFirstConsumer

[root@cmp-k8s-dev-master01 kube-wordpress]# kubectl apply -f kube-wordpress-storageclass.yaml

2、创建Namespace

[root@cmp-k8s-dev-master01 kube-wordpress]# vim kube-wordpress-namespace.yaml

  1. apiVersion: v1
  2. kind:Namespace
  3. metadata:
  4. name: kube-wordpress
  5. labels:
  6. app: wordpress

[root@cmp-k8s-dev-master01 kube-wordpress]# kubectl apply -f kube-wordpress-namespace.yaml

三、部署Mysql

1、创建Mysql的PV

注意:这里采用的是NFS,前提需要部署NFS

[root@cmp-k8s-dev-master01 kube-wordpress]# showmount -e 10.10.50.126

Export list for 10.10.50.126:

/data/mysql 10.10.50.0/24

/data/wordpress 10.10.50.0/24

[root@cmp-k8s-dev-master01 kube-wordpress]# vim kube-wordpress-mysql-pv.yaml

  1. apiVersion: v1
  2. kind:PersistentVolume
  3. metadata:
  4. name: mysql-pv
  5. labels:
  6. name: mysql-pv
  7. spec:
  8. capacity:
  9. storage:10Gi
  10. accessModes:
  11. -ReadWriteMany
  12. persistentVolumeReclaimPolicy:Retain
  13. storageClassName: nfs-wordpress
  14. nfs:
  15. path:/data/mysql
  16. server:10.10.50.126

[root@cmp-k8s-dev-master01 kube-wordpress]# kubectl apply -f kube-wordpress-mysql-pv.yaml

persistentvolume/mysql-pv created

2、创建Mysql的PVC

[root@cmp-k8s-dev-master01 kube-wordpress]# vim kube-wordpress-mysql-pvc.yaml

  1. apiVersion: v1
  2. kind:PersistentVolumeClaim
  3. metadata:
  4. name: mysql-pvc
  5. namespace: kube-wordpress
  6. spec:
  7. accessModes:
  8. -ReadWriteMany
  9. resources:
  10. requests:
  11. storage:10Gi
  12. storageClassName: nfs-wordpress

[root@cmp-k8s-dev-master01 kube-wordpress]# kubectl apply -f kube-wordpress-mysql-pvc.yaml

persistentvolumeclaim/mysql-pvc created

3、创建Mysql的Deployment

[root@cmp-k8s-dev-master01 kube-wordpress]# vim kube-wordpress-mysql-deploy.yaml

  1. apiVersion: apps/v1
  2. kind:Deployment
  3. metadata:
  4. labels:
  5. app: mysql
  6. name: mysql
  7. namespace: kube-wordpress
  8. spec:
  9. replicas:1
  10. revisionHistoryLimit:10
  11. selector:
  12. matchLabels:
  13. app: mysql
  14. template:
  15. metadata:
  16. labels:
  17. app: mysql
  18. spec:
  19. containers:
  20. - name: mysql
  21. image: mysql:5.7
  22. imagePullPolicy:IfNotPresent
  23. args:
  24. ---default_authentication_plugin=mysql_native_password
  25. ---character-set-server=utf8mb4
  26. ---collation-server=utf8mb4_unicode_ci
  27. ports:
  28. - name: mysql-port
  29. containerPort:3306
  30. env:
  31. - name: MYSQL_ROOT_PASSWORD
  32. value: sX6aXuSYSDpCr5Qt
  33. - name: MYSQL_DATABASE
  34. value: wordpress
  35. - name: MYSQL_USER
  36. value: wordpress
  37. - name: MYSQL_PASSWORD
  38. value: wordpress
  39. volumeMounts:
  40. - name: mysql-dir
  41. mountPath:/var/lib/mysql
  42. volumes:
  43. - name: mysql-dir
  44. persistentVolumeClaim:
  45. claimName: mysql-pvc

[root@cmp-k8s-dev-master01 kube-wordpress]# kubectl apply -f kube-wordpress-mysql-deploy.yaml

deployment.apps/mysql created

4、创建Mysql的Service

[root@cmp-k8s-dev-master01 kube-wordpress]# vim kube-wordpress-mysql-svc.yaml

  1. apiVersion: v1
  2. kind:Service
  3. metadata:
  4. name: wordpress-mysql
  5. namespace: kube-wordpress
  6. labels:
  7. app: wordpress
  8. spec:
  9. ports:
  10. - port:3306
  11. targetPort:3306
  12. selector:
  13. app: mysql

[root@cmp-k8s-dev-master01 kube-wordpress]# kubectl apply -f kube-wordpress-mysql-svc.yaml

service/wordpress-mysql created

四、部署WordPress

1、创建WordPress的Deployment

[root@cmp-k8s-dev-master01 kube-wordpress]# vim kube-wordpress-deploy.yaml

  1. apiVersion: apps/v1
  2. kind:Deployment
  3. metadata:
  4. name: wordpress
  5. namespace: kube-wordpress
  6. labels:
  7. app: wordpress
  8. spec:
  9. replicas:3
  10. selector:
  11. matchLabels:
  12. app: wordpress
  13. template:
  14. metadata:
  15. labels:
  16. app: wordpress
  17. spec:
  18. containers:
  19. - name: wordpress
  20. image: wordpress:latest
  21. imagePullPolicy:IfNotPresent
  22. ports:
  23. - containerPort:80
  24. env:
  25. - name: WORDPRESS_DB_HOST
  26. value: wordpress-mysql:3306
  27. - name: WORDPRESS_DB_USER
  28. value: wordpress
  29. - name: WORDPRESS_DB_PASSWORD
  30. value: wordpress

[root@cmp-k8s-dev-master01 kube-wordpress]# kubectl apply -f kube-wordpress-deploy.yaml

deployment.apps/wordpress created

2、创建WordPress的Service

[root@cmp-k8s-dev-master01 kube-wordpress]# vim kube-wordpress-svc.yaml

  1. apiVersion: v1
  2. kind:Service
  3. metadata:
  4. labels:
  5. app: wordpress
  6. name: wordpress-service
  7. namespace: kube-wordpress
  8. spec:
  9. ports:
  10. - name: wordpress
  11. port:80
  12. protocol: TCP
  13. targetPort:80
  14. nodePort:30088
  15. selector:
  16. app: wordpress
  17. type:NodePort

[root@cmp-k8s-dev-master01 kube-wordpress]# kubectl apply -f kube-wordpress-svc.yaml

service/wordpress-service created

3、kube-wordpress列表清单

[root@cmp-k8s-dev-master01 kube-wordpress]# kubectl get all -n kube-wordpress

NAME READY   STATUS              RESTARTS   AGE

pod/mysql-65479dd978-qg7z6 1/1     Running             0          20m

pod/wordpress-d857c8dcf-42f9p 1/1     Running             0          10m

pod/wordpress-d857c8dcf-8mdws 1/1     Running             0          10m

pod/wordpress-d857c8dcf-v4c7s 1/1     Running             0          10m

NAME TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE

service/wordpress-mysql ClusterIP   172.10.230.153   <none>        3306/TCP       3m12s

service/wordpress-service NodePort   172.10.12.234   <none>        80:30088/TCP   2m38s

NAME READY   UP-TO-DATE   AVAILABLE   AGE

deployment.apps/mysql 1/1     1            1           46m

deployment.apps/wordpress 3/3     3            3           10m

NAME DESIRED   CURRENT   READY   AGE

replicaset.apps/mysql-65479dd978 1         1         1       20m

replicaset.apps/wordpress-d857c8dcf 3         3         3       10m

4、访问Wordpress

1)浏览器输入http://节点IP:30088,出现以下界面

Kubernetes部署WordPress_mysql

2)输入对应信息,点击安装WordPress

Kubernetes部署WordPress_mysql_02

Kubernetes部署WordPress_vim_03

3)输入前面创建用户名和密码

Kubernetes部署WordPress_mysql_04

Kubernetes部署WordPress_mysql_05

至此,K8s搭建WordPress完毕。