首页 > 数据库 >k8s之构建Mysql和Wordpress集群

k8s之构建Mysql和Wordpress集群

时间:2024-01-19 22:56:39浏览次数:44  
标签:k8s Mysql Running Wordpress io mysql openebs jiva

一、实验目的
基于Kubernetes集群实现多负载的WordPress应用。将WordPress数据存储在后端Mysql,Mysql实现主从复制读写分离功能。

1、准备Kubernetes集群环境

root@k8s-master01:~# kubectl get nodes
NAME           STATUS     ROLES           AGE   VERSION
k8s-master01   Ready      control-plane   24h   v1.28.2
k8s-node01     Ready      <none>          24h   v1.28.2
k8s-node02     NotReady   <none>          24h   v1.28.2
k8s-node03     Ready      <none>          24h   v1.28.2
root@k8s-master01:~/learning-k8s/examples/wordpress# kubectl get pod -n kube-system
NAME                                   READY   STATUS    RESTARTS          AGE
coredns-66f779496c-hm4xz               1/1     Running   0                 25h
coredns-66f779496c-s8cvs               1/1     Running   0                 25h
etcd-k8s-master01                      1/1     Running   0                 25h
kube-apiserver-k8s-master01            1/1     Running   0                 25h
kube-controller-manager-k8s-master01   1/1     Running   0                 25h
kube-proxy-c6x89                       1/1     Running   0                 25h
kube-proxy-pk9tr                       1/1     Running   0                 25h
kube-proxy-r6wqw                       1/1     Running   0                 25h
kube-proxy-rt5sf                       1/1     Running   0                 25h
kube-scheduler-k8s-master01            1/1     Running   0                 25h

二、安装OpenEBS
https://openebs.io/docs/user-guides/installation

1、安装openebs-operator

root@k8s-master01:~# kubectl apply -f https://openebs.github.io/charts/openebs-operator.yaml
namespace/openebs created
serviceaccount/openebs-maya-operator created
clusterrole.rbac.authorization.k8s.io/openebs-maya-operator created
clusterrolebinding.rbac.authorization.k8s.io/openebs-maya-operator created
customresourcedefinition.apiextensions.k8s.io/blockdevices.openebs.io created
customresourcedefinition.apiextensions.k8s.io/blockdeviceclaims.openebs.io created
configmap/openebs-ndm-config created
daemonset.apps/openebs-ndm created
deployment.apps/openebs-ndm-operator created
deployment.apps/openebs-ndm-cluster-exporter created
service/openebs-ndm-cluster-exporter-service created
daemonset.apps/openebs-ndm-node-exporter created
service/openebs-ndm-node-exporter-service created
deployment.apps/openebs-localpv-provisioner created
storageclass.storage.k8s.io/openebs-hostpath created
storageclass.storage.k8s.io/openebs-device created

root@k8s-master01:~# kubectl get pod -n openebs
NAME                                            READY   STATUS    RESTARTS   AGE
openebs-localpv-provisioner-6787b599b9-xm2mm    1/1     Running   0          14s
openebs-ndm-7mg7r                               1/1     Running   0          14s
openebs-ndm-cbph8                               1/1     Running   0          14s
openebs-ndm-cluster-exporter-7bfd5746f4-hsmq6   1/1     Running   0          14s
openebs-ndm-cvt75                               1/1     Running   0          14s
openebs-ndm-node-exporter-4qlnc                 1/1     Running   0          14s
openebs-ndm-node-exporter-b7qjp                 1/1     Running   0          14s
openebs-ndm-node-exporter-g6vhb                 1/1     Running   0          14s
openebs-ndm-operator-845b8858db-psrj4           1/1     Running   0          14s

2、所有节点安装iSCSI tools
OpenEBS的Jiva引擎依赖ISCSI协议,需要每个节点安装open-iscsi插件。

