k8s 部署 mysql 高可用 示例
时间: 2023-08-29 20:12:35 浏览: 185
mysql高可用
k8s 部署 mysql 高可用示例可以采用主从复制和集群方式。
1. 主从复制方式
在 k8s 中采用 StatefulSet 进行部署,首先需要创建一个 ConfigMap,用于存放 MySQL 的配置文件。示例:
```
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
data:
my.cnf: |
[mysqld]
server-id=1
log-bin=mysql-bin
binlog-do-db=testdb
binlog-ignore-db=mysql
relay-log=mysqld-relay-bin
relay-log-index=mysqld-relay-bin.index
```
然后创建一个 PersistentVolumeClaim(PVC)用于存储数据,示例:
```
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
```
创建一个主节点的 StatefulSet,示例:
```
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql-master
spec:
serviceName: mysql
replicas: 1
selector:
matchLabels:
app: mysql
role: master
template:
metadata:
labels:
app: mysql
role: master
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "root"
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
- name: mysql-config
mountPath: /etc/mysql/conf.d
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-data
- name: mysql-config
configMap:
name: mysql-config
```
创建一个从节点的 StatefulSet,示例:
```
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql-slave
spec:
serviceName: mysql
replicas: 2
selector:
matchLabels:
app: mysql
role: slave
template:
metadata:
labels:
app: mysql
role: slave
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "root"
- name: MYSQL_MASTER_SERVICE_HOST
value: mysql-master
- name: MYSQL_MASTER_SERVICE_PORT
value: "3306"
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
- name: mysql-config
mountPath: /etc/mysql/conf.d
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-data
- name: mysql-config
configMap:
name: mysql-config
```
2. 集群方式
在 k8s 中采用 StatefulSet 进行部署,首先需要创建一个 ConfigMap,用于存放 MySQL 的配置文件。示例:
```
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
data:
my.cnf: |
[mysqld]
server-id=1
log-bin=mysql-bin
binlog-do-db=testdb
binlog-ignore-db=mysql
relay-log=mysqld-relay-bin
relay-log-index=mysqld-relay-bin.index
skip-name-resolve
bind-address=0.0.0.0
```
然后创建一个 PersistentVolumeClaim(PVC)用于存储数据,示例:
```
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
```
创建一个 StatefulSet,示例:
```
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: mysql
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "root"
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
- name: mysql-config
mountPath: /etc/mysql/conf.d
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-data
- name: mysql-config
configMap:
name: mysql-config
```
在 Kubernetes 中可以使用 StatefulSet 对于 MySQL 的集群进行管理,StatefulSet 可以保证 Pod 的唯一性,同时也可以保证 Pod 的有序性。在集群中,每个 Pod 会有一个唯一的 hostname,在 StatefulSet 中我们可以使用 hostname 来访问不同的 Pod。例如:mysql-0、mysql-1、mysql-2。在 MySQL 中,我们需要配置 GTID(Global Transaction Identifier)来保证所有节点的数据都是同步的。创建一个 ConfigMap,用于存放 GTID 相关的配置文件。示例:
```
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-gtid-config
data:
init.sql: |
SET @@GLOBAL.GTID_PURGED='';
CREATE USER repluser@'%' IDENTIFIED BY 'password';
GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'%';
FLUSH PRIVILEGES;
```
然后在 StatefulSet 中添加一个容器用于初始化集群,示例:
```
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: mysql
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "root"
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
- name: mysql-config
mountPath: /etc/mysql/conf.d
- name: init-mysql
image: mysql:5.7
command: ["/bin/bash", "-c", "while ! mysqladmin ping -h mysql-0 --silent; do sleep 1; done; mysql -h mysql-0 -uroot -proot -e \"$(cat /etc/mysql/init.sql)\""]
env:
- name: MYSQL_ROOT_PASSWORD
value: "root"
volumeMounts:
- name: mysql-gtid-config
mountPath: /etc/mysql
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-data
- name: mysql-config
configMap:
name: mysql-config
- name: mysql-gtid-config
configMap:
name: mysql-gtid-config
```
这样就完成了 k8s 部署 mysql 高可用的示例。
阅读全文