文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

docker如何部署etcd集群

2023-03-19 18:05

关注

需要安装:

参数详细:

创建etcd数据目录

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

创建docker网络

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

etcd-cluster-compose.yml

version: '3'

networks:
  etcd-cluster:
    external: true

services:
  etcd-node1:
    image: quay.io/coreos/etcd:v3.3.1
    container_name: etcd-node1
    ports:
      - "12379:2379"
      - "12380:2380"
    restart: always
    volumes:
      - ./etcd-node1:/data/app/etcd
    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
    networks:
      etcd-cluster:
        ipv4_address: 172.62.0.10

  etcd-node2:
    image: quay.io/coreos/etcd:v3.3.1
    container_name: etcd-node2
    ports:
      - "22379:2379"
      - "22380:2380"
    restart: always
    volumes:
      - ./etcd-node2:/data/app/etcd
    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
    networks:
      etcd-cluster:
        ipv4_address: 172.62.0.11

  etcd-node3:
    image: quay.io/coreos/etcd:v3.3.1
    container_name: etcd-node3
    ports:
      - "32379:2379"
      - "32380:2380"
    restart: always
    volumes:
      - ./etcd-node3:/data/app/etcd
    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
    networks:
      etcd-cluster:
        ipv4_address: 172.62.0.12

启动并验证集群

启动

[root@k8s-node1 etcd-cluster]# docker-compose -f etcd-cluster-compose.yml up -d
Pulling etcd-node1 (quay.io/coreos/etcd:v3.3.1)...
v3.3.1: Pulling from coreos/etcd
ff3a5c916c92: Pull complete
dec5fcc85a18: Pull complete
3944f16f0112: Pull complete
0b6d29b049fe: Pull complete
d8c39ae91d38: Pull complete
42fcea4864ba: Pull complete
Digest: sha256:454e69370d87554dcb4272833b8f07ce1b5d457caa153bda4070b76d89a1cc97
Status: Downloaded newer image for quay.io/coreos/etcd:v3.3.1
Creating etcd-node1 ... done
Creating etcd-node2 ... done
Creating etcd-node3 ... done
[root@k8s-node1 etcd-cluster]# docker-compose -f etcd-cluster-compose.yml ps -a
   Name                 Command               State                        Ports
------------------------------------------------------------------------------------------------------
etcd-node1   etcd --name etcd-node1 --d ...   Up      0.0.0.0:12379->2379/tcp, 0.0.0.0:12380->2380/tcp
etcd-node2   etcd --name etcd-node2 --d ...   Up      0.0.0.0:22379->2379/tcp, 0.0.0.0:22380->2380/tcp
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命令可以查询出所有集群节点的列表并且结果一致即为成功

[root@k8s-node1 etcd-cluster]# docker exec -it  etcd-node1 /bin/sh
/ # etcdctl member list
8cef47d732d4acff: name=etcd-node1 peerURLs=http://172.62.0.10:2380 clientURLs=http://172.62.0.10:2379 isLeader=false
c93af917b643516f: name=etcd-node3 peerURLs=http://172.62.0.12:2380 clientURLs=http://172.62.0.12:2379 isLeader=true
cdee7114ad135065: name=etcd-node2 peerURLs=http://172.62.0.11:2380 clientURLs=http://172.62.0.11:2379 isLeader=false
/ # exit
[root@k8s-node1 etcd-cluster]# docker exec -it  etcd-node2 /bin/sh
/ # etcdctl member list
8cef47d732d4acff: name=etcd-node1 peerURLs=http://172.62.0.10:2380 clientURLs=http://172.62.0.10:2379 isLeader=false
c93af917b643516f: name=etcd-node3 peerURLs=http://172.62.0.12:2380 clientURLs=http://172.62.0.12:2379 isLeader=true
cdee7114ad135065: name=etcd-node2 peerURLs=http://172.62.0.11:2380 clientURLs=http://172.62.0.11:2379 isLeader=false
[root@k8s-node1 etcd-cluster]# docker exec -it  etcd-node3 /bin/sh
/ # etcdctl member list
8cef47d732d4acff: name=etcd-node1 peerURLs=http://172.62.0.10:2380 clientURLs=http://172.62.0.10:2379 isLeader=false
c93af917b643516f: name=etcd-node3 peerURLs=http://172.62.0.12:2380 clientURLs=http://172.62.0.12:2379 isLeader=true
cdee7114ad135065: name=etcd-node2 peerURLs=http://172.62.0.11:2380 clientURLs=http://172.62.0.11:2379 isLeader=false

k/v操作

CURL

新增

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

查询

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

修改

同新建一样

删除

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

etcdctl

新增

/ # etcdctl set clownfish 1234567
1234567
/ # etcdctl get clownfish
1234567

查询

/ # etcdctl get clownfish
1234567

修改

/ # etcdctl get clownfish
1234567
/ # etcdctl set clownfish 987654321ddd
987654321ddd
/ # etcdctl get clownfish
987654321ddd

删除

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

总结

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

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-服务器
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