sudo apt-get update
sudo apt-get install open-iscsi
sudo systemctl enable --now iscsid
sudo apt install nfs-common

三、安装OpenEBS Jiva
Jiva会以CSI插件的形式把自己作为存储环境部署在k8s集群中,实现卷复制。
1、安装jiva-operator

root@k8s-master01:~# kubectl apply -f https://openebs.github.io/charts/jiva-operator.yaml
namespace/openebs unchanged
customresourcedefinition.apiextensions.k8s.io/jivavolumepolicies.openebs.io configured
customresourcedefinition.apiextensions.k8s.io/jivavolumes.openebs.io configured
customresourcedefinition.apiextensions.k8s.io/upgradetasks.openebs.io configured
serviceaccount/jiva-operator created
clusterrole.rbac.authorization.k8s.io/jiva-operator configured
clusterrolebinding.rbac.authorization.k8s.io/jiva-operator unchanged
deployment.apps/jiva-operator created
csidriver.storage.k8s.io/jiva.csi.openebs.io unchanged
serviceaccount/openebs-jiva-csi-controller-sa created
priorityclass.scheduling.k8s.io/openebs-jiva-csi-controller-critical configured
priorityclass.scheduling.k8s.io/openebs-jiva-csi-node-critical configured
clusterrole.rbac.authorization.k8s.io/openebs-jiva-csi-role unchanged
clusterrolebinding.rbac.authorization.k8s.io/openebs-jiva-csi-binding unchanged
statefulset.apps/openebs-jiva-csi-controller created
clusterrole.rbac.authorization.k8s.io/openebs-jiva-csi-attacher-role unchanged
clusterrolebinding.rbac.authorization.k8s.io/openebs-jiva-csi-attacher-binding unchanged
serviceaccount/openebs-jiva-csi-node-sa created
clusterrole.rbac.authorization.k8s.io/openebs-jiva-csi-registrar-role unchanged
clusterrolebinding.rbac.authorization.k8s.io/openebs-jiva-csi-registrar-binding unchanged
configmap/openebs-jiva-csi-iscsiadm created
daemonset.apps/openebs-jiva-csi-node created
root@k8s-master01:~# kubectl get pod -n openebs 
NAME                                            READY   STATUS    RESTARTS   AGE
jiva-operator-5d8846bc49-4f7fm                  1/1     Running   0          12s
openebs-jiva-csi-controller-0                   5/5     Running   0          12s
openebs-jiva-csi-node-25b6f                     3/3     Running   0          12s
openebs-jiva-csi-node-9l9mb                     3/3     Running   0          12s
openebs-jiva-csi-node-cglqn                     3/3     Running   0          12s
openebs-localpv-provisioner-6787b599b9-nncrc    1/1     Running   0          3m56s
openebs-ndm-7l8gw                               1/1     Running   0          3m56s
openebs-ndm-cg99s                               1/1     Running   0          3m56s
openebs-ndm-cluster-exporter-7bfd5746f4-jxn7k   1/1     Running   0          3m56s
openebs-ndm-node-exporter-q9l5w                 1/1     Running   0          3m56s
openebs-ndm-node-exporter-rr9zz                 1/1     Running   0          3m56s
openebs-ndm-node-exporter-sxf9g                 1/1     Running   0          3m56s
openebs-ndm-operator-845b8858db-5jssm           1/1     Running   0          3m56s
openebs-ndm-w7xxn                               1/1     Running   0          3m56s       

安装完成后生成两个新的资源类型

root@k8s-master01:~/learning-k8s/OpenEBS/jiva-csi# kubectl api-resources | grep jiva
jivavolumepolicies                jvp          openebs.io/v1                          true         JivaVolumePolicy
jivavolumes                       jv           openebs.io/v1                          true         JivaVolume  

2、创建openebs-jivavolumepolicy

root@k8s-master01:~/learning-k8s/OpenEBS/jiva-csi# cat openebs-jivavolumepolicy-demo.yaml 
apiVersion: openebs.io/v1alpha1
kind: JivaVolumePolicy
metadata:
  name: jivavolumepolicy-demo
  namespace: openebs
spec:
  replicaSC: openebs-hostpath
  target:
    # This sets the number of replicas for high-availability
    # replication factor <= no. of (CSI) nodes
    replicationFactor: 2
    # disableMonitor: false
    # auxResources:
    # tolerations:
    # resources:
    # affinity:
    # nodeSelector:
    # priorityClassName:
  # replica:
    # tolerations:
    # resources:
    # affinity:
    # nodeSelector:
    # priorityClassName:

root@k8s-master01:~/learning-k8s/OpenEBS/jiva-csi# kubectl apply -f openebs-jivavolumepolicy-demo.yaml 
jivavolumepolicy.openebs.io/jivavolumepolicy-demo created

root@k8s-master01:~/learning-k8s/OpenEBS/jiva-csi# kubectl get jivavolumepolicy -n openebs
NAME                    AGE
jivavolumepolicy-demo   3m7s

3、创建OpenEBS存储类
创建存储类实现PV的动态置备
创建OpenEBS后会自动生成两个本地卷

  • openebs-device
  • openebs-hostpath
root@k8s-master01:~/learning-k8s/OpenEBS/jiva-csi# cat openebs-jiva-csi-storageclass.yaml 
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: openebs-jiva-csi
provisioner: jiva.csi.openebs.io
allowVolumeExpansion: true
parameters:
  cas-type: "jiva"
  policy: "jivavolumepolicy-demo"
   
storageclass.storage.k8s.io/openebs-jiva-csi created
root@k8s-master01:~/learning-k8s/OpenEBS/jiva-csi# kubectl get sc -n openebs
NAME               PROVISIONER           RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
openebs-device     openebs.io/local      Delete          WaitForFirstConsumer   false                  26m
openebs-hostpath   openebs.io/local      Delete          WaitForFirstConsumer   false                  26m
openebs-jiva-csi   jiva.csi.openebs.io   Delete          Immediate              true                   13s

4、创建openebs nfs provider
openebs nfs provider,实现在OpenEBS的基础上实现多路读写
创建后生成openebs-nfs-provisioner pod和openebs-rwx的存储类

root@k8s-master01:~# kubectl apply -f https://openebs.github.io/charts/nfs-operator.yaml
namespace/openebs unchanged
serviceaccount/openebs-maya-operator unchanged
clusterrole.rbac.authorization.k8s.io/openebs-maya-operator configured
clusterrolebinding.rbac.authorization.k8s.io/openebs-maya-operator unchanged
deployment.apps/openebs-nfs-provisioner created
storageclass.storage.k8s.io/openebs-rwx created


root@k8s-master01:~# kubectl get pod -n openebs 
NAME                                            READY   STATUS    RESTARTS          AGE
jiva-operator-5d8846bc49-5x2d2                  1/1     Running   0                 10h
openebs-jiva-csi-controller-0                   5/5     Running   108 (6m18s ago)   10h
openebs-jiva-csi-node-5t6pt                     3/3     Running   0                 10h
openebs-jiva-csi-node-xqqbl                     3/3     Running   0                 10h
openebs-localpv-provisioner-6787b599b9-ns4nt    1/1     Running   101 (50s ago)     10h
openebs-ndm-cluster-exporter-7bfd5746f4-v97jr   1/1     Running   0                 10h
openebs-ndm-h9vkc                               1/1     Running   0                 10h
openebs-ndm-node-exporter-7zrl2                 1/1     Running   0                 10h
openebs-ndm-node-exporter-bt6kb                 1/1     Running   0                 10h
openebs-ndm-operator-845b8858db-9b6m5           1/1     Running   0                 10h
openebs-ndm-qqwv4                               1/1     Running   0                 10h
openebs-nfs-provisioner-5b595f4798-2ft9w        1/1     Running   0                 3m52s
root@k8s-master01:~# kubectl get sc  -n openebs 
NAME               PROVISIONER           RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
openebs-device     openebs.io/local      Delete          WaitForFirstConsumer   false                  10h
openebs-hostpath   openebs.io/local      Delete          WaitForFirstConsumer   false                  10h
openebs-jiva-csi   jiva.csi.openebs.io   Delete          Immediate              true                   10h
openebs-rwx        openebs.io/nfsrwx     Delete          Immediate              false                  5m59s

