设置固定IP

1
nano /etc/network/interfaces

内容:

1
2
3
4
5
6
allow-hotplug ens192
iface ens192 inet static
address 10.0.0.121
netmask 255.255.255.0
gateway 10.0.0.4
dns-nameservers 10.0.0.4
1
systemctl restart networking

永久禁用 IPv6

编辑 /etc/sysctl.conf 文件:

1
nano /etc/sysctl.conf

在文件末尾添加以下内容:

1
2
3
4
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv6.conf.lo.disable_ipv6 = 1
net.ipv4.ip_forward = 1

保存文件后,应用更改:

1
sysctl -p

修改主机名

1
hostnamectl set-hostname k8s-node-1

关闭 SWAP

1
2
swapoff -a
sed -i '/ swap / s/^/#/' /etc/fstab

修改 hosts

/etc/hosts 添加 Master 节点和 Node 节点的 IP:

1
2
3
4
10.0.0.120 k8s-master
10.0.0.121 k8s-node-1
10.0.0.122 k8s-node-2
10.0.0.123 k8s-node-3

安装必要软件

1
apt update && apt -y install htop curl wget containerd apt-transport-https ca-certificates gpg

配置 containerd

1
2
mkdir -p /etc/containerd
containerd config default | tee /etc/containerd/config.toml

修改 /etc/containerd/config.toml

1
sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml

重启 containerd:

1
2
systemctl restart containerd
systemctl enable containerd

开启内核模块

1
2
3
4
cat <<EOF | tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
modprobe br_netfilter
1
2
3
4
5
cat <<EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
sysctl --system

接下来的步骤

看官方文档 => https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/

执行 Kubeadm init

1
sudo kubeadm init --pod-network-cidr=192.168.0.0/16 --cri-socket unix:///run/containerd/containerd.sock

部署 CNI(网络插件)

Kubernetes 需要 CNI(Container Network Interface) 来让 Pod 之间能够互相通信。

安装 Flannel(推荐):

1
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml

Flannel 适用于 小型和简单的集群,默认使用 192.168.0.0/16 作为 Pod 网络。

让 Worker 节点加入集群

Master 节点运行:

1
kubeadm token create --print-join-command

输出类似:

1
2
kubeadm join 10.0.0.113:6443 --token abcdef.1234567890abcdef \
--discovery-token-ca-cert-hash sha256:xyzxyzxyz

Node(如 10.0.0.121) 上运行此命令:

1
2
sudo kubeadm join 10.0.0.113:6443 --token abcdef.1234567890abcdef \
--discovery-token-ca-cert-hash sha256:xyzxyzxyz --cri-socket /run/containerd/containerd.sock

然后在 Master 上再次检查:

1
kubectl get nodes

确保 所有节点都是 Ready 状态。

kubectl delete -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml