[From] https://blog.csdn.net/ShouTouDeXingFu/article/details/81167302linux
etcdgit
一、下載etcd二進制文件包
https://github.com/coreos/etcd/releases/download/v3.1.3/etcd-v3.1.3-linux-amd64.tar.gzgithub
二、解壓縮etcd-v3.1.3-linux-amd64.tar.gz,將其中的etcd和etcdctl兩個可執行文件複製到各節點的/usr/bin目錄
三、在各節點建立etcd的數據目錄
mkdir -p /data/etcdless
mkdir /etc/etcdurl
四、在每一個節點上建立etcd的systemd unit文件/usr/lib/systemd/system/etcd.service 和 /etc/etcd/etcd.conf
spa
etcd.service .net
[Unit] Description=Etcd Server After=network.target After=network-online.target Wants=network-online.target Documentation=https://github.com/coreos [Service] Type=notify WorkingDirectory=/var/lib/etcd/ EnvironmentFile=-/etc/etcd/etcd.conf ExecStart=/usr/bin/etcd \ --name ${ETCD_NAME} \ --cert-file=${ETCD_CERT_FILE} \ --key-file=${ETCD_KEY_FILE} \ --peer-cert-file=${ETCD_PEER_CERT_FILE} \ --peer-key-file=${ETCD_PEER_KEY_FILE} \ --trusted-ca-file=${ETCD_TRUSTED_CA_FILE} \ --peer-trusted-ca-file=${ETCD_PEER_TRUSTED_CA_FILE} \ --initial-advertise-peer-urls ${ETCD_INITIAL_ADVERTISE_PEER_URLS} \ --listen-peer-urls ${ETCD_LISTEN_PEER_URLS} \ --listen-client-urls ${ETCD_LISTEN_CLIENT_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} \ --data-dir=${ETCD_DATA_DIR} Restart=on-failure RestartSec=5 LimitNOFILE=65536 [Install] WantedBy=multi-user.target
etcd.conf etcd集羣中配置文件標紅的配置不同,拷貝出來修改便可3d
ETCD_NAME="etcd1" #20180108 by lys #ETCD_CERT_FILE="/etc/k8s/kubernetes.pem" #ETCD_KEY_FILE="/etc/k8s/kubernetes-key.pem" #ETCD_PEER_CERT_FILE="/etc/k8s/kubernetes.pem" #ETCD_PEER_KEY_FILE="/etc/k8s/kubernetes-key.pem" #ETCD_TRUSTED_CA_FILE="/etc/k8s/ca.pem" #ETCD_PEER_TRUSTED_CA_FILE="/etc/k8s/ca.pem" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://etcd1.server.biz:2380" ETCD_LISTEN_PEER_URLS="http://etcd1.server.biz:2380" ETCD_LISTEN_CLIENT_URLS="http://etcd1.server.biz:2379,http://127.0.0.1:2379" ETCD_ADVERTISE_CLIENT_URLS="http://etcd1.server.biz:2379" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-0" ETCD_INITIAL_CLUSTER="etcd1=http://etcd1.server.biz:2380,etcd2=http://etcd2.server.biz:2380,etcd3=http://etcd3.server.biz:2380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_DATA_DIR="/data/etcd"
配置信息說明
上面在啓動參數中指定了etcd的工做目錄和數據目錄是/data/etcd
--cert-file和--key-file分別指定etcd的公鑰證書和私鑰
--peer-cert-file和--peer-key-file分別指定了etcd的Peers通訊的公鑰證書和私鑰。
--trusted-ca-file指定了客戶端的CA證書
--peer-trusted-ca-file指定了Peers的CA證書
--initial-cluster-state new表示這是新初始化集羣,--name指定的參數值必須在--initial-cluster中
日誌
五、啓動etcd
systemctl daemon-reloadcode
systemctl enable etcd
systemctl start etcd
systemctl status etcd
六、檢查集羣是否健康,在任一節點執行
export ETCDCTL_API=3
etcdctl --endpoints=http://etcd2.server.biz:2379,http://etcd1.server.biz:2379,http://etcd3.server.biz:2379 member list
etcdctl --endpoints=http://etcd2.server.biz:2379,http://etcd1.server.biz:2379,http://etcd3.server.biz:2379 endpoint status -w table
etcdctl --endpoints=http://etcd2.server.biz:2379,http://etcd1.server.biz:2379,http://etcd3.server.biz:2379 endpoint health -w table
sudo journalctl -u etcd.service -l --no-pager|less 查看日誌
七、etcd集羣成員擴容增長
etcdctl --endpoints=http://etcd2.server.biz:2379,http://etcd1.server.biz:2379,http://etcd3.server.biz:2379 member add etcd4 --peer-urls=http://etcd4.server.biz:2380
Member 5b316fb0b94e59f8 added to cluster f0364a493c8924dc
ETCD_NAME="etcd4"
ETCD_INITIAL_CLUSTER="etcd1=http://etcd1.server.biz:2380,etcd3=http://etcd3.server.biz:2380,etcd2=http://etcd2.server.biz:2380,etcd4=http://etcd4.server.biz:2380"
ETCD_INITIAL_CLUSTER_STATE="existing"
此時,集羣會爲目標節點生成一個惟一的member ID
注意:新節點加入集羣是做爲一個全新的節點加入,若是data-dir有數據,etcd啓動時會讀取己經存在的數據,啓動時仍然用的老member ID,也會形成,集羣不沒法加入,因此必定要清空新節點的data-dir
新擴容的etcd機器的etcd.conf中配置修改成 ETCD_INITIAL_CLUSTER_STATE="existing"
刪除 rm-rf /data/etcd/*
啓動etcd服務便可
常見問題
1) systemd: Failed at step CHDIR spawning /usr/bin/etcd: No such file or directory
經過查看ll /usr/bin/etcd 啓動文件是存在的,後來發現etcd.service裏面指定了WorkingDirectory=/var/lib/etcd/ 沒有建立對應目錄致使,建立了/var/lib/etcd/ 目錄應用啓動成功
2)etcd 添加集羣成員報錯Jul 23 16:58:25 bjzw_82_137 etcd: failed to process raft message (raft: stopped)Jul 23 16:58:25 bjzw_82_137 etcd: failed to find member c79c567db2d16966 in cluster f0364a493c8924dcJul 23 16:58:25 bjzw_82_137 etcd: failed to find member c79c567db2d16966 in cluster f0364a493c8924dcJul 23 16:58:25 bjzw_82_137 etcd: failed to find member b13dd19d11c8d25d in cluster f0364a493c8924dcJul 23 16:58:25 bjzw_82_137 etcd: failed to find member b13dd19d11c8d25d in cluster f0364a493c8924dc須要將etcd.conf中的配置修改成