注意Etcd在每個版本都會有命令或功能變化,看教程時注意版本。
時間同步:
#根據提示選擇所在大洲、國家和城市 [root@Cent0S7 ~]# tzselect #使用阿里 NTP 校時 [root@Cent0S7 ~]# ntpdate ntp.aliyun.com #系統時間同步BIOS硬件時間 [root@Cent0S7 ~]# hwclock -w #-R 查看時區 [root@Cent0S7 ~]# date -R
單機部署三份(最少3台node才能組成集群,所以要三份),修改端口實現。
PS:很多人學習時,條件有限,沒有3台電腦或3台VM虛擬機都開不起來。
新建目錄:
[root@Cent0S7 ~]# mkdir /var/lib/etcd/ [root@Cent0S7 ~]# mkdir /var/lib/etcd1/ [root@Cent0S7 ~]# mkdir /var/lib/etcd2/
復制etcd(防止用一份出現奇怪的問題)
[root@Cent0S7 ~]# tar -zxf etcd-v3.1.5-linux-amd64.tar.gz [root@Cent0S7 ~]# cd etcd-v3.1.5-linux-amd64/ [root@Cent0S7 ~]# cp etcd /usr/local/sbin/etcd [root@Cent0S7 ~]# cp etcd /usr/local/sbin/etcd1 [root@Cent0S7 ~]# cp etcd /usr/local/sbin/etcd2
創建守護進程 etcd.service 文件
[root@Cent0S7 ~]# cat <<EOF | sudo tee /etc/systemd/system/etcd.service [Unit] Description=Etcd Server Documentation=https://github.com/coreos/etcd After=network.target [Service] User=root Type=notify EnvironmentFile=/etc/etcd.conf ExecStart=/usr/local/bin/etcd Restart=on-failure RestartSec=10s LimitNOFILE=40000 [Install] WantedBy=multi-user.target EOF
創建守護進程 etcd1.service 文件
[root@Cent0S7 ~]# cat <<EOF | sudo tee /etc/systemd/system/etcd1.service [Unit] Description=Etcd1 Server Documentation=https://github.com/coreos/etcd After=network.target [Service] User=root Type=notify EnvironmentFile=/etc/etcd1.conf ExecStart=/usr/local/bin/etcd1 Restart=on-failure RestartSec=10s LimitNOFILE=40000 [Install] WantedBy=multi-user.target EOF
創建守護進程 etcd2.service 文件
[root@Cent0S7 ~]# cat <<EOF | sudo tee /etc/systemd/system/etcd2.service [Unit] Description=Etcd2 Server Documentation=https://github.com/coreos/etcd After=network.target [Service] User=root Type=notify EnvironmentFile=/etc/etcd2.conf ExecStart=/usr/local/bin/etcd2 Restart=on-failure RestartSec=10s LimitNOFILE=40000 [Install] WantedBy=multi-user.target EOF
創建配置文件 etcd.conf 文件,端口2379、2380
[root@Cent0S7 ~]# cat <<EOF | sudo tee /etc/etcd.conf #節點名稱 ETCD_NAME=etcd #數據存放位置 ETCD_DATA_DIR=/var/lib/etcd/ ETCD_LISTEN_PEER_URLS="http://192.168.200.132:2380" ETCD_LISTEN_CLIENT_URLS="http://192.168.200.132:2379,http://127.0.0.1:2379" #[cluster] #leader多久發送一次心跳到followers,默認值是100ms ETCD_HEARTBEAT_INTERVAL="200" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.200.132:2380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_ADVERTISE_CLIENT_URLS="http://192.168.200.132:2379" ETCD_INITIAL_CLUSTER="etcd=http://192.168.200.132:2380,etcd1=http://192.168.200.132:3380,etcd2=http://192.168.200.132:4380" EOF
ps : 如果不寫127.0.0.1或localhost 在測試時 回環地址將被拒絕鏈接。
創建配置文件 etcd1.conf 文件,端口3379、3380
[root@Cent0S7 ~]# cat <<EOF | sudo tee /etc/etcd1.conf #節點名稱 ETCD_NAME=etcd1 #數據存放位置 ETCD_DATA_DIR=/var/lib/etcd1/ ETCD_LISTEN_PEER_URLS="http://192.168.200.132:3380" ETCD_LISTEN_CLIENT_URLS="http://192.168.200.132:3379,http://127.0.0.1:2379" #[cluster] #leader多久發送一次心跳到followers,默認值是100ms ETCD_HEARTBEAT_INTERVAL="200" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.200.132:3380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_ADVERTISE_CLIENT_URLS="http://192.168.200.132:3379" ETCD_INITIAL_CLUSTER="etcd=http://192.168.200.132:2380,etcd1=http://192.168.200.132:3380,etcd2=http://192.168.200.132:4380" EOF
ps : 如果不寫127.0.0.1或localhost 在測試時 回環地址將被拒絕鏈接。
創建配置文件 etcd2.conf 文件,端口4379、4380
[root@Cent0S7 ~]# cat <<EOF | sudo tee /etc/etcd2.conf #節點名稱 ETCD_NAME=etcd2 #數據存放位置 ETCD_DATA_DIR=/var/lib/etcd2/ ETCD_LISTEN_PEER_URLS="http://192.168.200.132:4380" ETCD_LISTEN_CLIENT_URLS="http://192.168.200.132:4379,http://127.0.0.1:2379" #[cluster] #leader多久發送一次心跳到followers,默認值是100ms ETCD_HEARTBEAT_INTERVAL="200" ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.200.132:4380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" ETCD_ADVERTISE_CLIENT_URLS="http://192.168.200.132:4379" ETCD_INITIAL_CLUSTER="etcd=http://192.168.200.132:2380,etcd1=http://192.168.200.132:3380,etcd2=http://192.168.200.132:4380" EOF
ps : 如果不寫127.0.0.1或localhost 在測試時 回環地址將被拒絕鏈接。
配置都完成了,刷新
[root@Cent0S7 ~]# systemctl daemon-reload
啟動節點
PS:啟動時光標一直閃,也沒報錯,因為其他節點還沒啟動,所以在查找,不用管,多開幾個CRT的Shell窗口啟動每個service就可以 了。
[root@Cent0S7 ~]# systemctl start etcd.service
[root@Cent0S7 ~]# systemctl start etcd1.service
[root@Cent0S7 ~]# systemctl start etcd2.service
開機啟動
[root@Cent0S7 ~]# systemctl enable etcd.service Created symlink from /etc/systemd/system/multi-user.target.wants/etcd.service to /etc/systemd/system/etcd.service. [root@Cent0S7 ~]# systemctl enable etcd1.service Created symlink from /etc/systemd/system/multi-user.target.wants/etcd1.service to /etc/systemd/system/etcd1.service. [root@Cent0S7 ~]# systemctl enable etcd2.service Created symlink from /etc/systemd/system/multi-user.target.wants/etcd2.service to /etc/systemd/system/etcd2.service.
查看列表里狀態,狀態enabled,設置成功
[root@Cent0S7 etc]# systemctl list-unit-files etcd*.service UNIT FILE STATE etcd.service enabled etcd1.service enabled etcd2.service enabled 3 unit files listed.
開放防火牆端口
firewall-cmd --zone=public --add-port=2379/tcp --permanent firewall-cmd --zone=public --add-port=2380/tcp --permanent firewall-cmd --reload firewall-cmd --list-all
日志滾動顯示最新的,用Ctrl+c退出
[root@Cent0S7 ~]# journalctl -ef
查看服務狀態
[root@Cent0S7 ~]# systemctl status etcd -l ● etcd.service - Etcd Server Loaded: loaded (/etc/systemd/system/etcd.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2020-11-30 15:40:52 CST; 6min ago Docs: https://github.com/coreos/etcd Main PID: 2338 (etcd) CGroup: /system.slice/etcd.service └─2338 /usr/local/bin/etcd Nov 30 15:40:56 Cent0S7 etcd[2338]: cannot get the version of member e9f1bc7810aec2f3 (Get http://192.168.200.132:3380/version: dial tcp 192.168.200.132:3380: getsockopt: connection refused) Nov 30 15:40:59 Cent0S7 etcd[2338]: health check for peer e9f1bc7810aec2f3 could not connect: dial tcp 192.168.200.132:3380: getsockopt: connection refused Nov 30 15:40:59 Cent0S7 etcd[2338]: peer e9f1bc7810aec2f3 became active Nov 30 15:40:59 Cent0S7 etcd[2338]: established a TCP streaming connection with peer e9f1bc7810aec2f3 (stream MsgApp v2 reader) Nov 30 15:40:59 Cent0S7 etcd[2338]: established a TCP streaming connection with peer e9f1bc7810aec2f3 (stream Message reader) Nov 30 15:40:59 Cent0S7 etcd[2338]: established a TCP streaming connection with peer e9f1bc7810aec2f3 (stream MsgApp v2 writer) Nov 30 15:40:59 Cent0S7 etcd[2338]: established a TCP streaming connection with peer e9f1bc7810aec2f3 (stream Message writer) Nov 30 15:41:00 Cent0S7 etcd[2338]: updating the cluster version from 3.0 to 3.1 Nov 30 15:41:00 Cent0S7 etcd[2338]: updated the cluster version from 3.0 to 3.1 Nov 30 15:41:00 Cent0S7 etcd[2338]: enabled capabilities for version 3.1
驗證:
[root@Cent0S7 ~]# etcdctl --endpoints="http://192.168.200.132:2379,http://192.168.200.132:3379,http://192.168.200.132:4379" member list 232f7a21c3110c92, started, etcd2, http://192.168.200.132:4380, http://192.168.200.132:4001,http://192.168.200.132:4379 3942148265deeadf, started, etcd, http://192.168.200.132:2380, http://192.168.200.132:2379,http://192.168.200.132:4001 e9f1bc7810aec2f3, started, etcd1, http://192.168.200.132:3380, http://192.168.200.132:3379,http://192.168.200.132:4001
[root@Cent0S7 ~]# etcdctl -w="table" --endpoints=192.168.200.132:2379 member list +------------------+---------+-------+-----------------------------+---------------------------------------------------------+ | ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | +------------------+---------+-------+-----------------------------+---------------------------------------------------------+ | 232f7a21c3110c92 | started | etcd2 | http://192.168.200.132:4380 | http://192.168.200.132:4001,http://192.168.200.132:4379 | | 3942148265deeadf | started | etcd | http://192.168.200.132:2380 | http://192.168.200.132:2379,http://192.168.200.132:4001 | | e9f1bc7810aec2f3 | started | etcd1 | http://192.168.200.132:3380 | http://192.168.200.132:3379,http://192.168.200.132:4001 | +------------------+---------+-------+-----------------------------+---------------------------------------------------------+
[root@Cent0S7 ~]# etcdctl --write-out=table --endpoints=192.168.200.132:2379,192.168.200.132:3379,192.168.200.132:4379 endpoint status +----------------------+------------------+---------+---------+-----------+-----------+------------+ | ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX | +----------------------+------------------+---------+---------+-----------+-----------+------------+ | 192.168.200.132:2379 | 3942148265deeadf | 3.1.5 | 25 kB | true | 7 | 9 | | 192.168.200.132:3379 | e9f1bc7810aec2f3 | 3.1.5 | 25 kB | false | 7 | 9 | | 192.168.200.132:4379 | 232f7a21c3110c92 | 3.1.5 | 25 kB | false | 7 | 9 | +----------------------+------------------+---------+---------+-----------+-----------+------------+
批量關閉、清除緩存:
[root@Cent0S7 ~]# cat <<EOF | sudo tee etcd_all_stop.sh # /bin/bash echo " ------ etcd_all stop--------------"
echo "" echo " ------ etcd stop--------------" systemctl stop etcd.service echo " ------ etcd2 stop--------------" systemctl stop etcd1.service echo " ------ etcd3 stop--------------" systemctl stop etcd2.service echo "" echo " ------ clear etcd cache --------------" rm -rf /var/lib/etcd/ mkdir -p /var/lib/etcd/ echo " ------ clear etcd1 cache --------------" rm -rf /var/lib/etcd1/ mkdir -p /var/lib/etcd1/ echo " ------ clear etcd2 cache --------------" rm -rf /var/lib/etcd2/ mkdir -p /var/lib/etcd2/ echo "" echo "-------etcd_all stop and clear OK ------------"
echo
echo EOF
以下是配置變量說明參考:
#單機配置 # [member] # etcd集群中的節點名,這里可以隨意,可區分且不重復就行 ETCD_NAME=$(hostname -s) #緩存數據存放位置,保存日志和快照的目錄,默認為當前工作目錄default.etcd/目錄下 #新該配置后,要刪除緩存,否則啟動還是要用之前的緩存。 ETCD_DATA_DIR=/var/lib/etcd/ #ETCD_WAL_DIR="" #指定有多少事務被提交時,觸發和截取快照保存到磁盤 #ETCD_SNAPSHOT_COUNT="10000" #leader多久發送一次心跳到followers,默認值是100ms #ETCD_HEARTBEAT_INTERVAL="100" #重新投票的超時時間,如果follow在該時間間隔沒有收到心跳包,會觸發重新投票,默認為1000ms。 #ETCD_ELECTION_TIMEOUT="1000" #監聽的用於節點之間通信的url,可監聽多個,集群內部將通過這些url進行數據交互(如選舉,數據同步等) ETCD_LISTEN_PEER_URLS="http://localhost:2380" #監聽的用於客戶端通信的url,同樣可以監聽多個。 #如果不寫htt://localhost:2379,http://127.0.0.1:2379的本地回環地址,測試時localhost和127.0.0.1將拒絕連接 ETCD_LISTEN_CLIENT_URLS="http://localhost:4001,http://localhost:2379" #要保留的最大快照文件數(0表示不受限制),Windows上的用戶默認值不受限制 #ETCD_MAX_SNAPSHOTS="5" #要保留的最大wal文件數(0表示不受限制),Windows上的用戶默認值不受限制 #ETCD_MAX_WALS="5" #逗號分隔的CORS原始白名單(跨源資源共享) #ETCD_CORS="" # #集群配置 #[cluster] #告知集群其他節點的URL,建議用於節點之間通信的url,節點間將以該值進行通信,一般是2380端口。 ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380" #也就是集群中所有的initial-advertise-peer-urls 的合集, #所有節點的IP都寫在下面,etcd-node1、etcd-node2、etcd-node3根據實際情況改成相應ip或hostname ETCD_INITIAL_CLUSTER="etcd-node1=http://etcd-node1:2380,etcd-node2=http://etcd-node2:2380,etcd-master=http://etcd-master:2380" #新建集群的標志 new和existing,即加入與被加入 ETCD_INITIAL_CLUSTER_STATE="new" #自定義的一個token值,設置該值后集群將生成唯一id,並為每個節點也生成唯一id,當使用相同配置文件再啟動一個集群時,只要該token值不一樣,etcd集群就不會相互影響。 ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster" #建議使用的客戶端通信url,該值用於etcd代理或etcd成員與etcd節點通信。 ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379"
##################### 報錯 #################################
錯誤:
Error: grpc: timed out when dialing
解決方法:將心跳值設置到200在試試
##################### 報錯 #################################
參考文章:
http://blog.51cto.com/zlyang/1951164
http://cnblogs.com/davygeek/p/7154780.html 老版本端口4001 V3.3.5
http://dazhuanlan.com/2019/12/13/5df3ad5424c46/
http://cnblogs.com/linuxws/p/11194403.html 配置文件詳解
http://localhost:2379