kubeadm安装k8s-v1.27.1和calico-v3.26.0
环境准备(每台主机都得设置)
-
系统配置
cat << EOF > /etc/hosts 192.168.1.10 master 192.168.1.11 node 192.168.1.12 node EOF
-
关闭防火墙
systemctl stop firewalld systemctl disable firewalld
-
关闭swap
sed -ri 's/.*swap.*/#&/' /etc/fstab swapoff -a
-
禁用selinx
# 临时生效 setenforce 0 #重启永久生效 vi /etc/selinux/config SELINUX=disabled
-
添加内核模块
# 为了开机识别自动添加 cat << EOF > /etc/modules-load.d/k8s.conf overlay br_netfilter EOF # 手动添加 modprobe overlay modprobe br_netfilter
-
开启ipv4转发
cat << EOF > /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 user.max_user_namespaces=28633 EOF # 执行生效 sysctl -p /etc/sysctl.d/k8s.conf
-
配置服务器支撑ipvs
# 安装 ipset 和 ipvsadm yum install ipset ipvsadm -y # 需要加载的内核脚本 cat > /etc/sysconfig/modules/ipvs.modules <<EOF #!/bin/bash modprobe -- ip_vs modprobe -- ip_vs_rr modprobe -- ip_vs_wrr modprobe -- ip_vs_sh modprobe -- nf_conntrack_ipv4 EOF chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4
安装容器运行时(containerd)
-
安装服务
# 安装必须的系统工具 yum install -y yum-utils device-mapper-persistent-data lvm2 # 添加repo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 安装 containerd yum install containerd -y
-
生成配置
# 生成配置 mkdir -p /etc/containerd containerd config default > /etc/containerd/config.toml # 修改下面2个配置 SystemdCgroup = true sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"
-
启动服务
systemctl enable containerd --now systemctl restart containerd
安装kubeadm和kubelet(每个节点都要)
-
添加源
# 添加repo cat <<EOF > /etc/yum.repos.d/kubernetes.repo [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 yum makecache fast
-
安装服务
# master 还需要安装kubectl yum install -y kubectl-1.27.1 kubelet-1.27.1 kubeadm-1.27.1 # node节点服务器只需要安装 kubelet 和 kubeadm yum install -y kubelet-1.27.1 kubeadm-1.27.1
-
开机启动kubelet
systemctl enable kubelet.service systemctl start kubelet.service
时间同步(chrony)
-
安装chrony
yum install chrony -y
-
加入开机启动
systemctl start chronyd systemctl enable chronyd systemctl status chronyd
-
设置时区
timedatectl set-timezone "Asia/Shanghai"
-
设置时间服务器
# 注释掉默认的时间服务器 #server 0.centos.pool.ntp.org iburst #server 1.centos.pool.ntp.org iburst #server 2.centos.pool.ntp.org iburst #server 3.centos.pool.ntp.org iburst # 设置为阿里云的 server ntp1.aliyun.com iburst server ntp2.aliyun.com iburst server ntp3.aliyun.com iburst
-
做内网段的时间服务器
# 允许那个网段的请求时间 allow 192.168.1.0/24
-
重启并手动时间
# 重启服务 systemctl restart chronyd # 手动同步系统时钟 chronyc -a makestep # 查看时间同步源 chronyc sources -v
创建集群
-
kubeadm初始化
kubeadm init \ --apiserver-advertise-address=192.168.1.10 \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.27.1 \ --service-cidr=10.96.0.0/12 \ --pod-network-cidr=10.244.0.0/16 \ --cri-socket=unix:///var/run/containerd/containerd.sock \ --ignore-preflight-errors=all
-
配置kubectl访问集群
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
-
节点加入集群(节点服务器上操作)
kubeadm join 192.168.1.10:6443 --token 3kaxq7.sxd28ivdfdvmxcfd \ --discovery-token-ca-cert-hash sha256:d54b81923658143eb85530c317c28a4e375389e04f2db4cf6fcb1b927ce3997c
部署calico-v3.26.0
-
解压后只要如下物品:
- images/calico-cni.tar
- images/calico-kube-controllers.tar
- images/calico-node.tar
- manifests/calico.yaml
-
containerd 导入镜像
# 导入镜像到 k8s.io 命名空间 ctr -n k8s.io images import calico-cni.tar ctr -n k8s.io images import calico-kube-controllers.tar ctr -n k8s.io images import calico-node.tar
-
执行yaml部署pod
kubectl apply -f calico.yaml
-
查看集群状态
kubectl get cs Warning: v1 ComponentStatus is deprecated in v1.19+ NAME STATUS MESSAGE ERROR scheduler Healthy ok controller-manager Healthy ok etcd-0 Healthy {"health":"true","reason":""}
至此kubeadm快速部署集群到此结束
故障排查参考: https://kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/troubleshooting-kubeadm/