- List item 

一、master节点上部署etcd:
1、编写脚本:
vim /root/scripts/etcd_master_install.sh
#!/bin/bash
master_ip=`cat /etc/hosts |grep master|awk '{print $1}'`
node01_ip=`cat /etc/hosts |grep node01|awk '{print $1}'`
node02_ip=`cat /etc/hosts |grep node02|awk '{print $1}'`
node03_ip=`cat /etc/hosts |grep node03|awk '{print $1}'`

#etcd二进制包下载
wget https://raw.githubusercontent.com/guancongcong/kubernetes/master/pkg/etcd/etcd-v3.2.12-linux-amd64.tar.gz
tar xf etcd-v3.2.12-linux-amd64.tar.gz
cd etcd-v3.2.12-linux-amd64
mv etcd* /opt/kubernetes/bin/

#创建etcd集群的配置文件:
cat >/opt/kubernetes/cfg/etcd <<EOF
#{Member}
ETCD_NAME="etcd01"
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_PEER_URLS="https://${master_ip}:2380"
ETCD_LISTEN_CLIENT_URLS="https://${master_ip}:2379"

#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://${master_ip}:2380"
ETCD_ADVERTISE_CLIENT_URLS="https://${master_ip}:2379"
ETCD_INITIAL_CLUSTER="etcd01=https://${master_ip}:2380,etcd02=https://${node01_ip}:2380,etcd03=https://${node02_ip}:2380,etcd04=https://${node03_ip}:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
EOF

#创建系统文件,通过systemctl管理etcd:
cat >/usr/lib/systemd/system/etcd.service <<EOF
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
EnvironmentFile=-/opt/kubernetes/cfg/etcd
ExecStart=/opt/kubernetes/bin/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} \\
--initial-advertise-peer-urls=\${ETCD_INITIAL_ADVERTISE_PEER_URLS} \\
--initial-cluster=\${ETCD_INITIAL_CLUSTER} \\
--initial-cluster-token=\${ETCD_INITIAL_CLUSTER} \\
--initial-cluster-state=new \\
--cert-file=/opt/kubernetes/ssl/server.pem \\
--key-file=/opt/kubernetes/ssl/server-key.pem \\
--peer-cert-file=/opt/kubernetes/ssl/server.pem \\
--peer-key-file=/opt/kubernetes/ssl/server-key.pem \\
--trusted-ca-file=/opt/kubernetes/ssl/ca.pem \\
--peer-trusted-ca-file=/opt/kubernetes/ssl/ca.pem
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

#启动etcd服务:
systemctl daemon-reload
systemctl restart etcd
systemctl enable etcd

2、执行脚本:
sh /root/scripts/etcd_master_install.sh

二、node节点上部署etcd:
1、主机间互信:
1>在master节点编写主机间互信脚本:
vim /root/scripts/believe.sh
#!/bin/bash
[ -x /usr/bin/expect ] || yum install expect -y  &>/dev/null
if [ ! -f /root/.ssh/id_rsa.pub ]
then
/usr/bin/expect <<EOF
set timeout 180
spawn ssh-keygen 
expect {
    "Enter file *"  {send "\n";exp_continue}
    "Enter passphrase * " {send "\n";exp_continue} 
    "Enter same passphrase again: " {send "\n"}
}   
expect eof
EOF
fi
for ip in 75 76 77
do
/usr/bin/expect <<EOF
spawn ssh-copy-id  192.168.1.${ip}
expect {
    "yes/no"  {send "yes\n";exp_continue}
    "password"  {send "1\n"}
    }
expect eof
EOF
done

#从master节点将相关文件传到node01、node02、node03节点上:
scp -r /opt/kubernetes/{bin,cfg,ssl}  192.168.1.75:/opt/kubernetes/
scp -r /opt/kubernetes/{bin,cfg,ssl}  192.168.1.76:/opt/kubernetes/
scp -r /opt/kubernetes/{bin,cfg,ssl}  192.168.1.77:/opt/kubernetes/
scp /usr/lib/systemd/system/etcd.service 192.168.1.75:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/etcd.service 192.168.1.76:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/etcd.service 192.168.1.77:/usr/lib/systemd/system/

#让命令全局可用
ln -s /opt/kubernetes/bin/* /usr/local/sbin/

2>执行脚本:
sh /root/scripts/believe.sh

2、在三台node节点上修改配置文件并启动etcd服务:
vim /root/scripts/change.sh
#!/bin/bash
hostname=`hostname`
master_ip=`cat /etc/hosts|grep master|awk '{print $1}'`
local_ip=`cat /etc/hosts|grep ${hostname}|awk '{print $1}'`
number=$((`hostname|awk -F"0" '{print $2}'`+1))
etcd_name=etcd0${number}
#修改etcd名字
eval sed -i '1,2s/etcd01/${etcd_name}/' /opt/kubernetes/cfg/etcd
#修改etcd的ip
eval sed -i '1,9s/${master_ip}/${local_ip}/' /opt/kubernetes/cfg/etcd
#让命令全局可用
ln -s /opt/kubernetes/bin/* /usr/local/sbin/
#启动etcd服务
systemctl daemon-reload
systemctl restart etcd
systemctl enable etcd

3、在任意一个节点上查看etcd集群健康状态:
cd /opt/kubernetes/ssl/
/opt/kubernetes/bin/etcdctl --ca-file=ca.pem --cert-file=server.pem --key-file=server-key.pem --endpoints="https://192.168.1.250:2379,https://192.168.1.75:2379,https://192.168.1.76:2379,https://192.168.1.77:2379" cluster-health

4、验证集群可用性:
集群做好的四台服务器的etcd数据应该是互通的,即在mater上创建一个文件或者目录,在任意node上都是可以查询到的,同理,在node上设置的key值,在master上也是可以get到的

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