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

centos7 部署 etcd 集群

程序员文章站 2022-07-13 22:39:29
...

etcd 集群部署

实验环境:3 台 centos7.6 vm
etcd 3.3.11
1、3 个节点

node ip
etcd1 192.168.116.14
etcd2 192.168.116.22
etcd3 192.168.116.12

2、配置etcd1 service,etcd2 etcd3 类似

[[email protected]~]# cat /etc/systemd/system/etcd.service 
[Unit]
Description=etcd server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
ExecStart=/usr/bin/etcd \
  --name etcd1 \
  --initial-advertise-peer-urls http://192.168.116.14:2380 \
  --listen-peer-urls http://192.168.116.14:2380 \
  --listen-client-urls http://192.168.116.14:2379,http://127.0.0.1:2379 \
  --advertise-client-urls http://192.168.116.14:2379 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster etcd1=http://192.168.116.14:2380,etcd2=http://192.168.116.22:2380,etcd3=http://192.168.116.12:2380 \
  --initial-cluster-state new \
  --data-dir=/var/lib/etcd

Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

注意:–initial-advertise-peer-urls 不能出现 http://127.0.0.1:2380
etcd3 节点配置

[[email protected] ~]# cat /etc/systemd/system/etcd.service 
[Unit]
Description=etcd server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
ExecStart=/usr/bin/etcd \
  --name etcd3 \
  --initial-advertise-peer-urls http://192.168.116.12:2380 \
  --listen-peer-urls http://192.168.116.12:2380 \
  --listen-client-urls http://192.168.116.12:2379,http://127.0.0.1:2379 \
  --advertise-client-urls http://192.168.116.12:2379 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster etcd1=http://192.168.116.14:2380,etcd2=http://192.168.116.22:2380,etcd3=http://192.168.116.12:2380 \
  --initial-cluster-state new \
  --data-dir=/var/lib/etcd

Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

3、安装 etcd Version: 3.3.1 ,3 个节点都执行

yum install etcd -y
systemctl daemon-reload
systemctl start etcd
systemctl enable etcd

etcd 常用操作

set 指定键值

etcdctl set /testdir/testkey "hello"

get 获取键值

etcdctl get /testdir/testkey 

update 更新键值

etcdctl update /testdir/testkey "hello, ciao"

rm 删除键值

etcdctl rm /testdir/testkey

mk 创建键值

etcdctl mk /testdir/testkey "hello"

mkdir 创建键目录

etcdctl mkdir testdir2

setdir 创建键目录,存在就更新

etcdctl setdir testdir3

updatedir 更新一个已经存在的目录

etcdctl updatedir testdir2

rmdir 删除空目录或者键值对

etcdctl rmdir testdir2

ls 列出目录

etcdctl ls

backup 备份 etcd 的数据

etcdctl backup --data-dir /var/lib/etcd --back-dir /home/etcd_backup

watch 监控键值对的变化,一旦值发生变化,就会输出最新的值并退出

etcdctl get /testdir/testkey

exec-watch 监控键值对变化,一旦更新,执行给定命令

etcdctl exec-watch testdir/testkey -- sh -c 'ls'

member 通过 list、 add、remove 列出、添加、删除 etcd 实例到 etcd 集群中

etcdctl member list
etcdctl member remove  8413a3648cf25b08
etcdctl member add etcd2 
http://192.168.116.22:2380

注意事项:
etcdctl member remove 后,执行 etcdctl member add,实例并没有添加到集群

[[email protected] ~]# etcdctl cluster-health
member 4bda9d95f7b1cd43 is healthy: got healthy result from http://192.168.116.12:2379
member 8413a3648cf25b08 is healthy: got healthy result from http://192.168.116.14:2379
member 9d1cfa2c91e4fdcd is unreachable: no available published client urls

需要启动该实例中的 etcd 服务(启动之前,建议修改添加的实例配置 --initial-cluster-state new 为 --initial-cluster-state existing)。先卸载 etced,再安装,不然会报错:Failed at step CHDIR spawning /usr/bin/etcd: No such file or directory。错误日志位置/var/log/message,目前还未找到专门存放 etcd 的日志文件。