Etcd集群搭建過程和命令總結
什麼是etcd?
etcd是CoreOS團隊於2013年6月發起的開源項目,它的目標是構建一個高可用的分布式鍵值數據庫,基於Go語言實現。主要解決分布式係統中各種服務的配置信息的管理分享,服務的發現。
一、Etcd三節點集群配置:
10.100.50.123
10.100.50.124
10.100.50.125
https://github.com/coreos/etcd/releases/下載
1、解壓縮、運行etcd
[root@Etcd01 /]# tar -zvxf etcd-v3.2.4-linux-amd64.tar.gz
[root@Etcd01 /]# mv etcd-v3.2.4-linux-amd64 etcd
[root@Etcd01 /]# cd etcd
[root@Etcd01 etcd]# ls
Documentation etcd etcdctl README-etcdctl.md README.md READMEv2-etcdctl.md
[root@Etcd01 etcd]# ./etcd
2、添加環境變量
在"/etc/profile" 中添加環境變量
Etcd
export PATH="$PATH:/etcd"
export ETCDCTL_API=3
3、添加systemctl的服務
[root@Etcd01 etcd]# vi /etc/systemd/system/etcd.service
[Unit]
Description=etcd
[Service]
ExecStart=/etcd/etcd
[Install]
WantedBy=multi-user.target
4、關閉防火牆
[root@Etcd01 etcd]# systemctl stop firewalld
[root@Etcd01 etcd]# systemctl disable firewalld
關閉SElinux
[root@Etcd01 etcd]#sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
以上三步在其他兩個節點重複操作。
5、運行腳本
etcd --name infra0 --initial-advertise-peer-urls https://10.100.50.123:2380 \
--listen-peer-urls https://0.0.0.0:2380 \
--listen-client-urls https://0.0.0.0:2379 \
--advertise-client-urls https://10.100.50.123:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra0=https://10.100.50.123:2380,infra1=https://10.100.50.124:2380,infra2=https://10.100.50.125:2380 \
--initial-cluster-state new
etcd --name infra1 --initial-advertise-peer-urls https://10.100.50.124:2380 \
--listen-peer-urls https://0.0.0.0:2380 \
--listen-client-urls https://0.0.0.0:2379 \
--advertise-client-urls https://10.100.50.124:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra0=https://10.100.50.123:2380,infra1=https://10.100.50.124:2380,infra2=https://10.100.50.125:2380 \
--initial-cluster-state new
etcd --name infra2 --initial-advertise-peer-urls https://10.100.50.125:2380 \
--listen-peer-urls https://0.0.0.0:2380 \
--listen-client-urls https://0.0.0.0:2379 \
--advertise-client-urls https://10.100.50.125:2379 \
--initial-cluster-token etcd-cluster-1 \
--initial-cluster infra0=https://10.100.50.123:2380,infra1=https://10.100.50.124:2380,infra2=https://10.100.50.125:2380 \
--initial-cluster-state new
後台運行: nohup /path/to/start_etcd.sh &
6、命令參數解釋
https://coreos.com/etcd/docs/latest/op-guide/configuration.html官網解釋
etcd --name infra0節點名稱
--initial-advertise-peer-urls https://HOST-IP:2380 \通知其他Etcd實例地址
--listen-peer-urls https://0.0.0.0:2380 \監聽其他Etcd實例地址
--listen-client-urls https://0.0.0.0:2379 \監聽客戶端地址
--advertise-client-urls https://HOST-IP:2379 \通知客戶端地址
--initial-cluster-token etcd-cluster-1 \初始化集群Token,同一個集群Token必須相同。
--initial-cluster infra0=https://HOST-IP:2380,infra1=https://HOST-IP:2380,infra2=https://HOST-IP:2380 \初始化集群節點地址
--initial-cluster-state new新建集群,如果是已經存在的集群將new更改為existing
二、使用etcdctl命令操作數據庫
set指定某個鍵的值
格式:set --
OPTIONS:
--ttl value key time-to-live in seconds (default: 0)
--swap-with-value value previous value
--swap-with-index value previous index (default: 0)
例1:設置鍵值
[root@Etcd01 ~]# etcdctl set key1 value1
value1
[root@Etcd01 ~]# etcdctl get key1
value1
[root@Etcd01 ~]#
例2:設置鍵值的超時時間
[root@Etcd01 ~]# etcdctl set --ttl 10 key2 value2
value2
[root@Etcd01 ~]# etcdctl get key2
Error: 100: Key not found (/key2) [10]
[root@Etcd01 ~]#
例3:若該鍵現在的值是 value,則進行設置操作
[root@Etcd01 ~]# etcdctl set key1 value1
value1
[root@Etcd01 ~]# etcdctl get key1
value1
[root@Etcd01 ~]# etcdctl set key1 "value2" --swap-with-value "value1" 如果value=value1則 ,把key1的鍵值改為value2。
value2
[root@Etcd01 ~]#
get獲取鍵值
格式: get
選項:
--sort 對結果進行排序
--consistent 將請求發給主節點,保證獲取內容的一致性
例1:獲取鍵值
[root@Etcd01 ~]# etcdctl set key1 value1
value1
[root@Etcd01 ~]# etcdctl get key1
value1
update更新鍵值
格式:etcdctl update
選項:
--ttl ‘0’ 超時時間默認為0,永不超時。
[root@Etcd01 ~]# etcdctl set key1 value1
value1
[root@Etcd01 ~]# etcdctl update --ttl '5' key1 value2 更新key1的鍵值為value2,並設置5秒超時時間。
value2
[root@Etcd01 ~]# etcdctl get key1
value2
[root@Etcd01 ~]# etcdctl get key1 超過5秒鍾後鍵值自動刪除。
Error: 100: Key not found (/key1) [22]
[root@Etcd01 ~]#
rm刪除某個鍵值
格式:etcdctl rm
選項:
--dir 如果鍵是個空目錄或者鍵值對則刪除
--recursive 刪除目錄和所有子鍵
--with-value 檢查現有的值是否匹配
--with-index '0' 檢查現有的 index 是否匹配
例1:刪除鍵值
[root@Etcd01 ~]# etcdctl set key1 value
value
[root@Etcd01 ~]# etcdctl rm key1
PrevNode.Value: value
[root@Etcd01 ~]# etcdctl get key1
Error: 100: Key not found (/key1) [24]
[root@Etcd01 ~]#
mk如果給定的鍵不存在,則創建一個新的鍵值
格式: etcdctl mk
選項:
--ttl '0' 超時時間(單位為秒),不配置(默認為 0)則永不超時
例:當鍵值不存在時
[root@Etcd01 ~]# etcdctl mk key2 value1
value1
[root@Etcd01 ~]# etcdctl get key2
value1
例:當鍵值存在時會報錯
[root@Etcd01 ~]# etcdctl mk key1 value2
Error: 105: Key already exists (/key1) [25]
mkdir如果給定的目錄不存在,則創建一個新的鍵目錄
格式:etcdctl mkdir
選項:
--ttl '0' 超時時間(單位為秒),不配置(默認為 0)則永不超時
例:
[root@Etcd01 ~]# etcdctl mkdir keydir 創建一個mkdir的目錄
[root@Etcd01 ~]# etcdctl set mkdir/key1 value1 在mkdir目錄下創建一個key1的鍵且值為value1
value1
[root@Etcd01 ~]# etcdctl ls /mkdir 使用ls命令查看mkdir下的鍵
/mkdir/key1
setdir創建一個目錄,無論存在與否
格式:etcdctl setdir
--ttl '0' 超時時間(單位為秒),不配置(默認為 0)則永不超時
例:
[root@Etcd01 ~]# etcdctl setdir keydir2
[root@Etcd01 ~]# etcdctl ls
/keydir1
/keydir2
updatedir更新一個已經存在的目錄(僅在目錄中設置了TTL或生存時間時才有用)
格式: etcdctl updatedir
選項:
--ttl '0' 超時時間(單位為秒),不配置(默認為 0)則永不超時
例:設置TTL時間表示keydir的存活時間。
[root@Docker01 ~]# etcdctl ls
/keydir
[root@Docker01 ~]# etcdctl updatedir keydir --ttl '10' 設置超時時間為10秒
[root@Docker01 ~]# etcdctl ls
/keydir
[root@Docker01 ~]# etcdctl ls 十秒中後目錄自動刪除,如果為0則永不超時。
[root@Docker01 ~]#
rmdir刪除一個目錄,或者鍵值對
格式:etcdctl rmdir 或
例1:刪除目錄
[root@Docker01 ~]# etcdctl ls
/keydir
[root@Docker01 ~]# etcdctl rmdir /keydir
[root@Docker01 ~]# etcdctl ls
[root@Docker01 ~]#
例2:刪除鍵值對
[root@Docker01 ~]# etcdctl set key1 value1
value1
[root@Docker01 ~]# etcdctl rmdir key1 刪除鍵值對,格式可以是etcdctl rmdir 而不用加鍵對應的值。
PrevNode.Value: value1
[root@Docker01 ~]# etcdctl get key1
Error: 100: Key not found (/key1) [22]
[root@Docker01 ~]# etcdctl ls
[root@Docker01 ~]#
ls 列出目錄(默認為根目錄)下的鍵或者子目錄,默認不顯示子目錄中的內容
格式:etcdctl ls
例:
[root@Docker01 ~]# etcdctl mkdir /keydir1/key1 value1
[root@Docker01 ~]# etcdctl mkdir /keydir2/
[root@Docker01 ~]# etcdctl ls
/keydir1
/keydir2
[root@Docker01 ~]#
三、非數據庫操作
backup備份 etcd 的數據。
選項
--data-dir etcd 的數據目錄
--backup-dir 備份到指定路徑
watch檢測一個鍵值的變化
格式:etcdctl watch
--forever 一直監測,直到用戶按 CTRL+C
退出
--after-index '0' 在指定 index 之前一直監測
--recursive 返回所有的鍵值和子鍵值
exec-watch檢測一個鍵值的變化,一旦鍵值發生更新,就執行給定命令
格式:etcdctl exec-watch --
選項:
--after-index '0' 在指定 index 之前一直監測
--recursive 返回所有的鍵值和子鍵值
member通過list、add、remove命令列出、添加、刪除etcd實例到etcd集群中。
[root@Docker01 ~]# etcdctl member list
8e9e05c52164694d: name=default peerURLs=https://localhost:2380 clientURLs=https://localhost:2379 isLeader=true
[root@Docker01 ~]#
四、命令選項:
--debug 輸出 cURL 命令,顯示執行命令的時候發起的請求
--no-sync 發出請求之前不同步集群信息
--output, -o 'simple' 輸出內容的格式 (simple 為原始信息,json 為進行json格式解碼,易讀性好一些)
--peers, -C 指定集群中的同伴信息,用逗號隔開 (默認為: "127.0.0.1:4001")
--cert-file HTTPS 下客戶端使用的 SSL 證書文件
--key-file HTTPS 下客戶端使用的 SSL 密鑰文件
--ca-file 服務端使用 HTTPS 時,使用 CA 文件進行驗證
--help, -h 顯示幫助命令信息
--version, -v 打印版本信息
https://yeasy.gitbooks.io/docker_practice/content/etcd/intro.html資料:
最後更新:2017-08-13 22:38:40