Etcd集群部署


官网:etcd

其一:用到的部分参数详解

--name 节点名称

default: "default"
env variable: ETCD_NAME

这个值和--initial-cluster flag (e.g., default=http://localhost:2380)中的key值一一对应,如果在集群环境中,name必须是唯一的,建议用主机名称或者机器ID。

--data-dir 数据存储目录

default: "${name}.etcd"
env variable: ETCD_DATA_DIR

--listen-peer-urls

本节点与其他节点进行数据交换(选举,数据同步)的监听地址,地址写法是 scheme://IP:port,可以多个并用逗号隔开,如果配置是http://0.0.0.0:2379,将不限制node访问地址

default: "http://localhost:2380"
env variable: ETCD_LISTEN_PEER_URLS
example: "http://10.0.0.1:2380"
invalid example: "http://example.com:2380" (domain name is invalid for binding)

--listen-client-urls

本节点访问地址,地址写法是 scheme://IP:port,可以多个并用逗号隔开,如果配置是http://0.0.0.0:2379,将不限制node访问地址

default: "http://localhost:2379"
env variable: ETCD_LISTEN_CLIENT_URLS
example: "http://10.0.0.1:2379"
invalid example: "http://example.com:2379" (domain name is invalid for binding)

--initial-advertise-peer-urls

通知其他节点与本节点进行数据交换(选举,同步)的地址,URL可以使用domain地址。

与--listener-peer-urls不同在于listener-peer-urls用于请求客户端的接入控制,initial-advertise-peer-urls是告知其他集群节点访问哪个URL,一般来说,initial-advertise-peer-urlsl将是istener-peer-urls的子集

default: "http://localhost:2380"
env variable: ETCD_INITIAL_ADVERTISE_PEER_URLS
example: "http://example.com:2380, http://10.0.0.1:2380"

--initial-cluster

集群所有节点配置,多个用逗号隔开。

default: "default=http://localhost:2380"
env variable: ETCD_INITIAL_CLUSTER
The key is the value of the --name flag for each node provided. The default uses default for the key because this is the default for the --name flag.

--initial-cluster-state

节点初始化方式,new 表示如果没有集群不存在,创建新集群,existing表示如果集群不存在,节点将处于加入集群失败状态。

default: "new"
env variable: ETCD_INITIAL_CLUSTER_STATE

--initial-cluster-token

集群唯一标识,相同标识的节点将视为在一个集群内。

default: "etcd-cluster"
env variable: ETCD_INITIAL_CLUSTER_TOKEN

--advertise-client-urls

用于通知其他ETCD节点,客户端接入本节点的监听地址,一般来说advertise-client-urls是listen-client-urls子集

default: "http://localhost:2379"
env variable: ETCD_ADVERTISE_CLIENT_URLS
example: "http://example.com:2379, http://10.0.0.1:2379"

注意,不能写http://localhost:237,这样就是通知其他节点,可以用localhost访问,将导致ectd的客户端用localhost访问本地,导致访问不通。还有一个更可怕情况,ectd布置了代理层,代理层将一直通过locahost访问自己的代理接口,导致无限循环。

其二:详细的安装部署流程



mkdir -p /var/log/etcd/ /data/etcd /usr/lib/systemd/system

cd /opt
wget https://github.com/etcd-io/etcd/releases/download/v3.5.2/etcd-v3.5.2-linux-amd64.tar.gz

tar zxvf etcd-v3.5.2-linux-amd64.tar.gz
ln -s /opt/etcd-v3.5.2-linux-amd64/etcd /usr/local/bin/
ln -s /opt/etcd-v3.5.2-linux-amd64/etcdctl /usr/local/bin/

ected --version
ectedctl --version

vim /usr/lib/systemd/system/etcd.service

#注意替换IP地址
[Unit]
Description=etcd
Documentation=https://github.com/coreos/etcd
Conflicts=etcd.service

[Service]
Type=notify
Restart=always
RestartSec=5s
LimitNOFILE=40000
TimeoutStartSec=0

ExecStart=/usr/local/bin/etcd --name etcd3 --data-dir /data/etcd \
--initial-advertise-peer-urls http://10.80.40.176:2380 \
--listen-peer-urls http://10.80.40.176:2380 \
--listen-client-urls http://10.80.40.176:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://10.80.40.176:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster etcd1=http://10.80.40.80:2380,etcd2=http://10.80.40.101:2380,etcd3=http://10.80.40.176:2380 \
--initial-cluster-state new

[Install]
WantedBy=multi-user.target


vim /usr/lib/systemd/system/etcd-proxy.service

[Unit]
Description=etcd grpc-proxy
Documentation=https://github.com/coreos/etcd
Conflicts=etcd-proxy.service

[Service]
Type=notify
Restart=always
RestartSec=5s
LimitNOFILE=40000
TimeoutStartSec=0

ExecStart=/usr/local/bin/etcd grpc-proxy start --endpoints=10.80.40.80:2379,10.80.40.101:2379,10.80.40.176:2379 --listen-addr=127.0.0.1:23790

[Install]
WantedBy=multi-user.target


systemctl daemon-reload
systemctl enable etcd.service
systemctl enable etcd-proxy.service
systemctl start etcd.service
systemctl start etcd-proxy.service

ETCDCTL_API=3 etcdctl --endpoints=127.0.0.1:2379 member list