etcd集群部署

Posted YJ.li

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了etcd集群部署相关的知识,希望对你有一定的参考价值。

1 脚本启动方式

  1. 创建/data目录,关闭防火墙,selinux;上传etcd软件包etcd-v3.2.12-linux-amd64.tar.gz

  2. 解压软件包

    # tar xf etcd-v3.2.12-linux-amd64.tar.gz -C /data/
    # mv /data/etcd-v3.2.12-linux-amd64 /data/etcd-v3.2.12
    
  3. 配置etcd.sh启动文件

    1)node1节点

    [root@192-168-2-29 etcd-v3.2.12]# cat etcd.sh 
    # For each machine
    TOKEN=my-etcd-token-1
    CLUSTER_STATE=new
    NAME_1=etcd_1
    NAME_2=etcd_2
    NAME_3=etcd_3
    HOST_1=192.168.2.29
    HOST_2=192.168.2.56
    HOST_3=192.168.2.71
    CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380
    
    # For node 1
    THIS_NAME=${NAME_1}
    THIS_IP=${HOST_1}
    /data/etcd-v3.2.12/etcd --data-dir=/data/etcd-v3.2.12/data.etcd --name ${THIS_NAME} 	--initial-advertise-peer-urls http://${THIS_IP}:2380 	--listen-peer-urls http://${THIS_IP}:2380 	--advertise-client-urls http://${THIS_IP}:2379,http://127.0.0.1:2379 	--listen-client-urls http://${THIS_IP}:2379,http://127.0.0.1:2379 	--initial-cluster ${CLUSTER} 	--initial-cluster-state ${CLUSTER_STATE} 	--initial-cluster-token ${TOKEN}  > /data/etcd-v3.2.12/etcd.out 2>&1 &
    

    2)node2节点

    [root@192-168-2-56 etcd-v3.2.12]# cat etcd.sh 
    # For each machine
    TOKEN=my-etcd-token-1
    CLUSTER_STATE=new
    NAME_1=etcd_1
    NAME_2=etcd_2
    NAME_3=etcd_3
    HOST_1=192.168.2.29
    HOST_2=192.168.2.56
    HOST_3=192.168.2.71
    CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380
    
    # For node 2
    THIS_NAME=${NAME_2}
    THIS_IP=${HOST_2}
    /data/etcd-v3.2.12/etcd --data-dir=/data/etcd-v3.2.12/data.etcd --name ${THIS_NAME} 	--initial-advertise-peer-urls http://${THIS_IP}:2380 	--listen-peer-urls http://${THIS_IP}:2380 	--advertise-client-urls http://${THIS_IP}:2379,http://127.0.0.1:2379 	--listen-client-urls http://${THIS_IP}:2379,http://127.0.0.1:2379 	--initial-cluster ${CLUSTER} 	--initial-cluster-state ${CLUSTER_STATE} 	--initial-cluster-token ${TOKEN}  > /data/etcd-v3.2.12/etcd.out 2>&1 &
    

    3)node3节点

    [root@192-168-2-71 etcd-v3.2.12]# cat etcd.sh 
    # For each machine
    TOKEN=my-etcd-token-1
    CLUSTER_STATE=new
    NAME_1=etcd_1
    NAME_2=etcd_2
    NAME_3=etcd_3
    HOST_1=192.168.2.29
    HOST_2=192.168.2.56
    HOST_3=192.168.2.71
    CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380
    
    # For node 3
    THIS_NAME=${NAME_3}
    THIS_IP=${HOST_3}
    /data/etcd-v3.2.12/etcd --data-dir=/data/etcd-v3.2.12/data.etcd --name ${THIS_NAME} 	--initial-advertise-peer-urls http://${THIS_IP}:2380 	--listen-peer-urls http://${THIS_IP}:2380 	--advertise-client-urls http://${THIS_IP}:2379,http://127.0.0.1:2379 	--listen-client-urls http://${THIS_IP}:2379,http://127.0.0.1:2379 	--initial-cluster ${CLUSTER} 	--initial-cluster-state ${CLUSTER_STATE} 	--initial-cluster-token ${TOKEN}  > /data/etcd-v3.2.12/etcd.out 2>&1 &
    
  4. 给etcd.sh赋予执行权限并启动

    # chmod +x /data/etcd-v3.2.12/etcd.sh
    # /data/etcd-v3.2.12/etcd.sh
    
  5. 查看cluster成员

    [root@192-168-2-29 etcd-v3.2.12]# /data/etcd-v3.2.12/etcdctl member list
    30c42f05af63bfb4: name=etcd_2 peerURLs=http://192.168.2.56:2380 clientURLs=http://127.0.0.1:2379,http://192.168.2.56:2379 isLeader=false
    85c8cfeb60178e73: name=etcd_3 peerURLs=http://192.168.2.71:2380 clientURLs=http://127.0.0.1:2379,http://192.168.2.71:2379 isLeader=false
    c497a3d3216ce778: name=etcd_1 peerURLs=http://192.168.2.29:2380 clientURLs=http://127.0.0.1:2379,http://192.168.2.29:2379 isLeader=true
    
  6. 查看cluster健康状态

    方法①
    [root@192-168-2-29 etcd-v3.2.12]# /data/etcd-v3.2.12/etcdctl cluster-health
    member 30c42f05af63bfb4 is healthy: got healthy result from http://127.0.0.1:2379
    member 85c8cfeb60178e73 is healthy: got healthy result from http://127.0.0.1:2379
    member c497a3d3216ce778 is healthy: got healthy result from http://127.0.0.1:2379
    cluster is healthy
    
    方法②
    [root@192-168-2-29 etcd-v3.2.12]# curl http://192.168.2.29:2379/health
    {"health": "true"}[root@192-168-2-29 etcd-v3.2.12]# curl http://192.168.2.56:2379/health
    {"health": "true"}[root@192-168-2-29 etcd-v3.2.12]# curl http://192.168.2.71:2379/health
    {"health": "true"}[root@192-168-2-29 etcd-v3.2.12]# 
    

