k8s-mysql主从数据库
(1)mysql-secret(记录密码)apiVersion: v1kind: Secretmetadata:name: mysqlnamespace: defaulttype: Opaquedata:root_pass: MTIzNDU2# 用base64转码后的字符 123456(2)mysql-configmap(注册主节点配置以及从节点配置)apiVersion: v1kind: Con
·
(1)mysql-secret(记录密码)
apiVersion: v1
kind: Secret
metadata:
name: mysql
namespace: default
type: Opaque
data:
root_pass: MTIzNDU2 # 用base64转码后的字符 123456
(2)mysql-configmap(注册主节点配置以及从节点配置)
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql
namespace: default
data:
master.cnf: |
[mysqld]
datadir = /var/lib/mysql
server_id = 0
log_bin = binlog
slave.cnf: |
[mysqld]
datadir = /var/lib/mysql
server_id = 0
(3) Headless Service用于DNS
apiVersion: v1
kind: Service
metadata:
name: mysqlsvc
labels:
app: mysql
spec:
selector:
app: mysql
clusterIP: None # Headless Service
ports:
- name: mysql
port: 3306
(4)mysql-statefulSet
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: mysqlsvc
replicas: 3 # 3副本就是一主二从
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: registry.cn-shenzhen.aliyuncs.com/leedon/mysql-5.6
imagePullPolicy: IfNotPresent
env:
- name: SVC_NAME # 这里一定要跟service名称一致
value: mysqlsvc
- name: SLAVE_HOSTS
value: mysql-_.mysqlsvc%
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql
key: root_pass
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
ports:
- name: mysql
containerPort: 3306
volumeMounts:
- name: mysqldata
mountPath: /var/lib/mysql
- name: configfile
mountPath: /etc/mysql
volumes:
- name: configfile
configMap:
name: mysql
items:
- key: master.cnf
path: master.cnf
- key: slave.cnf
path: slave.cnf
volumeClaimTemplates:
- metadata:
name: mysqldata
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
Kubernetes: Host ‘x.x.x.x’ is not allowed to connect to this MySQL
CREATE USER 'root'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
FLUSH PRIVILEGES;
更多推荐
已为社区贡献16条内容
所有评论(0)