etcd集群部署(持续更新)

  • Post author:
  • Post category:其他




集群部署

切换命令行版本到v3:

export ETCDCTL_API=3



本地集群部署

官方工具:goreman。提供基于Procfile配置文件的方式,部署简单。

实际使用时,考虑服务的可用性,一般采用多机集群。本地集群可用于测试时快速搭建服务,具体操作参考

官方文档

,个人推荐测试时也是用多机集群部署方案。




多机集群

参考自

官方文档

。共有三种集群部署方案:

假设要部署的三台机器:

  • 192.168.1.11
  • 192.168.1.12
  • 192.168.1.13

集群配置参数详解:

etcd --help查看

–name:当前节点唯一名称

–data-dir:数据存储目录,可用于备份、恢复。

–initial-advertise-peer-urls:用于节点成员间的识别地址。

–initial-cluster:所有集群成员的识别地址,initial-advertise-peer-urls集合。

–initial-cluster-token:集群的唯一标识,token不变,重启时复用集群,否则新建。

–initial-cluster-state:集群状态,新建时填new。

–listen-peer-urls:与其他节点成员间通信的监听地址,≥1。

–listen-client-urls:用于监听客户端请求的地址,≥1。

–advertise-client-urls:用于监听所有请求,广义上的,包括客户端、其他成员、代理等。




1. 静态部署

要求:启动前需要知道所有etcd成员地址。

将以下shell脚本(替换HOST_1-3)分别在三台对应的机器上直接执行即可。

#!/bin/bash
set -e

HOST_1=192.168.1.11
HOST_2=192.168.1.12
HOST_3=192.168.1.13
NAME_1=machine-1
NAME_2=machine-2
NAME_3=machine-3
# 数据存储目录
DATADIR=data.etcd

# 获取本机ip
IP=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`

# 启动时清空旧的数据
rm -fr $DATADIR

# 获取本机名称
if [[ $IP =~ $HOST_1 ]]; then 
    IP=$HOST_1
    NAME=${NAME_1}

elif [[ $IP =~ $HOST_2 ]]; then
    IP=$HOST_2
    NAME=${NAME_2}

elif [[ $IP =~ $HOST_3 ]]; then
    IP=$HOST_3
    NAME=${NAME_3}
    
else
    echo "deploy on incorrected machine:" $IP
    exit
fi

# 启动节点
etcd --name $NAME \
  --data-dir=$DATADIR
  --initial-advertise-peer-urls http://$IP:2380 \
  --listen-peer-urls http://$IP:2380 \
  # 注:http://127.0.0.1:2379,允许本地访问,方便操作
  --listen-client-urls http://$IP:2379,http://127.0.0.1:2379 \
  --advertise-client-urls http://$IP:2379 \
  --initial-cluster $NAME_1=http://$HOST_1:2380,$NAME_2=http://$HOST_2:2380,$NAME_3=http://$HOST_3:2380 \
  --initial-cluster-token etcd-cluster-01 \
  --initial-cluster-state new



1.1 查看集群成员列表:


etcdctl --endpoints=192.168.1.11:2379,192.168.1.12:2379,192.168.1.13:2379 member list

如果listen-client-urls指定了http://127.0.0.1:2379,则可以简化操作命令:

etcdctl member list

8e9e05c52164694d, started, machine-1,

http://192.168.1.11:2380

,

http://192.168.1.11:2379


75e05c5dg64694qs, started, machine-2,

http://192.168.1.12:2380

,

http://192.168.1.12:2379


3kf5c5216469qo5d, started, machine-3,

http://192.168.1.13:2380

,

http://192.168.1.13:2379



1.2 查看集群成员健康状态:


etcdctl --endpoints=192.168.1.11:2379,192.168.1.12:2379,192.168.1.13:2379 endpoint health

如果listen-client-urls指定了http://127.0.0.1:2379,则可以简化操作命令:

etcdctl endpoint health

192.168.1.11:2379 is healthy: successfully committed proposal: took = 2.322755ms

192.168.1.12:2379 is healthy: successfully committed proposal: took = 2.322755ms

192.168.1.13:2379 is healthy: successfully committed proposal: took = 2.322755ms




2. etcd发现部署

要求:已经存在一个etcd服务,基于此服务创建新的集群。

可以理解为,用一个已有的etcd来管理集群成员的注册信息。

(未完待续)




3. DNS发现部署

(未完待续)



版权声明:本文为ys5773477原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。