网络插件
一般网络策略:
名称空间,拒绝所有出站,入站,放行所有出站目标本名称空间内的所有Pod
Docker 四种网络模型
bridge
joined
open
none
Kubernetes 网络通信
1、容器间通信
2、POD通信
3、POD与Service通信
4、Service 与集群
CNI 网络插件
提供网络分配功能
flannel
calico
canel
kube-router
kubernetes网络解决方案:
虚拟网桥
多路复用:MacVLAN
硬件交换:SR-IOV (单根IO虚拟化)
网络插件配置文件目录 /etc/cni/net.d/
Flannel 支持多种后端
VxLAN
vxlan (隧道叠加网络通信+L3)
Directrouting (直接路由)
host-ge: Host Gateway (同一个网段)
UDP
Flannel 配置
kubectl get configmap -n kube-system kubectl get configmap -n kube-system kubectl get configmap kube-flannel-cfg -o json -n kube-system
Network:flannel 使用CIDR格式的网络地址,用于为POD配置网络功能
10.244.0.0/16
SubnetLen:把Network切分子网供各节点使用时,使用多少的掩码切分,默认为24位
SubnetMin: 10.244.10.0/24
SubnetMax 10.244.100.0./24
Backend: 使用什么方式作为POD后端通信
vxlan
host-gw
udp
方法1:
vim flannet.json { "Network": "10.244.0.0/16"" "Bacend": { "Type": "vxlan", "Directrouting": "true" } }
方法2:
改初始安装Flannel时候的yaml,卸载重新安装 验证: ip route show
Calico 网络策略
kubectl explain networkpolicy.spec kubectl explain networkpolicy.spec.ingress kubectl get netpolicy -n xxx
Ingress 进方向规则
Egress 出方向规则
policyTypes 默认存在什么就生效
配置拒绝所有入站例子
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metedata: name: deny-all-ingress namespace: dev spec: podSelector: {} policyTypes: - Ingress
配置允许所有入站例子
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metedata: name: all-ingress namespace: dev spec: podSelector: {} ingress: - {} policyTypes: - Ingress
配置特定网段进站,特定IP允许进站规则例子
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metedata: name: allow-myapp-ingress namespace: dev spec: podSelector: {} matchLabels: app: myapp ingress: - from: - ipBlock: cidr: 10.244.0.0/16 except: - 10.244.1.2/32 ports: - protocol: TCP port: 80 ports: - protocol: TCP port: 443
配置禁止所有出站规则例子
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metedata: name: deny-all-Egress namespace: dev spec: podSelector: {} policyTypes: - Egress
配置禁止所有出站规则例子
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metedata: name: deny-all-Egress namespace: dev spec: podSelector: {} policyTypes: - Egress
配置允许所有出站规则例子
apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metedata: name: deny-all-Egress namespace: dev spec: podSelector: {} - {} policyTypes: - Egress
停留在世界边缘,与之惜别