51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Kubernetes部署WordPress

一、WordPress简介

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

WordPress是一款个人博客系统,并逐步演化成一款内容管理系统软件,它是使用PHP语言和MySQL数据库开发的,用户可以在支持PHP和MySQL数据库的服务器上使用自己的博客。
文章源自小柒网-https://www.yangxingzhen.cn/8309.html

WordPress有许多第三方开发的免费模板,安装方式简单易用。不过要做一个自己的模板,则需要你有一定的专业知识。比如你至少要懂的标准通用标记语言下的一个应用HTML代码、CSS、PHP等相关知识。
文章源自小柒网-https://www.yangxingzhen.cn/8309.html

WordPress官方支持中文版,同时有爱好者开发的第三方中文语言包,如wopus中文语言包。WordPress拥有成千上万个各式插件和不计其数的主题模板样式。
文章源自小柒网-https://www.yangxingzhen.cn/8309.html

二、创建StorageClass、Namespace

1、创建StorageClass

[root@cmp-k8s-dev-master01 ~]# mkdir kube-wordpress && cd kube-wordpress
文章源自小柒网-https://www.yangxingzhen.cn/8309.html

[root@cmp-k8s-dev-master01 kube-wordpress]# vim kube-wordpress-storageclass.yaml文章源自小柒网-https://www.yangxingzhen.cn/8309.html

kind: StorageClass
apiVersion: storage.https://51tbox.com/.io/v1
metadata:
  name: nfs-wordpress
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

[root@cmp-k8s-dev-master01 kube-wordpress]# kubectl apply -f kube-wordpress-storageclass.yaml
文章源自小柒网-https://www.yangxingzhen.cn/8309.html

2、创建Namespace

[root@cmp-k8s-dev-master01 kube-wordpress]# vim kube-wordpress-namespace.yaml文章源自小柒网-https://www.yangxingzhen.cn/8309.html

apiVersion: v1
kind: Namespace
metadata:
  name: kube-wordpress
  labels:
    app: wordpress

[root@cmp-k8s-dev-master01 kube-wordpress]# kubectl apply -f kube-wordpress-namespace.yaml
文章源自小柒网-https://www.yangxingzhen.cn/8309.html

三、部署Mysql

1、创建Mysql的PV

注意:这里采用的是NFS,前提需要部署NFS文章源自小柒网-https://www.yangxingzhen.cn/8309.html

[root@cmp-k8s-dev-master01 kube-wordpress]# showmount -e 10.10.50.126文章源自小柒网-https://www.yangxingzhen.cn/8309.html

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

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
  labels:
    name: mysql-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
  - ReadWriteMany 
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs-wordpress
  nfs:
    path: /data/mysql
    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

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
  namespace: kube-wordpress
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  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

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mysql
  name: mysql
  namespace: kube-wordpress
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        imagePullPolicy: IfNotPresent
        args: 
        - --default_authentication_plugin=mysql_native_password
        - --character-set-server=utf8mb4
        - --collation-server=utf8mb4_unicode_ci
        ports:
        - name: mysql-port
          containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: sX6aXuSYSDpCr5Qt
        - name: MYSQL_DATABASE
          value: wordpress
        - name: MYSQL_USER
          value: wordpress
        - name: MYSQL_PASSWORD
          value: wordpress          
        volumeMounts:
        - name: mysql-dir
          mountPath: /var/lib/mysql
      volumes:
        - name: mysql-dir
          persistentVolumeClaim: 
            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

apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  namespace: kube-wordpress
  labels:
    app: wordpress
spec:
  ports:
  - port: 3306
    targetPort: 3306 
  selector:
    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

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  namespace: kube-wordpress
  labels:
    app: wordpress
spec:
  replicas: 3
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - name: wordpress
        image: wordpress:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        env:
        - name: WORDPRESS_DB_HOST
          value: wordpress-mysql:3306
        - name: WORDPRESS_DB_USER
          value: wordpress
        - name: WORDPRESS_DB_PASSWORD
          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

apiVersion: v1
kind: Service
metadata:
  labels:
    app: wordpress
  name: wordpress-service
  namespace: kube-wordpress   
spec:
  ports:
  - name: wordpress 
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30088
  selector:
    app: wordpress
  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,出现以下界面

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

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

至此,K8s搭建WordPress完毕。
继续阅读 Kubernetes最后更新:2023-5-14

赞(0)
未经允许不得转载:工具盒子 » Kubernetes部署WordPress