写的很乱、建议前往文档

搭建环境

  • VMware虚拟机平台
  • Linux内核3.10
  • CentOS~7.9发行版
  • 最小安装版
  • 主节点一个,工作节点两个及以上(至少三台虚拟机)
  • k8s,因为k到s中间有8个字母

搭建需要

主节点需要组件

  • docker
  • kubectl - 集群命令行交互工具
  • kubeadm - 集群初始化工具

工作节点需要组件

  • docker
  • kubelet - 管理 Pod 和容器,确保他们健康稳定运行
  • kube-proxy - 网络代理,负责网络相关的工作

所有节点配置

修改hosts

1
vi /etc/hosts
1
2
3
4
主节点ip master
工作节点1ip work1
工作节点2ip work2
工作节点3ip work3

关闭selinux

1
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

添加k8s安装源

1
2
3
4
5
6
7
8
9
10
cat <<EOF > kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
mv kubernetes.repo /etc/yum.repos.d/

添加Docker安装源

1
sudo yum install -y yum-utils
1
sudo yum-config-manager     --add-repo     http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

安装所需组件

1
yum install -y kubelet-1.22.4 kubectl-1.22.4 kubeadm-1.22.4 docker-ce

启动 kubelet、docker,并设置开机启动

1
2
3
4
systemctl enable kubelet
systemctl start kubelet
systemctl enable docker
systemctl start docker

修改 docker 配置

1
2
3
4
5
6
7
8
9
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["你自己的加速地址"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

主节点配置

修改主机名

1
hostnamectl set-hostname master

用kubeadm初始化集群

1
kubeadm init --image-repository=registry.aliyuncs.com/google_containers
  • 记得把 kubeadm join xxx 保存起来
  • 忘记了重新获取:kubeadm token create –print-join-command

安装网络插件

1
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

等待STATUS全变为Ready

1
2
3
4
5
6
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane,master 2d23h v1.22.4
work1 Ready <none> 2d23h v1.22.4
work2 Ready <none> 2d23h v1.22.4
work3 Ready <none> 2d23h v1.22.4

ping work1 确保内网连通性

1
2
3
4
5
6
[root@master ~]# ping work1
PING work1 (192.168.144.167) 56(84) bytes of data.
64 bytes from work1 (192.168.144.167): icmp_seq=1 ttl=64 time=0.568 ms
64 bytes from work1 (192.168.144.167): icmp_seq=2 ttl=64 time=0.341 ms
64 bytes from work1 (192.168.144.167): icmp_seq=3 ttl=64 time=0.338 ms
64 bytes from work1 (192.168.144.167): icmp_seq=4 ttl=64 time=0.273 ms

工作节点配置

修改主机名,有几个工作节点就配置几个

1
hostnamectl set-hostname work1
1
hostnamectl set-hostname work2
1
hostnamectl set-hostname work3

把工作节点加入集群

1
kubeadm join ip:6443 --token xxx --discovery-token-ca-cert-hash xxx

ping master 确保内网连通性

1
2
3
4
5
6
[root@work1 ~]# ping master
PING master (192.168.144.166) 56(84) bytes of data.
64 bytes from master (192.168.144.166): icmp_seq=1 ttl=64 time=0.223 ms
64 bytes from master (192.168.144.166): icmp_seq=2 ttl=64 time=0.336 ms
64 bytes from master (192.168.144.166): icmp_seq=3 ttl=64 time=0.258 ms
64 bytes from master (192.168.144.166): icmp_seq=4 ttl=64 time=0.234 ms