Kubernetes 1.25 安装

2,683 阅读3分钟

Kubernetes也被简称为K8S.它一个开源的容器编排平台.它处于CNCF生态体系中的核心,也是目前最流行的经受了大规模生产环境考验的编排系统,其市场份额对比其他产品有绝对的压倒性优势.K8S自从Google捐赠给CNCF社区后得到了大规模的使用,生态非常活跃.本文简单介绍下怎么安装K8S,方便大家进到入后续的有关K8S的学习中去.

在安装前我们需要简单的核对下本文的环境,虽然其他的版本也是一样的安装流程,但是为了保证安装的成功率,建议以我的为准,当你学会了后就可以自己选择版本了.

环境

软件版本

NameVersionDescription
RHEL/CentOS Stream9.xAMD64指令集
kubeadm1.25.x
kubectl1.25.x
kubelet1.25.x
containerd1.6.8你的集群准备好使用 v1.24 版本了吗?

自从1.24版本开始,kubernetes不再直接支持Docker,建议后续直接使用containerd;如果你想继续使用Docker也行,因为Docker也是基于containerd,只是在搭建的时候要安装一些插件,略微麻烦一点

服务器列表

HostIpDescription
k8s01192.168.50.236control panel
k8s02192.168.50.113worker node
k8s03192.168.50.30worker node
k8s04192.168.50.248worker node
k8s05192.168.50.70worker node
k8s06192.168.50.149worker node
k8s07192.168.50.141worker node

Kubernetes 安装

修改hostname和hosts

我们需要统一这些服务器的hostname和hosts,为每个服务器取不同的hostname;在这次搭建过程中我将这些服务器分别按序号命名

截图: image

hostname修改完成后,我们在hosts中配置好ip映射表

截图: image

hostname和hosts是每个服务器都需要设置的,不漏掉.

关闭swap

禁用交换分区。为了保证 kubelet 正常工作,你 必须 禁用交换分区。详情见安装 kubeadm

[root@k8s01 thinktik]# swapoff -a
[root@k8s01 thinktik]# sed -i 's/.*swap.*/#&/' /etc/fstab

截图: image

关闭 SELinux

为了避免一些权限上的麻烦,我们直接关闭SELinux;关闭SELinux实际上是不可取的,它相对不安全,不建议在生产环境 这样做.

[root@k8s01 thinktik]# setenforce 0
[root@k8s01 thinktik]# sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

截图: image

关闭firewalld

kubernetes的运行需要linux开放一些端口,详情见检查所需端口,你可以精确的只打开所需的端口,这样更安全; 我为了方便直接关闭了防火墙,实际上是不安全的,不建议在生产环境这样做.

[root@k8s01 thinktik]# systemctl disable firewalld
Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
[root@k8s01 thinktik]# systemctl stop firewalld
[root@k8s01 thinktik]# 

截图: image

配置Kubernetes

详情见Install and configure prerequisites,我们需要提前配置好kubernetes的一些通用配置项

[root@k8s02 thinktik]# cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

截图: image

[root@k8s01 thinktik]# modprobe overlay
[root@k8s01 thinktik]# modprobe br_netfilter
[root@k8s02 thinktik]# cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF

截图: image

让我们的配置生效

[root@k8s01 thinktik]# sysctl --system

截图: image

containerd 安装

Kubernates中pod的运行必须有容器运行环境,以前kubernetes是使用Docker,后面主要使用containerd等,目前支持的运行时可以参考container-runtimes

本文使用containerd,前面我们也讲过Docker也是基于containerd的,那么我们直接使用Docker的安装源,只安装containerd就行.

移除可能安装过的Docker包

[root@k8s01 thinktik]# sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

截图: image

安装yum-utils

[root@k8s01 thinktik]# sudo yum install -y yum-utils

截图: image

添加docker软件源

[root@k8s01 thinktik]# sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

截图: image

安装containerd

[root@k8s01 thinktik]# sudo dnf install  containerd.io -y

截图: image

设置containerd开机自启动

[root@k8s01 thinktik]# systemctl enable containerd
Created symlink /etc/systemd/system/multi-user.target.wants/containerd.service → /usr/lib/systemd/system/containerd.service.
[root@k8s01 thinktik]# systemctl start containerd
[root@k8s01 thinktik]# 

截图: image

修改containerd使用systemd


[root@k8s01 thinktik]# containerd  config default > /etc/containerd/config.toml
[root@k8s01 thinktik]# sed -i 's#k8s.gcr.io#registry.aliyuncs.com/google_containers#g' /etc/containerd/config.toml
[root@k8s01 thinktik]# sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml 
[root@k8s01 thinktik]# 

截图: image

重启containerd

[root@k8s01 thinktik]# systemctl daemon-reload
[root@k8s01 thinktik]# systemctl enable --now containerd
[root@k8s01 thinktik]# systemctl restart  containerd
[root@k8s01 thinktik]# 

截图: image

安装Kubernetes组件

配置软件源,这里使用aliyun的源,官方的源是google的,没法在国内访问

[root@k8s01 thinktik]# 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
[root@k8s01 thinktik]# 

安装kubelet,kubeadm,kubectl

[root@k8s01 thinktik]# yum install kubelet kubeadm kubectl --nogpgcheck -y

设置kubelet自启动

[root@k8s01 thinktik]# systemctl enable --now kubelet
Created symlink /etc/systemd/system/multi-user.target.wants/kubelet.service → /usr/lib/systemd/system/kubelet.service.
[root@k8s01 thinktik]# 

安装crictl

[root@k8s01 thinktik]# VERSION="v1.25.0"
curl -L https://github.com/kubernetes-sigs/cri-tools/releases/download/$VERSION/crictl-${VERSION}-linux-amd64.tar.gz --output crictl-${VERSION}-linux-amd64.tar.gz
tar zxvf crictl-$VERSION-linux-amd64.tar.gz -C /usr/local/bin
rm -f crictl-$VERSION-linux-amd64.tar.gz

截图: image

设置crictl

[root@k8s01 thinktik]# crictl config --set runtime-endpoint=unix:///run/containerd/containerd.sock 

截图: image

在担任control-panel的k8s01节点上执行,使用kubeadm初始化集群,这里apiserver-advertise-address的值是control-panel的主机的ip,这里就是k8s01的ip

[root@k8s01 thinktik]# kubeadm init  --apiserver-advertise-address=192.168.50.236 --image-repository registry.aliyuncs.com/google_containers

截图: image

全部的worker节点上执行我高亮的部分,加入集群

截图: image

节点加入集群后,起初是NotReady的,这是正常的,我们配置好网络就行,在担任control-panel的k8s01节点上执行执行下面的命令,等待一段时间全部节点就会处于Ready状态

[root@k8s01 thinktik]# kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

截图: image

等待中,没有节点是Ready的,不要着急等一段时间就全部Ready

截图: image

我们可以执行如下的命令,来不断的检查状态

[root@k8s01 thinktik]# kubectl get pod --all-namespaces
[root@k8s01 thinktik]# kubectl get nodes -o wide

截图: image

就绪后的样子是这样的 image