docker怎么部署etcd集群

其他教程   发布日期:2023年08月31日   浏览次数:529

本篇内容介绍了“docker怎么部署etcd集群”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

需要安装:

    1. docker
    1. docker-compose

参数详细:

    1. –name
    :设置成员节点的别名,建议为每个成员节点配置可识别的命名
    1. –advertise-client-urls
    :广播到集群中本成员的监听客户端请求的地址
    1. –initial-advertise-peer-urls
    :广播到集群中本成员的Peer监听通信地址
    1. –listen-client-urls
    :客户端请求的监听地址列表
    1. –listen-peer-urls
    :Peer消息的监听服务地址列表
    1. –initial-cluster-token
    :启动集群的时候指定集群口令,只有相同token的几点才能加入到同一集群
    1. –initial-cluster
    :所有集群节点的地址列表
    1. –initial-cluster-state
    :初始化集群状态,默认为new,也可以指定为exi-string表示要加入到一个已有集群

创建etcd数据目录

  1. mkdir -p ./etcd-node{1,2,3}

创建docker网络

  1. docker network create --driver bridge --subnet 172.62.0.0/16 --gateway 172.62.0.1 etcd-cluster

etcd-cluster-compose.yml

  1. version: '3'
  2. networks:
  3. etcd-cluster:
  4. external: true
  5. services:
  6. etcd-node1:
  7. image: quay.io/coreos/etcd:v3.3.1
  8. container_name: etcd-node1
  9. ports:
  10. - "12379:2379"
  11. - "12380:2380"
  12. restart: always
  13. volumes:
  14. - ./etcd-node1:/data/app/etcd
  15. command: etcd --name etcd-node1 --data-dir /data/app/etcd/ --advertise-client-urls http://172.62.0.10:2379 --initial-advertise-peer-urls http://172.62.0.10:2380 --listen-client-urls http://0.0.0.0:2379 --listen-peer-urls http://0.0.0.0:2380 --initial-cluster-token etcd-cluster --initial-cluster "etcd-node1=http://172.62.0.10:2380,etcd-node2=http://172.62.0.11:2380,etcd-node3=http://172.62.0.12:2380" --initial-cluster-state new
  16. networks:
  17. etcd-cluster:
  18. ipv4_address: 172.62.0.10
  19. etcd-node2:
  20. image: quay.io/coreos/etcd:v3.3.1
  21. container_name: etcd-node2
  22. ports:
  23. - "22379:2379"
  24. - "22380:2380"
  25. restart: always
  26. volumes:
  27. - ./etcd-node2:/data/app/etcd
  28. command: etcd --name etcd-node2 --data-dir /data/app/etcd/ --advertise-client-urls http://172.62.0.11:2379 --initial-advertise-peer-urls http://172.62.0.11:2380 --listen-client-urls http://0.0.0.0:2379 --listen-peer-urls http://0.0.0.0:2380 --initial-cluster-token etcd-cluster --initial-cluster "etcd-node1=http://172.62.0.10:2380,etcd-node2=http://172.62.0.11:2380,etcd-node3=http://172.62.0.12:2380" --initial-cluster-state new
  29. networks:
  30. etcd-cluster:
  31. ipv4_address: 172.62.0.11
  32. etcd-node3:
  33. image: quay.io/coreos/etcd:v3.3.1
  34. container_name: etcd-node3
  35. ports:
  36. - "32379:2379"
  37. - "32380:2380"
  38. restart: always
  39. volumes:
  40. - ./etcd-node3:/data/app/etcd
  41. command: etcd --name etcd-node3 --data-dir /data/app/etcd/ --advertise-client-urls http://172.62.0.12:2379 --initial-advertise-peer-urls http://172.62.0.12:2380 --listen-client-urls http://0.0.0.0:2379 --listen-peer-urls http://0.0.0.0:2380 --initial-cluster-token etcd-cluster --initial-cluster "etcd-node1=http://172.62.0.10:2380,etcd-node2=http://172.62.0.11:2380,etcd-node3=http://172.62.0.12:2380" --initial-cluster-state new
  42. networks:
  43. etcd-cluster:
  44. ipv4_address: 172.62.0.12

启动并验证集群

启动

  1. [root@k8s-node1 etcd-cluster]# docker-compose -f etcd-cluster-compose.yml up -d
  2. Pulling etcd-node1 (quay.io/coreos/etcd:v3.3.1)...
  3. v3.3.1: Pulling from coreos/etcd
  4. ff3a5c916c92: Pull complete
  5. dec5fcc85a18: Pull complete
  6. 3944f16f0112: Pull complete
  7. 0b6d29b049fe: Pull complete
  8. d8c39ae91d38: Pull complete
  9. 42fcea4864ba: Pull complete
  10. Digest: sha256:454e69370d87554dcb4272833b8f07ce1b5d457caa153bda4070b76d89a1cc97
  11. Status: Downloaded newer image for quay.io/coreos/etcd:v3.3.1
  12. Creating etcd-node1 ... done
  13. Creating etcd-node2 ... done
  14. Creating etcd-node3 ... done
  1. [root@k8s-node1 etcd-cluster]# docker-compose -f etcd-cluster-compose.yml ps -a
  2. Name Command State Ports
  3. ------------------------------------------------------------------------------------------------------
  4. etcd-node1 etcd --name etcd-node1 --d ... Up 0.0.0.0:12379->2379/tcp, 0.0.0.0:12380->2380/tcp
  5. etcd-node2 etcd --name etcd-node2 --d ... Up 0.0.0.0:22379->2379/tcp, 0.0.0.0:22380->2380/tcp
  6. etcd-node3 etcd --name etcd-node3 --d ... Up 0.0.0.0:32379->2379/tcp, 0.0.0.0:32380->2380/tcp

