我正在学习 Kubernetes 网络策略。我试图创建这样一种情况:同一命名空间中的两个 Pod 关联不同的网络策略:
- pod A 可以从任何地方进入
- pod B 无处不在(但最终只有 pod A)
我发现 Kubernetes 似乎接受了网络策略,但没有执行它们。已部署的 Pod 使用镜像ealen/echo-server:latest
来回显有关其运行环境的信息,为了测试策略,我从一个 Pod 向另一个 Pod 发出 HTTP 请求:
kubectl exec \
-n private-networking \
POD_A_NAME \
-- wget -O - service-b.private-networking
如果这些策略有效,我预计从 A 到 B 的调用会因超时而失败,而从 B 到 A 的调用会成功。目前,这两种方式都是成功的。
该集群使用 Amazon EKS 部署,我没有使用 Calico 或其他任何东西(尽管你会在 github repo 中看到我尝试过)。
Pod 通过部署对象进行部署,并且仅在名称上有所不同。(注:Pod 未部署在 Fargate 上)
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-a
namespace: private-networking
spec:
selector:
matchLabels:
service: service-a
template:
metadata:
labels:
service: service-a
spec:
containers:
- name: echo-a
image: ealen/echo-server:latest
resources:
limits:
memory: "128Mi"
cpu: "100m"
ports:
- containerPort: 8080
env:
- name: PORT
value: "8080"
应用的网络策略如下,也可以在 GitHub 上访问
我错过了什么?
---
# Deny all ingress and egress traffic across the board
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all
namespace: private-networking
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
---
# Allow all pods in the namespace to egress traffic to kube-dns
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-dns
namespace: private-networking
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- namespaceSelector: {}
podSelector:
matchLabels:
k8s-app: kube-dns
ports:
- port: 53
protocol: UDP
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: service-a-ingress-from-anywhere
namespace: private-networking
spec:
podSelector:
matchLabels:
service: service-a
policyTypes:
- Ingress
ingress:
- from:
- ipBlock:
cidr: 0.0.0.0/0
ports:
- port: 8080
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: service-a-egress-to-anywhere
namespace: private-networking
spec:
podSelector:
matchLabels:
service: service-a
egress:
- {}
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: service-b-ingress-from-nowhere
namespace: private-networking
spec:
podSelector:
matchLabels:
service: service-b
policyTypes:
- Ingress
ingress: [ ]
答案1
这个问题的答案是在 Amazon EKS 集群上安装 Calico。我误解了文档,以为 Calico 是一个可选的附加功能,并且 Amazon EKS 集群默认安装了容器网络接口插件。
但看来并非如此。