经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Docker » 查看文章
docker如何部署etcd集群
来源:jb51  时间:2023/3/17 8:57:50  对本文有异议

需要安装:

  • docker
  • docker-compose

参数详细:

  • –name:设置成员节点的别名,建议为每个成员节点配置可识别的命名
  • –advertise-client-urls:广播到集群中本成员的监听客户端请求的地址
  • –initial-advertise-peer-urls:广播到集群中本成员的Peer监听通信地址
  • –listen-client-urls:客户端请求的监听地址列表
  • –listen-peer-urls:Peer消息的监听服务地址列表
  • –initial-cluster-token:启动集群的时候指定集群口令,只有相同token的几点才能加入到同一集群
  • –initial-cluster:所有集群节点的地址列表
  • –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.  
  3. networks:
  4. etcd-cluster:
  5. external: true
  6.  
  7. services:
  8. etcd-node1:
  9. image: quay.io/coreos/etcd:v3.3.1
  10. container_name: etcd-node1
  11. ports:
  12. - "12379:2379"
  13. - "12380:2380"
  14. restart: always
  15. volumes:
  16. - ./etcd-node1:/data/app/etcd
  17. 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
  18. networks:
  19. etcd-cluster:
  20. ipv4_address: 172.62.0.10
  21.  
  22. etcd-node2:
  23. image: quay.io/coreos/etcd:v3.3.1
  24. container_name: etcd-node2
  25. ports:
  26. - "22379:2379"
  27. - "22380:2380"
  28. restart: always
  29. volumes:
  30. - ./etcd-node2:/data/app/etcd
  31. 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
  32. networks:
  33. etcd-cluster:
  34. ipv4_address: 172.62.0.11
  35.  
  36. etcd-node3:
  37. image: quay.io/coreos/etcd:v3.3.1
  38. container_name: etcd-node3
  39. ports:
  40. - "32379:2379"
  41. - "32380:2380"
  42. restart: always
  43. volumes:
  44. - ./etcd-node3:/data/app/etcd
  45. 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
  46. networks:
  47. etcd-cluster:
  48. 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]

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号