四、部署Mysql集群
1、创建数据库集群(一主俩从)

root@k8s-master01:~/learning-k8s/examples/statefulsets/mysql# kubectl create namespace blog 
namespace/blog created
root@k8s-master01:~/learning-k8s/examples/statefulsets/mysql# cat 01-configmap-mysql.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql
data:
  primary.cnf: |
    # Apply this config only on the primary.
    [mysql]
    default-character-set=utf8mb4
    [mysqld]
    log-bin
    character-set-server=utf8mb4
    [client]
    default-character-set=utf8mb4

  replica.cnf: |
    # Apply this config only on replicas.
    [mysql]
    default-character-set=utf8mb4
    [mysqld]
    super-read-only    
    character-set-server=utf8mb4
    [client]
    default-character-set=utf8mb4
root@k8s-master01:~/learning-k8s/examples/statefulsets/mysql# cat 02-services-mysql.yaml 
# Headless service for stable DNS entries of StatefulSet members.
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - name: mysql
    port: 3306
  clusterIP: None
  selector:
    app: mysql
---
# Client service for connecting to any MySQL instance for reads.
# For writes, you must instead connect to the primary: mysql-0.mysql.
apiVersion: v1
kind: Service
metadata:
  name: mysql-read
  labels:
    app: mysql
spec:
  ports:
  - name: mysql
    port: 3306
  selector:
    app: mysql
