etcd集羣的搭建(3節點無加密證書,基於CentOS7)

前提條件:html

  一、須要有3個節點linux

  二、etcd二進制文件在3各節點的/kubernetes/etcdworkspace/目錄(沒有可自行建立)bash

  三、3個節點時間偏差不要太大,不然影響集羣功能,多節點ntp配置https://www.cnblogs.com/ouyanghuanlin/articles/11207797.htmlssh

下載連接:工具

  一、etcd:https://pan.baidu.com/s/1CufZFyCQsNoP5T2vmAnYZg     提取碼:968surl

  二、sshpass:https://pan.baidu.com/s/1AlXOael_zBltASwEysjagg     提取碼:7h2xspa

下面是啓動etcd集羣的腳本:命令行

  一、ip請替換爲實際3個節點ip(15-1七、32-34行)、sshpass命令行中的密碼(84-90行)替換爲實際密碼debug

  二、sshpass工具系統沒有自帶,將準備好的sshpass二進制文件放置在腳本執行節點的/usr/bin/目錄下,權限和用戶組與該目錄下的其餘文件一致便可rest

 

#!/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集羣啓動正常

相關文章
相關標籤/搜索