compose文件在swarm中创建集群

简介

一、与上面环境不一致之处:
1.使用了etcd集群
集群地址为:10.0.102.214:2379,10.0.102.175:2379,10.0.102.191:2379
2.解决网络通信问题:
让docker stack 共用同一网络:因stack部署时会以stack名称创建网络,所以保持相同stack名字在同一网络中。如下所示:
docker stack deploy -c compose_swarm_1.yaml stack名
docker stack deploy -c compose_swarm_2.yaml stack名
docker stack deploy -c compose_swarm_3.yamlstack名

准备工作

各个节点创建需要挂载的目录及准备挂载的文件(如开启binlog的Mariadb配置文件)
#mkdir /data1/
#mkdir /etc/my.cnf.d
在k8s-node-3节点上创建开启binlog的mariadb配置文件
# vim /etc/my.cnf.d/bin-log.cnf
[mysqld]
log-bin= mysql-bin
log_slave_updates = 1
expire_logs_days = 20
server-id = 211

master节点部署操作

[root@node-1 ~]# docker stack deploy -c compose_swarm_1.yaml swarm_mariadb
 【见图1】
[root@node-1 ~]# docker stack deploy -c compose_swarm_2.yaml swarm_mariadb
 【见图2】
[root@node-1 ~]# docker stack deploy -c compose_swarm_3.yaml swarm_mariadb
  【见图3】
检查
[root@node-1 ~]#docker service ls
  【见图4】

[root@node-1 ~]# docker stack ps swarm_mariadb
 【见图5】
[root@node-1 ~]# docker exec -it a40e41c2219a /bin/bash
[root@a40e41c2219a /]# mysql -uroot –pmypassword
MariaDB [(none)]> show status like 'wsrep%';
  【见图6】
或者
[root@node-1 ~]# mysql -uroot -pmypassword -h node-1 -P 3311 -e "show status like 'wsrep%';"
1.png

2.png

3.png

4.png

5.png

6.png

yaml文件

compose_swarm_1.yaml

version: '3'
services:
  mariadb_galera_swarm0:  #service名,3个文件各不相同
    deploy:
      replicas: 1  #每个service下只有1个容器
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 10
        window: 100s
      placement:
        constraints: [node.hostname==node-1] #将容器运行在固定节点之上
      update_config:
        parallelism: 1
        delay: 3m # higher than SST duration

    image: severalnines/mariadb:10.1
    ports:
      - "3310:3306" #暴露的端口3个节点不能相同
    environment:  #创建集群相关环境务必一致
      CLUSTER_NAME: "mariadb_cluster_swarm"
      DISCOVERY_SERVICE: "10.0.102.218:2379,10.0.102.151:2379,10.0.102.162:2379"
      MYSQL_ROOT_PASSWORD: "mypassword"
      XTRABACKUP_PASSWORD: "mypassword"
    command:
      - --innodb_buffer_pool_size=256M
      - --max_connections=81
    volumes:  #根据需求进行挂载
      - /etc/my.cnf.d:/etc/my.cnf.d
      - /data2:/var/lib/mysql
      - /etc/localtime:/etc/localtime
    healthcheck:
      interval: 5s
      timeout: 3s
      retries: 200 # interval * retries > SST duration
    networks:
      - galera_swarm   #网络
networks:
  galera_swarm:
    driver: overlay

compose_swarm_2.yaml

version: '3'
services:
  mariadb_galera_swarm1:
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 10
        window: 100s
      placement:
        constraints: [node.hostname==node-2]
      update_config:
        parallelism: 1
        delay: 3m # higher than SST duration

    image: severalnines/mariadb:10.1
    ports:
      - "3312:3306"
    network_mode: host
    environment:
      CLUSTER_NAME: "mariadb_cluster_swarm"
      DISCOVERY_SERVICE: "10.0.102.218:2379,10.0.102.151:2379,10.0.102.162:2379"
      MYSQL_ROOT_PASSWORD: "mypassword"
      XTRABACKUP_PASSWORD: "mypassword"
    command:
      - --innodb_buffer_pool_size=256M
      - --max_connections=81
    volumes:
      - /etc/my.cnf.d:/etc/my.cnf.d
      - /data2:/var/lib/mysql
      - /etc/localtime:/etc/localtime
    healthcheck:
      interval: 5s
      timeout: 3s
      retries: 200 # interval * retries > SST duration
    networks:
      - galera_swarm
networks:
  galera_swarm:
    driver: overlay

compose_swarm_3.yaml

version: '3'
services:
  mariadb_galera_swarm2:
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 10
        window: 100s
      placement:
        constraints: [node.hostname==node-3]
      update_config:
        parallelism: 1
        delay: 3m # higher than SST duration

    image: severalnines/mariadb:10.1
    ports:
      - "3313:3306"
    network_mode: host
    environment:
      CLUSTER_NAME: "mariadb_cluster_swarm"
      DISCOVERY_SERVICE: "10.0.102.218:2379,10.0.102.151:2379,10.0.102.162:2379"
      MYSQL_ROOT_PASSWORD: "mypassword"
      XTRABACKUP_PASSWORD: "mypassword"
    command:
      - --innodb_buffer_pool_size=256M
      - --max_connections=81
    volumes:
      - /etc/my.cnf.d:/etc/my.cnf.d
      - /data2:/var/lib/mysql
      - /etc/localtime:/etc/localtime
    healthcheck:
      interval: 5s
      timeout: 3s
      retries: 200 # interval * retries > SST duration
    networks:
      - galera_swarm
networks:
  galera_swarm:
    driver: overla

你可能感兴趣的:(compose文件在swarm中创建集群)