2 systemd启动方式

  1. 创建/data目录,关闭防火墙,selinux;上传etcd软件包etcd-v3.2.12-linux-amd64.tar.gz

  2. 解压软件包

    # tar xf etcd-v3.2.12-linux-amd64.tar.gz -C /data/
    # mv /data/etcd-v3.2.12-linux-amd64 /data/etcd-v3.2.12
    
  3. 准备配置文件存放目录

    # mkdir /etc/etcd
    
  4. 编写配置文件etcd.conf

    1)node1节点

    [root@192-168-2-29 ~]# cat /etc/etcd/etcd.conf 
    #[Member]
    ETCD_NAME="etcd_1"
    ETCD_DATA_DIR="/data/etcd-v3.2.12/data.etcd"
    ETCD_LISTEN_PEER_URLS="http://192.168.2.29:2380"
    ETCD_LISTEN_CLIENT_URLS="http://192.168.2.29:2379"
    
    #[Clustering]
    ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.2.29:2380"
    ETCD_ADVERTISE_CLIENT_URLS="http://192.168.2.29:2379"
    ETCD_INITIAL_CLUSTER="etcd_1=http://192.168.2.29:2380,etcd_2=http://192.168.2.56:2380,etcd_3=http://192.168.2.71:2380"
    ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
    ETCD_INITIAL_CLUSTER_STATE="new"
    

    2)node2节点

    [root@192-168-2-56 ~]# cat /etc/etcd/etcd.conf 
    #[Member]
    ETCD_NAME="etcd_2"
    ETCD_DATA_DIR="/data/etcd-v3.2.12/data.etcd"
    ETCD_LISTEN_PEER_URLS="http://192.168.2.56:2380"
    ETCD_LISTEN_CLIENT_URLS="http://192.168.2.56:2379"
    
    #[Clustering]
    ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.2.56:2380"
    ETCD_ADVERTISE_CLIENT_URLS="http://192.168.2.56:2379"
    ETCD_INITIAL_CLUSTER="etcd_1=http://192.168.2.29:2380,etcd_2=http://192.168.2.56:2380,etcd_3=http://192.168.2.71:2380"
    ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
    ETCD_INITIAL_CLUSTER_STATE="new"
    

    3)node3节点

    [root@192-168-2-71 ~]# cat /etc/etcd/etcd.conf 
    #[Member]
    ETCD_NAME="etcd_3"
    ETCD_DATA_DIR="/data/etcd-v3.2.12/data.etcd"
    ETCD_LISTEN_PEER_URLS="http://192.168.2.71:2380"
    ETCD_LISTEN_CLIENT_URLS="http://192.168.2.71:2379"
    
    #[Clustering]
    ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.2.71:2380"
    ETCD_ADVERTISE_CLIENT_URLS="http://192.168.2.71:2379"
    ETCD_INITIAL_CLUSTER="etcd_1=http://192.168.2.29:2380,etcd_2=http://192.168.2.56:2380,etcd_3=http://192.168.2.71:2380"
    ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
    ETCD_INITIAL_CLUSTER_STATE="new"
    
  5. 编写systemd启动文件

    # cat /usr/lib/systemd/system/etcd.service 
    [Unit]
    Description=Etcd Server
    After=network.target
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    Type=notify
    EnvironmentFile=-/etc/etcd/etcd.conf
    ExecStart=/data/etcd-v3.2.12/etcd --name=${ETCD_NAME} --data-dir=${ETCD_DATA_DIR} --listen-peer-urls=${ETCD_LISTEN_PEER_URLS} --listen-client-urls=${ETCD_LISTEN_CLIENT_URLS},http://127.0.0.1:2379 --advertise-client-urls=${ETCD_ADVERTISE_CLIENT_URLS},http://127.0.0.1:2379 --initial-advertise-peer-urls=${ETCD_INITIAL_ADVERTISE_PEER_URLS} --initial-cluster=${ETCD_INITIAL_CLUSTER} --initial-cluster-token=${ETCD_INITIAL_CLUSTER} --initial-cluster-state=${ETCD_INITIAL_CLUSTER_STATE}
    
    Restart=on-failure
    RestartSec=5
    LimitNOFILE=65536
    
    [Install]
    WantedBy=multi-user.target
    
  6. 重载服务配置文件

    # systemctl daemon-reload
    
  7. 启动服务etcd 注意:这里启动需要三台一起启动,不然先启动的会一直卡在那里

    [root@192-168-2-29 ~]# systemctl start etcd
    [root@192-168-2-56 ~]# systemctl start etcd
    [root@192-168-2-71 ~]# systemctl start etcd
    
  8. 查看cluster成员

    [root@192-168-2-29 ~]# /data/etcd-v3.2.12/etcdctl member list
    829071e6c4e48ea: name=etcd_2 peerURLs=http://192.168.2.56:2380 clientURLs=http://127.0.0.1:2379,http://192.168.2.56:2379 isLeader=false
    79d3438e3a7d6f7e: name=etcd_3 peerURLs=http://192.168.2.71:2380 clientURLs=http://127.0.0.1:2379,http://192.168.2.71:2379 isLeader=false
    fe64de8659b559f1: name=etcd_1 peerURLs=http://192.168.2.29:2380 clientURLs=http://127.0.0.1:2379,http://192.168.2.29:2379 isLeader=true
    
  9. 查看cluster集群状态

    [root@192-168-2-29 ~]# /data/etcd-v3.2.12/etcdctl cluster-health
    member 829071e6c4e48ea is healthy: got healthy result from http://127.0.0.1:2379
    member 79d3438e3a7d6f7e is healthy: got healthy result from http://127.0.0.1:2379
    member fe64de8659b559f1 is healthy: got healthy result from http://127.0.0.1:2379
    cluster is healthy
    

以上是关于etcd集群部署的主要内容,如果未能解决你的问题,请参考以下文章

kubernetes容器集群部署Etcd集群

k8s-外置ETCD集群部署

一键部署ETCD集群脚本

部署etcd集群

2etcd单机部署和集群部署

ETCD集群部署