艾克斯 |网络政策 |使用 Azure CNI 时阻止入口流量
AKS | NetworkPolicy | Blocking ingress traffic while using Azure CNI
我在获取基本 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 上,我仍然可以连接到服务端点和 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: /
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 这样的网络控制器?
非常感谢对此行为的任何见解。
谢谢!
1. Network policy options in AKS
Azure 提供了两种实现网络策略的方法。创建 AKS 群集时选择网络策略选项。策略选项在集群创建后不能更改:
- Azure 自己的实现,称为 Azure 网络策略。
- Calico Network Policies,一个由 Tigera 创立的 open-source 网络和网络安全解决方案。
两种实现都使用 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 and define your own virtual network and subnets. For more detailed information on how to plan out the required subnet ranges, see configure advanced networking。
Calico 网络策略可以与相同的 Azure CNI plug-in 或 Kubenet CNI plug-in.
一起使用
4. 我个人没用过Azure CNI plug-in。始终使用
创建集群
az aks create --resource-group <RG> --name <NAME> --network-policy calico
请看例子:
我在获取基本 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 上,我仍然可以连接到服务端点和 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: /
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 这样的网络控制器?
非常感谢对此行为的任何见解。
谢谢!
1. Network policy options in AKS
Azure 提供了两种实现网络策略的方法。创建 AKS 群集时选择网络策略选项。策略选项在集群创建后不能更改:
- Azure 自己的实现,称为 Azure 网络策略。
- Calico Network Policies,一个由 Tigera 创立的 open-source 网络和网络安全解决方案。
两种实现都使用 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 and define your own virtual network and subnets. For more detailed information on how to plan out the required subnet ranges, see configure advanced networking。
Calico 网络策略可以与相同的 Azure CNI plug-in 或 Kubenet CNI plug-in.
一起使用4. 我个人没用过Azure CNI plug-in。始终使用
创建集群az aks create --resource-group <RG> --name <NAME> --network-policy calico
请看例子: