ETCD集群的配置与扩容

目标:组建3台节点的etcd集群

节点信息:

node010.211.55.9
node110.211.55.10
node210.211.55.11

一. 准备工作:

1.关闭并停用防火墙

    systemctl stop firewalld.service 

    systemctl disable firewalld.service

2.永久关闭SELinux 

    vim /etc/selinux/config 

    SELINUX=disabled

3.同步集群系统时间

    yum -y install ntp

    ntpdate 0.asia.pool.ntp.org

4.重启机器

    reboot

二.安装与配置参数:

安装ETCD

    yum -y install etcd (由于是rpm包所以可以用remove卸载)

    

配置工作目录:

    mkdir /ETCD

    cd /ETCD

    mkdir data (用于放置数据,清除该文件夹内容,相当于重置集群)

    mkdir log  (用于输出日志)


编写启动与重启脚本:

1启动脚本(build.sh)

    #!/bin/bash

    etcd --name node1 \

    --data-dir /ETCD/data/ \

    --listen-peer-urls http://10.211.55.10:2380 \

    --listen-client-urls http://10.211.55.10:2379,http://127.0.0.1:2379 \

    --initial-advertise-peer-urls http://10.211.55.10:2380 \

    --advertise-client-urls http://10.211.55.10:2379 \

    --initial-cluster node0=http://10.211.55.9:2380,node1=http://10.211.55.10:2380,node2=http://10.211.55.11:2380 \

    --initial-cluster-token Achilles \

    --initial-cluster-state new

  

2重启脚本(restart.sh)

    etcd --name node1 \

    --data-dir /ETCD/data/ \

    --listen-peer-urls http://10.211.55.10:2380 \

    --listen-client-urls http://10.211.55.10:2379,http://127.0.0.1:2379 \

    --advertise-client-urls http://10.211.55.10:2379 \


注意:为了方便管理,我将两个脚本都放置于/ETCD目录下,启动脚本build.sh只在集群第一次初始化时启用,节点掉线恢复均使用restart.sh

更改脚本的执行权限:

    chmod +x ./build.sh

    chmod +x ./restart.sh


每台节点均执行以上操作后,分别启动build.sh脚本:

    nohup ./build.sh > /ETCD/log/etcd.log 2>&1 &

    该命令表示后台执行,并且将日志输出至“/ETCD/log/etcd.log”


此时,3节点的etcd集群已建立,执行健康检查:

健康检查:

    etcdctl cluster-health


节点掉线或者重启,则执行相应节点的restart.sh脚本:

    nohup ./restart.sh > /ETCD/log/etcd.log 2>&1 &


三.增减成员节点:

新增节点(例如10.211.55.12)

    在集群任意节点输入:

    etcdctl member add http://10.211.55.12:2380

    命令行返回:

    added member 9bf1b35fc7761a23 to cluster

    ETCD_NAME="node3"

    ETCD_INITIAL_CLUSTER="node0=http://10.211.55.9:2380,node1=http://10.211.55.10:2380,node2=http://10.211.55.11:2380,node3=http://10.211.55.12:2380"

    ETCD_INITIAL_CLUSTER_STATE=existing

    其中ETCD_INITIAL_CLUSTER是最为重要的信息,记录了集群成员。

    编写新增节点脚本:

    #!/bin/bash

    etcd --name node3 \

    --data-dir /ETCD/data/ \

    --listen-peer-urls http://10.211.55.12:2380 \

    --listen-client-urls http://10.211.55.12:2379,http://127.0.0.1:2379 \

    --initial-advertise-peer-urls http://10.211.55.12:2380 \

    --advertise-client-urls http://10.211.55.12:2379 \

    --initial-cluster node0=http://10.211.55.9:2380,node1=http://10.211.55.10:2380,node2=http://10.211.55.11:2380,node3=http://10.211.55.12:2380 \

    --initial-cluster-token Achilles \

    --initial-cluster-state existing

    可以发现,新增脚本与启动脚本的区别仅在于--initial-cluster参数多了新增节点的ip:port,--initial-cluster-state参数表示集群状态,existing表示加入一个已存在的集群,如果填new,该脚本会新生成一个集群ID,产生冲突。

    执行新增脚本,新节点加入集群。

删除节点:

    etcdctl member remove 节点ID

    删除节点的etcd服务会自动停止,若要将其加入其他集群,需要先清除/ETCD/data下的数据内容。

备份数据:

    etcdctl backup --data-dir /ETCD/data/ --backup-dir /backup/etcd


四.总结:

    ETCD集群的操作分两个阶段,即启动与运行阶段,启动阶段的集群有3种配置方式,

  1. 静态配置
  2. etcd自发现模式
  3. DNS自发现模式

    本文采用的是静态配置,etcd自发现模式与静态配置的区别在于静态模式预先知道所有节点的ip信息,而etcd自发现则不用,

    配置方法与静态配置类似,仅仅将启动脚本build.sh中--initial-cluster参数去掉,新增:

   --discovery https://discovery.etcd.io/964f7149b939aa1199536505cc88eacc \

    其中,discovery参数的获取方法:

    curl https://discovery.etcd.io/new?size=3

    返回 https://discovery.etcd.io/f1b620b986fae36fc2994c06c00e30ed

    两种启动方法的运行阶段操作基本一致,DNS发现模式暂且不表。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值