docker如何部署etcd集群

目录
  • 创建etcd数据目录
  • 创建docker网络
  • etcd-cluster-compose.yml
  • 启动并验证集群
    • 启动
    • 验证集群
  • k/v操作
    • CURL
    • etcdctl
  • 总结

需要安装:

  • 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数据目录

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]

总结

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

(0)

相关推荐

  • 基于Docker的Etcd分布式部署的方法步骤

    一 环境准备 1.1 基础环境 ntp配置:略 #建议配置ntp服务,保证时间一致性 etcd版本:v3.3.9 防火墙及SELinux:关闭防火墙和SELinux 名称 地址 主机名 备注 etcd1 172.24.8.71 etcd1.example.com 用于保存相关IP信息 docker01 172.24.8.72 docker01.example.com   docker02 172.24.8.73 docker02.example.com   # hostnamectl set-h

  • docker-compose部署etcd集群的实现步骤

    目录 编写docker-compose.yml 运行docker-compose 检查搭建状态 测试节点 Golang 与 etcd 简单交互 编写docker-compose.yml version: "3.0" networks: etcd-net: # 网络 driver: bridge # 桥接模式 volumes: etcd1_data: # 挂载到本地的数据卷名 driver: local etcd2_data: driver: local etcd3_data: driv

  • 十分钟学会用docker部署微服务

    2013年发布至今, Docker 一直广受瞩目,被认为可能会改变软件行业. 但是,许多人并不清楚 Docker 到底是什么,要解决什么问题,好处又在哪里?今天就来详细解释,帮助大家理解它,还带有简单易懂的实例,教你如何将它用于日常开发并用其部署微服务. 一. Docker简介 Docker是一个开源的容器引擎,它有助于更快地交付应用. Docker可将应用程序和基础设施层隔离,并且能将基础设施当作程序一样进行管理.使用 Docker可更快地打包.测试以及部署应用程序,并可以缩短从编写到部署运行

  • docker如何部署etcd集群

    目录 创建etcd数据目录 创建docker网络 etcd-cluster-compose.yml 启动并验证集群 启动 验证集群 k/v操作 CURL etcdctl 总结 需要安装: docker docker-compose 参数详细: –name:设置成员节点的别名,建议为每个成员节点配置可识别的命名 –advertise-client-urls:广播到集群中本成员的监听客户端请求的地址 –initial-advertise-peer-urls:广播到集群中本成员的Peer监听通信地址

  • 聊聊docker 单机部署redis集群的问题

    目录 docker部署redis集群 1.创建redis网卡 2.创建redis配置 使用cluster集群配置 3.创建redis集群 测试 docker 部署redis集群 1.创建redis网卡 docker network create redis --subnet 172.38.0.0/16 查看网卡信息 docker network ls docker network inspect redis 2.创建redis配置 #使用脚本创建6个redis配置 for port in $(s

  • 使用docker快速部署Elasticsearch集群的方法

    本文将使用Docker容器(使用docker-compose编排)快速部署Elasticsearch 集群,可用于开发环境(单机多实例)或生产环境部署. 注意,6.x版本已经不能通过 -Epath.config 参数去指定配置文件的加载位置,文档说明: For the archive distributions, the config directory location defaults to $ES_HOME/config. The location of the >config direc

  • Docker Stack 部署web集群的方法步骤

    Docker越来越成熟,功能也越来越强大.使用Dokcer Stack做服务集群也是非常的方便,docker自己就提供了负载功能,感觉很方便,就想给大家分享一下,做一个简单的教程. 环境 我是用了两台centos7的虚拟机来做这个教程他们的ip分别是 主服务器:192.168.0.105 // 也是私有仓库服务器 服务器2: 192.168.0.49 这篇帖子中所有的代码 github地址:https://github.com/lpxxn/godockerswarm 设置Docker Swarm

  • Docker微服务的ETCD集群搭建教程详解

    目录 etcd的特性 Etcd构建自身高可用集群主要有三种形式 本次搭建的基础环境 1.将服务器挨个添加进集群 2.将服务器统一添加进集群 etcd api接口 服务注册与发现 etcd是一个高可用的键值存储系统,主要用于共享配置和服务发现.etcd是由CoreOS开发并维护的,灵感来自于 ZooKeeper 和 Doozer,它使用Go语言编写,并通过Raft一致性算法处理日志复制以保证强一致性.Raft是一个来自Stanford的新的一致性算法,适用于分布式系统的日志复制,Raft通过选举的

  • 使用docker部署hadoop集群的详细教程

    最近要在公司里搭建一个hadoop测试集群,于是采用docker来快速部署hadoop集群. 0. 写在前面 网上也已经有很多教程了,但是其中都有不少坑,在此记录一下自己安装的过程. 目标:使用docker搭建一个一主两从三台机器的hadoop2.7.7版本的集群 准备: 首先要有一台内存8G以上的centos7机器,我用的是阿里云主机. 其次将jdk和hadoop包上传到服务器中. 我安装的是hadoop2.7.7.包给大家准备好了,链接:https://pan.baidu.com/s/15n

  • Docker部署Mysql集群的实现

    单节点数据库的弊病 大型互联网程序用户群体庞大,所以架构必须要特殊设计 单节点的数据库无法满足性能上的要求 单节点的数据库没有冗余设计,无法满足高可用 单节点MySQL的性能瓶领颈 2016年春节微信红包巨大业务量,数据库承受巨大负载 常见MySQL集群方案 mysql 集群方案介绍,建议使用pxc,因为弱一致性会有问题,比如说a节点数据库显示我购买成功,b 节点数据库显示没有成功,这就麻烦了,pxc 方案是在全部节点都写入成功之后才会告诉你成功,是可读可写双向同步的,但是replication

  • Docker部署MySQL8集群(一主二从)的实现步骤

    目录 一.CentOS7.9安装Docker20 二.部署MySQL集群(一主二从) 三.结果 一.CentOS7.9安装Docker20 1.安装yum-utils工具 yum install -y yum-utils 2.设置docker的依赖源 yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo 注释:CentOS直接使用yum命令安装的Docker版本为1.13.1属于

  • 基于Docker部署Tomcat集群、 Nginx负载均衡的问题小结

    写在前面 看完Dokcer相关的书籍,正好有个项目要这样搞,所以自己练习一下. 当作一百世一样.这里的道理很明白:我思故我在,既然我存在,就不能装作不存在.无论如何,我要为自己负起责任.--王小波<三十而立> 结构图: 这里仅作为一种学习,一般这种负载的话,Nginx是放到主机侧的, JavaWeb(Tomcat)应用放到容器里. 效果 新建文件夹. D=uag;mkdir $D;cd $D;mkdir uag_nginx uag_tomcat8; ls uag_nginx uag_tomca

随机推荐