alreadydockerjava面试

docker-compose启动etcd集群

2022-07-01  本文已影响0人  玄德公笔记

1. 环境说明

节点 IP
etcd1 10.10.239.31
etcd2 10.10.239.32
etcd3 10.10.239.33

2. 部署

etcd1

version: "3.0"
services:
  etcd1:
    image: bitnami/etcd:latest
    container_name: etcd1
    restart: always
    ports:
      - "2379:2379"
      - "2380:2380"
    environment:
      - ALLOW_NONE_AUTHENTICATION=yes #示例中我们不设置密码
      - ETCD_NAME=etcd1 #节点自己的名字
      - ETCD_ADVERTISE_CLIENT_URLS=http://10.10.239.31:2379  #告知集群自己的客户端地址
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379 #设置监听客户端通讯的URL列表
      - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://10.10.239.31:2380 #告知集群自己集群通讯地址
      - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380 #用于监听伙伴通讯的URL列表
      - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster #etcd 集群的初始化集群记号
      - ETCD_INITIAL_CLUSTER=etcd1=http://10.10.239.31:2380,etcd2=http://10.10.239.32:2380,etcd3=http://10.10.239.33:2380 #集群成员
      - ETCD_INITIAL_CLUSTER_STATE=new #初始化集群状态
    volumes:
      - ./data:/bitnami/etcd

如果此时启动,会在当前目录下创建 ./data 目录,挂载到容器的 /bitnami/etcd ,但我们要注意的是,容器中启动etcd的用户并不是root用户,很可能没有在目录下创建文件的权限。我们有两个方案:

【方法一】给777权限(不建议)

【方法二】所属主改成对应宿主机用户(建议)

方法二操作:

  • 先给./data目录777权限
  • 查看新生成的文件属主是宿主机哪个用户,就将./data目录属主改为哪个用户
  • 再将权限改回755或750,重启容器
[root@liubei-01 etcd1]# docker-compose up -d
[root@liubei-01 etcd1]# docker-compose ps
Name               Command               State                       Ports
-----------------------------------------------------------------------------------------------
etcd1   /opt/bitnami/scripts/etcd/ ...   Up      0.0.0.0:2379->2379/tcp, 0.0.0.0:2380->2380/tcp

etcd2

version: "3.0"
services:
  etcd1:
    image: bitnami/etcd:latest
    container_name: etcd2
    restart: always
    ports:
      - "2379:2379"
      - "2380:2380"
    environment:
      - ALLOW_NONE_AUTHENTICATION=yes
      - ETCD_NAME=etcd2
      - ETCD_ADVERTISE_CLIENT_URLS=http://10.10.239.32:2379
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
      - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://10.10.239.32:2380
      - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
      - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
      - ETCD_INITIAL_CLUSTER=etcd1=http://10.10.239.31:2380,etcd2=http://10.10.239.32:2380,etcd3=http://10.10.239.33:2380
      - ETCD_INITIAL_CLUSTER_STATE=new
    volumes:
      - ./data:/bitnami/etcd

etcd3

version: "3.0"
services:
  etcd1:
    image: bitnami/etcd:latest
    container_name: etcd3
    restart: always
    ports:
      - "2379:2379"
      - "2380:2380"
    environment:
      - ALLOW_NONE_AUTHENTICATION=yes
      - ETCD_NAME=etcd3
      - ETCD_ADVERTISE_CLIENT_URLS=http://10.10.239.33:2379
      - ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
      - ETCD_INITIAL_ADVERTISE_PEER_URLS=http://10.10.239.33:2380
      - ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
      - ETCD_INITIAL_CLUSTER_TOKEN=etcd-cluster
      - ETCD_INITIAL_CLUSTER=etcd1=http://10.10.239.31:2380,etcd2=http://10.10.239.32:2380,etcd3=http://10.10.239.33:2380
      - ETCD_INITIAL_CLUSTER_STATE=new
    volumes:
      - ./data:/bitnami/etcd

3. 验证

[root@liubei-01 etcd3]# docker exec -it 788 bash
I have no name!@788dd01aaa79:/opt/bitnami/etcd$ etcd
etcd     etcdctl
I have no name!@788dd01aaa79:/opt/bitnami/etcd$ etcdctl put name "liubei"
OK
[root@liubei-02 ~]# docker exec -it 555 bash
I have no name!@555187fb758c:/opt/bitnami/etcd$ etcdctl get name
name
liubei

上一篇下一篇

猜你喜欢

热点阅读