以下是Ubuntu上Kubernetes网络配置的关键技巧:
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')
cat >> /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
EOF
sysctl -p
sudo modprobe overlay br_netfilter
sudo ufw allow 6443/tcp
sudo ufw default deny incoming
sudo ufw reload
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-frontend-to-database
spec:
podSelector:
matchLabels:
app: database
ingress:
- from:
- namespaceSelector:
matchLabels:
env: backend
ip link set dev <网卡名> mtu 1450
calico.yaml调整IP分配模式(如使用IPAM)。calicoctl patch felixconfiguration default --patch '{"spec": {"bpfEnabled": true}}'
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
kubectl exec -it <pod-name> -- ping <另一pod-ip>
kubectl get pods -n kube-system -l k8s-app=calico-node
通过以上配置,可确保Ubuntu上Kubernetes集群的网络连通性、安全性和性能。根据实际需求选择合适的网络插件,并定期通过监控工具(如Prometheus)优化网络参数。