如何在kubernetes安装WordPress

前置准备

首先需要一个用于部署WordPress的的kubernetes集群,部署手册可参考 kubeadm高可用集群部署 ,然后因为WordPress需要网页数据和数据库存储,需要用到持久卷(Persistent Volume), 这里为了部署方便,部署一个rancher的Local Path Provisioner ,可以在pod创建时动态创建符合要求的pv。

rancher local-path部署

kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/v0.0.24/deploy/local-path-storage.yaml

部署后

kubectl get sc

NAME                    PROVISIONER             RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
local-path              rancher.io/local-path   Delete          WaitForFirstConsumer   false                  22d

可以看到rancher local-path已经部署成功,这里需要记下这个storageclass的name 也就是local-path,后续pod的pvc需要指定storageclass。

部署博客

创建一个文件夹比如wordpress,

内部目录结构

.
├── mysql-deployment-and-service.yaml #MySQL配置
├── wordpress-deployment-and-service.yaml #WordPress程序部署
├── domain.yaml  #域名配置
├── kustomization.yaml #使用kustomization集中管理本博客安装删除

mysql-deployment-and-service.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deploy
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      containers:
        - image: mysql:5.7
          name: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-pass
                  key: password
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:
            - name: mysql-volume
              mountPath: /var/lib/mysql
      volumes:
        - name: mysql-volume
          persistentVolumeClaim:
            claimName: mysql-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: local-path #rancher local-path
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-deploy
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql

wordpress-deployment-and-service.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-deploy
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: frontend
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
      maxSurge: 1
  replicas: 1
  template:
    metadata:
      labels:
        app: wordpress
        tier: frontend
    spec:
      containers:
        - image: wordpress:5-php8.0-apache
          name: wordpress
          env:
            - name: WORDPRESS_DB_HOST
              value: mysql-deploy # 这里需要注意填写 mysql-service 中 metadata name 的值(使用一致的 DNS 名称而非 IP 地址来访问服务)
            - name: WORDPRESS_DB_NAME
              value: chenjie # 这个需要手动创建数据库
            - name: WORDPRESS_DB_USER
              value: root
            - name: WORDPRESS_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-pass
                  key: password
          ports:
            - containerPort: 80
              name: wordpress
          volumeMounts:
            - name: wordpress-volume
              mountPath: /var/www/html
      volumes:
        - name: wordpress-volume
          persistentVolumeClaim:
            claimName: wordpress-pvc
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wordpress-pvc
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: local-path #rancher local-path
---
apiVersion: v1
kind: Service
metadata:
  name: wordpress-deploy
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
    tier: frontend

domain.yaml

这里只给了一个traefik的http的route示例,这里需要先安装traefik,关于traefik的安装和https的示例请参考 Traefik安装和域名配置

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: chenjieinfo
spec:
  entryPoints:
    - web
  routes:
  - match: Host(`chenjie.info`,`www.chenjie.info`)  #指定域名
    kind: Rule
    services:
    - name: wordpress-deploy
      port: 80

kustomization.yaml

resources:
  - mysql-deployment-and-service.yaml
  - wordpress-deployment-and-service.yaml
  - domain.yaml

secretGenerator:
- name: mysql-pass
  literals:
  - password=!@#123 #博客数据密码设置

博客部署命令

kubectl create ns wordpress
kubectl apply -k . -n wordpress

部署后可检查wordpress下的资源是否就绪,例如

kubectl get pods -n wordpress

NAME                                READY   STATUS    RESTARTS   AGE
mysql-deploy-658d4dfb5c-94mp5       1/1     Running   0          5d12h
wordpress-deploy-7678b98bd4-q7ssm   1/1     Running   0          5d12h

需要注意第一次安装需要手动创建db, 本例中我的db名是chenjie, mysql的pod名是mysql-deploy-658d4dfb5c-94mp5

kubectl exec mysql-deploy-658d4dfb5c-94mp5  -n wordpress  -it -- mysql -uroot -p

按提示输入密码,密码就是在kustomization.yaml中填入的密码,然后进入mysql客户端,执行命令创建db

CREATE DATABASE IF NOT EXISTS `chenjie`;

到这里博客部署就完成了,然后需要将域名指向到kubernetes的部署了traefik的边缘节点即可,traefik部署参考 Traefik安装和域名配置 ,接着在浏览器打开站点按提示进行wordpress的部署。

博客清理命令

kubectl delete -k .

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据