centos7 部署 etcd 集群

本文详细介绍了在三台CentOS 7.6虚拟机上部署etcd 3.3.11集群的过程,包括各节点的配置、服务安装与启动步骤,以及etcd的常见操作指令,如设置、获取、更新键值等。
etcd 集群部署

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

nodeip
etcd1192.168.116.14
etcd2192.168.116.22
etcd3192.168.116.12

2、配置etcd1 service,etcd2 etcd3 类似

[root@etcd1~]# 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 节点配置

[root@etcd3 ~]# 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,实例并没有添加到集群

[root@etcd1 ~]# 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 的日志文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值