Kubernetes v1.24 基于containerd部署

环境信息

主机名 IP地址
master1 192.168.0.1
node1 192.168.0.2
node2 192.168.0.3

服务器密码:123456

主机名解析

guage-bash
  • 01
  • 02
  • 03
  • 04
# 主机名成解析 编辑三台服务器的/etc/hosts文件,添加下面内容 192.168.0.1 master1 192.168.0.2 node1 192.168.0.3 node2

k8s每个节点安装docker。

docker安装参考:docker安装

时间同步

kubernetes要求集群中的节点时间必须精确一直,这里使用chronyd服务从网络同步时间

guage-powershell
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
yum install chrony -y # 启动chronyd服务 systemctl start chronyd systemctl enable chronyd chronyc sources date

禁用iptable和firewalld服务

kubernetes和docker 在运行的中会产生大量的iptables规则,为了不让系统规则跟它们混淆,直接关闭系统的规则

guage-powershell
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
# 1 关闭firewalld服务 systemctl stop firewalld systemctl disable firewalld # 2 关闭iptables服务 systemctl stop iptables systemctl disable iptables

禁用selinux

selinux是linux系统下的一个安全服务,如果不关闭它,在安装集群中会产生各种各样的奇葩问题

guage-powershell
  • 01
  • 02
  • 03
# 编辑 /etc/selinux/config 文件,修改SELINUX的值为disable # 注意修改完毕之后需要重启linux服务 SELINUX=disabled

禁用swap分区

swap分区指的是虚拟内存分区,它的作用是物理内存使用完,之后将磁盘空间虚拟成内存来使用,启用swap设备会对系统的性能产生非常负面的影响,因此kubernetes要求每个节点都要禁用swap设备,但是如果因为某些原因确实不能关闭swap分区,就需要在集群安装过程中通过明确的参数进行配置说明

guage-powershell
  • 01
  • 02
  • 03
  • 04
  • 05
# 编辑分区配置文件/etc/fstab,注释掉swap分区一行 # 注意修改完毕之后需要重启linux服务 vim /etc/fstab 注释掉 /dev/mapper/centos-swap swap # /dev/mapper/centos-swap swap

修改hostname

guage-shell
  • 01
  • 02
  • 03
  • 04
# 到对应机器上执行 hostnamectl set-hostname master1 hostnamectl set-hostname node1 hostnamectl set-hostname node2

安装kubeadm环境

配置软件仓库

guage-bash
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
cat > /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=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 # 清除缓存 yum clean all # 更新yum源 yum update # 列出kubectl可用的版本 yum list kubectl --showduplicates | sort -r # 安装最新版本,也可安装指定版本 yum install -y kubelet kubeadm kubectl # 安装指定版本的kubelet,kubeadm,kubectl yum install -y kubectl-1.23.6 kubeadm-1.23.6 kubelet-1.23.6

初始化k8s集群

每台k8s节点设置加载模块,内核参数调优(可选)

guage-bash
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
cat > /etc/modules-load.d/modules.conf << EOF ip_vs br_netfilter EOF modprobe ip_vs modprobe br_netfilter

内核优化(可选)

guage-bash
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
rm -rf /etc/sysctl.conf cat > /etc/sysctl.conf << EOF net.ipv4.ip_forward=1 vm.max_map_count=262144 kernel.pid_max=4194303 fs.file-max=1000000 net.ipv4.tcp_max_tw_buckets=6000 net.netfilter.nf_conntrack_max=2097152 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 vm.swappiness=0 EOF sysctl -p

master执行初始化集群,只需要在master上执行

guage-bash
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
kubeadm init \ --apiserver-advertise-address=172.23.34.167 \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.23.6 \ --service-cidr=10.96.0.0/12 \ --pod-network-cidr=10.244.0.0/16 # 选项解释: # --apiserver-advertise-address api-server地址也就是master节点地址 # --apiserver-bind-port api-server服务端口号 # --kubernetes-version k8s版本号 # --pod-network-cidr pod网络的地址建议16位或者8位地址 # --service-cidr svc网络地址建议16位或者8位地址与pod网络区别开来 # --service-dns-domain=cluster.local 集群dns域名地址,默认为cluster.local # --image-repository k8s镜像下载地址 # --ignore-preflight-errors=swap 忽略初始化错位,如果开启了swap,可以忽略这个错误

执行完毕后会打印

guage-bash
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
  • 07
  • 08
  • 09
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
Your Kubernetes control-plane has initialized successfully! To start using your cluster, you need to run the following as a regular user: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config Alternatively, if you are the root user, you can run: export KUBECONFIG=/etc/kubernetes/admin.conf You should now deploy a pod network to the cluster. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/ Then you can join any number of worker nodes by running the following on each as root: kubeadm join 192.168.111.128:6443 --token 7hoayb.zd25inepz0ez8bun \ --discovery-token-ca-cert-hash sha256:eeb975f49ee2e6b09d0364b09d4067eefab76abafd7cde658ca022d0f936cf55

kubectl凭据配置

kubectl默认使用~/.kube/config文件中凭据信息管理kubernetes。

guage-bash
  • 01
  • 02
  • 03
  • 04
  • 05
  • 06
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config kubectl get nodes # 这时候master还是NoReady状态,等网络配置完成后,STATUS状态由NotReady变更为Ready

节点加入集群

如果master节点初始化集群时终端输出的加入集群命令丢失,可以使用以下命令重新获取:

guage-bash
  • 01
kubeadm token create --print-join-command

依次在每个node节点执行

配置网络

下载flannel网络插件,如下载不要下来需要手动下载上传上去

guage-bash
  • 01
  • 02
  • 03
  • 04
# https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml kubectl apply -f https://file.codei.top/upload/2024-03/kube-flannel.yml

最后

guage-bash
  • 01
  • 02
  • 03
  • 04
  • 05
# 查看集群状态 kubectl get nodes # 查看Pod状态 kubectl get pods -A