1 脚本启动方式
-
创建/data目录,关闭防火墙,selinux;上传etcd软件包
etcd-v3.2.12-linux-amd64.tar.gz
-
解压软件包
# 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
-
配置
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 &
-
给etcd.sh赋予执行权限并启动
# chmod +x /data/etcd-v3.2.12/etcd.sh # /data/etcd-v3.2.12/etcd.sh
-
查看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
-
查看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启动方式
-
创建/data目录,关闭防火墙,selinux;上传etcd软件包
etcd-v3.2.12-linux-amd64.tar.gz
-
解压软件包
# 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
-
准备配置文件存放目录
# mkdir /etc/etcd
-
编写配置文件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"
-
编写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
-
重载服务配置文件
# systemctl daemon-reload
-
启动服务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
-
查看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
-
查看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