转载 基于K8s部署MySQL cluster
原文章地址:https://zhuanlan.zhihu.com/p/104951736
使用kubectl部署
1,部署ConfigMap
编写gemfield-mysql-configmap.yaml,内容如下
apiVersion: v1
kind: ConfigMap
metadata:
name: gemfield-mysql
labels:
app: gemfield-mysql
data:
master.cnf: |
# Apply this config only on the master.
[mysqld]
log-bin
slave.cnf: |
# Apply this config only on slaves.
[mysqld]
super-read-only
然后执行apply命令:
kubectl apply -f gemfield-mysql-configmap.yaml
检查命令执行结果:
gemfield@deepvac:/bigdata/gemfield$ kubectl describe configmap gemfield-mysql
Name: gemfield-mysql
Namespace: default
Labels: app=gemfield-mysql
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","data":{"master.cnf":"# Apply this config only on the master.\n[mysqld]\nlog-bin\n","slave.cnf":"# Apply this config on...
Data
====
master.cnf:
----
# Apply this config only on the master.
[mysqld]
log-bin
slave.cnf:
----
# Apply this config only on slaves.
[mysqld]
super-read-only
Events: <none>
这个ConfigMap将会在Pod启动后覆盖mysql服务的my.cnf文件。因为配置是区分master和slave的,所以这里是告诉mysql master需要将logs同步到mysql slaves上,并且让mysql slaves拒绝掉所有的写入操作(同步操作除外)。
2,部署Services
新建gemfield-mysql-services.yaml:
# Headless service for stable DNS entries of StatefulSet members.
apiVersion: v1
kind: Service
metadata:
name: gemfield-mysql
labels:
app: gemfield-mysql
spec:
ports:
- name: gemfield-mysql
port: 3306
clusterIP: None
selector:
app: gemfield-mysql
---
# Client service for connecting to any MySQL instance for reads.
# For writes, you must instead connect to the master: mysql-0.mysql.
apiVersion: v1
kind: Service
metadata:
name: gemfield-mysql-read
labels:
app: gemfield-mysql
spec:
ports:
- name: gemfield-mysql
port: 3306
selector:
app: gemfield-mysql
部署Service:
kubectl apply -f gemfield-mysql-services.yaml
检查命令执行结果:
gemfield@deepvac:/bigdata/gemfield$ kubectl get svc | grep gemfield-mysql
gemfield-mysql ClusterIP None <none> 3306/TCP 13s
gemfield-mysql-read ClusterIP 10.106.228.197 <none> 3306/TCP 13s
3,部署StatefulSet
新建gemfield-mysql-statefulset.yaml:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: gemfield-mysql
spec:
selector:
matchLabels:
app: gemfield-mysql
serviceName: gemfield-mysql
replicas: 3
template:
metadata:
labels:
app: gemfield-mysql
spec:
initContainers:
- name: init-gemfield-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-gemfield-mysql
image: gcr.azk8s.cn/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 ]]