人生几何?K8s

二进制安装-k8s高可用集群12-部署docker组件

2021-09-01  本文已影响0人  Chris0Yang

docker 是容器的运行环境,管理它的生命周期。kubelet 通过 Container Runtime Interface (CRI) 与 docker 进行交互。

1、下载和分发 docker 二进制文件

https://download.docker.com/linux/static/stable/x86_64/ 页面下载最新发布包:

wget https://download.docker.com/linux/static/stable/x86_64/docker-18.03.1-ce.tgz
tar -xvf docker-18.03.1-ce.tgz

分发二进制文件到所有 worker 节点:

cat > magic51_distribute_binary_file.sh << "EOF"
#!/bin/bash
# 分发二进制文件到所有 worker节点
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
    echo ">>> ${node_ip}" 
    scp docker/docker*  k8s@${node_ip}:/opt/k8s/bin/
    ssh k8s@${node_ip} "chmod +x /opt/k8s/bin/*"
done
EOF

2、创建和分发 systemd unit 文件

cat > docker.service <<"EOF"
[Unit]
Description=Docker Application Container Engine
Documentation=http://docs.docker.io
[Service]
Environment="PATH=/opt/k8s/bin:/bin:/sbin:/usr/bin:/usr/sbin"
EnvironmentFile=-/run/flannel/docker
ExecStart=/opt/k8s/bin/dockerd --log-level=error $DOCKER_NETWORK_OPTIONS
ExecReload=/bin/kill -s HUP $MAINPID
Restart=on-failure
RestartSec=5
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Delegate=yes
KillMode=process
[Install]
WantedBy=multi-user.target
EOF
sudo iptables -P FORWARD ACCEPT

并且把以下命令写入 /etc/rc.local 文件中,防止节点重启 iptables FORWARD chain 的默认策略又还原为 DROP

 /sbin/iptables -P FORWARD ACCEPT

分发 systemd unit 文件到所有 worker 机器

cat > magic52_distribute_systemd_file.sh << "EOF"
#!/bin/bash
# 分发 systemd unit 文件到所有 worker 机器
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
    echo ">>> ${node_ip}" 
    scp docker.service root@${node_ip}:/etc/systemd/system/
done
EOF

3、配置和分发 docker 配置文件

使用国内的仓库镜像服务器以加快 pull image 的速度,同时增加下载的并发数 (需要重启 dockerd 生效):

cat > docker-daemon.json <<EOF
{
    "registry-mirrors": ["https://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn"],
    "max-concurrent-downloads": 20
}
EOF

分发 docker 配置文件到所有 work 节点:

cat > magic53_distribute_docker_config_file.sh << "EOF"
#!/bin/bash
# 分发 docker 配置文件到所有 work 节点
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
    echo ">>> ${node_ip}" 
    ssh root@${node_ip} "mkdir -p  /etc/docker/"
    scp docker-daemon.json root@${node_ip}:/etc/docker/daemon.json
done
EOF

4、启动 docker 服务

cat > magic54_start_docker_server.sh << "EOF"
#!/bin/bash
# 启动 docker 服务
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
    echo ">>> ${node_ip}" 
    ssh root@${node_ip} "systemctl stop firewalld && systemctl disable firewalld"
    ssh root@${node_ip} "/usr/sbin/iptables -F && /usr/sbin/iptables -X && /usr/sbin/iptables -F -t nat && /usr/sbin/iptables -X -t nat"
    ssh root@${node_ip} "/usr/sbin/iptables -P FORWARD ACCEPT"
    ssh root@${node_ip} "systemctl daemon-reload && systemctl enable docker && systemctl start docker"
    ssh root@${node_ip} 'for intf in /sys/devices/virtual/net/docker0/brif/*; do echo 1 > $intf/hairpin_mode; done'
    ssh root@${node_ip} "sudo sysctl -p /etc/sysctl.d/kubernetes.conf"
done
EOF

5、检查服务运行状态

cat > magic55_check_server_run_status.sh << "EOF"
#!/bin/bash
# 检查服务运行状态
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
    echo ">>> ${node_ip}" 
    ssh k8s@${node_ip} "systemctl status docker|grep Active"
done
EOF

如果输出如下:

bash magic55_check_server_run_status.sh
>>> 172.68.96.101
   Active: active (running) since Wed 20XX-XX-XX XX:XX:XX CST; XXh ago
>>> 172.68.96.102
   Active: active (running) since Wed 20XX-XX-XX XX:XX:XX CST; XXh ago
>>> 172.68.96.103
   Active: active (running) since Wed 20XX-XX-XX XX:XX:XX CST; XXh ago

则正常,如果启动失败,则检查日志:

journalctl -xu docker

6、检查 docker0 网桥

cat > magic56_ckeck.sh << "EOF"
#!/bin/bash
# 检查 docker0 网桥
source /opt/k8s/bin/environment.sh
for node_ip in ${NODE_IPS[@]}
do
    echo ">>> ${node_ip}" 
    ssh k8s@${node_ip} "/usr/sbin/ip addr show flannel.1 && /usr/sbin/ip addr show docker0"
done
EOF

输出:

$bash magic.sh
>>> 172.68.96.101
3: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default
    link/ether b2:29:a7:da:fa:d8 brd ff:ff:ff:ff:ff:ff
    inet 172.30.84.0/32 scope global flannel.1
       valid_lft forever preferred_lft forever
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default
    link/ether 02:42:fc:9f:7d:c9 brd ff:ff:ff:ff:ff:ff
    inet 172.30.84.1/24 brd 172.30.84.255 scope global docker0
       valid_lft forever preferred_lft forever

>>> 172.68.96.102
3: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default
    link/ether f2:14:20:50:4f:af brd ff:ff:ff:ff:ff:ff
    inet 172.30.8.0/32 scope global flannel.1
       valid_lft forever preferred_lft forever
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default
    link/ether 02:42:a1:25:5f:c9 brd ff:ff:ff:ff:ff:ff
    inet 172.30.8.1/24 brd 172.30.8.255 scope global docker0
       valid_lft forever preferred_lft forever

>>> 172.68.96.103
3: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default
    link/ether b2:fe:60:ff:53:be brd ff:ff:ff:ff:ff:ff
    inet 172.30.29.0/32 scope global flannel.1
       valid_lft forever preferred_lft forever
4: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default
    link/ether 02:42:a1:8a:c7:9c brd ff:ff:ff:ff:ff:ff
    inet 172.30.29.1/24 brd 172.30.29.255 scope global docker0
       valid_lft forever preferred_lft forever

确认各 work 节点的 docker0 网桥和 flannel.1 接口的 IP 处于同一个网段中,如上 kube-node1 节点的 172.30.84.0 和 172.30.84.1

上一篇下一篇

猜你喜欢

热点阅读