【问题标题】:AKS | NetworkPolicy | Blocking ingress traffic while using Azure CNIAKS |网络政策 |在使用 Azure CNI 时阻止入口流量
【发布时间】:2021-02-02 20:12:09
【问题描述】:

我在获取基本的 NetworkPolicy 资源以阻止 Azure Kubernetes 服务 (AKS) 实例上的所有入口流量时遇到了一些问题。 AKS 使用 azure 网络插件(即 Azure CNI)进行设置。

我们的问题是,通过 VNet 对等到本地网络,AKS 工作负载现在暴露给来自内部网络的不良行为者。所以我们有一个入口控制器,但想让它成为所有非系统工作负载的唯一入口点。

这里是NetworkPolicy 资源:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: hello-node-network-policy
  namespace: hello-namespace-2
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  ingress: []

在不同命名空间中的 Pod 上,我仍然可以连接到 Service 端点和 Pod IP 地址(在 kubectl get pods --output=wide --namespace=hello-namespace-2 中可见)。在同一 VNet 中的 Azure VM 上,我也可以直接连接到 IP 地址。

Namespace、StatefulSet、Service、Ingress 和 NetworkPolicy 定义如下。

apiVersion: v1
kind: Namespace
metadata:
  name: hello-namespace-2
  labels:
    ingress-allowed: "allowed"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  creationTimestamp: null
  labels:
    app: hello-node
  name: hello-node
  namespace: hello-namespace-2
spec:
  serviceName: hello-node
  replicas: 1
  selector:
    matchLabels:
      app: hello-node
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: hello-node
    spec:
      containers:
      - image: k8s.gcr.io/echoserver:1.4
        name: echoserver
        resources: {}
---
apiVersion: v1
kind: Service
metadata:
  name: hello-node-service
  namespace: hello-namespace-2
spec:
  type: ClusterIP
  selector:
    app: hello-node
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 8080
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: hello-node-ingress
  namespace: hello-namespace-2
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  rules:
  - host: hello-namespace-2
    http:
      paths:
      - path: /hello-node(/|$)(.*)
        backend:
          serviceName: hello-node-service
          servicePort: 80
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: hello-node-network-policy
  namespace: hello-namespace-2
spec:
  podSelector: {}
  policyTypes:
  - Ingress
  ingress: []

这就像没有安装网络控制器一样,我认为 Azure CNI 的 azure 网络插件代表了它。我们是否必须显式安装像 Calico 这样的网络控制器?

非常感谢您对此行为的任何见解。

谢谢!

【问题讨论】:

    标签: kubernetes azure-aks kubernetes-networkpolicy


    【解决方案1】:

    1. Network policy options in AKS

    Azure 提供了两种实施网络策略的方法。创建 AKS 群集时选择网络策略选项。集群创建后无法更改策略选项

    • Azure 自己的实现,称为 Azure 网络策略。
    • Calico Network Policies,一个由 Tigera 创立的开源网络和网络安全解决方案。

    两种实现都使用 Linux IPTables 来执行指定的策略。策略被转换为允许和不允许的 IP 对集。然后将这些对编程为 IPTable 过滤规则。

    2. Differences between Azure and Calico policies and their capabilities

    3. Create an AKS cluster and enable network policy

    要使用 Azure 网络策略,您必须使用 Azure CNI plug-in 并定义您自己的虚拟网络和子网。有关如何规划所需子网范围的更多详细信息,请参阅configure advanced networking

    Calico 网络策略可以与同一个 Azure CNI 插件或 Kubenet CNI 插件一起使用。

    4. 我个人从未使用过Azure CNI plug-in。始终使用

    创建集群
    az aks create --resource-group <RG> --name <NAME> --network-policy calico 
    

    请看例子:

    a. Tutorial: Calico Network Policies with Azure Kubernetes Service

    b. Network Policy in Kubernetes using Calico

    【讨论】:

    • 我明白了,谢谢 Vitalii!我混淆了网络插件和网络策略配置。两者都需要设置 NetworkPolicy 资源才能生效。这里是来自az aks show 的部分结果,供其他遇到此问题的人使用:text "loadBalancerSku": "Standard", "networkMode": null, "networkPlugin": "azure", "networkPolicy": null, "outboundType": "loadBalancer", "podCidr": null, "serviceCidr": "10.0.0.0/25"
    猜你喜欢
    • 2021-08-18
    • 2019-02-04
    • 1970-01-01
    • 2020-11-21
    • 2022-11-05
    • 1970-01-01
    • 2021-12-02
    • 2021-04-02
    • 2020-06-29
    相关资源
    最近更新 更多