在Debian上部署Kubernetes时,网络配置是一个关键步骤,以确保集群中的各个节点能够相互通信。以下是一个详细的步骤指南,帮助你在Debian系统上配置Kubernetes网络:
设置主机名和更新hosts文件:
在每个节点上设置主机名,并在 /etc/hosts
文件中添加节点IP和主机名的映射。
sudo hostnamectl set-hostname "k8s-master01.test.local"
sudo hostnamectl set-hostname "k8s-worker01.test.local"
sudo hostnamectl set-hostname "k8s-worker02.test.local"
echo "192.168.16.20 k8s-master01.test.local k8s-master01" | sudo tee -a /etc/hosts
echo "192.168.16.21 k8s-worker01.test.local k8s-worker01" | sudo tee -a /etc/hosts
echo "192.168.16.22 k8s-worker02.test.local k8s-worker02" | sudo tee -a /etc/hosts
关闭所有节点的swap分区: 为了确保kubelet顺利运行,建议禁用swap分区。
sudo swapoff -a
sudo sed -i '/ swap / s/1/g' /etc/fstab
为Kubernetes集群添加防火墙规则: 如果你的Debian系统已启用操作系统防火墙,请允许必要的端口。
sudo ufw allow 6443/tcp
sudo ufw allow 2379/tcp
sudo ufw allow 2380/tcp
sudo ufw allow 10250/tcp
sudo ufw allow 10251/tcp
sudo ufw allow 10252/tcp
sudo ufw allow 10255/tcp
sudo ufw reload
安装containerd运行时: 在所有节点上安装containerd运行时,并设置必要的内核参数。
sudo apt update
sudo apt install -y containerd
sudo tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
sudo tee /etc/sysctl.d/99-kubernetes-k8s.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.ipv4.ip_forward=1
net.bridge.bridge-nf-call-ip6tables=1
EOF
sudo sysctl --system
初始化Kubernetes集群: 使用kubeadm初始化控制平面组件,并配置网络插件。
sudo apt update
sudo apt install -y apt-transport-https curl
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
sudo kubeadm init --apiserver-advertise-address 192.168.16.20 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.28.0 --control-plane-endpoint 192.168.16.20:6443 --service-cidr 10.100.0.0/16 --token-ttl 0 --pod-network-cidr 10.244.0.0/16
安装网络插件: 安装并配置网络插件,例如Calico。
wget https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/calico.yaml
kubectl apply -f calico.yaml
sudo ufw allow 179/tcp
sudo ufw allow 4789/udp
sudo ufw allow 51820/udp
sudo ufw allow 51821/udp
sudo ufw reload
验证网络配置: 验证网络配置是否成功,例如通过创建Pod和Service来测试网络是否正常工作。
kubectl create deployment nginx-app --image=nginx --replicas=2
kubectl expose deployment nginx-app --name=nginx-web-svc --type=NodePort --port=80 --target-port=80
kubectl describe svc nginx-web-svc
curl http://node1:31743
通过以上步骤,你可以在Debian系统上成功配置Kubernetes网络,确保集群中的各个节点能够相互通信。