root@k8s-master01:~/learning-k8s/examples/statefulsets/mysql# cat 03-statefulset-mysql.yaml 
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: mysql
  replicas: 3
  template:
    metadata:
      labels:
        app: mysql
    spec:
      initContainers:
      - name: init-mysql
        image: mysql:5.7
        command:
        - bash
        - "-c"
        - |
          set -ex
          # Generate mysql server-id from pod ordinal index.
          [[ $(cat /proc/sys/kernel/hostname) =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          echo [mysqld] > /mnt/conf.d/server-id.cnf
          # Add an offset to avoid reserved server-id=0 value.
          echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
          # Copy appropriate conf.d files from config-map to emptyDir.
          if [[ $ordinal -eq 0 ]]; then
            cp /mnt/config-map/primary.cnf /mnt/conf.d/
          else
            cp /mnt/config-map/replica.cnf /mnt/conf.d/
          fi
        volumeMounts:
        - name: conf
          mountPath: /mnt/conf.d
        - name: config-map
          mountPath: /mnt/config-map
      - name: clone-mysql
        image: ikubernetes/xtrabackup:1.0
        command:
        - bash
        - "-c"
        - |
          set -ex
          # Skip the clone if data already exists.
          [[ -d /var/lib/mysql/mysql ]] && exit 0
          # Skip the clone on primary (ordinal index 0).
          [[ $(cat /proc/sys/kernel/hostname) =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          [[ $ordinal -eq 0 ]] && exit 0
          # Clone data from previous peer.
          ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
          # Prepare the backup.
          xtrabackup --prepare --target-dir=/var/lib/mysql
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
      containers:
      - name: mysql
        image: mysql:5.7
        env:
        - name: LANG
          value: "C.UTF-8"
        - name: MYSQL_ALLOW_EMPTY_PASSWORD
          value: "1"
        ports:
        - name: mysql
          containerPort: 3306
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
        livenessProbe:
          exec:
            command: ["mysqladmin", "ping"]
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
        readinessProbe:
          exec:
            # Check we can execute queries over TCP (skip-networking is off).
            command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
          initialDelaySeconds: 5
          periodSeconds: 2
          timeoutSeconds: 1
      - name: xtrabackup
        image: ikubernetes/xtrabackup:1.0
        ports:
        - name: xtrabackup
          containerPort: 3307
        command:
        - bash
        - "-c"
        - |
          set -ex
          cd /var/lib/mysql

          # Determine binlog position of cloned data, if any.
          if [[ -f xtrabackup_slave_info && "x$(<xtrabackup_slave_info)" != "x" ]]; then
            # XtraBackup already generated a partial "CHANGE MASTER TO" query
            # because we're cloning from an existing replica. (Need to remove the tailing semicolon!)
            cat xtrabackup_slave_info | sed -E 's/;$//g' > change_master_to.sql.in
            # Ignore xtrabackup_binlog_info in this case (it's useless).
            rm -f xtrabackup_slave_info xtrabackup_binlog_info
          elif [[ -f xtrabackup_binlog_info ]]; then
            # We're cloning directly from primary. Parse binlog position.
            [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
            rm -f xtrabackup_binlog_info xtrabackup_slave_info
            echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
                  MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
          fi

          # Check if we need to complete a clone by starting replication.
          if [[ -f change_master_to.sql.in ]]; then
            echo "Waiting for mysqld to be ready (accepting connections)"
            until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done

            echo "Initializing replication from clone position"
            mysql -h 127.0.0.1 \
                  -e "$(<change_master_to.sql.in), \
                          MASTER_HOST='mysql-0.mysql', \
                          MASTER_USER='root', \
                          MASTER_PASSWORD='', \
                          MASTER_CONNECT_RETRY=10; \
                        START SLAVE;" || exit 1
            # In case of container restart, attempt this at-most-once.
            mv change_master_to.sql.in change_master_to.sql.orig
          fi

          # Start a server to send backups when requested by peers.
          exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
            "xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
      volumes:
      - name: conf
        emptyDir: {}
      - name: config-map
        configMap:
          name: mysql
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      storageClassName: "openebs-hostpath"
      resources:
        requests:
          storage: 10Gi
root@k8s-master01:~/learning-k8s/examples/statefulsets/mysql# kubectl apply -f . -n blog
configmap/mysql created
service/mysql created
service/mysql-read created
statefulset.apps/mysql created

root@k8s-master01:~# kubectl get pod -n blog
NAME      READY   STATUS    RESTARTS   AGE
mysql-0   2/2     Running   0          28m
mysql-1   2/2     Running   0          21m
mysql-2   2/2     Running   0          6m34s

root@k8s-master01:~# kubectl get pv -n blog
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                  STORAGECLASS       REASON   AGE
pvc-082e3c6c-27fe-4f03-8dbc-459c5ee54b20   10Gi       RWO            Delete           Bound    blog/data-mysql-0      openebs-hostpath            30m
pvc-7785640d-623d-4dd6-90ff-0ce026254733   10Gi       RWO            Delete           Bound    default/data-mysql-0   openebs-hostpath            30m
pvc-87166c09-1fa3-403a-a0b4-bf88875b29d2   10Gi       RWO            Delete           Bound    blog/data-mysql-1      openebs-hostpath            19m
pvc-95948b7e-22e7-4fb9-8ade-84c76fc42fb6   10Gi       RWO            Delete           Bound    blog/data-mysql-2      openebs-hostpath            8m23s

root@k8s-master01:~# kubectl get pvc -n blog
NAME           STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS       AGE
data-mysql-0   Bound    pvc-082e3c6c-27fe-4f03-8dbc-459c5ee54b20   10Gi       RWO            openebs-hostpath   28m
data-mysql-1   Bound    pvc-87166c09-1fa3-403a-a0b4-bf88875b29d2   10Gi       RWO            openebs-hostpath   21m
data-mysql-2   Bound    pvc-95948b7e-22e7-4fb9-8ade-84c76fc42fb6   10Gi       RWO            openebs-hostpath   6m49s

创建完成后自动置备PVC,关联到数据库Pod。

2、进入数据库进行初始化

root@k8s-master01:~# kubectl exec -it -n blog mysql-0 bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Defaulted container "mysql" out of: mysql, xtrabackup, init-mysql (init), clone-mysql (init)
bash-4.2# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 890
Server version: 5.7.44-log MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE DATABASE wpdb;
Query OK, 1 row affected (0.01 sec)

mysql> CREATE USER wpuser@'%' IDENTIFIED BY 'wppass';
Query OK, 0 rows affected (31.12 sec)

mysql> GRANT ALL PRIVILEGES ON wpdb.* TO wpuser@'%';
Query OK, 0 rows affected (0.01 sec)

3、登录从库查看同步状态

root@k8s-master01:~# kubectl exec -it -n blog mysql-1 bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Defaulted container "mysql" out of: mysql, xtrabackup, init-mysql (init), clone-mysql (init)
bash-4.2# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 987
Server version: 5.7.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW DATABASES;
+------------------------+
| Database               |
+------------------------+
| information_schema     |
| mysql                  |
| performance_schema     |
| sys                    |
| wpdb                   |
| xtrabackup_backupfiles |
+------------------------+
6 rows in set (0.10 sec)

五、部署WordPress集群
1、创建Secret用于WordPress连接数据库的信息

root@k8s-master01:~/learning-k8s/examples/wordpress# kubectl create secret generic mysql-secret -n blog --from-literal=wordpress.db=wpdb --from-literal=wordpress.user=wpuser --from-literal=wordpress.password=wppass --dry-run=client -oyaml > mysql-secret.yaml

root@k8s-master01:~/learning-k8s/examples/wordpress# kubectl apply -f mysql-secret.yaml secret/mysql-secret created
root@k8s-master01:~/learning-k8s/examples/wordpress# kubectl get secret -n blog
NAME           TYPE     DATA   AGE
mysql-secret   Opaque   3      43s

2、准备wordpress存储

root@k8s-master01:~/learning-k8s/examples/wordpress# cat openebs-sc.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  annotations:
    cas.openebs.io/config: |
      - name: NFSServerType
        value: "kernel"
      - name: BackendStorageClass
        value: "openebs-jiva-csi"
      # NFSServerResourceRequests defines the resource requests for NFS Server
      #- name: NFSServerResourceRequests
      #  value: |-
      #      memory: 50Mi
      #      cpu: 50m
      # NFSServerResourceLimits defines the resource limits for NFS Server
      #- name: NFSServerResourceLimits
      #  value: |-
      #      memory: 100Mi
      #      cpu: 100m
      # LeaseTime defines the renewal period(in seconds) for client state
      #- name: LeaseTime
      #  value: 30
      # GraceTime defines the recovery period(in seconds) to reclaim locks
      #- name: GraceTime
      #  value: 30
      # FilePermissions defines the file ownership and mode specifications
      # for the NFS server's shared filesystem volume.
      # File permission changes are applied recursively if the root of the
      # volume's filesystem does not match the specified value.
      # Volume-specific file permission configuration can be specified by
      # using the FilePermissions config key in the PVC YAML, instead of
      # the StorageClass's.
      #- name: FilePermissions
      #  data:
      #    UID: "1000"
      #    GID: "2000"
      #    mode: "0744"
      # FSGID defines the group permissions of NFS Volume. If it is set
      # then non-root applications should add FSGID value under pod
      # Suplemental groups.
      # The FSGID config key is being deprecated. Please use the
      # FilePermissions config key instead.
      #- name: FSGID
      #  value: "120"
    openebs.io/cas-type: nfsrwx
  name: openebs-rwx
  resourceVersion: "52027"
provisioner: openebs.io/nfsrwx
reclaimPolicy: Delete                     #一般生产用Retain策略                        
volumeBindingMode: Immediate
root@k8s-master01:~/learning-k8s/examples/wordpress# kubectl apply -f openebs-sc.yaml 
storageclass.storage.k8s.io/openebs-rwx configured
root@k8s-master01:~/learning-k8s/examples/wordpress# kubectl get sc -n openebs
NAME               PROVISIONER           RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
openebs-device     openebs.io/local      Delete          WaitForFirstConsumer   false                  12h
openebs-hostpath   openebs.io/local      Delete          WaitForFirstConsumer   false                  12h
openebs-jiva-csi   jiva.csi.openebs.io   Delete          Immediate              true                   12h
openebs-rwx        openebs.io/nfsrwx     Delete          Immediate              false                  116m

3、创建wordpress

root@k8s-master01:~/learning-k8s/examples/wordpress# cat 05-wordpress-service.yaml 
apiVersion: v1
kind: Service
metadata:
  labels:
    app: wordpress
  name: wordpress
  namespace: blog
spec:
  ports:
  - name: 80-80
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: wordpress
  type: LoadBalancer
root@k8s-master01:~/learning-k8s/examples/wordpress# cat 06-pvc-wordpress.yaml 
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wordpress-pvc
  namespace: blog
spec:
  accessModes: ["ReadWriteMany"]
  volumeMode: Filesystem
  resources:
    requests:
      storage: 5Gi
  storageClassName: openebs-rwx
root@k8s-master01:~/learning-k8s/examples/wordpress# cat 07-deployment-wordpress.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: wordpress
  name: wordpress
  namespace: blog
spec:
  replicas: 2
  selector:
    matchLabels:
      app: wordpress
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - image: wordpress:6-apache
        name: wordpress
        env:
        - name: WORDPRESS_DB_HOST
          value: mysql-0.mysql
        - name: WORDPRESS_DB_NAME
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: wordpress.db
        - name: WORDPRESS_DB_USER
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: wordpress.user
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: wordpress.password
        volumeMounts:
        - name: data
          mountPath: /var/www/html/
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: wordpress-pvc
root@k8s-master01:~/learning-k8s/examples/wordpress# kubectl apply -f 05-wordpress-service.yaml -f 06-pvc-wordpress.yaml -f 07-deployment-wordpress.yaml -n blog
service/wordpress created
persistentvolumeclaim/wordpress-pvc created
deployment.apps/wordpress created

root@k8s-master01:~/learning-k8s/examples/wordpress# kubectl get pod -n blog 
NAME                         READY   STATUS              RESTARTS   AGE
mysql-0                      2/2     Running             0          119m
mysql-1                      2/2     Running             0          112m
mysql-2                      2/2     Running             0          97m
wordpress-777c96d554-8wzpj   1/1     Running             0          24m
wordpress-777c96d554-zw5zz   1/1     Running             0          24m

root@k8s-master01:~/learning-k8s/examples/wordpress# kubectl get pvc -n blog
NAME            STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS       AGE
data-mysql-0    Bound    pvc-082e3c6c-27fe-4f03-8dbc-459c5ee54b20   10Gi       RWO            openebs-hostpath   99m
data-mysql-1    Bound    pvc-87166c09-1fa3-403a-a0b4-bf88875b29d2   10Gi       RWO            openebs-hostpath   92m
data-mysql-2    Bound    pvc-95948b7e-22e7-4fb9-8ade-84c76fc42fb6   10Gi       RWO            openebs-hostpath   77m
wordpress-pvc   Bound    pvc-10ebaa05-44bd-4573-8e6d-80d53c213b34   5Gi        RWX            openebs-rwx        4m13s

六、访问WordPress


标签:k8s,Mysql,Running,Wordpress,io,mysql,openebs,jiva
From: https://www.cnblogs.com/OpenSourceSite/p/17969118

相关文章

  • k8s之存储卷OpenEBS
    一、OpenEBS简介OpenEBS是一种开源云原生存储解决方案,托管于CNCF基金会,目前该项目处于沙箱阶段。OpenEBS能够将Kubernetes工作节点上可用的住何存储转换为术卷或分布式复制卷。OpenEBS支持两大类卷——本地卷和复制卷。本地卷本地卷,即节点级卷,仅支持在卷所在的节点本地......
  • Linux离线安装MySQL
    在Linux上离线安装MySQL,可以按照以下步骤进行操作:下载MySQL安装包:去MySQL官网下载适合Linux系统的MySQL安装包,选择一个合适的版本和文件格式进行下载。根据gclib安装对应的版本和架构进行选择。将安装包复制到Linux系统中:将下载的MySQL安装包复制到Linux系统中的一个合适的目录,例......
  • pymysql模块
    支持python代码操作数据库MySQL#安装,不用考虑版本pip3installpymysql1 链接、执行sql、关闭(游标) importpymysqluser=input('用户名:').strip()pwd=input('密码:').strip()#链接conn=pymysql.connect(host='localhost',port=3306,user='ro......
  • Mysql Row size too large (> 8126)
    问题描述:Rowsizetoolarge(>8126).ChangingsomecolumnstoTEXTorBLOBorusingROW_FORMAT=DYNAMICorROW_FORMAT=COMPRESSEDmayhelp.Incurrentrowformat,BLOBprefixof768bytesisstoredinline.解决问题有几种潜在的解决方案可以解决这个问题。将表转......
  • Docker、K8S
    .Netcore微服务基础1.Docker2.K8S 参考资料1.docker官网https://www.docker.com/products/docker-desktop/2.docker学习教程 https://blog.csdn.net/javaboyweng/article/details/1326220753..netcore微服务之ASP.NETCoreOnDocker https://www.cnblogs.com/edis......
  • mysql常用查询日期语句
    --最近30天 selectdate_add(curdate(),interval(cast(help_topic_idassignedinteger)-30)day)dayfrommysql.help_topicwherehelp_topic_id <=day(last_day(curdate()))orderbyhelp_topic_id--最近7天 selectdate_add(curdate(),interval(cast(help_to......
  • elasticsearch学习笔记2 - logstash jdbc 同步MYSQL到ES
    logstash是一个管道工具input输入output输出filter过滤条件咋们初级先了解到这些再说比较优秀的教程文档作为基础知识需要了解:https://blog.csdn.net/qq_19283249/article/details/130839158?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170564710516800215......
  • K8s 网关选型血泪史
    Sealos公有云几乎打爆了市面上所有主流的开源网关,本文可以给大家很好的避坑,在网关选型方面做一些参考。SealosCloud的复杂场景Sealos公有云上线以来,用户呈爆发式增长,目前总共注册用户8.7w,每个用户都去创建应用,每个应用都需要有自己的访问入口,就导致整个集群路由条目非常巨......
  • 如何在 Debian 12 上安装 MySQL
    MySQL是一个关系型数据库管理系统,目前依旧是最流行的数据库之一,很多系统都在使用MySQL数据库。自从MySQL被甲骨文收购之前后,基本上所有Linux发行版的源默认都不包含MySQL了,而是改成了开源的MariaDB。本文主要介绍如何在Debian12中安装MySQL。前期准备1、一台安......
  • MySQL索引使用原则
    1、最左前缀法则:     如果索引多列(联合索引),要遵循最左前缀法则,最左前缀法则指的是查询从索引的最左列开始,并且不跳过索引中的列。如果跳过某一列,索引将部分失效(后面的字段索引失效)。2、范围查询   联合索引中,出现范围查询(<,>),范围查询右侧的列索引失效(业务允许......