1 單點部署
1.1 安裝
確認一下linux操作系統內核版本,版本號需要大于3.10
$ uname -a
Linux 4.14.0_1-0-0-45 #2 SMP Tue Oct 19 18:27:28 CST 2021 x86_64 x86_64 x86_64 GNU/Linux
直接從官網下載相應的二進制文件包etcd-v3.5.0-linux-amd64.tar.gz
,并建立相應的工作目錄結構
$ mkdir etcd
$ cp etcd-v3.5.0-linux-amd64.tar.gz ./etcd
$ cd ./etcd
$ tar -xzf etcd-v3.5.0-linux-amd64.tar.gz
$ mkdir -p bin conf log data
$ cp etcd-v3.5.0-linux-amd64/etcd ./bin && cp etcd-v3.5.0-linux-amd64/etcdctl ./bin && cp etcd-v3.5.0-linux-amd64/etcdutl ./bin
查看一下etcd的版本
$ ./bin/etcd --version
etcd Version: 3.5.0
Git SHA: 946a5a6f2
Go Version: go1.16.3
Go OS/Arch: linux/amd64
1.2 啟動
1.2.1 前臺啟動
只是簡單臨時測試試用,可以直接啟動
$ ./bin/etcd
也可以指定啟動參數, --name
為節點取一個名字,--log-level
指定打印日志的級別
$ ./bin/etcd --name=etcd-cnlab0 --log-level=info
啟動參數也可以放到配置文件中進行統一管理,然后通過--config-file
啟動參數指定啟動的配置文件
$ cat conf/etcd.yaml
# 節點名字
name: etcd-cnlab0
# etcd數據存儲目錄
data-dir: ./data
# 接收客戶端連接的服務端點
listen-client-urls: http://0.0.0.0:2379
./bin/etcd --config-file=./conf/etcd.yaml
1.2.2 后臺啟動
$ nohup ./bin/etcd --config-file=conf/etcd.yaml 2>&1 1>log/etcd.log &
1.2.3 systemd服務啟動
簡單測試時上面兩種方式啟動非常方便快捷,線上生產環境傾向于配置成系統服務
制作一個etcd.service
文件:
$ sudo su -
# cd /usr/lib/systemd/system
# vim etcd.service
[Unit]
Description=Etcd Server
# 啟動etcd服務后,需要啟動network.target服務
After=network.target
[Service]
# 服務類型
Type=simple
# 工作目錄
WorkingDirectory=/home/work/etcd
# 服務啟動目錄
ExecStart=/home/work/etcd/bin/etcd --config-file=/home/work/etcd/conf/etcd.yaml
# 服務啟動用戶名
User=work
# 服務啟動組
Group=work
PIDFile=/home/work/etcd/etcd.pid
# 服務失敗后重啟服務
Restart=on-failure
# 服務運行打開的文件句柄數
LimitNOFILE=65536
[Install]
# 系統以多用戶模式啟動時自動啟動本服務
WantedBy=multi-user.target
然后可以使用systemctl
系統命令管理了etcd服務了
# 查看文件內容
# systemctl cat etcd.service
# 服務還未使能
# systemctl list-unit-files --type=service | grep etcd
etcd.service disabled
# 重新加載服務文件
# systemctl daemon-reload
# 使能自啟動
# systemctl enable etcd.service
# 啟動服務
# systemctl start etcd.service
其他的幾個管理命令
# 關閉服務
# systemctl stop etcd.service
# 重啟服務
# systemctl restart etcd.service
# 顯示服務的狀態
# systemctl status etcd.service
# 在開機時不啟動服務
# systemctl disable etcd.service
# 查看服務是否開機啟動
# systemctl is-enabled etcd.service
# 查看啟動失敗的服務
# systemctl --failed | grep etcd
1.2.4 配置logrotate
配置logrotate,對etcd日志文件進行切割,并限制日志文件最大大小
在/etc/logrotate.d/
目錄下創建etcd
服務的logrotate
的配置文件:
/home/work/etcd/log/*.log {
# 切割周期為天
daily
# 最多保留10個文件
rotate 10
# 切割后的文件名添加日期后綴
dateext
# 日期格式
dateformat -%Y%m%d
# 切割后文件的后綴
extension log
# 如果日志文件不存在,不報錯
missingok
# 日志文件大小為50M,超過50M后進行切割
size 50M
compress
delaycompress
notifempty
# 文件權限,user,group
create 0644 work work
sharedscripts
# 切割后執行的命令,讓etcd重新加載配置
postrotate
/usr/bin/killall -HUP etcd
endscript
}
1.3 驗證
查看啟動的進程和端口
# ps -ef | grep etcd
work 3000 25754 0 15:14 pts/0 00:00:00 ./bin/etcd --name=etcd-cnlab0 --log-level=info
# lsof -i :2379
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
etcd 3000 work 8u IPv4 2434566770 0t0 TCP localhost:2379 (LISTEN)
etcd 3000 work 13u IPv4 2434581997 0t0 TCP localhost:59605->localhost:2379 (ESTABLISHED)
etcd 3000 work 14u IPv4 2434569656 0t0 TCP localhost:2379->localhost:59605 (ESTABLISHED)
# lsof -i :2380
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
etcd 3000 work 7u IPv4 2434566767 0t0 TCP localhost:2380 (LISTEN)
查看服務端點狀態
$ ./bin/etcdctl endpoint status --cluster -w table
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| http://localhost:2379 | 8e9e05c52164694d | 3.5.0 | 20 kB | true | false | 2 | 4 | 4 | |
+-----------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
查看服務端點健康情況
$ ./bin/etcdctl endpoint health --cluster -w table
+-----------------------+--------+------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+-----------------------+--------+------------+-------+
| http://localhost:2379 | true | 2.059608ms | |
+-----------------------+--------+------------+-------+
查看成員
$ ./bin/etcdctl member list
8e9e05c52164694d, started, etcd-cnlab0, http://localhost:2380, http://localhost:2379, false
寫入和讀取數據
$ ./bin/etcdctl put key1 hello
OK
$ ./bin/etcdctl --endpoints=http://127.0.0.1:2379 get key1
key1
hello
2 集群部署
假設集群有三個節點,服務ip分別為10.131.21.11
、10.131.21.12
、10.131.21.13
,為這三個節點分別創建相應的配置文件
2.1 節點配置
節點1(10.131.21.11
)配置文件內容:
$ cat conf/etcd.yaml
# 節點名字,集群內唯一
name: etcd-cnlab0
# 數據存儲目錄
data-dir: ./data
# 日志文件配置
# logger: zap
log-outputs: ['./log/etcd.log']
# log-level: info
# 客戶端連接相關配置
# 服務監聽端點
listen-client-urls: http://0.0.0.0:2379
# 向客戶端發布的服務端點
advertise-client-urls: http://10.131.21.11:2379
# 集群相關配置
# 監聽集群其他節點連接的端點
listen-peer-urls: http://0.0.0.0:2380
# 向集群其他節點發布的服務端點
initial-advertise-peer-urls: http://10.131.21.11:2380
# 集群成員的名字以及服務端點列表,名字與每個節點配置的name字段值對應
initial-cluster: etcd-cnlab0=http://10.131.21.11:2380,etcd-cnlab1=http://10.131.21.12:2380,etcd-cnlab2=http://10.131.21.13:2380
# 集群標識token,可以認為是集群名字
initial-cluster-token: etcd-cnlab
# 創建一個新的集群。如果data-dir目錄下的數據屬于另外一個集群,則無法啟動
inital-cluster-state: new
節點2(10.131.21.12
)配置文件內容:
$ cat conf/etcd.yaml
name: etcd-cnlab1
data-dir: ./data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://10.131.21.12:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://10.131.21.12:2380
initial-cluster: etcd-cnlab0=http://10.131.21.11:2380,etcd-cnlab1=http://10.131.21.12:2380,etcd-cnlab2=http://10.131.21.13:2380
initial-cluster-token: etcd-cnlab
inital-cluster-state: new
節點3(10.131.21.13
)配置文件內容:
$ cat conf/etcd.yaml
name: etcd-cnlab2
data-dir: ./data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://10.131.21.13:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://10.131.21.13:2380
initial-cluster: etcd-cnlab0=http://10.131.21.11:2380,etcd-cnlab1=http://10.131.21.12:2380,etcd-cnlab2=http://10.131.21.13:2380
initial-cluster-token: etcd-cnlab
inital-cluster-state: new
2.2 啟動三個節點
$ ./bin/etcd --config-file=conf/etcd.yaml
2.3 驗證
$ ./bin/etcdctl endpoint health --cluster -w table
+--------------------------+--------+------------+-------+
| ENDPOINT | HEALTH | TOOK | ERROR |
+--------------------------+--------+------------+-------+
| http://10.131.21.11:2379 | true | 1.405365ms | |
| http://10.131.21.12:2379 | true | 2.106704ms | |
| http://10.131.21.13:2379 | true | 2.257057ms | |
+--------------------------+--------+------------+-------+
$ ./bin/etcdctl member list
2d3298f7865acc32, started, etcd-cnlab1, http://10.131.21.12:2380, http://10.131.21.12:2379, false
71e4f8695d490d37, started, etcd-cnlab0, http://10.131.21.11:2380, http://10.131.21.11:2379, false
c2a413a1e752b148, started, etcd-cnlab2, http://10.131.21.13:2380, http://10.131.21.13:2379, false
$ ./bin/etcdctl endpoint status --cluster -w table
+--------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+--------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| http://10.131.21.12:2379 | 2d3298f7865acc32 | 3.5.0 | 20 kB | false | false | 2 | 10 | 10 | |
| http://10.131.21.11:2379 | 71e4f8695d490d37 | 3.5.0 | 20 kB | true | false | 2 | 10 | 10 | |
| http://10.131.21.13:2379 | c2a413a1e752b148 | 3.5.0 | 20 kB | false | false | 2 | 10 | 10 | |
+--------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+