k8s 启动mysql 主从

启动两个docker ,服务为mysql主从模式;

首先需要构建两个docker,使用mysql5.7的镜像作为基础镜像

1. mysql-master  Dockerfile

FROM mysql:latest

RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/shanghai" >> /etc/timezone

COPY mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf
COPY entrypoint.sh /
COPY init-master.sh /docker-entrypoint-initdb.d/

在当前目录下执行如下命令:

docker built -t mysql-master:v1.0 .

其他文件请参照文章末尾的GitHub链接地址

2. mysql-slave Dockerfile

FROM mysql:latest

RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/shanghai" >> /etc/timezone
COPY mysqld.cnf /etc/mysql/mysql.conf.d/
COPY entrypoint.sh /
COPY slave-init.sh /docker-entrypoint-initdb.d/

在当前目录下执行如下命令:

docker built -t mysql-slave:v1.0 .

其他文件请参照文章末尾的GitHub链接地址

3. 执行k8s 的yaml文件 master, 文件名:mysql-master.yaml

确保本地有 /data/mysql_data目录

apiVersion: v1
kind: Pod
metadata:
  name: master-tdbwv
  labels:
    name: master-tdbwv
spec:
  volumes:
    - name: data
      hostPath:
        path: /data/mysql_data
  containers:
    - name: master-tdbwv
      image: mysql-master:v1.0
      env:
        - name: MYSQL_ROOT_PASSWORD
          value: uggTKd3xsIMM
        - name: MYSQL_REPLICATION_USER
          value: repl
        - name: MYSQL_REPLICATION_PASSWORD
          value: RQfUjBff
      ports:
        - containerPort: 3306
          name: master-tdbwv
      volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
      resources:
          limits:
            cpu: 2
            memory: 200Mi
  nodeName: localhost
---
apiVersion: v1
kind: Service
metadata:
  name: master-tdbwv
  labels:
    name: master-tdbwv
spec:
  type: NodePort
  ports:
    - port: 3306      
      nodePort: 
  selector:
    name: master-tdbwv

4. 执行k8s 的yaml文件 slave, 文件名:mysql-slave.yaml

确保本地有 /data/mysql_slave_data目录

apiVersion: v1
kind: Pod
metadata:
  name: slave-ncrxq
  labels:
    name: slave-ncrxq
spec:
  volumes:
    - name: data
      hostPath:
        path: /data/mysql_slave_data

  containers:
    - name: slave-ncrxq
      image: mysql-slave:v1.0
      env:
        - name: MYSQL_ROOT_PASSWORD
          value: uggTKd3xsIMM
        - name: MYSQL_REPLICATION_USER
          value: repl
        - name: MYSQL_REPLICATION_PASSWORD
          value: RQfUjBff
        - name: MYSQL_MASTER_SERVICE_HOST
          value: 127.0.0.1
      ports:
        - containerPort: 3306
          name: slave-ncrxq
      volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
      resources:
          limits:
            cpu: 2
            memory: 200Mi
  nodeName: localhost
---
apiVersion: v1
kind: Service
metadata:
  name: slave-ncrxq
  labels:
    name: slave-ncrxq
spec:
  type: NodePort
  ports:
    - port: 3306      
      nodePort: 
  selector:
    name: slave-ncrxq

基本用法就是这样,具体文件请参照GitHub

https://github.com/haozhixin/docker-mysql

转载于:https://my.oschina.net/FrankXin/blog/875414

你可能感兴趣的:(k8s 启动mysql 主从)