K8S安装具体步骤
操作系统:centos 7.9
配置:内存4vCPU、40G硬盘
| 节点IP | 主机名 | 核心组件/职责 |
|---|---|---|
| 192.168.20.126 | k8s-master |
kube-apiserver, kube-scheduler, kube-controller-manager, etcd |
| 192.168.20.127 | k8s-node01 |
kubelet, kube-proxy, 容器运行时 (Docker/Containerd),运行业务Pod |
| 192.168.20.128 | k8s-node02 |
kubelet, kube-proxy, 容器运行时 (Docker/Containerd),运行业务Pod |
配置主机名和hosts文件
1# 设置主机名
2hostnamectl set-hostname k8s-master
3
4# 配置hosts文件
5cat >> /etc/hosts <<EOF
6192.168.20.126 k8s-master
7192.168.20.127 k8s-node01
8192.168.20.128 k8s-node02
9EOF
10
11# 新增
12echo "192.168.20.xx k8s-node03" >> /etc/hosts
关闭交换分区
注意注意:我们的服务器如果没有swaf分区,则这一步直接跳过。
1# 临时关闭
2swapoff -a
3
4# 永久关闭,注释swap挂载
5sed -i 's/.*swap.*/#&/' /etc/fstab
配置内核参数
1# 加载网络过滤模块
2modprobe br_netfilter
3echo "modprobe br_netfilter" >> /etc/profile
4
5# 配置内核参数
6cat > /etc/sysctl.d/k8s.conf <<EOF
7net.bridge.bridge-nf-call-ip6tables = 1
8net.bridge.bridge-nf-call-iptables = 1
9net.ipv4.ip_forward = 1
10EOF
11
12# 生效配置
13sysctl -p /etc/sysctl.d/k8s.conf
关闭SELinux
注意注意:我们的服务器如果没有swaf分区,则这一步直接跳过。
1# 临时关闭
2setenforce 0
3
4# 永久关闭
5sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
配置镜像源
1rm -rf /etc/yum.repos.d/*
2curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.huaweicloud.com/repository/conf/CentOS-7-anon.repo
3cat > /etc/yum.repos.d/epel.repo <<EOF
4[epel]
5name=Extra Packages for Enterprise Linux 7 - \$basearch
6baseurl=https://mirrors.huaweicloud.com/epel/7/\$basearch
7failovermethod=priority
8enabled=1
9gpgcheck=0
10gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
11
12[epel-debuginfo]
13name=Extra Packages for Enterprise Linux 7 - \$basearch - Debug
14baseurl=https://mirrors.huaweicloud.com/epel/7/\$basearch/debug
15failovermethod=priority
16enabled=0
17gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
18gpgcheck=0
19
20[epel-source]
21name=Extra Packages for Enterprise Linux 7 - \$basearch - Source
22baseurl=https://mirrors.huaweicloud.com/epel/7/SRPMS
23failovermethod=priority
24enabled=0
25gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
26gpgcheck=0
27EOF
28
29# Docker 源
30wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.huaweicloud.com/docker-ce/linux/centos/docker-ce.repo
31sudo sed -i 's+download.docker.com+mirrors.huaweicloud.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
32
33# k8s 源
34cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
35[kubernetes]
36name=Kubernetes
37baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.32/rpm/
38enabled=1
39gpgcheck=1
40gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.32/rpm/repodata/repomd.xml.key
41EOF
启用IPVS
1cat > /etc/sysconfig/modules/ipvs.modules <<EOF
2#!/bin/bash
3ipvs_modules="ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_nq ip_vs_sed ip_vs_ftp nf_conntrack"
4for kernel_module in \${ipvs_modules}; do
5 /sbin/modinfo -F filename \${kernel_module} > /dev/null 2>&1
6 if [ 0 -eq 0 ]; then
7 /sbin/modprobe \${kernel_module}
8 fi
9done
10EOF
11
12# 加载IPVS模块
13chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules
14
15# 验证模块加载
16lsmod | grep ip_vs
安装基础软件包
1yum install -y yum-utils device-mapper-persistent-data lvm2 wget net-tools nfs-utils gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-devel python-devel epel-release openssh-server socat ipvsadm conntrack ntpdate telnet ipvsadm
K8S集群初始化步骤
安装containerd.io与kubernetes
1yum install kubelet-1.32.11 kubeadm-1.32.11 kubectl-1.32.11 containerd container-selinux
2
3systemctl enable --now containerd
4systemctl enable --now kubelet
配置Containerd所需的模块
1cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
2overlay
3br_netfilter
4EOF
5
6# 加载 br_netfilter 系统模块
7modprobe br_netfilter
8
9# 参数解释:
10#
11# containerd是一个容器运行时,用于管理和运行容器。它支持多种不同的参数配置来自定义容器运行时的行为和功能。
12#
13# 1. overlay:overlay是容器的默认使用的存储驱动,它提供了一种轻量级的、可堆叠的、逐层增量的文件系统。它通过在现有文件系统上叠加文件系统层来创建容器的文件系统视图。每个容器可以有自己的一组文件系统层,这些层可以共享基础镜像中的文件,并在容器内部进行修改。使用overlay可以有效地使用磁盘空间,并使容器更加轻量级。
14#
15# 2. br_netfilter:br_netfilter是Linux内核提供的一个网络过滤器模块,用于在容器网络中进行网络过滤和NAT转发。当容器和主机之间的网络通信需要进行DNAT或者SNAT时,br_netfilter模块可以将IP地址进行转换。它还可以提供基于iptables规则的网络过滤功能,用于限制容器之间或容器与外部网络之间的通信。
16#
17# 这些参数可以在containerd的配置文件或者命令行中指定。例如,可以通过设置--storage-driver参数来选择使用overlay作为存储驱动,通过设置--iptables参数来启用或禁用br_netfilter模块。具体的使用方法和配置细节可以参考containerd的官方文档。
修改Containerd默认配置
1containerd config default | tee /etc/containerd/config.toml
2
3sed -i "s#registry.k8s.io/pause:3.6#registry.aliyuncs.com/google_containers/pause:3.10#g" /etc/containerd/config.toml
4sed -i "s#SystemdCgroup = false#SystemdCgroup = true#g" /etc/containerd/config.toml
5
6# SystemdCgroup参数是containerd中的一个配置参数,用于设置containerd在运行过程中使用的Cgroup(控制组)路径。Containerd使用SystemdCgroup参数来指定应该使用哪个Cgroup来跟踪和管理容器的资源使用。
7#
8# Cgroup是Linux内核提供的一种资源隔离和管理机制,可以用于限制、分配和监控进程组的资源使用。使用Cgroup,可以将容器的资源限制和隔离,以防止容器之间的资源争用和不公平的竞争。
9#
10# 通过设置SystemdCgroup参数,可以确保containerd能够找到正确的Cgroup路径,并正确地限制和隔离容器的资源使用,确保容器可以按照预期的方式运行。如果未正确设置SystemdCgroup参数,可能会导致容器无法正确地使用资源,或者无法保证资源的公平分配和隔离。
11#
12# 总而言之,SystemdCgroup参数的作用是为了确保containerd能够正确地管理容器的资源使用,以实现资源的限制、隔离和公平分配。
配置crictl客户端连接的运行时
1wget https://github.chenc.dev/github.com/kubernetes-sigs/cri-tools/releases/download/v1.34.0/crictl-v1.34.0-linux-amd64.tar.gz
2
3#解压
4tar xf crictl-v1.34.0-linux-amd64.tar.gz -C /usr/bin/
5
6#生成配置文件
7cat > /etc/crictl.yaml <<EOF
8runtime-endpoint: unix:///run/containerd/containerd.sock
9image-endpoint: unix:///run/containerd/containerd.sock
10timeout: 10
11debug: false
12EOF
13
14#测试
15systemctl restart containerd
16
17crictl info
18
19# 注意!
20# 下面是参数`crictl`的详细解释
21#
22# `crictl`是一个用于与容器运行时通信的命令行工具。它是容器运行时接口(CRI)工具的一个实现,可以对容器运行时进行管理和操作。
23#
24# 1. `runtime-endpoint: unix:///run/containerd/containerd.sock`
25# 指定容器运行时的终端套接字地址。在这个例子中,指定的地址是`unix:///run/containerd/containerd.sock`,这是一个Unix域套接字地址。
26#
27# 2. `image-endpoint: unix:///run/containerd/containerd.sock`
28# 指定容器镜像服务的终端套接字地址。在这个例子中,指定的地址是`unix:///run/containerd/containerd.sock`,这是一个Unix域套接字地址。
29#
30# 3. `timeout: 10`
31# 设置与容器运行时通信的超时时间,单位是秒。在这个例子中,超时时间被设置为10秒。
32#
33# 4. `debug: false`
34# 指定是否开启调式模式。在这个例子中,调式模式被设置为关闭,即`false`。如果设置为`true`,则会输出更详细的调试信息。
35#
36# 这些参数可以根据需要进行修改,以便与容器运行时进行有效的通信和管理。
初始化k8s
备注生成K8S Master节点初始化默认配置文件的命令:kubeadm config print init-defaults > kubeadm-init-config.yaml
1# 重点
2# 重点
3# 重点
4# 只在 master 节点上执行
5kubeadm init --kubernetes-version=1.32.11 \
6--apiserver-advertise-address=192.168.20.126 \
7--image-repository registry.aliyuncs.com/google_containers \
8--pod-network-cidr=10.244.0.0/16 \
9--service-cidr=10.96.0.0/12
开始初始化k8s-master
初始化过程示例
1[root@k8s-master ~]# kubeadm init --kubernetes-version=1.32.11 \
2> --apiserver-advertise-address=192.168.20.126 \
3> --image-repository registry.aliyuncs.com/google_containers \
4> --pod-network-cidr=10.244.0.0/16 \
5> --service-cidr=10.96.0.0/12
6[init] Using Kubernetes version: v1.32.11
7[preflight] Running pre-flight checks
8 [WARNING SystemVerification]: cgroups v1 support is in maintenance mode, please migrate to cgroups v2
9 [WARNING Hostname]: hostname "k8s-master" could not be reached
10 [WARNING Hostname]: hostname "k8s-master": lookup k8s-master on 114.114.114.114:53: no such host
11[preflight] Pulling images required for setting up a Kubernetes cluster
12[preflight] This might take a minute or two, depending on the speed of your internet connection
13[preflight] You can also perform this action beforehand using 'kubeadm config images pull'
14[certs] Using certificateDir folder "/etc/kubernetes/pki"
15[certs] Generating "ca" certificate and key
16[certs] Generating "apiserver" certificate and key
17[certs] apiserver serving cert is signed for DNS names [k8s-master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.20.126]
18[certs] Generating "apiserver-kubelet-client" certificate and key
19[certs] Generating "front-proxy-ca" certificate and key
20[certs] Generating "front-proxy-client" certificate and key
21[certs] Generating "etcd/ca" certificate and key
22[certs] Generating "etcd/server" certificate and key
23[certs] etcd/server serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.20.126 127.0.0.1 ::1]
24[certs] Generating "etcd/peer" certificate and key
25[certs] etcd/peer serving cert is signed for DNS names [k8s-master localhost] and IPs [192.168.20.126 127.0.0.1 ::1]
26[certs] Generating "etcd/healthcheck-client" certificate and key
27[certs] Generating "apiserver-etcd-client" certificate and key
28[certs] Generating "sa" key and public key
29[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
30[kubeconfig] Writing "admin.conf" kubeconfig file
31[kubeconfig] Writing "super-admin.conf" kubeconfig file
32[kubeconfig] Writing "kubelet.conf" kubeconfig file
33[kubeconfig] Writing "controller-manager.conf" kubeconfig file
34[kubeconfig] Writing "scheduler.conf" kubeconfig file
35[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
36[control-plane] Using manifest folder "/etc/kubernetes/manifests"
37[control-plane] Creating static Pod manifest for "kube-apiserver"
38[control-plane] Creating static Pod manifest for "kube-controller-manager"
39[control-plane] Creating static Pod manifest for "kube-scheduler"
40[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
41[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
42[kubelet-start] Starting the kubelet
43[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests"
44[kubelet-check] Waiting for a healthy kubelet at http://127.0.0.1:10248/healthz. This can take up to 4m0s
45[kubelet-check] The kubelet is healthy after 1.556036316s
46[api-check] Waiting for a healthy API server. This can take up to 4m0s
47[api-check] The API server is healthy after 24.503010649s
48[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
49[kubelet] Creating a ConfigMap "kubelet-config" in namespace kube-system with the configuration for the kubelets in the cluster
50[upload-certs] Skipping phase. Please see --upload-certs
51[mark-control-plane] Marking the node k8s-master as control-plane by adding the labels: [node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
52[mark-control-plane] Marking the node k8s-master as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]
53[bootstrap-token] Using token: ay0ssx.stiv0bzymxiyhac1
54[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
55[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to get nodes
56[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
57[bootstrap-token] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
58[bootstrap-token] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
59[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
60[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
61[addons] Applied essential addon: CoreDNS
62[addons] Applied essential addon: kube-proxy
63
64Your Kubernetes control-plane has initialized successfully!
65
66To start using your cluster, you need to run the following as a regular user:
67
68 mkdir -p $HOME/.kube
69 sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
70 sudo chown $(id -u):$(id -g) $HOME/.kube/config
71
72Alternatively, if you are the root user, you can run:
73
74 export KUBECONFIG=/etc/kubernetes/admin.conf
75
76You should now deploy a pod network to the cluster.
77Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
78 https://kubernetes.io/docs/concepts/cluster-administration/addons/
79
80Then you can join any number of worker nodes by running the following on each as root:
81
82kubeadm join 192.168.20.126:6443 --token ay0ssx.stiv0bzymxiyhac1 \
83 --discovery-token-ca-cert-hash sha256:a9c96e0b0fa3f28931d10794de63e730c642f707b71c942ddbd01f0ce50f9b25
84[root@k8s-master ~]#
1# 在主节点运行
2mkdir -p $HOME/.kube
3sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
4sudo chown $(id -u):$(id -g) $HOME/.kube/config
1# 重新初始化
2
3# 只有一个CRI的情况下
4kubeadm reset
5# 指定CRI重置
6kubeadm reset --cri-socket unix:///var/run/cri-dockerd.sock
7kubeadm reset --cri-socket unix:///var/run/containerd/containerd.sock
将node节点加入集群
如果出现,Token过期,或者找不到token的情况下,可以通过在 Master 节点上执行:kubeadm token create –print-join-command ,得到最新的Token
在工作节点运行
1kubeadm join 192.168.20.126:6443 --token ay0ssx.stiv0bzymxiyhac1 \
2 --discovery-token-ca-cert-hash sha256:a9c96e0b0fa3f28931d10794de63e730c642f707b71c942ddbd01f0ce50f9b25
网络插件安装步骤
给容器运行时配置“镜像加速器”
修改所有节点上的 containerd 配置文件,在 Master 和 所有 Node 节点上,执行以下步骤:
使用 vim 打开 containerd 的主配置文件:
1vim /etc/containerd/config.toml
检查并开启镜像配置目录(CRI 插件配置)
在文件中使用 / 搜索 [plugins."io.containerd.grpc.v1.cri".registry],确保或者修改为以下内容:
1[plugins."io.containerd.grpc.v1.cri".registry]
2 # 告诉 containerd 去这个目录下寻找每个镜像仓库的加速配置
3 config_path = "/etc/containerd/certs.d"
注意注意:较新版本的
containerd推荐使用certs.d目录来分主机管理镜像源,而不是直接死写在config.toml里,这样更清晰也更容易维护。
1# 保存并重启 containerd
2systemctl restart containerd
创建真正的镜像加速规则(重点)
在每个节点上执行以下命令,创建对应的加速规则目录和文件:
1# 1. 创建 docker.io 的配置专属目录
2mkdir -p /etc/containerd/certs.d/docker.io
3
4# 创建 registry.k8s.io 的配置目录
5mkdir -p /etc/containerd/certs.d/registry.k8s.io
6
7# 2. 写入加速器配置
8cat > /etc/containerd/certs.d/docker.io/hosts.toml <<EOF
9server = "https://registry-1.docker.io"
10
11[host."https://docker.m.daocloud.io"]
12 capabilities = ["pull", "resolve"]
13
14[host."https://docker.1ms.run"]
15 capabilities = ["pull", "resolve"]
16
17[host."https://dockerpull.com"]
18 capabilities = ["pull", "resolve"]
19EOF
20
21# 写入阿里云或国内开发者维护的同步源
22cat > /etc/containerd/certs.d/registry.k8s.io/hosts.toml <<EOF
23server = "https://registry.k8s.io"
24
25[host."https://k8s.m.daocloud.io"]
26 capabilities = ["pull", "resolve"]
27
28[host."https://k8s-gcr.1ms.run"]
29 capabilities = ["pull", "resolve"]
30
31[host."https://registry.aliyuncs.com/google_containers"]
32 capabilities = ["pull", "resolve"]
33EOF
1# 下载并应用Calico配置
2kubectl apply -f calico.yaml
3
4# 查看安装状态
5kubectl get pods -n kube-system
验证网络功能
1# 创建测试Pod
2kubectl run busybox --image docker.io/library/busybox:latest --restart=Never --rm -it -- sh
3
4# 在Pod内测试网络连通性
5nslookup kubernetes.default.svc.cluster.local
出现如下则配置成功
1If you don't see a command prompt, try pressing enter.
2/ # nslookup kubernetes.default.svc.cluster.local
3Server: 10.96.0.10
4Address: 10.96.0.10:53
5
6Name: kubernetes.default.svc.cluster.local
7Address: 10.96.0.1