我是靠谱客的博主 幽默学姐,最近开发中收集的这篇文章主要介绍k8s-mysql主从数据库,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

(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;

最后

以上就是幽默学姐为你收集整理的k8s-mysql主从数据库的全部内容,希望文章能够帮你解决k8s-mysql主从数据库所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部