Debian系统中Kubernetes部署的网络配置指南
导读:Pre-Installation Checks Set Hostnames and Update /etc/hosts: On each node, set a unique hostname (e.g., k8s-master01, k...
Pre-Installation Checks
- Set Hostnames and Update /etc/hosts: On each node, set a unique hostname (e.g.,
k8s-master01,k8s-worker01) usingsudo hostnamectl set-hostname. Add entries to/etc/hostsmapping IPs to hostnames (e.g.,192.168.1.20 k8s-master01), ensuring name resolution works across the cluster. - Disable Swap Partition: Kubernetes requires swap to be disabled for proper kubelet operation. Run
sudo swapoff -ato disable swap temporarily, then edit/etc/fstabto comment out or remove the swap line. Usesudo swapon --showto verify swap is off.
Install Container Runtime
- Install containerd: containerd is the recommended runtime for Kubernetes. On all nodes, run:
Configure containerd to use required kernel modules by creatingsudo apt update sudo apt install -y containerd/etc/modules-load.d/containerd.confwith:
Load the modules immediately withoverlay br_netfiltersudo modprobe overlay & & sudo modprobe br_netfilter. Set up sysctl rules for bridging in/etc/sysctl.d/99-kubernetes-k8s.conf:
Apply sysctl changes withnet.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1sudo sysctl --system.
Install Kubernetes Components
- Add Kubernetes Repository: On all nodes, add the Kubernetes APT repository to install signed packages. Run:
sudo apt update sudo apt install -y apt-transport-https curl curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list sudo apt update - Install kubeadm, kubelet, and kubectl: Install the core Kubernetes tools and hold them at the current version to prevent accidental upgrades:
sudo apt install -y kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl
Initialize Kubernetes Cluster
- Initialize Master Node: On the master node, run
kubeadm initwith critical parameters for networking. For example:
Replacesudo kubeadm init --apiserver-advertise-address=< MASTER_IP> --pod-network-cidr=10.244.0.0/16 --service-cidr=10.100.0.0/16 --image-repository registry.aliyuncs.com/google_containers< MASTER_IP>with the master’s static IP. The--pod-network-cidrmust match your chosen network plugin’s requirements (e.g., Flannel uses10.244.0.0/16). - Configure kubectl: After initialization, set up
kubectlto communicate with the cluster:mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
Deploy a Network Plugin
- Choose a Plugin: Popular plugins include Flannel (simple) and Calico (advanced features like network policies). Below are steps for both:
- Flannel: Apply the Flannel manifest to set up overlay networking:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml - Calico: Download and apply the Calico manifest (adjust versions as needed):
wget https://docs.projectcalico.org/v3.26.1/manifests/calico.yaml kubectl apply -f calico.yaml - Firewall Rules: Open required ports for your plugin. For Calico, allow BGP (179/tcp) and VXLAN (4789/udp):
sudo ufw allow 179/tcp sudo ufw allow 4789/udp sudo ufw reload
- Flannel: Apply the Flannel manifest to set up overlay networking:
Verify Network Configuration
- Check Pod Status: Ensure all network plugin pods are running in the
kube-systemnamespace:
Look forkubectl get pods -n kube-systemRunningstatus next to Flannel/Calico pods. - Test Pod Connectivity: Create a test deployment and service to validate inter-pod communication:
Get the NodePort assigned to the 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=80kubectl get svc nginx-web-svc) and access it from any node or external machine using< NODE_IP> :< NODE_PORT>.
Join Worker Nodes (Optional)
- Get Join Command: On the master node, run
kubeadm token create --print-join-commandto generate a command like:sudo kubeadm join < MASTER_IP> :6443 --token < TOKEN> --discovery-token-ca-cert-hash sha256:< HASH> - Run on Worker Nodes: Execute the join command on each worker node to add them to the cluster. Verify worker nodes are
Readywithkubectl get nodes.
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Debian系统中Kubernetes部署的网络配置指南
本文地址: https://pptw.com/jishu/733937.html
