前提条件:
1、需要有3个节点
2、etcd二进制文件在3各节点的/kubernetes/etcdworkspace/目录(没有可自行创建)
3、3个节点时间误差不要太大,否则影响集群功能,多节点ntp配置https://www.cnblogs.com/ouyanghuanlin/articles/11207797.html
下载链接:
1、etcd:https://pan.baidu.com/s/1CufZFyCQsNoP5T2vmAnYZg 提取码:968s
2、sshpass:https://pan.baidu.com/s/1AlXOael_zBltASwEysjagg 提取码:7h2x
下面是启动etcd集群的脚本:
1、ip请替换为实际3个节点ip(15-17、32-34行)、sshpass命令行中的密码(84-90行)替换为实际密码
2、sshpass工具系统没有自带,将准备好的sshpass二进制文件放置在脚本执行节点的/usr/bin/目录下,权限和用户组与该目录下的其他文件一致即可
#!/bin/bash
##########################################
########### etcd集群启动脚本 ###########
##########################################
# 需要关闭etcd所在节点的防火墙,才能组建集群
# 关闭防火墙
# systemctl stop firewalld.service
# 禁止防火墙开机自启
# systemctl disable firewalld.service
# etcd所在三个节点的IP
# 要求三个节点的etcd工作目录为/kubernetes/etcdworkspace/
# 该目录内有etcd二进制文件,data/、log/、wal/文件夹,etcdctl(etcd客户端工具,非必须)
# 权限为755
# IP请使用实际ip替代
IP01="192.168.10.111"
IP02="192.168.10.112"
IP03="192.168.10.113"
WORKDIR=/kubernetes/etcdworkspace
# 生成etcd的执行脚本
echo '#!/bin/bash
flag=$1
WORKDIR=/kubernetes/etcdworkspace
PEER_PORT=2380
CLIENT_PORT=2379
NAME_PREFIX="etcd-kubernetes"
ETCD_CLUSTER_TOKEN="${NAME_PREFIX}-cluster"
# IP请使用实际ip替代
IP01="192.168.10.111"
IP02="192.168.10.112"
IP03="192.168.10.113"
etcd_name="0"
if [[ $(ifconfig eth0 | grep "${IP01}" | wc -l)x = "1"x ]]; then
etcd_name="01"
IP=${IP01}
elif [[ $(ifconfig eth0 | grep "${IP02}" | wc -l)x = "1"x ]]; then
etcd_name="02"
IP=${IP02}
elif [[ $(ifconfig eth0 | grep "${IP03}" | wc -l)x = "1"x ]]; then
etcd_name="03"
IP=${IP03}
else
echo "no ip match"
exit 1
fi
echo "etcd_name is [${etcd_name}]"
if [[ ${flag}x == stopx ]] || [[ ${flag}x == restartx ]]; then
pid=$(ps -ef | grep etcd | grep ${NAME_PREFIX}-${etcd_name} | grep -v grep | awk '"'"'{print $2}'"'"')
if [[ ${pid}x != x ]]; then
echo "kill etcd pid [${pid}]"
kill -9 ${pid}
echo "etcd is stop"
fi
fi
if [[ ${flag}x == startx ]] || [[ ${flag}x == restartx ]]; then
echo "etcd ready to start"
elif [[ ${flag}x == stopx ]]; then
echo "etcd stop"
exit 0
else
echo "command in [start, restart, stop]"
exit 1
fi
# 启动节点etcd的命令
${WORKDIR}/etcd --name ${NAME_PREFIX}-${etcd_name} --debug \
--data-dir ${WORKDIR}/data \
--wal-dir ${WORKDIR}/wal \
--initial-advertise-peer-urls http://${IP}:${PEER_PORT} \
--listen-peer-urls http://${IP}:${PEER_PORT} \
--listen-client-urls http://${IP}:${CLIENT_PORT},http://127.0.0.1:${CLIENT_PORT} \
--advertise-client-urls http://${IP}:${CLIENT_PORT} \
--initial-cluster-token ${ETCD_CLUSTER_TOKEN} \
--initial-cluster ${NAME_PREFIX}-01=http://${IP01}:${PEER_PORT},${NAME_PREFIX}-02=http://${IP02}:${PEER_PORT},${NAME_PREFIX}-03=http://${IP03}:${PEER_PORT} \
--initial-cluster-state new
' > ./etcd.sh
sshpass -p "123456" scp -o StrictHostKeychecking=no ./etcd.sh root@${IP01}:${WORKDIR}/
sshpass -p "123456" scp -o StrictHostKeychecking=no ./etcd.sh root@${IP02}:${WORKDIR}/
sshpass -p "123456" scp -o StrictHostKeychecking=no ./etcd.sh root@${IP03}:${WORKDIR}/
sshpass -p "123456" ssh -o StrictHostKeychecking=no root@${IP01} "cd ${WORKDIR}/; sh ./etcd.sh restart >> ${WORKDIR}/log/etcd-01.log 2>&1 &"
sshpass -p "123456" ssh -o StrictHostKeychecking=no root@${IP02} "cd ${WORKDIR}/; sh ./etcd.sh restart >> ${WORKDIR}/log/etcd-02.log 2>&1 &"
sshpass -p "123456" ssh -o StrictHostKeychecking=no root@${IP03} "cd ${WORKDIR}/; sh ./etcd.sh restart >> ${WORKDIR}/log/etcd-03.log 2>&1 &"
启动成功后可以使用压缩包中的etcdctl工具查看
[root@k8s-01 /]# cd /kubernetes/tools/etcd-v3.3.10-linux-amd64/ [root@k8s-01 etcd-v3.3.10-linux-amd64]# ls -al total 34304 drwxr-xr-x. 3 6810230 users 4096 Jul 18 11:48 . drwxr-xr-x. 4 root root 4096 Jul 18 11:46 .. drwxr-xr-x. 11 6810230 users 4096 Oct 11 2018 Documentation -rwxr-xr-x. 1 6810230 users 19237536 Oct 11 2018 etcd -rwxr-xr-x. 1 6810230 users 15817472 Oct 11 2018 etcdctl -rw-r--r--. 1 6810230 users 38864 Oct 11 2018 README-etcdctl.md -rw-r--r--. 1 6810230 users 7262 Oct 11 2018 README.md -rw-r--r--. 1 6810230 users 7855 Oct 11 2018 READMEv2-etcdctl.md [root@k8s-01 etcd-v3.3.10-linux-amd64]# export ETCDCTL_API=3 [root@k8s-01 etcd-v3.3.10-linux-amd64]# ENDPOINTS=192.168.10.111:2379,192.168.10.112:2379,192.168.10.113:2379 [root@k8s-01 etcd-v3.3.10-linux-amd64]# ./etcdctl --endpoints=${ENDPOINTS} member list b03f97aeb38e9543, started, etcd-kubernetes-02, http://192.168.10.112:2380, http://192.168.10.112:2379 cf9781b88104dfa2, started, etcd-kubernetes-03, http://192.168.10.113:2380, http://192.168.10.113:2379 d628d02b4a6c4fa8, started, etcd-kubernetes-01, http://192.168.10.111:2380, http://192.168.10.111:2379 [root@k8s-01 etcd-v3.3.10-linux-amd64]# ./etcdctl --endpoints=${ENDPOINTS} endpoint health 192.168.10.111:2379 is healthy: successfully committed proposal: took = 5.280084ms 192.168.10.113:2379 is healthy: successfully committed proposal: took = 5.72184ms 192.168.10.112:2379 is healthy: successfully committed proposal: took = 5.559199ms [root@k8s-01 etcd-v3.3.10-linux-amd64]# ./etcdctl --endpoints=${ENDPOINTS} endpoint status --write-out=table +---------------------+------------------+---------+---------+-----------+-----------+------------+ | ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX | +---------------------+------------------+---------+---------+-----------+-----------+------------+ | 192.168.10.111:2379 | d628d02b4a6c4fa8 | 3.3.10 | 20 kB | true | 2 | 8 | | 192.168.10.112:2379 | b03f97aeb38e9543 | 3.3.10 | 20 kB | false | 2 | 8 | | 192.168.10.113:2379 | cf9781b88104dfa2 | 3.3.10 | 20 kB | false | 2 | 8 | +---------------------+------------------+---------+---------+-----------+-----------+------------+
如上显示表示etcd集群启动正常