Etcd集羣環境搭建

參考自

https://github.com/etcd-io/etcd/releases/
https://etcd.io/docs/v3.3.12/op-guide/clustering/
https://www.cnblogs.com/li-peng/p/9259793.html

單個Etcd

  • 版本:v3.3.15

1、部署安裝

ETCD_VER=v3.3.15

# choose either URL
GOOGLE_URL=https://storage.googleapis.com/etcd
GITHUB_URL=https://github.com/etcd-io/etcd/releases/download
DOWNLOAD_URL=${GOOGLE_URL}

rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
rm -rf /tmp/etcd-download-test && mkdir -p /tmp/etcd-download-test

curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
tar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /tmp/etcd-download-test --strip-components=1
rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz

# copy execute file to /usr/local/bin
cd /tmp/etcd-download-test
sudo cp etcd* /usr/local/bin/

2、啓動檢查

# version check
etcd --version
ETCDCTL_API=3 etcdctl version

# start a local etcd server
etcd

# write,read to etcd
ETCDCTL_API=3 etcdctl --endpoints=localhost:2379 put foo bar
ETCDCTL_API=3 etcdctl --endpoints=localhost:2379 get foo

Etcd集羣搭建

  • 三個Etcd實例組成的集羣,每個實例部署在一臺機器上
  • name選擇每臺機器的hostname值

1、創建etcd數據存儲目錄

mkdir -p /home/admin/etcd

2、10.0.0.1上創建/etc/systemd/system/etcd0.service

[Unit]
Description=etcd
Documentation=https://github.com/coreos/etcd
Conflicts=etcd.service
Conflicts=etcd2.service

[Service]
Type=notify
Restart=always
RestartSec=5s
LimitNOFILE=40000
TimeoutStartSec=0

ExecStart=/usr/local/bin/etcd --name hostname1
--data-dir /home/admin/etcd   
--initial-advertise-peer-urls http://10.0.0.1:2380   
--listen-peer-urls http://10.0.0.1:2380   
--listen-client-urls http://10.0.0.1:2379,http://127.0.0.1:2379   
--advertise-client-urls http://10.0.0.1:2379   
--initial-cluster-token etcd-cluster-1   
--initial-cluster hostname1=http://10.0.0.1:2380,hostname2=http://10.0.0.2:2380,hostname3=http://10.0.0.3:2380   
--initial-cluster-state new

[Install]
WantedBy=multi-user.target

3、10.0.0.2上創建/etc/systemd/system/etcd1.service

[Unit]
Description=etcd
Documentation=https://github.com/coreos/etcd
Conflicts=etcd.service
Conflicts=etcd2.service

[Service]
Type=notify
Restart=always
RestartSec=5s
LimitNOFILE=40000
TimeoutStartSec=0

ExecStart=/usr/local/bin/etcd --name hostname2  
--data-dir /home/admin/etcd   
--initial-advertise-peer-urls http://10.0.0.2:2380   
--listen-peer-urls http://10.0.0.2:2380   
--listen-client-urls http://10.0.0.2:2379,http://127.0.0.1:2379   
--advertise-client-urls http://10.0.0.2:2379   
--initial-cluster-token etcd-cluster-1   
--initial-cluster hostname1=http://10.0.0.1:2380,hostname2=http://10.0.0.2:2380,hostname3=http://10.0.0.3:2380   
--initial-cluster-state new

[Install]
WantedBy=multi-user.target

4、10.0.0.3上創建/etc/systemd/system/etcd2.service

[Unit]
Description=etcd
Documentation=https://github.com/coreos/etcd
Conflicts=etcd.service

[Service]
Type=notify
Restart=always
RestartSec=5s
LimitNOFILE=40000
TimeoutStartSec=0

ExecStart=/usr/local/bin/etcd --name hostname3   
--data-dir /home/admin/etcd
--initial-advertise-peer-urls http://10.0.0.3:2380   
--listen-peer-urls http://10.0.0.3:2380   
--listen-client-urls http://10.0.0.3:2379,http://127.0.0.1:2379   
--advertise-client-urls http://10.0.0.3:2379   
--initial-cluster-token etcd-cluster-1   
--initial-cluster hostname1=http://10.0.0.1:2380,hostname2=http://10.0.0.2:2380,hostname3=http://10.0.0.3:2380   
--initial-cluster-state new

[Install]
WantedBy=multi-user.target

5、啓動服務

sudo systemctl daemon-reload
sudo systemctl enable etcd0.service
sudo systemctl start etcd0.service

sudo systemctl daemon-reload
sudo systemctl enable etcd1.service
sudo systemctl start etcd1.service

sudo systemctl daemon-reload
sudo systemctl enable etcd2.service
sudo systemctl start etcd2.service

6、集羣測試

# check status
ETCDCTL_API=3  etcdctl   --endpoints 10.0.0.1:2379,10.0.0.2:2379,10.0.0.3:2379 endpoint status  --write-out="table"

# write on one machine
ETCDCTL_API=3 etcdctl  put foo1 bar1
#read on another machine
ETCDCTL_API=3 etcdctl  get foo1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章