美文网首页
K8S-mysql主从部署2018-05-102018-05-3

K8S-mysql主从部署2018-05-102018-05-3

作者: TerryTsai | 来源:发表于2018-05-30 15:10 被阅读0次

    在kubernetes集群中部署mysql 主从,并开启同步

    版本:

    Kubernetes version:1.10.0
    mysql:5.6.40
    Mysql Image的github地址如下,其中包含Dockerfile及docker-entrypoint.sh
    https://github.com/docker-library/mysql/tree/fc3e856313423dc2d6a8d74cfd6b678582090fc7/5.6

    部署:

    • 单个实例

    如果仅运行单个实例则运行
    docker pull docker.io/mysql:5.6.40
    mysql.yml文件如下

    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      name: mysql-master
    spec:
      replicas: 1
      template:
        metadata:
          name: mysql-master
          labels:
            app: mysql-master
        spec:
          containers:
            - name: mysql-master
              image: mysql:5.6.40
              imagePullPolicy: Always
              env:
                - name: MYSQL_ROOT_PASSWORD
                  value: mysql123
              ports:
                - containerPort: 3306
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: mysql-master-service
    spec:
      type: NodePort
      ports:
      - port: 3306
        targetPort: 3306
      selector:
        app: mysql-master
    

    • 主从部署

    1. 重新构建Mysql Image

    docker-entrypoint.sh 文件的第187行添加如下代码

    if [ -n "$MYSQL_MASTER_SERVICEIP" ];then
    "${mysql[@]}" <<-EOSQL
    CHANGE MASTER TO MASTER_HOST = "$MYSQL_MASTER_SERVICE_SERVICE_HOST",MASTER_PORT = 3306,MASTER_USER = 'root',MASTER_PASSWORD = "$MYSQL_ROOT_PASSWO
    RD",MASTER_AUTO_POSITION = 1;start slave;
    EOSQL
    fi
    

    其中$MYSQL_MASTER_SERVICE_SERVICE_HOST为MASTER的service IP。

    重新构建mysql image 并上传本地私仓
    docker build -t xxxx:5000/mysql:5.6.40 .
    xxxx替换为具体私仓IP
    上传images
    docker push xxxx:5000/mysql:5.6.40

    2. mysql-configmap.yml配置
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: yqt-configmap
      namespace: mysql-ns
    data:
      my.cnf: |
        [mysqld]
        user = mysql
        datadir = /var/lib/mysql/data
        socket = /var/run/mysqld/mysqld.sock
        max_allowed_packet = 32M
        character_set_server=utf8
        net_read_timeout = 60
        open_files_limit = 8192
        max_connections = 1000
        max_connect_errors = 10000
        skip-name-resolve
        log-output=file
        slow_query_log = 1
        slow_query_log_file =/var/lib/mysql/data/slow.log
        log-error = /var/lib/mysql/data/error.log
        log_warnings = 2
        pid-file = /var/lib/mysql/data/mysql.pid
        long_query_time = 0.1
        log-queries-not-using-indexes = 1
        log_bin_trust_function_creators=1
        table_definition_cache = 256
        table_open_cache = 256
        #binlog & replicate    
        log-bin = mysql-bin
        skip-slave-start = 0
        log-slave-updates  = 1
        expire_logs_days = 7
        sync_binlog = 1
        binlog_format = row
        max_binlog_size = 1G
        binlog_cache_size = 4M
        max_binlog_cache_size = 1G
        #innodb
        innodb_additional_mem_pool_size = 16M
        innodb_buffer_pool_size = 1G
        #innodb_data_file_path = ibdata1:1G:autoextend
        innodb_file_io_threads = 4
        innodb_thread_concurrency = 20
        innodb_flush_log_at_trx_commit = 1
        innodb_log_buffer_size = 16M
        innodb_log_file_size = 512M
        innodb_log_files_in_group = 2
        innodb_file_per_table = 1
        innodb_status_file = 1
        transaction_isolation = READ-COMMITTED
        innodb_flush_method = O_DIRECT
        innodb_write_io_threads=16
        #GTID
        gtid_mode=ON
        enforce_gtid_consistency=ON
        [mysqldump]
        quick
        max_allowed_packet = 32M
      master-docker.cnf: |
        [mysqld]
        skip-host-cache
        skip-name-resolve
        server-id = 10
      slave-docker.cnf: |
        [mysqld]
        skip-host-cache
        skip-name-resolve
        server-id = 20
        read-only = 1
    

    创建configmap
    kubectl create -f mysql-configmap.yml

    3. mysql-pv-pvc.yml配置
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: mysql-master-pv
      namespace: mysql-ns
      labels:
        type: mysql-master-pv
    spec:
      capacity:
        storage: 10Gi
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Retain
    #  nfs:
    #    server: x.x.x.x
    #    path: "/usr/local/mysql"  
      hostPath:
        path: "/usr/local/mysql/master-data"
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: mysql-slave-pv
      namespace: ymysql-ns
      labels:
        type: mysql-slave-pv
    spec:
      capacity:
        storage: 10Gi
      accessModes:
        - ReadWriteMany
      persistentVolumeReclaimPolicy: Retain
    #  nfs:
    #    server: x.x.x.x
    #    path: "/usr/local/mysql"  
      hostPath:
        path: "/usr/local/mysql/slave-data"
    ---
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: mysql-master-pvc
      namespace: mysql-ns
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
         requests:
           storage: 10Gi
      selector:
         matchLabels:
           type: mysql-master-pv
    ---
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: mysql-slave-pvc
      namespace: mysql-ns
    spec:
      accessModes:
      - ReadWriteMany
      resources:
         requests:
           storage: 10Gi
      selector:
         matchLabels:
           type: mysql-slave-pv
    

    创建pv-pvc
    kubectl create -f mysql-pv-pvc.yml

    4. mysql-master.yml配置
    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      namespace: mysql-ns
      name: mysql-master
    spec:
      replicas: 1
      template:
        metadata:
          name: mysql-master
          labels:
            app: mysql-master
        spec:
          containers:
            - name: mysql-master
              image: xxxx:5000/mysql:5.6.40
              imagePullPolicy: IfNotPresent
              env:
                - name: MYSQL_ROOT_PASSWORD
                  value: mysql123
              #resources:
              #  requests:
              #    memory: 1Gi
              #    cpu: 1
              #  limits:
              #    memory: 2Gi
              #    cpu: 2
              ports:
                - containerPort: 3306
              volumeMounts:
                - name: mysql-config
                  mountPath: /etc/mysql/mysql.conf.d/
                - name: master-config
                  mountPath: /etc/mysql/conf.d/
                - name: mysql-data
                  mountPath: /var/lib/mysql/data
    #            - name: time-cnf
    #              mountPath: /etc/localtime
    #            readOnly: true
          volumes:
            - name: mysql-data
              persistentVolumeClaim:
                claimName: mysql-master-pvc
            - name: mysql-config
              configMap:
                name: yqt-configmap
                items:
                - key: my.cnf
                  path: mysqld.cnf
            - name: master-config
              configMap:
                name: yqt-configmap
                items:
                - key: master-docker.cnf
                  path: docker.cnf
                  
    #        - name: time-cnf
    #          hostPath:
    #            path: /usr/share/zoneinfo/Asia/Shanghai
    ---
    apiVersion: v1
    kind: Service
    metadata:
      namespace: mysql-ns
      name: mysql-master-service
    spec:
      type: NodePort
      ports:
      - port: 3306
        targetPort: 3306
      selector:
        app: mysql-master
    

    部署mysql主节点
    kubectl create -f mysql-master.yml

    5. mysql-slave.yml配置
    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      namespace: mysql-ns
      name: mysql-slave
    spec:
      replicas: 1
      template:
        metadata:
          name: mysql-slave
          labels:
            app: mysql-slave
        spec:
          containers:
            - name: mysql-slave
              image: xxxx:5000/mysql:5.6.40
              imagePullPolicy: IfNotPresent
              env:
                - name: MYSQL_ROOT_PASSWORD
                  value: mysql123
              #resources:
              #  requests:
              #    memory: 1Gi
              #    cpu: 1
              #  limits:
              #    memory: 2Gi
              #    cpu: 2
              ports:
                - containerPort: 3306
              volumeMounts:
                - name: mysql-config
                  mountPath: /etc/mysql/mysql.conf.d/
                - name: slave-config
                  mountPath: /etc/mysql/conf.d/
                - name: mysql-data
                  mountPath: /var/lib/mysql/data
    #            - name: time-cnf
    #              mountPath: /etc/localtime
    #            readOnly: true
          volumes:
            - name: mysql-data
              persistentVolumeClaim:
                claimName: mysql-slave-pvc
            - name: mysql-config
              configMap:
                name: yqt-configmap
                items:
                - key: my.cnf
                  path: mysqld.cnf
            - name: slave-config
              configMap:
                name: yqt-configmap
                items:
                - key: slave-docker.cnf
                  path: docker.cnf
    
    #        - name: time-cnf
    #          hostPath:
    #            path: /usr/share/zoneinfo/Asia/Shanghai
    ---
    apiVersion: v1
    kind: Service
    metadata:
      namespace: mysql-ns
      name: mysql-slave-service
    spec:
      type: NodePort
      ports:
      - port: 3306
        targetPort: 3306
      selector:
        app: mysql-slave
    

    启动mysql从节点,并且自动做主从同步
    kubectl create -f mysql-slave.yml

    相关文章

      网友评论

          本文标题:K8S-mysql主从部署2018-05-102018-05-3

          本文链接:https://www.haomeiwen.com/subject/vmctsftx.html