Vagrant 做 Docker V8s 的测试环境 (2) Ansible 搭建 Kubeadm 基础环境

开始前使用快照备份

1
2
3
4
5
6
7
vagrant snapshot save s1 base
vagrant snapshot save s2 base
vagrant snapshot save s3 base
vagrant snapshot save s4 base
vagrant snapshot save s5 base
vagrant snapshot save host base
vagrant snapshot save master base

Master 主机安装 Ansible

主机使用 CentOS 系统

1
2
3
4
sudo yum -y update
sudo yum -y install gcc libffi-devel openssl-devel python-pip python-devel

sudo yum -y install ansible

配置文件 /etc/ansible/ansible.cfg, Host 服务器组 /etc/ansible/hosts

参考:

脚本安装 Docker

1
2
3
4
5
6
7
# 添加源
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 安装
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum install -y docker-ce

sudo usermod -aG docker $(whoami)

开机启动 Docker

1
2
3
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker

镜像源加速

1
2
3
4
5
6
7
8
9
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
EOF
sudo cat /etc/docker/daemon.json
sudo systemctl daemon-reload
sudo systemctl restart docker

脚本安装 Kubeadm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 添加源
sudo tee /etc/yum.repos.d/kubernetes.repo <<-'EOF'
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
sudo cat /etc/yum.repos.d/kubernetes.repo
# 配置关闭 SELinux
setenforce 0
# 安装
yum install -y kubelet kubeadm kubectl ipvsadm

开机启动

1
2
3
sudo systemctl start kubelet
sudo systemctl enable kubelet
sudo systemctl status kubelet

配置基础环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 禁用 SELinux
sudo sed -i -e "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
sudo setenforce 0

# 关闭 swap
sudo sed -ri 's/(^\/.*swap.*)/#\1/g' /etc/fstab
sudo swapoff -a

# 禁用防火墙
sudo systemctl stop firewalld
sudo systemctl disable firewalld

# 配置 CentOS 7
sudo tee /etc/sysctl.d/k8s.conf <<-'EOF'
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system

# 价值 IPVS 模块
sudo modprob ip_vs
sudo modprob ip_vs_rr
sudo modprob ip_vs_wrr
sudo modprob ip_vs_sh
sudo modprob nf_conntrack_ipv4

sudo lsmod | grep ip_vs

sudo tee /etc/modules-load.d/k8s-ipvs.conf <<-'EOF'
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack_ipv4
EOF
sudo cat /etc/modules-load.d/k8s-ipvs.conf

# 配置 Kubeadm
DOCKER_CGROUPS=$(sudo docker info | grep 'Cgroup' | cut -d' ' -f 3)
echo ${DOCKER_CGROUPS}

sudo tee /etc/sysconfig/kubelet <<-'EOF'
KUBELET_EXTRA_ARGS="--cgroup-driver=${DOCKER_CGROUPS}"
EOF
sudo cat /etc/sysconfig/kubelet
sudo systemctl daemon-reload

sudo systemctl restart kubelet

使用 Ansible 批量操作

配置好 Ansible 服务集群

在使用 Ansible 之前, 需要将目标机器加入当前 Ansible 机器的 know_hosts 中, 方法很简单 ssh IP 然后填入 yes 回车确认.

配置 Ansible Host 列表 /etc/ansible/hosts

1
2
3
4
5
6
7
8
9
[k8smaster]
10.0.5.200

[k8snode]
10.0.5.101
10.0.5.102
10.0.5.103
10.0.5.104
10.0.5.105

配置好后, 测试连通性 ansible k8smaster -m ping, ansible k8snode -m ping

开始基于集群的批量部署

在 master 和 node 上部署 docker

1
2
ansible k8smaster -m shell -a "/data/install_docker.sh"
ansible k8snode -m shell -a "/data/install_docker.sh"

在 master 和 node 上部署 kubeadm

1
2
ansible k8smaster -m shell -a "/data/install_kubeadm.sh"
ansible k8snode -m shell -a "/data/install_kubeadm.sh"
Donate - Support to make this site better.
捐助 - 支持我让我做得更好.