参考文档
https://kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
https://zhuanlan.zhihu.com/p/612051521

安装准备

3台Linux机器
每台机器至少2G内存
CPU至少2核
机器网络互通

主机名 IP地址 操作系统
k8s-m1(master) 192.168.7.201 Debian12
k8s-n1(node1) 192.168.7.211 Debian12
k8s-n2(node2) 192.168.7.212 Debian12

K8s1.28.2 + containerd1.7.7

关闭防火墙

1
2
3
4
5
# debian
ufw disable
# centos
systemctl stop firewalld
systemctl disable firewalld

关闭selinux

永久

1
sed -i 's/enforcing/disabled/' /etc/selinux/config  

临时

1
setenforce 0  

禁用交换分区

为了保证 kubelet 正常工作,你必须禁用交换分区

永久

1
sed -ri 's/.*swap.*/#&/' /etc/fstab

临时

1
swapoff -a 

修改hosts

vi /etc/hosts

1
2
3
192.168.7.201 k8s-m1
192.168.7.211 k8s-n1
192.168.7.212 k8s-n2

设置网桥参数

转发 IPv4 并让 iptables 看到桥接流量,三台服务服务器都需要执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cat <<EOF | tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

modprobe overlay
modprobe br_netfilter

cat <<EOF | 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
# 生效
sysctl --system

# 检验配置是否生效
lsmod | grep br_netfilter
lsmod | grep overlay

时间同步

1
2
3
4
5
6
7
8
# 设置市区
timedatectl set-timezone "Asia/Shanghai"
# 校正
apt install ntpdate -y
ntpdate ntp.sjtu.edu.cn
# 设置同步任务
crontab -e
# */60 * * * * /usr/sbin/ntpdate ntp.sjtu.edu.cn

安装容器运行时

下载地址

三台机器都要执行,这边选择 cri-containerd-cni的压缩包(runc和cli都包含)

下载

1
2
3
cd /
curl -O https://github.com/containerd/containerd/releases/download/v1.7.7/cri-containerd-cni-1.7.7-linux-amd64.tar.gz
tar -zxvf cri-containerd-cni-1.7.7-linux-amd64.tar.gz

配置containerd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 生成默认配置文件
mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml

--/etc/containerd/config.toml--

[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
[plugins."io.containerd.grpc.v1.cri"]
sandbox_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.8"
#sandbox_image = "k8s.gcr.io/pause:3.8"

# 设置镜像
[plugins."io.containerd.grpc.v1.cri".registry]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
endpoint = ["http://mirrors.ustc.edu.cn"]
#endpoint = ["https://registry-1.docker.io"]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."*"]
endpoint = ["http://hub-mirror.c.163.com"]
--

配置cli

创建配置文件,运行 crictl config 命令可获取参数说明

1
2
3
4
5
6
7
8
9
10
11
cat <<EOF | sudo tee /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 2
debug: false
pull-image-on-create: false
disable-pull-on-run: false
EOF

# 测试
crictl pods

启动

1
systemctl start containerd

组建集群

配置k8s镜像源

这边使用阿里云的镜像源,参考文档

1
2
3
4
5
6
7
8
9
10
11
12
apt-get update && apt-get install -y apt-transport-https
# 添加apt key
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
# 添加apt源
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt-get update

cat <<EOF | tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

安装kubelet kubeadm kubectl(各节点都需要安装)

1
2
3
4
5
6
7
# 安装
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
# 查看版本
kubeadm version
kubectl version --client
kubelet --version

由于官网未开放同步方式, 可能会有索引gpg检查失败的情况, 这时请用 yum install -y --nogpgcheck kubelet kubeadm kubectl 安装

部署master节点

初始化

1
2
3
4
5
6
kubeadm init \
--kubernetes-version=1.28.2 \
--apiserver-advertise-address=192.168.7.201 \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.100.0.0/16

–apiserver-advertise-address 集群通告地址,主节点id
–image-repository 由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址
–kubernetes-version K8s版本,与上面安装的一致
–service-cidr 集群内部虚拟网络,Pod统一访问入口
–pod-network-cidr Pod网络,与下面部署的CNI网络组件yaml中保持一

pod-network-cidr 此参数必须设置,否则后面安装calico网络插件是会报错

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
[init] Using Kubernetes version: v1.28.2
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
W1020 11:07:11.217406 9188 checks.go:835] detected that the sandbox image "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.8" of the container runtime is inconsistent with that used by kubeadm. It is recommended that using "registry.aliyuncs.com/google_containers/pause:3.9" as the CRI sandbox image.
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [k8s-m1 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.7.201]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8s-m1 localhost] and IPs [192.168.7.201 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8s-m1 localhost] and IPs [192.168.7.201 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 6.001868 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node k8s-m1 as control-plane by adding the labels: [node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node k8s-m1 as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]
[bootstrap-token] Using token: pv0jnm.swwlb4mafc2ak81m
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy

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.7.201:6443 --token pv0jnm.swwlb4mafc2ak81m \
--discovery-token-ca-cert-hash sha256:1a62c6b09803dd0d23a3f2d21c297d1bbe05b3b9f3b62c4039c10bc26bcdc83d

配置环境变量

按照初始化的输出操作即可

  • 普通用户
    1
    2
    3
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
  • root用户
    1
    2
    3
    4
    5
    6
    export KUBECONFIG=/etc/kubernetes/admin.conf
    # 建议直接写文件
    cat >> ~/.bashrc <<EOF
    export KUBECONFIG=/etc/kubernetes/admin.conf
    EOF
    source ~/.bashrc

安装网络插件

安装calico依赖说明 文档

下载

1
wget https://raw.githubusercontent.com/projectcalico/calico/v3.26.3/manifests/calico.yaml

修改配置

1
2
3
4
5
6

# - name: CALICO_IPV4POOL_CIDR
# value: "192.168.0.0/16"
修改为
- name: CALICO_IPV4POOL_CIDR
value: "10.100.0.0/16"

安装

1
kubectl apply -f calico.yaml

部署node节点

执行master节点init后输出的join命令

1
2
kubeadm join 192.168.7.201:6443 --token pv0jnm.swwlb4mafc2ak81m \
--discovery-token-ca-cert-hash sha256:1a62c6b09803dd0d23a3f2d21c297d1bbe05b3b9f3b62c4039c10bc26bcdc83d

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
[preflight] Running pre-flight checks
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.