验证集群

通过etcdctl member list命令可以查询出所有集群节点的列表并且结果一致即为成功

  1. [root@k8s-node1 etcd-cluster]# docker exec -it etcd-node1 /bin/sh
  2. / # etcdctl member list
  3. 8cef47d732d4acff: name=etcd-node1 peerURLs=http://172.62.0.10:2380 clientURLs=http://172.62.0.10:2379 isLeader=false
  4. c93af917b643516f: name=etcd-node3 peerURLs=http://172.62.0.12:2380 clientURLs=http://172.62.0.12:2379 isLeader=true
  5. cdee7114ad135065: name=etcd-node2 peerURLs=http://172.62.0.11:2380 clientURLs=http://172.62.0.11:2379 isLeader=false
  6. / # exit
  7. [root@k8s-node1 etcd-cluster]# docker exec -it etcd-node2 /bin/sh
  8. / # etcdctl member list
  9. 8cef47d732d4acff: name=etcd-node1 peerURLs=http://172.62.0.10:2380 clientURLs=http://172.62.0.10:2379 isLeader=false
  10. c93af917b643516f: name=etcd-node3 peerURLs=http://172.62.0.12:2380 clientURLs=http://172.62.0.12:2379 isLeader=true
  11. cdee7114ad135065: name=etcd-node2 peerURLs=http://172.62.0.11:2380 clientURLs=http://172.62.0.11:2379 isLeader=false
  12. [root@k8s-node1 etcd-cluster]# docker exec -it etcd-node3 /bin/sh
  13. / # etcdctl member list
  14. 8cef47d732d4acff: name=etcd-node1 peerURLs=http://172.62.0.10:2380 clientURLs=http://172.62.0.10:2379 isLeader=false
  15. c93af917b643516f: name=etcd-node3 peerURLs=http://172.62.0.12:2380 clientURLs=http://172.62.0.12:2379 isLeader=true
  16. cdee7114ad135065: name=etcd-node2 peerURLs=http://172.62.0.11:2380 clientURLs=http://172.62.0.11:2379 isLeader=false

k/v操作

CURL

新增

  1. [root@k8s-node1 etcd-cluster]# docker-compose -f etcd-cluster-compose.yml ps -a
  2. Name Command State Ports
  3. ------------------------------------------------------------------------------------------------------
  4. etcd-node1 etcd --name etcd-node1 --d ... Up 0.0.0.0:12379->2379/tcp, 0.0.0.0:12380->2380/tcp
  5. etcd-node2 etcd --name etcd-node2 --d ... Up 0.0.0.0:22379->2379/tcp, 0.0.0.0:22380->2380/tcp
  6. etcd-node3 etcd --name etcd-node3 --d ... Up 0.0.0.0:32379->2379/tcp, 0.0.0.0:32380->2380/tcp
  7. [root@k8s-node1 etcd-cluster]# curl -L http://127.0.0.1:12379/version
  8. {"etcdserver":"3.3.1","etcdcluster":"3.3.0"}[root@k8s-node1 etcd-cluster]#
  9. [root@k8s-node1 etcd-cluster]# curl -L http://127.0.0.1:12379/v2/keys/Alexclownfish -X PUT -d value=https://blog.alexcld.com
  10. {"action":"set","node":{"key":"/Alexclownfish","value":"https://blog.alexcld.com","modifiedIndex":19,"createdIndex":19}}

查询

  1. [root@k8s-node1 etcd-cluster]# curl -L http://127.0.0.1:12379/v2/keys/Alexclownfish -X GET
  2. {"action":"get","node":{"key":"/Alexclownfish","value":"https://blog.alexcld.com","modifiedIndex":19,"createdIndex":19}}
  3. [root@k8s-node1 etcd-cluster]# curl -L http://127.0.0.1:22379/v2/keys/Alexclownfish -X GET
  4. {"action":"get","node":{"key":"/Alexclownfish","value":"https://blog.alexcld.com","modifiedIndex":19,"createdIndex":19}}
  5. [root@k8s-node1 etcd-cluster]# curl -L http://127.0.0.1:32379/v2/keys/Alexclownfish -X GET
  6. {"action":"get","node":{"key":"/Alexclownfish","value":"https://blog.alexcld.com","modifiedIndex":19,"createdIndex":19}}

修改

同新建一样

删除

  1. [root@k8s-node1 etcd-cluster]# curl -L http://127.0.0.1:12379/v2/keys/Alexclownfish -X DELETE
  2. {"action":"delete","node":{"key":"/Alexclownfish","modifiedIndex":20,"createdIndex":19},"prevNode":{"key":"/Alexclownfish","value":"https://blog.alexcld.com","modifiedIndex":19,"createdIndex":19}}
  3. [root@k8s-node1 etcd-cluster]# curl -L http://127.0.0.1:12379/v2/keys/Alexclownfish -X GET
  4. {"errorCode":100,"message":"Key not found","cause":"/Alexclownfish","index":20}

etcdctl

新增

  1. / # etcdctl set clownfish 1234567
  2. 1234567
  3. / # etcdctl get clownfish
  4. 1234567

查询

  1. / # etcdctl get clownfish
  2. 1234567

修改

  1. / # etcdctl get clownfish
  2. 1234567
  3. / # etcdctl set clownfish 987654321ddd
  4. 987654321ddd
  5. / # etcdctl get clownfish
  6. 987654321ddd

删除

  1. / # etcdctl rm clownfish
  2. PrevNode.Value: 987654321ddd
  3. / # etcdctl get clownfish
  4. Error: 100: Key not found (/clownfish) [23]

以上就是docker怎么部署etcd集群的详细内容,更多关于docker怎么部署etcd集群的资料请关注九品源码其它相关文章!