教程
简单搭建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# 创建命名空间 apiVersion: v1 kind: Namespace metadata: name: mgr --- # 密码管理 apiVersion: v1 kind: Secret metadata: name: mysql-secret namespace: mgr labels: app: mysql type: Opaque data: # 加密echo -n '123456'|base64 # 解密echo -n 'MTIzNDU2'|base64 --decode mysql-root-password: "MTIzNDU2" --- apiVersion: v1 kind: Service metadata: name: mysql-service namespace: mgr labels: app: mysql spec: clusterIP: None selector: app: mysql ports: - name: client-port port: 3306 protocol: TCP targetPort: 3306 --- apiVersion: v1 kind: ConfigMap metadata: name: mysql-config namespace: mgr labels: app: mysql data: mysql.cnf: | [mysqld] # 动态参数会在上面加入 gtid-mode=on enforce-gtid-consistency=on master-info-repository=TABLE relay-log-info-repository=TABLE binlog-checksum=none log-slave-updates=on log-bin=binlog binlog-format=ROW transaction-write-set-extraction=XXHASH64 loose-group_replication_group_name='d7554cee-bc3f-11eb-bdca-0242ac16210a' loose-group_replication_start_on_boot=off loose-group_replication_bootstrap_group=off loose-group_replication_single_primary_mode=off loose-group_replication_enforce_update_everywhere_checks=on # 这里根据集群网段改 loose-group_replication_ip_whitelist="10.233.0.0/16,127.0.0.1/8" init-mysql.sh: | set -ex # Generate mysql server-id from pod ordinal index. hostname=`hostname` [[ $hostname =~ ^(.*)-([0-9]+)$ ]] || exit 1 service_name=${BASH_REMATCH[1]} ordinal=${BASH_REMATCH[2]} server_id=$((100 + $ordinal)) k8s_server_name=mysql-service local_address="$hostname.$k8s_server_name" mgr_port=24901 seeds="" # 生成seeds for i in {0..8}; do seeds+="$service_name-$i.$k8s_server_name:$mgr_port," done seeds=${seeds%?} # Copy appropriate conf.d files from config-map to emptyDir. cp /mnt/config-map/mysql.cnf /mnt/conf.d/ # modify cnf sed -i '/server-id/d' /mnt/conf.d/mysql.cnf sed -i '/\[mysqld\]/a\server-id='"$server_id"'' /mnt/conf.d/mysql.cnf sed -i '/loose-group_replication_local_address/d' /mnt/conf.d/mysql.cnf sed -i '/\[mysqld\]/a\loose-group_replication_local_address='"$local_address:$mgr_port"'' /mnt/conf.d/mysql.cnf # report_host可以修改hostname sed -i '/\[mysqld\]/a\report_host='"$local_address"'' /mnt/conf.d/mysql.cnf sed -i '/loose-group_replication_group_seeds/d' /mnt/conf.d/mysql.cnf sed -i '/\[mysqld\]/a\loose-group_replication_group_seeds='"$seeds"'' /mnt/conf.d/mysql.cnf #if [[ $ordinal -eq 0 ]]; then # cp /mnt/config-map/master.cnf /mnt/initdb #else # cp /mnt/config-map/slave.cnf /mnt/initdb #fi cp /mnt/config-map/common-initdb.sql /mnt/initdb common-initdb.sql: | SET SQL_LOG_BIN=0; grant replication slave on *.* to 'repl'@'%' identified by '123456'; flush privileges; reset master; SET SQL_LOG_BIN=1; change master to master_user='repl',master_password='123456' for channel 'group_replication_recovery'; install PLUGIN group_replication SONAME 'group_replication.so'; master-initdb.sql: | SET GLOBAL group_replication_bootstrap_group=ON; START GROUP_REPLICATION; SET GLOBAL group_replication_bootstrap_group=OFF; slave-initdb.sql: | set global group_replication_allow_local_disjoint_gtids_join=ON; start group_replication; --- apiVersion: apps/v1 kind: StatefulSet metadata: namespace: mgr name: mysql spec: selector: matchLabels: app: mysql serviceName: mysql-service replicas: 2 template: metadata: labels: app: mysql spec: initContainers: - name: init-mysql image: mysql:5.7 imagePullPolicy: "IfNotPresent" command: - bash - "/mnt/config-map/init-mysql.sh" volumeMounts: - name: conf mountPath: /mnt/conf.d - name: initdb mountPath: /mnt/initdb - name: config-map mountPath: /mnt/config-map containers: - name: mysql image: mysql:5.7 imagePullPolicy: "IfNotPresent" env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-secret key: mysql-root-password ports: - name: client-port containerPort: 3306 - name: join-port containerPort: 24901 livenessProbe: exec: command: [ "mysqladmin", "ping", "-uroot", "-p123456"] initialDelaySeconds: 60 periodSeconds: 10 timeoutSeconds: 5 readinessProbe: exec: # Check we can execute queries over TCP (skip-networking is off). command: [ "mysql", "-uroot", "-p123456", "-e", "SELECT 1" ] initialDelaySeconds: 30 periodSeconds: 2 timeoutSeconds: 1 volumeMounts: - name: conf mountPath: /etc/mysql/conf.d - name: initdb mountPath: /docker-entrypoint-initdb.d volumes: - name: conf emptyDir: {} - name: initdb emptyDir: {} - name: config-map configMap: name: mysql-config |
k8s容器之间不能通过容器的hostname相互直接访问,需要借助service,如:mysql-0.mysql-service
,mysql-0是hostname,mysql-service是service,只有这样容器间才能访问。
MySQL的MGR默认使用容器的hostname作为MEMBER_HOST,修改MEMBER_HOST
需要通过mysql.cnf
配置report_host
,如:report_host=mysql-0.mysql-service
1 2 3 4 5 6 7 |
mysql> select * from performance_schema.replication_group_members; +---------------------------+--------------------------------------+-----------------------+-------------+--------------+ | CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE | +---------------------------+--------------------------------------+-----------------------+-------------+--------------+ | group_replication_applier | eb36928c-c600-11eb-9a3f-0242ac10010a | mysql-0.mysql-service | 3306 | ONLINE | | group_replication_applier | eb92f01c-c600-11eb-b34b-0242ac10010b | mysql-1.mysql-service | 3306 | ONLINE | +---------------------------+--------------------------------------+-----------------------+-------------+--------------+ |