在Kubernetes中部署Drupal

本文假设Kubernetes集群已经安装正常,且通过GlusterFS来提供卷服务。

准备GlusterFS卷

创建卷文件夹

mkdir /data/brick1/drupal
mkdir /data/brick1/drupal-mysql

创建卷

gluster volume create drupal replica 2 node1:/data/brick1/drupal node2:/data/brick1/drupal
gluster volume create drupal-mysql replica 2 node1:/data/brick1/drupal-mysql node2:/data/brick1/drupal-mysql

启动卷

gluster volume start drupal
gluster volume start drupal-mysql

Glusterfs 调优

gluster volume quota drupal enable
gluster volume quota drupal limit-usage / 40GB
gluster volume set drupal performance.cache-size 4GB
gluster volume set drupal performance.io-thread-count 16
gluster volume set drupal network.ping-timeout 10
gluster volume set drupal performance.write-behind-window-size 1024MB

gluster volume quota drupal-mysql enable
gluster volume quota drupal-mysql limit-usage / 10GB
gluster volume set drupal-mysql performance.cache-size 4GB
gluster volume set drupal-mysql performance.io-thread-count 16
gluster volume set drupal-mysql network.ping-timeout 10
gluster volume set drupal-mysql performance.write-behind-window-size 1024MB

创建Persistent Volume 和 Persistent Volume Claim

创建GlusterFS的EndPoints

文件gluster-endpoints.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: glusterfs-cluster
spec:
  ports:
  - port: 1

---
kind: Endpoints
apiVersion: v1
metadata:
  name: glusterfs-cluster
subsets:
- addresses:
  - ip: 10.81.250.11
  ports:
  - port: 1
- addresses:
  - ip: 10.81.250.12
  ports:
  - port: 1

创建Endpoints

kubectl apply -f gluster-endpoints.yaml

创建 Persistent Volume

文件 gfs-pv.yaml

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: gfs-pv-mysql
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  glusterfs:
    endpoints: "glusterfs-cluster"
    path: "drupal-mysql"
    readOnly: false

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: gfs-pv-drupal
spec:
  capacity: 
    storage: 40Gi
  accessModes:
    - ReadWriteMany
  glusterfs:
    endpoints: "glusterfs-cluster"
    path: "drupal"
    readOnly: false

创建PV

kubectl apply -f gfs-pv.yaml

创建PVC

文件gfs-pvc.yaml

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: drupal-mysql-pvc
  labels:
    app: drupal
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: drupal-pvc
  labels:
    app: drupal
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 40Gi

运行创建PVC

kubectl apply -f gfs-pvc.yaml

创建数据库部署(Deployment)

该示例已经处理了utf8mb4问题,同时创建了一个名称为drupal的数据库,用户为root,密码可以根据实际情况修改

文件 drupal-mysql.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: drupal-mysql-service
spec:
  ports:
  - name: mysql
    port: 3306
    protocol: TCP
  selector:
    app: drupal-mysql

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: drupal-mysql
  labels:
    app: drupal-mysql
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: drupal-mysql
        tier: mysql
    spec:
      containers:
        - image: mysql:5.7
          name: drupal-mysql
          args:
            - --character-set-server=utf8mb4
            - --collation-server=utf8mb4_general_ci
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: 
            - name: MYSQL_DATABASE
              value: drupal
          ports:
            - containerPort: 3306
              name: drupal-mysql
              protocol: TCP
          volumeMounts:
            - name: drupal-mysql
              mountPath: /var/lib/mysql
              subPath: data
      volumes:
        - name: drupal-mysql
          persistentVolumeClaim:
            claimName: drupal-mysql-pvc

运行命令创建部署

kubectl apply -f drupal-mysql.yaml

创建Drupal部署

文件 drupal.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: drupal
  labels:
    app: drupal
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30080
  selector:
    app: drupal
    tier: frontend

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: drupal
  labels:
    app: drupal
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: drupal
        tier: frontend
    spec:
      initContainers:
      - name: init-site-volume
        image: drupal:7.66
        command: ['/bin/bash', '-c']
        args: ['cp -r /var/www/html /data/; chown www-data:www-data /data/ -R']
        volumeMounts:
        - mountPath: /data
          name: drupal
      containers:
      - image: drupal:7.66
        name: drupal
        ports:
        - containerPort: 80
          name: drupal
        volumeMounts:
        - name: drupal
          mountPath: /var/www/html
          subPath: html
      volumes:
      - name: drupal
        persistentVolumeClaim:
          claimName: drupal-pvc

安装Drupal

之后访问http://IP:30080就可以进行安装了,在数据库选项的页面,数据库服务器需要填写数据库的服务名称,也就是drupal-mysql-service。

这样安装后,Drupal的utf8mb4问题解决的不完善,需要执行SQL脚本:

INSERT INTO variable SET name='drupal_all_databases_are_utf8mb4', value='b:1;';

总结

以上安装做到了有状态的网站部署,属于比较简单的部署,后续可以部署ingress以支持高并发。

你可能感兴趣的:(在Kubernetes中部署Drupal)