部署環(huán)境三臺(tái)機(jī)子:
10.42.5.187
10.42.5.188
10.42.5.189
在10.42.5.187機(jī)子上部署:
解壓版本包
tar -zxvf etcd-v3.3.2-linux-amd64.tar.gz將解壓文件重命名并移到/etc/目錄下
mv etcd-v3.3.2-linux-amd64 /etc/etcd-v3.3.2將etcd和etcdctl加入系統(tǒng)變量PATH中(可以省略)
在 文件~/.bashrc中加入一行
export PATH=/etc/etcd-v3.3.2:$PATH
然后執(zhí)行:source ~/.bashrc 立即生效
4.進(jìn)入/etc/etcd-v3.3.2赡若,并創(chuàng)建配置文件etcd.conf达布,內(nèi)容:
name: etcd-1
data-dir: /etc/etcd-v3.3.2/data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://10.42.5.187:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://10.42.5.187:2380
initial-cluster: etcd-1=http://10.42.5.187:2380,etcd-2=http://10.42.5.188:2380,etcd-3=http://10.42.5.189:2380
initial-cluster-token: etcd-cluster-my
initial-cluster-state: new
注釋:listen-client-urls: http://10.42.5.187:2379 會(huì)報(bào)錯(cuò),所以直接用0.0.0.0斩熊;
listen-peer-urls: http://10.42.5.187:2380 也會(huì)報(bào)錯(cuò)往枣,所以直接用0.0.0.0;
name:本member的名稱;
data-dir:存儲(chǔ)的數(shù)據(jù)目錄分冈;
listen-client-urls:用于監(jiān)聽客戶端etcdctl或者curl連接圾另;0.0.0.0表示監(jiān)聽本機(jī)所有地址;
listen-peer-urls:用于監(jiān)聽集群中其它member的連接雕沉;0.0.0.0表示監(jiān)聽本機(jī)所有地址集乔;
advertise-client-urls: 本機(jī)地址, 用于通知客戶端坡椒,客戶端通過此IPs與集群通信;
initial-advertise-peer-urls:本機(jī)地址扰路,用于通知集群member,與member通信倔叼;
initial-cluster:描述集群中所有節(jié)點(diǎn)的信息汗唱,描述每個(gè)節(jié)點(diǎn)名稱、ip丈攒、端口哩罪,集群靜態(tài)啟動(dòng)使用,本member根據(jù)此信息去聯(lián)系其他member巡验;
initial-cluster-token:集群唯一標(biāo)示际插;
initial-cluster-state:集群狀態(tài),新建集群時(shí)候設(shè)置為new显设,若是想加入某個(gè)已經(jīng)存在的集群設(shè)置為existing
5.設(shè)置etcd版本號(hào)框弛,默認(rèn)為2,我們安裝的版本為3.3.2捕捂,所以此處設(shè)置為3
默認(rèn)示例:
[root@forimg system]# etcdctl
NAME:
etcdctl - A simple command line client for etcd.
WARNING:
Environment variable ETCDCTL_API is not set; defaults to etcdctl v2.
Set environment variable ETCDCTL_API=3 to use v3 API or ETCDCTL_API=2 to use v2 API.
USAGE:
etcdctl [global options] command [command options] [arguments...]
VERSION:
3.3.2
在 文件~/.bashrc中加入一行改為3
export ETCDCTL_API=3
- 設(shè)置開機(jī)啟動(dòng)瑟枫,新建文件/usr/lib/systemd/system/etcd.service,(可以跳過绞蹦,設(shè)置方便管理)配置內(nèi)容:
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
WorkingDirectory=/etc/etcd-v3.3.2
User=etcd
ExecStart=/etc/etcd-v3.3.2/etcd --config-file /etc/etcd-v3.3.2/etcd.conf
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
注釋:
WorkingDirectory和ExecStart填寫對應(yīng)的目錄和啟動(dòng)命令力奋。
- 啟動(dòng)etcd,啟動(dòng)第一個(gè)member的時(shí)候幽七,此member等待其它member初始化完畢景殷,處于watting狀態(tài),此時(shí)緊接著啟動(dòng)第二個(gè)就行了
systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
systemctl restart etcd
6.10.42.5.188澡屡,10.42.5.189部署如同上面步驟猿挚,只是將步驟4里面ip改為自己ip即可。
————————————————
常用指令:
etcd --version
etcdctl member list
檢查集群狀態(tài)
etcdctl endpoint health --
etcdctl put key1 value1
etcdctl get key1
etcdctl get foo --print-value-only
讀取foo為前綴的所有鍵/值
etcdctl get --prefix foo
etcdctl del key
設(shè)置過期時(shí)間
設(shè)置一個(gè)lease(id=3c27623e1704580d)過期時(shí)間為30秒
[root@pipeline-nj-2 etcd-v3.3.2]# etcdctl lease grant 30
lease 3c27623e1704580d granted with TTL(30s)
將id=3c27623e1704580d的lease應(yīng)用key5鍵值驶鹉,即設(shè)置key5鍵值對生命周期為30秒绩蜻,到期后自動(dòng)消亡
[root@pipeline-nj-2 etcd-v3.3.2]# etcdctl put key5 value5 --lease=3c27623e1704580d
OK
[root@pipeline-nj-2 etcd-v3.3.2]# etcdctl get key5
key5
value5
使id=3c27623e1704580d的lease繼續(xù)存活30秒,到時(shí)間后自動(dòng)消亡
[root@pipeline-nj-2 etcd-v3.3.2]# etcdctl lease keep-alive 3c27623e1704580d
lease 3c27623e1704580d keepalived with TTL(30)
撤銷id=3c27623e1704580d的release室埋,立即終止生命周期
[root@pipeline-nj-2 etcd-v3.3.2]# etcdctl lease revoke 3c27623e1704580d
lease 3c27623e1704580d revoked