K8s黑科技:MySQL主从部署,助你打造高性能数据魔法世界!

K8s黑科技:MySQL主从部署,助你打造高性能数据魔法世界!

kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: master-mysql
  labels:
    app: master-mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: master-mysql
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: master-mysql
    spec:
      volumes:
        - name: host-time
          hostPath:
            path: /etc/localtime
            type: ''
        - name: mysql-data
          persistentVolumeClaim:
            claimName: mysql-master-data
        - name: mysql-cnf
          configMap:
            name: master-mysql-conf
            defaultMode: 420
      containers:
        - name: master-mysql
          image: mysql
          ports:
            - name: tcp-3306
              containerPort: 3306
              protocol: TCP
            - name: tcp-33060
              containerPort: 33060
              protocol: TCP
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: UnionStrong@2023
          resources: {}
          volumeMounts:
            - name: host-time
              readOnly: true
              mountPath: /etc/localtime
            - name: mysql-data
              mountPath: /var/lib/mysql
            - name: mysql-cnf
              readOnly: true
              mountPath: /etc/mysql/conf.d
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      serviceAccountName: default
      serviceAccount: default
      securityContext: {}
      schedulerName: default-scheduler
  serviceName: mysql-svc
  podManagementPolicy: OrderedReady
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      partition: 0
  revisionHistoryLimit: 10
---
kind: StatefulSet
apiVersion: apps/v1
metadata:
  name: slave-mysql
  labels:
    app: slave-mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: slave-mysql
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: slave-mysql
    spec:
      volumes:
        - name: host-time
          hostPath:
            path: /etc/localtime
            type: ''
        - name: mysql-data
          persistentVolumeClaim:
            claimName: mysql-slave-data
        - name: mysql-cnf
          configMap:
            name: slave-mysql-conf
            defaultMode: 420
      containers:
        - name: slave-mysql
          image: mysql
          ports:
            - name: tcp-3306
              containerPort: 3306
              protocol: TCP
            - name: tcp-33060
              containerPort: 33060
              protocol: TCP
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: UnionStrong@2023
          resources: {}
          volumeMounts:
            - name: host-time
              readOnly: true
              mountPath: /etc/localtime
            - name: mysql-data
              mountPath: /var/lib/mysql
            - name: mysql-cnf
              readOnly: true
              mountPath: /etc/mysql/conf.d
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      serviceAccountName: default
      serviceAccount: default
      securityContext: {}
      schedulerName: default-scheduler
  serviceName: mysql-slave-svc
  podManagementPolicy: OrderedReady
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      partition: 0
  revisionHistoryLimit: 10

---
kind: Service
apiVersion: v1
metadata:
  name: master-mysql-svc
  labels:
    app: master-mysql-svc
spec:
  ports:
    - name: tcp-3306
      protocol: TCP
      port: 3306
      targetPort: 3306
  selector:
    app: master-mysql
---
kind: Service
apiVersion: v1
metadata:
  name: slave-mysql-svc
  labels:
    app: slave-mysql-svc
spec:
  ports:
    - name: tcp-3306
      protocol: TCP
      port: 3306
      targetPort: 3306
  selector:
    app: slave-mysql
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: master-mysql-conf
  labels:
    app: master-mysql-conf
  annotations:
    kubesphere.io/creator: admin
data:
  mysqld.cnf: |-
    [mysqld]
    server_id=1
    log-bin=/var/lib/mysql/mysql-bin
    default_authentication_plugin=mysql_native_password
    default-time-zone = '+08:00'
    character-set-server=utf8
    character-set-server = utf8mb4
    collation-server = utf8mb4_bin
    character-set-client-handshake = FALSE
    innodb_buffer_pool_size = 128M
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    skip-name-resolve
    [client]
    default-character-set = utf8mb4
    [mysql]
    default-character-set = utf8mb4

---
kind: ConfigMap
apiVersion: v1
metadata:
  name: slave-mysql-conf
  labels:
    app: slave-mysql-conf
  annotations:
    kubesphere.io/creator: admin
data:
  mysqld.cnf: |-
    [mysqld]
    server_id=2
    log-bin=/var/lib/mysql/mysql-bin
    default_authentication_plugin=mysql_native_password
    default-time-zone = '+08:00'
    character-set-server=utf8
    character-set-server = utf8mb4
    collation-server = utf8mb4_bin
    character-set-client-handshake = FALSE
    innodb_buffer_pool_size = 128M
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    skip-name-resolve
    [client]
    default-character-set = utf8mb4
    [mysql]
    default-character-set = utf8mb4
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-master-data
  namespace: fcd
  annotations:
    pv.kubernetes.io/bind-completed: 'yes'
    pv.kubernetes.io/bound-by-controller: 'yes'
    volume.beta.kubernetes.io/storage-provisioner: k8s-sigs.io/nfs-subdir-external-provisioner
  finalizers:
    - kubernetes.io/pvc-protection
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 300Gi
  storageClassName: nfs-storage
  volumeMode: Filesystem
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-slave-data
  namespace: fcd
  annotations:
    pv.kubernetes.io/bind-completed: 'yes'
    pv.kubernetes.io/bound-by-controller: 'yes'
    volume.beta.kubernetes.io/storage-provisioner: k8s-sigs.io/nfs-subdir-external-provisioner
  finalizers:
    - kubernetes.io/pvc-protection
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 300Gi
  storageClassName: nfs-storage
  volumeMode: Filesystem

进入主节点获取二进制日志信息
mysql > show master status;
配置从服务器
change master to master_host='master-mysql-svc.fcd',
master_port=3306,
master_user='root',
master_password='UnionStrong@2023',
master_log_file='mysql-bin.000003',
master_log_pos=156;

启动主从同步
mysql > start slave;

查看同步状态
mysql > show slave status G

                                                                 点在看,K8s一年不出问题👇

原文始发于微信公众号(背带裤的云原生):K8s黑科技:MySQL主从部署,助你打造高性能数据魔法世界!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/219106.html

(0)
青莲明月的头像青莲明月

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!