docker-compose部署etcd集群

1. docker-compose.yml

cat > docker-compose.yml << EOF
version: "3.0"

networks:
  etcd-net:           # 网络
    driver: bridge    # 桥接模式

volumes:
  etcd1_data:         # 挂载到本地的数据卷名
    driver: local
  etcd2_data:
    driver: local
  etcd3_data:
    driver: local
###
### etcd 其他环境配置见:https://doczhcn.gitbook.io/etcd/index/index-1/configuration
###
services:
  etcd1:
    image: bitnami/etcd:latest  # 镜像
    container_name: etcd1       # 容器名 --name
    restart: always             # 总是重启
    networks:
      - etcd-net                # 使用的网络 --network
    ports:                      # 端口映射 -p
      - "20000:2379"
      - "20001:2380"
    environment:                # 环境变量 --env
      - ALLOW_NONE_AUTHENTICATION=yes                       # 允许不用密码登录
      - ETCD_NAME=etcd1                                     # etcd 的名字
      - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd1:2380  # 列出这个成员的伙伴 URL 以便通告给集群的其他成员
      - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380           # 用于监听伙伴通讯的URL列表
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379         # 用于监听客户端通讯的URL列表
      - ETCD_ADVERTISE_CLIENT_URLS=http://etcd1:2379        # 列出这个成员的客户端URL,通告给集群中的其他成员
      - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster             # 在启动期间用于 etcd 集群的初始化集群记号
      - ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380 # 为启动初始化集群配置
      - ETCD_INITIAL_CLUSTER_STATE=new                      # 初始化集群状态
    volumes:
      - etcd1_data:/bitnami/etcd                            # 挂载的数据卷

  etcd2:
    image: bitnami/etcd:latest
    container_name: etcd2
    restart: always
    networks:
      - etcd-net
    ports:
      - "20002:2379"
      - "20003:2380"
    environment:
      - ALLOW_NONE_AUTHENTICATION=yes
      - ETCD_NAME=etcd2
      - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd2:2380
      - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
      - ETCD_ADVERTISE_CLIENT_URLS=http://etcd2:2379
      - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
      - ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
      - ETCD_INITIAL_CLUSTER_STATE=new
    volumes:
      - etcd2_data:/bitnami/etcd

  etcd3:
    image: bitnami/etcd:latest
    container_name: etcd3
    restart: always
    networks:
      - etcd-net
    ports:
      - "20004:2379"
      - "20005:2380"
    environment:
      - ALLOW_NONE_AUTHENTICATION=yes
      - ETCD_NAME=etcd3
      - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://etcd3:2380
      - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
      - ETCD_ADVERTISE_CLIENT_URLS=http://etcd3:2379
      - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
      - ETCD_INITIAL_CLUSTER=etcd1=http://etcd1:2380,etcd2=http://etcd2:2380,etcd3=http://etcd3:2380
      - ETCD_INITIAL_CLUSTER_STATE=new
    volumes:
      - etcd3_data:/bitnami/etcd
EOF

docker-compose up -d #运行

root@k8s-master:~#  docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS         PORTS                                                                                      NAMES
db11e79ee1c1   bitnami/etcd:latest   "/opt/bitnami/script…"   8 minutes ago   Up 8 minutes   0.0.0.0:20004->2379/tcp, :::20004->2379/tcp, 0.0.0.0:20005->2380/tcp, :::20005->2380/tcp   etcd3
249d8c83b745   bitnami/etcd:latest   "/opt/bitnami/script…"   8 minutes ago   Up 8 minutes   0.0.0.0:20002->2379/tcp, :::20002->2379/tcp, 0.0.0.0:20003->2380/tcp, :::20003->2380/tcp   etcd2
7d0a06d1d3c9   bitnami/etcd:latest   "/opt/bitnami/script…"   8 minutes ago   Up 8 minutes   0.0.0.0:20000->2379/tcp, :::20000->2379/tcp, 0.0.0.0:20001->2380/tcp, :::20001->2380/tcp   etcd1
root@k8s-master:~# 
root@k8s-master:~#  docker volume ls  #查看挂载的数据卷
DRIVER    VOLUME NAME
local     root_etcd1_data
local     root_etcd2_data
local     root_etcd3_data
root@k8s-master:~# 
root@k8s-master:~# docker inspect etcd1 |grep Source
                "Source": "/var/lib/docker/volumes/root_etcd1_data/_data",
root@k8s-master:~# 

2. 测试节点

从etcd1写一个key

root@k8s-master:~# docker exec -it etcd1 bash
I have no name!@7d0a06d1d3c9:/opt/bitnami/etcd$ etcdctl put name "i am mqq"
OK
I have no name!@7d0a06d1d3c9:/opt/bitnami/etcd$ exit

从etcd2读一个value

root@k8s-master:~# docker exec -it etcd2 bash
I have no name!@249d8c83b745:/opt/bitnami/etcd$ etcdctl get name
name
i am mqq
I have no name!@249d8c83b745:/opt/bitnami/etcd$ exit

从etcd3读一个value

root@k8s-master:~# docker exec -it etcd3  bash
I have no name!@db11e79ee1c1:/opt/bitnami/etcd$ etcdctl get name
name
i am mqq
I have no name!@db11e79ee1c1:/opt/bitnami/etcd$ 

搭建成功!

你可能感兴趣的:(docker容器相关,linux,docker,etcd,容器)