极客时间运维进阶训练营第十四周作业
作者:9527
- 2023-02-16 美国
本文字数:5319 字
阅读完需:约 17 分钟
本周作业
wordpress 示例中:使用 statefulset 编排运行 mysql,实例数为 1;换成使用 Operator 编排运行 mysql,实例数为 1+;将 mysql 以传统模型的主从复制的形式运行于 Kubernetes 外部,让运行在 Kubernetes 集群上的 wordpress 去访问外部的 MySQL 服务。
wordpress 实例扩展至多个,测试应用是否工作正常。
Nginx 实例扩展至多个,测试应用是否工作正常;额外为 nginx 添加 https 虚拟主机。
Overall Architecture
Storage for WordPress Application Files: AWS EFS
Database Cluster: MySQL with xtrabackup for parity
Application Level: A WordPress DockerHub image mounted to NFS Storage
Load Balancing and Networking: Kubernetes-based load balancers and service networking
MySQL StatefulSet
MySQL ConfigMap
apiVersion: v1kind: ConfigMapmetadata: name: mysql labels: app: mysqldata: master.cnf: | # Apply this config only on the master. [mysqld] log-bin skip-host-cache skip-name-resolve slave.cnf: | # Apply this config only on slaves. [mysqld] skip-host-cache skip-name-resolve复制代码
MySQL Service
# mysql-services.yaml# Headless service for stable DNS entries of StatefulSet members.apiVersion: v1kind: Servicemetadata: name: mysql labels: app: mysqlspec: ports: - name: mysql port: 3306 clusterIP: None selector: app: mysql复制代码
MySQL StatefulSet
apiVersion: apps/v1kind: StatefulSetmetadata: name: mysqlspec: 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. [[ `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/master.cnf /mnt/conf.d/ else cp /mnt/config-map/slave.cnf /mnt/conf.d/ fi volumeMounts: - name: conf mountPath: /mnt/conf.d - name: config-map mountPath: /mnt/config-map - name: clone-mysql image: gcr.io/google-samples/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 master (ordinal index 0). [[ `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: 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 resources: requests: cpu: 500m memory: 1Gi 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: gcr.io/google-samples/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 ]]; then # XtraBackup already generated a partial "CHANGE MASTER TO" query # because we're cloning from an existing slave. mv xtrabackup_slave_info change_master_to.sql.in # Ignore xtrabackup_binlog_info in this case (it's useless). rm -f xtrabackup_binlog_info elif [[ -f xtrabackup_binlog_info ]]; then # We're cloning directly from master. Parse binlog position. [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1 rm xtrabackup_binlog_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" # In case of container restart, attempt this at-most-once. mv change_master_to.sql.in change_master_to.sql.orig mysql -h 127.0.0.1 <<EOF $(<change_master_to.sql.orig), MASTER_HOST='mysql-0.mysql', MASTER_USER='root', MASTER_PASSWORD='', MASTER_CONNECT_RETRY=10; START SLAVE; EOF 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 resources: requests: cpu: 100m memory: 100Mi volumes: - name: conf emptyDir: {} - name: config-map configMap: name: mysql volumeClaimTemplates: - metadata: name: data spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 10Gi复制代码
WordPress Service and Deployment
apiVersion: v1kind: Servicemetadata: name: wordpress labels: app: wordpressspec: ports: - port: 80 selector: app: wordpress tier: frontend type: LoadBalancer
---
apiVersion: v1kind: PersistentVolumemetadata: name: nfsspec: capacity: storage: 20G accessModes: - ReadWriteMany nfs: # FIXME: use the right IP server: <IP of the NFS Service> path: "/"
---
apiVersion: v1kind: PersistentVolumeClaimmetadata: name: nfsspec: accessModes: - ReadWriteMany storageClassName: "" resources: requests: storage: 20G
---
apiVersion: apps/v1kind: Deploymentmetadata: name: wordpress labels: app: wordpressspec: selector: matchLabels: app: wordpress tier: frontend strategy: type: Recreate template: metadata: labels: app: wordpress tier: frontend spec: containers: - image: wordpress:4.9-apache name: wordpress env: - name: WORDPRESS_DB_HOST value: mysql - name: WORDPRESS_DB_PASSWORD value: "" ports: - containerPort: 80 name: wordpress volumeMounts: - name: wordpress-persistent-storage mountPath: /var/www/html volumes: - name: wordpress-persistent-storage persistentVolumeClaim: claimName: nfs复制代码
MySQL Operator
Clone operator repo
$ git clone git@github.com:oracle/mysql-operator.git
$ cd mysql-operator
$ helm repo update复制代码
Deploy
$ kubectl create ns mysql-operator$ helm install --name mysql-operator mysql-operator...NAME: mysql-operatorLAST DEPLOYED: Tue Feb 14 15:48:53 2023NAMESPACE: defaultSTATUS: DEPLOYEDRESOURCES:==> v1beta1/CustomResourceDefinitionNAME AGEmysqlbackupschedules.mysql.oracle.com 4smysqlclusters.mysql.oracle.com 4smysqlbackups.mysql.oracle.com 4smysqlrestores.mysql.oracle.com 4s==> v1beta1/ClusterRolemysql-operator 4smysql-agent 4s==> v1beta1/ClusterRoleBindingNAME AGEmysql-operator 3smysql-agent 3s==> v1beta1/DeploymentNAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGEmysql-operator 1 1 1 0 3s==> v1/Pod(related)NAME READY STATUS RESTARTS AGEmysql-operator-d99c84c9-sldb7 0/1 ContainerCreating 0 3s==> v1/ServiceAccountNAME SECRETS AGEmysql-agent 1 5smysql-operator 1 4sNOTES:Thanks for installing the MySQL Operator.Check if the operator is running withkubectl -n mysql-operator get po复制代码
Status of operator
$ kubectl -n mysql-operator get poNAME READY STATUS RESTARTS AGEmysql-operator-d99c84c9-sldb7 1/1 Running 0 2m复制代码
Create Cluster
$ kubectl create ns mysql-cluster
$ cat <<EOF | kubectl create -f -apiVersion: v1kind: ServiceAccountmetadata: name: mysql-agent namespace: mysql-cluster---kind: RoleBindingapiVersion: rbac.authorization.k8s.io/v1beta1metadata: name: mysql-agent namespace: mysql-clusterroleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: mysql-agentsubjects:- kind: ServiceAccount name: mysql-agent namespace: mysql-clusterEOF
serviceaccount "mysql-agent" createdrolebinding "mysql-agent" created
$ cat cluster.ymlapiVersion: mysql.oracle.com/v1kind: Clustermetadata: name: my-first-db namespace: mysql-cluster $ kubectl apply -f cluster.yamlmysqlcluster "my-first-db" created
$ kubectl -n mysql-cluster get mysqlclustersNAME AGEmy-first-db 32s
$ kubectl -n mysql-cluster get pods NAME READY STATUS RESTARTS AGEmy-first-db-0 2/2 Running 0 5mmy-first-db-1 2/2 Running 0 5mmy-first-db-2 2/2 Running 0 5m复制代码
划线
评论
复制
发布于: 刚刚阅读数: 3
版权声明: 本文为 InfoQ 作者【9527】的原创文章。
原文链接:【http://xie.infoq.cn/article/ca9361f5102ed8a3390043710】。文章转载请联系作者。
9527
关注
还未添加个人签名 2020-04-22 加入
还未添加个人简介










评论