環境node
主節點:172.19.2.50 從節點: 172.19.2.51 172.19.2.140
部署完成後經過各節點的30336端口訪問mysqlmysql
帳號root,密碼abcd1234 如: mysql -h 172.19.2.50 -P 30336 -uroot -pabcd1234
部署完成後經過galera能夠讓集羣3個節點間的數據一致sql
容器內訪問mysql時,能夠經過全部k8s節點的30336端口訪問,也能夠使用k8s服務中的內部入口訪問,如mysql.mysql:3306,dns會自動解析mysql.mysql到對應的服務集羣docker
mkdir -pv /mysql_data/datadir-mariadb-0 mkdir -pv /mysql_data/datadir-mariadb-1 mkdir -pv /mysql_data/datadir-mariadb-2
cd /opt/kubernetes-mysql-cluster
命名空間部署文件數據庫
vim 00namespace.yml --- apiVersion: v1 kind: Namespace metadata: name: mysql
pvc部署文件bootstrap
vim 10pvc.yml --- kind: PersistentVolumeClaim apiVersion: v1 metadata: name: mysql-mariadb-0 namespace: mysql spec: accessModes: - ReadWriteOnce #這裏爲pvc的訪問模式 resources: requests: storage: 10Gi #這裏調整要掛載的pvc大小 selector: matchLabels: #這裏要和pv的標籤對應 app: mariadb podindex: "0" --- kind: PersistentVolumeClaim apiVersion: v1 metadata: name: mysql-mariadb-1 namespace: mysql spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi selector: matchLabels: app: mariadb podindex: "1" --- kind: PersistentVolumeClaim apiVersion: v1 metadata: name: mysql-mariadb-2 namespace: mysql spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi selector: matchLabels: app: mariadb podindex: "2"
mariadb服務文件vim
vim 20mariadb-service.yml # the "Headless Service, used to control the network domain" --- apiVersion: v1 kind: Service metadata: name: mariadb namespace: mysql spec: clusterIP: None selector: app: mariadb ports: - port: 3306 name: mysql - port: 4444 name: sst - port: 4567 name: replication - protocol: UDP port: 4567 name: replicationudp - port: 4568 name: ist
mysql服務文件api
vim 30mysql-service.yml --- apiVersion: v1 kind: Service metadata: name: mysql namespace: mysql spec: ports: - port: 3306 name: mysql targetPort: 3306 nodePort: 30336 #這裏爲porxy映射端口 selector: app: mariadb type: NodePort
載入配置文件bash
vim 40configmap.sh #!/bin/bash DIR=`dirname "$BASH_SOURCE"` kubectl create configmap "conf-d" --from-file="$DIR/conf-d/" --namespace=mysql
輸入mysql初始密碼文件app
vim 41secret.sh #!/bin/bash echo -n Please enter mysql root password for upload to k8s secret: read -s rootpw echo kubectl create secret generic mysql-secret --namespace=mysql --from-literal=rootpw=$rootpw
部署有狀態集羣文件
vim 50mariadb.yml apiVersion: apps/v1beta1 kind: StatefulSet metadata: name: mariadb namespace: mysql spec: serviceName: "mariadb" replicas: 1 #這裏是要啓動的節點的數量 template: metadata: labels: app: mariadb spec: terminationGracePeriodSeconds: 10 containers: - name: mariadb image: mariadb:10.1.22 #這裏修改使用的鏡像文件 ports: - containerPort: 3306 name: mysql - containerPort: 4444 name: sst - containerPort: 4567 name: replication - containerPort: 4567 protocol: UDP name: replicationudp - containerPort: 4568 name: ist env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-secret key: rootpw - name: MYSQL_INITDB_SKIP_TZINFO value: "yes" args: - --character-set-server=utf8mb4 - --collation-server=utf8mb4_unicode_ci # Remove after first replicas=1 create - --wsrep-new-cluster #這裏在執行的時候表明會建立新集羣,新增節點的時候要註釋掉 volumeMounts: - name: mysql mountPath: /var/lib/mysql - name: conf mountPath: /etc/mysql/conf.d - name: initdb mountPath: /docker-entrypoint-initdb.d volumes: - name: conf configMap: name: conf-d - name: initdb emptyDir: {} volumeClaimTemplates: - metadata: name: mysql spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 10Gi
調整集羣節點到3個的文件
vim 70unbootstrap.sh #!/bin/bash DIR=`dirname "$BASH_SOURCE"` set -e set -x cp "$DIR/50mariadb.yml" "$DIR/50mariadb.yml.unbootstrap.yml" sed -i 's/replicas: 1/replicas: 3/' "$DIR/50mariadb.yml.unbootstrap.yml" sed -i 's/- --wsrep-new-cluster/#- --wsrep-new-cluster/' "$DIR/50mariadb.yml.unbootstrap.yml" kubectl apply -f "$DIR/50mariadb.yml.unbootstrap.yml" rm "$DIR/50mariadb.yml.unbootstrap.yml"
建立目錄
mkdir -pv bootstrap conf-d
建立pv腳本
vim bootstrap/pv.sh #!/bin/bash echo "Note that in for example GKE a PetSet will have PersistentVolume(s) and PersistentVolumeClaim(s) created for it automatically" dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )" path="$dir/data" echo "Please enter a path where to store data during local testing: ($path)" read newpath [ -n "$newpath" ] && path=$newpath cat bootstrap/pv-template.yml | sed "s|/tmp/k8s-data|$path|" | kubectl create -f -
建立掛載pv文件
vim bootstrap/pv-template.yml --- apiVersion: v1 kind: PersistentVolume metadata: name: datadir-mariadb-0 labels: #這裏的標籤要和pvc的matchLabels對應 app: mariadb podindex: "0" spec: accessModes: - ReadWriteOnce #這裏是pv的訪問模式,必需要與pvc相同 capacity: storage: 10Gi #這裏是要建立的pv的大小 hostPath: path: /mysql_data/datadir-mariadb-0 #這裏爲掛載到本地的路徑 --- apiVersion: v1 kind: PersistentVolume metadata: name: datadir-mariadb-1 labels: app: mariadb podindex: "1" spec: accessModes: - ReadWriteOnce capacity: storage: 10Gi hostPath: path: /mysql_data/datadir-mariadb-1 --- apiVersion: v1 kind: PersistentVolume metadata: name: datadir-mariadb-2 labels: app: mariadb podindex: "2" spec: accessModes: - ReadWriteOnce capacity: storage: 10Gi hostPath: path: /mysql_data/datadir-mariadb-2
刪除pv腳本
vim bootstrap/rm.sh #!/bin/bash echo "Note that in for example GKE a PetSet will have PersistentVolume(s) and PersistentVolumeClaim(s) created for it automatically" dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )" path="$dir/data" echo "Please enter a path where to store data during local testing: ($path)" read newpath [ -n "$newpath" ] && path=$newpath cat bootstrap/pv-template.yml | sed "s|/tmp/k8s-data|$path|" | kubectl delete -f -
集羣同步galera的配置文件
vim conf-d/galera.cnf [server] [mysqld] [galera] wsrep_on=ON wsrep_provider="/usr/lib/galera/libgalera_smm.so" wsrep_cluster_address="gcomm://mariadb-0.mariadb,mariadb-1.mariadb,mariadb-2.mariadb" binlog_format=ROW default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 wsrep-sst-method=rsync bind-address=0.0.0.0 [embedded] [mariadb] [mariadb-10.1]
kubernetes全部節點執行
docker pull mariadb:10.1.22
主節點執行
cd /opt/kubernetes-mysql-cluster sh bootstrap/pv.sh kubectl create -f 00namespace.yml kubectl create -f 10pvc.yml ./40configmap.sh ./41secret.sh 設置數據庫root密碼爲abcd1234 kubectl create -f 20mariadb-service.yml kubectl create -f 30mysql-service.yml kubectl create -f 50mariadb.yml
./70unbootstrap.sh
主節點上執行
cd /opt/kubernetes-mysql-cluster kubectl delete -f ./ sh bootstrap/rm.sh
全部節點執行
rm -rf /mysql_data/datadir-mariadb-0/* rm -rf /mysql_data/datadir-mariadb-1/* rm -rf /mysql_data/datadir-mariadb-2/*