etcd 主要提供 key-value 的儲存機制。
以下操作的作業系統為 ubuntu 18.04,不同的版本或者作業系統安裝指令可能略顯不同。
wget https://github.com/etcd-io/etcd/releases/download/v3.5.14/etcd-v3.5.14-linux-amd64.tar.gz
tar xvf etcd-v3.5.14-linux-amd64.tar.gz
cd etcd-v3.5.14-linux-amd64
sudo mv etcd etcdctl /usr/local/bin
sudo mkdir -p /var/lib/etcd/
sudo mkdir /etc/etcd
創建啟動腳本 system service
cd /etc/systemd/system/
vim etcd.service
檔案內容如下
[Unit]
Description=etcd key-value store
Documentation=https://github.com/etcd-io/etcd
After=network.target
[Service]
User=root
Type=notify
Environment=ETCD_DATA_DIR=/var/lib/etcd
Environment=ETCD_NAME=%m
ExecStart=/usr/local/bin/etcd
Restart=always
RestartSec=10s
LimitNOFILE=40000
[Install]
WantedBy=multi-user.target
重新 reload systemd service
sudo systemctl daemon-reload
設定開啟時啟動
sudo systemctl enable etcd.service
也可以直接手動啟動不要背景執行
/usr/local/bin/etcd --data-dir /var/lib/etcd
上面的 etcd 是使用 — xxx 去設定參數,也可以直接指定 config file,指令如下
/usr/local/bin/etcd --config-file /etc/etcd/config.yml
所以假如 systemd 想改為使用 config.yml 的話要改成如下設定
[Unit]
Description=etcd key-value store
Documentation=https://github.com/etcd-io/etcd
After=network.target
[Service]
User=root
Type=notify
#Environment=ETCD_DATA_DIR=/var/lib/etcd
#Environment=ETCD_NAME=%m
Environment=ETCD_CONFIG_FILE=/etc/etcd/config.yml
ExecStart=/usr/local/bin/etcd
Restart=always
RestartSec=10s
LimitNOFILE=40000
[Install]
WantedBy=multi-user.target
以下為 etcd config.yml.sample
https://github.com/etcd-io/etcd/blob/main/etcd.conf.yml.sample
倘若 etcd 要給外部連線的話,記得以下參數要設定 public ip , 否則外部會連線不到,因為預設只允許 localhost 連線。
listen-client-urls = http://<public ip>:2379, http://localhost:2379
備註: 因為也會在機器內部操作,所以一樣設定 localhost。
etcd 參數官網說明
etcd 安裝教學
etcd 下載 github 來源
而 etcd 預設開啟的 port 為 2379, 因此 ec2 security group 記得開啟 2379 port
etcd 基本指令
取得會員列表
etcdctl member list
倘若不是在 etcd server 執行的話需要設定 ENDPOINTS 環境變數為 etcd server 的位址
輸出結果如下:
etcd 的指令也可以指定輸出的格式,例如以下為輸出格式為 table
etcdctl --write-out table member list
輸出結果如下
etcd 設定 key value 指令如下
etcdctl put {key} {value}
倘若設定 key 為 foo, value 為 hello world 指令就會如下
etcdctl put foo "Hello world"
而要取出該 key 的 value 指令如下
etcdctl get {key}
取得 prefix 的 key
etcdctl get {key} --prefix
上面的步驟中已設定 foo 這個 key, 可以用以上指令取得 f 開頭的 key
etcdctl get f --prefix
刪除指定的 key
etcdctl del {key}
etcd transaction
etcdctl txn --interactive
以上的範例為,倘若 user1 這個 key 的值為 bad, 那就刪除 user1, 否則就將 user1 改為 good。
以下指令可以監聽 key 的變化
etcdctl watch {key}
倘若你監聽 user1
etcdctl watch user1
然後再開一個 tab 去 put value 到 user1 結果會如下
檢查節點的狀態
etcdctl endpoint status --write-out table
檢查節點的健康狀態
etcdctl endpoint health --write-out table
snapshot database
etcdctl snapshot save {snapshot name}
檢查 snapshot 出來的狀態
etcdutl snapshot status {snapshot name} --write-out table
使用 snapshot 還原
etcdutl --data-dir {data directory} snapshot restore {snapshot name}
參考資源: