欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Etcd集群环境搭建

程序员文章站 2022-07-13 22:19:28
...

参考自

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