debian9使用systemd部署etcd集群
在centos上,是可以直接使用yum安装etcd的:
# yum list | grep etcd
etcd.x86_64 3.2.9-3.el7 @extras
但是,在debian上却没有安装包,所以就只能手动安装了,不过也很简单。
可以找台centos的机器,安装好之后把配置文件拿过来套用一下就好了。
etcd version: 3.2.9
OS version: debian stretch
三个etcd节点:
etcd0 192.168.5.42
etcd1 192.168.5.104
etcd2 192.168.5.105
1、下载软件包并解压
# wget https://github.com/coreos/etcd/releases/download/v3.2.9/etcd-v3.2.9-linux-arm64.tar.gz
# tar xvf etcd-v3.2.9-linux-arm64.tar.gz
# cd etcd-v3.2.9-linux-amd64/
# cp -a etcd etcdctl /usr/bin/
2、创建unit文件,该文件三个节点相同,复制到每个节点即可
# vim /lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
Documentation=https://github.com/coreos/etcd
[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/10-etcd.conf
User=root
# set GOMAXPROCS to number of processors
ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/bin/etcd --name=\"${ETCD_NAME}\" \
--data-dir=\"${ETCD_DATA_DIR}\" \
--listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\" \
--initial-advertise-peer-urls=\"${ETCD_INITIAL_ADVERTISE_PEER_URLS}\" \
--listen-peer-urls=\"${ETCD_LISTEN_PEER_URLS}\" \
--advertise-client-urls=\"${ETCD_ADVERTISE_CLIENT_URLS}\" \
--initial-cluster-token=\"${ETCD_INITIAL_CLUSTER_TOKEN}\" \
--initial-cluster=\"${ETCD_INITIAL_CLUSTER}\" \
--initial-cluster-state=\"${ETCD_INITIAL_CLUSTER_STATE}\""
Restart=on-failure
LimitNOFILE=65536
RestartSec=5s
[Install]
WantedBy=multi-user.target
3、创建配置文件,这里每个节点有所不同(实例名称和IP不一样)
# vim /etc/etcd/10-etcd.conf
ETCD_NAME=etcd0
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.5.42:2380"
ETCD_LISTEN_PEER_URLS="http://192.168.5.42:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379"
ETCD_INITIAL_CLUSTER_TOKEN="my-etcd-token"
ETCD_INITIAL_CLUSTER="etcd0=http://192.168.5.42:2380,etcd1=http://192.168.5.104:2380,etcd2=http://192.168.5.105:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
# vim /etc/etcd/10-etcd.conf
ETCD_NAME=etcd1
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.5.104:2380"
ETCD_LISTEN_PEER_URLS="http://192.168.5.104:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379"
ETCD_INITIAL_CLUSTER_TOKEN="my-etcd-token"
ETCD_INITIAL_CLUSTER="etcd0=http://192.168.5.42:2380,etcd1=http://192.168.5.104:2380,etcd2=http://192.168.5.105:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
# vim /etc/etcd/10-etcd.conf
ETCD_NAME=etcd2
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.5.105:2380"
ETCD_LISTEN_PEER_URLS="http://192.168.5.105:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
ETCD_ADVERTISE_CLIENT_URLS="http://0.0.0.0:2379"
ETCD_INITIAL_CLUSTER_TOKEN="my-etcd-token"
ETCD_INITIAL_CLUSTER="etcd0=http://192.168.5.42:2380,etcd1=http://192.168.5.104:2380,etcd2=http://192.168.5.105:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
4、启动服务。这里要说一下的是,集群模式下,单独起一个节点的时候会挂起等待选举,而且等待超时之后服务状态是failed,不过其实服务已经正常启动了。为了防止这种情况,在超时时间之内把三个节点都启动就好了。这个超时时间也是可以设置的,以及如果超时服务状态failed时,只需要在三个节点都启动后再重启一次服务状态就正常了。
# systemctl daemon-reload
# systemctl start etcd && systemctl enable etcd
5、验证集群状态
# etcdctl member list
93ac7045b7c80fe2, started, etcd2, http://192.168.5.105:2380, http://0.0.0.0:2379
cceea3802386922f, started, etcd1, http://192.168.5.104:2380, http://0.0.0.0:2379
e1f394bfa58b2a7f, started, etcd0, http://192.168.5.42:2380, http://0.0.0.0:2379
也可以通过curl访问:
# curl http://192.168.6.15:2379/v2/members
{"members":[{"id":"93ac7045b7c80fe2","name":"etcd2","peerURLs":["http://192.168.5.105:2380"],"clientURLs":["http://0.0.0.0:2379"]},{"id":"cceea3802386922f","name":"etcd1","peerURLs":["http://192.168.5.104:2380"],"clientURLs":["http://0.0.0.0:2379"]},{"id":"e1f394bfa58b2a7f","name":"etcd0","peerURLs":["http://192.168.5.42:2380"],"clientURLs":["http://0.0.0.0:2379"]}]}