k8s HA 高可用集群之etcd集群

    xiaoxiao2025-10-31  4

    Etcd集群简介

    etcd组件作为一个高可用强一致性的服务发现存储仓库。我们此次在三个主机上搭建了一个包含三个Etcd节点的集群,实现了集群的动态扩展和收缩,并测试和验证了Etcd集群键——值存储的一致性和高可用性。

    一、环境准备

    OS: Ubuntu 14.04

    user: root

    ipaddress: etcd01: 192.168.200.24

    etcd02: 192.168.200.25

    etcd03: 192.168.200.26

    下载etcd 源码包:https://github.com/coreos/etcd/releases/

    这里用的是etcd-v2.3.7-linux-amd64.tar.gz

    二、安装配置etcd

    etcd01上安装

    tar xf etcd-v2.3.7-linux-amd64.tar.gz

    cd etcd-v2.3.7-linux-amd64

    cp etcd* /usr/local/bin/

    创建etcd01脚本,可方便配置etcd启动参数

    #cat etcd01

    /usr/local/bin/etcd  -name etcd01 \

    -data-dir /data/etcd01 \

    -advertise-client-urls http://192.168.200.24:2379,http://192.168.200.24:4001  \

    -listen-client-urls  http://0.0.0.0:2379,http://192.168.200.24:4001 \

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

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

    -initial-cluster-token etcd-cluster-1 \

    -initial-cluster etcd01=http://192.168.200.24:2380,etcd02=http://192.168.200.25:2380,etcd03=http://192.168.200.26:2380 \

    -initial-cluster-state new

    参数说明:-name 指定名字

    -data-dir 指定数据保存目录,默认是当前目录

    -initial-cluster-state 集群状态 new为新创建集群 existing为已存在(可不指定)

    在etcd02 etcd03上分别做相似操作

    脚本中-advertise-client-urls 和 -initial-advertis-peer-urls 参数修改一下即可

    然后分别运行脚本:nohup ./etcd01 &

    三、测试

    在任一台主机上执行etcdctl member list

    #etcdctl member list

    6a223770249e927d: name=etcd02 peerURLs=http://192.168.200.25:2380 clientURLs=http://192.168.200.25:2379,http://192.168.200.25:4001 isLeader=false

    7e0ce16121dfea24: name=etcd01 peerURLs=http://192.168.200.24:2380 clientURLs=http://192.168.200.24:2379,http://192.168.200.24:4001 isLeader=true

    bfc28be8765b503e: name=etcd03 peerURLs=http://192.168.200.26:2380 clientURLs=http://192.168.200.26:2379,http://192.168.200.26:4001 isLeader=false

    可以看到集群的节点情况,并能看出哪个是leader节点

    我们在etcd01上设置一个key/value

    root@etcd1:~# etcdctl set api_server http://192.168.5.44:8080

    http://192.168.5.44:8080

    这时就可以在任意一台主机上获取这个key/value

    root@etcd2:~# etcdctl get api_server

    http://192.168.5.44:8080

    root@etcd3:~# etcdctl get api_server

    http://192.168.5.44:8080

    在member list上看到etcd01是leader ,这时把etcd01停掉(kill)

    用etcdctl cluster-health查看

    root@etcd2:~# etcdctl cluster-health

    member 6a223770249e927d is healthy: got healthy result from http://192.168.200.25:2379

    failed to check the health of member 7e0ce16121dfea24 on http://192.168.200.24:2379: Get http://192.168.200.24:2379/health: dial tcp 192.168.200.24:2379: getsockopt: connection refused

    failed to check the health of member 7e0ce16121dfea24 on http://192.168.200.24:4001: Get http://192.168.200.24:4001/health: dial tcp 192.168.200.24:4001: getsockopt: connection refused

    member 7e0ce16121dfea24 is unreachable: [http://192.168.200.24:2379 http://192.168.200.24:4001] are all unreachable

    member bfc28be8765b503e is healthy: got healthy result from http://192.168.200.26:2379

    cluster is healthy

    并且集群leader进行了重新选举

    root@etcd2:~# etcdctl member list

    6a223770249e927d: name=etcd02 peerURLs=http://192.168.200.25:2380 clientURLs=http://192.168.200.25:2379,http://192.168.200.25:4001 isLeader=true

    7e0ce16121dfea24: name=etcd01 peerURLs=http://192.168.200.24:2380 clientURLs=http://192.168.200.24:2379,http://192.168.200.24:4001 isLeader=false

    bfc28be8765b503e: name=etcd03 peerURLs=http://192.168.200.26:2380 clientURLs=http://192.168.200.26:2379,http://192.168.200.26:4001 isLeader=false

    现在etcd02是leader了,这时我们在群集中设置两个key/value

    root@etcd2:~# etcdctl set test01 123456

    123456

    root@etcd2:~# etcdctl set test02 abcdefg

    abcdefg

    重新启动etcd01

    root@etcd1:~# etcdctl cluster-health

    member 6a223770249e927d is healthy: got healthy result from http://192.168.200.25:2379

    member 7e0ce16121dfea24 is healthy: got healthy result from http://192.168.200.24:2379

    member bfc28be8765b503e is healthy: got healthy result from http://192.168.200.26:2379

    cluster is healthy

    root@etcd1:~# etcdctl get test01

    123456

    root@etcd1:~# etcdctl get test02

    abcdefg

    但这时在etcd01重新加入集群,并保持了key/value的全局一致性,由此可见 etcd 搭建的集群是可以实现高可用的。

    Etcd集群的扩展与收缩

    etcd集群如果收缩很简单,直接在命令行输入

    etcdctl member remove {$memberID}

    $memberID是你即将要删除节点的etcd的ID,etcd的扩展有一些地方需要注意一下,我在这里操作的时候遇到了不少坑。从上文写到现在,有一个文件夹很重要,几乎每个坑都与它有关,那就是-data-dir所声明的文件夹,注意要扩展一个etcd集群时,首先在集群内的任一台机器上输入

    etcdctl member add $etcd_name$peer_url

    $etcd_name:新加入的etcd节点的名字  $peer_url:一般为新加入的节点 IP:2380 

    转载请注明原文地址: https://ju.6miu.com/read-1303705.html
    最新回复(0)