kubernetes
1. statefullset部署mysql主从集群的原理
mysql-0是master
mysql-1和mysql-2是两个备份
当mysql写的时候,找headless service中的mysql-0.mysql;当mysql读的时候,找clusterip service中的mysql读,实现读写分离。
statefulset维护三个副本,一个主两个从
因为主从的配置不同所以通过configmap来解决主从配置不同的问题
2. 配置configmap
configmap(作用配置文件和镜像解藕)
[kubeadm@server2 mysql]$ cat configmap.yaml
application/mysql/mysql-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql
labels:
app: mysql
data:
master.cnf: |
# Apply this config only on the master.
[mysqld]
log-bin # 主mysql激活二进制日志
slave.cnf: |
# Apply this config only on slaves.
[mysqld]
super-read-only # 从mysql上面设置为只读
[kubeadm@server2 mysql]$ kubectl get cm
NAME DATA AGE
cm1 1 8d
cm2 1 8d
cm3 2 8d
cm4 2 8d
nginxconf 1 8d
[kubeadm@server2 mysql]$ kubectl delete cm --all # 删除之前实验留下来的cm(cm是configmap的简写)
configmap "cm1" deleted
configmap "cm2" deleted
configmap "cm3" deleted
configmap "cm4" deleted
configmap "nginxconf" deleted
[kubeadm@server2 mysql]$ kubectl get cm
No resources found in default namespace.
[kubeadm@server2 mysql]$ kubectl apply -f configmap.yaml # 创建configmap
configmap/mysql created
[kubeadm@server2 mysql]$ kubectl get cm
NAME DATA AGE
mysql 2 5s
[kubeadm@server2 mysql]$ kubectl describe cm mysql # 生成的mysql配置
Name: mysql
Namespace: default
Labels: app=mysql
Annotations:
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>
3. headless service和clusterip service
[kubeadm@server2 mysql]$ cat service.yaml
# Headless service for stable DNS entries of StatefulSet members.
apiVersion: v1
kind: Service
metadata:
name: mysql
labels:
app: 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 master: mysql-0.mysql.
apiVersion: v1
kind: Service
metadata:
name: mysql-read
labels:
app: mysql
spec:
ports:
- name: mysql
port: 3306
selector:
app: mysql
[kubeadm@server2 mysql]$ kubectl apply -f service.yaml
service/mysql created
service/mysql-read created
[kubeadm@server2 mysql]$ kubectl get service # 两个服务mysql和mysql-read
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16d
mysql ClusterIP None <none> 3306/TCP 59s
mysql-read ClusterIP 10.104.24.16 <none> 3306/TCP 58s
4. statefulset配置
4.1 先尝试拉起一个主mysql
[kubeadm@server2 mysql]$ cat statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
serviceName: mysql
replicas: 1
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