Google kubernetes engine GKE网络策略和外部名称

Google kubernetes engine GKE网络策略和外部名称,google-kubernetes-engine,kubernetes-networkpolicy,Google Kubernetes Engine,Kubernetes Networkpolicy,我需要禁用命名空间中我的pod中的所有出口流量,除了一个方向,例如yahho.com My service.yaml看起来像: kind: Service apiVersion: v1 metadata: name: yahoo labels: output: allow spec: type: ExternalName externalName: yahoo.com 我的网络策略文件阻止所有输出流量: apiVersion: networking.k8s.io/v1 kind:

我需要禁用命名空间中我的pod中的所有出口流量,除了一个方向,例如yahho.com My service.yaml看起来像:

kind: Service
apiVersion: v1
metadata:
 name: yahoo
 labels:
   output: allow
spec:
 type: ExternalName
 externalName: yahoo.com
我的网络策略文件阻止所有输出流量:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-all
  namespace: np-test
spec:
  podSelector: {}
  policyTypes:
  - Egress

我尝试使用结构来允许:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-yahoo
  namespace: np-test
spec:
  policyTypes:
  - Egress
  podSelector:
    matchLabels:
      name: test-ubuntu
  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          namespace: np-test 
    - podSelector:
        matchLabels:
          output: allow
    ports:
    - protocol: TCP
      port: 443


你能为过往车辆纠正我的结构吗?我在文档中找不到任何想法。

不幸的是,由于Calico的限制,GKE不支持基于DNS名称的出口过滤“开箱即用”的网络策略:基于CIDR的过滤不灵活且容易出错,对于外部资源来说效果不佳。2017年,GitHub上发布了一项关于在Kubernetes API中引入对新参数
DNSSelector
的支持的声明

目前,第三方解决方案Cilium CNI插件提供基于DNS的出口过滤。重要的是,它支持通配符

简要步骤:

  • 为当前项目启用GKE API(如果尚未启用)
  • 构建一个标准的3节点GKE集群,无需网络策略支持(使用
    kubenet
  • 连接到新创建的Kubernetes群集
  • 在GKE中创建集群管理绑定
  • 如有必要,为您的工作负载创建自定义命名空间
  • 如有必要,为外部资源创建无头服务
  • 运行Busybox容器以检查网络策略效果
  • 安装舵
  • 使用头盔展开纤毛
  • 在应用网络策略之前,请检查外部资源的可用性
  • 创建并部署Cilium网络策略
  • 应用网络策略后检查外部资源的可用性
  • 一旦在命名空间中创建第一个网络策略,所有其他通信都将被拒绝。没有必要使用
    ExternalName
    类型的无头服务,但如果您更愿意使用它,通过此类服务进行受限访问也可以
    kube dns
    负责名称解析。网络策略授予对它的访问权限

    具体步骤如下:

  • 为当前项目启用GKE API(如果尚未启用)
  • 构建一个标准的3节点GKE集群,无需网络策略支持(使用
    kubenet
    )。参数
    --username
    导致启用基本身份验证
  • 连接到新创建的Kubernetes群集
  • 在GKE中创建集群管理绑定
  • 为您的工作负载创建自定义命名空间
    np test
    ,并将其标记为:
  • 如果需要,为外部资源
    example.com
    创建无头服务
  • 运行Busybox容器以检查网络策略效果
  • 在CloudShell实例或您正在使用的工作站上安装Helm:
  • 使用头盔展开纤毛 生成所需的YAML文件:

    $ helm template cilium \
      --namespace cilium \
      --set global.nodeinit.enabled=true \
      --set nodeinit.reconfigureKubelet=true \
      --set nodeinit.removeCbrBridge=true \
      --set global.cni.binPath=/home/kubernetes/bin \
      > cilium.yaml
    $ kubectl create namespace cilium
    $ kubectl get namespaces
    $ kubectl create -f cilium.yaml
    
    重新启动
    kube系统
    命名空间中的所有POD,以便Cilium可以管理它们:

    $ kubectl delete pods -n kube-system $(kubectl get pods -n kube-system -o custom-columns=NAME:.metadata.name,HOSTNETWORK:.spec.hostNetwork --no-headers=true | grep '<none>' | awk '{ print $1 }') 
    $ kubectl -n cilium get pods
    
    请注意,
    example服务
    解析为与
    example.com

  • 创建并部署Cilium网络策略。现在是时候限制从Pod到Internet的访问,只允许访问
    example.com
  • 检查网络策略的效果。在应用网络策略后,检查Busybox pod中外部资源的可用性 正如您所见,Cilium网络策略允许直接或通过无头服务访问
    example.com
    ,而对其他站点的访问受到限制

    这种方法允许在GKE上获得基于DNS名称的出口过滤

    以下链接提供了有关所述解决方案的更多详细信息:


    不幸的是,由于印花布的限制,GKE不支持基于DNS名称的“开箱即用”出口过滤的网络策略:基于CIDR的过滤不灵活且容易出错,并且不适合外部资源。2017年,GitHub上发布了一项关于在Kubernetes API中引入对新参数
    DNSSelector
    的支持的声明

    目前,第三方解决方案Cilium CNI插件提供基于DNS的出口过滤。重要的是,它支持通配符

    简要步骤:

  • 为当前项目启用GKE API(如果尚未启用)
  • 构建一个标准的3节点GKE集群,无需网络策略支持(使用
    kubenet
  • 连接到新创建的Kubernetes群集
  • 在GKE中创建集群管理绑定
  • 如有必要,为您的工作负载创建自定义命名空间
  • 如有必要,为外部资源创建无头服务
  • 运行Busybox容器以检查网络策略效果
  • 安装舵
  • 使用头盔展开纤毛
  • 在应用网络策略之前,请检查外部资源的可用性
  • 创建并部署Cilium网络策略
  • 应用网络策略后检查外部资源的可用性
  • 一旦在命名空间中创建第一个网络策略,所有其他通信都将被拒绝。没有必要使用
    ExternalName
    类型的无头服务,但如果您更愿意使用它,通过此类服务进行受限访问也可以
    kube dns
    负责名称解析。网络策略授予对它的访问权限

    具体步骤如下:

  • 为当前项目启用GKE API(如果尚未启用)
  • 构建一个标准的3节点GKE集群,无需网络策略支持(使用
    kubenet
    )。参数
    --username
    导致启用基本身份验证
  • 连接到新创建的Kubernetes群集
  • 在GKE中创建集群管理绑定
  • 创建一个cus
    $ gcloud auth list
    $ gcloud auth login
    $ gcloud container clusters get-credentials standard-cluster-1 --zone europe-west3-c --project mylab
    $ kubectl config get-clusters
    $ kubectl cluster-info`
    
    $ kubectl create clusterrolebinding cluster-admin-binding --clusterrole cluster-admin --user youraccount@google.com
    
    $ kubectl create namespace np-test
    $ kubectl label namespace/np-test namespace=np-test
    $ kubectl get namespace/np-test --show-labels`
    
    $ vi example-service.yaml
    
    apiVersion: v1
    kind: Service
    metadata:
      name: example-service
      namespace: np-test
    spec:
      type: ExternalName
      externalName: example.com
    
    $ kubectl apply -f example-service.yaml
    $ kubectl get services -n np-test
    
    $ vi busybox-pod.yaml
    
    apiVersion: v1
    kind: Pod
    metadata:
      name: busybox-pod
      namespace: np-test
      labels: 
        namespace: np-test
    spec:
      containers:
      - image: radial/busyboxplus:curl
        command:
          - sleep
          - "3600"
        imagePullPolicy: IfNotPresent
        name: busybox-container
      restartPolicy: Always
    
    $ kubectl create -f ./busybox-pod.yaml
    $ kubectl get pods -n np-test --show-labels
    
    $ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
    $ chmod 700 get_helm.sh
    $ ./get_helm.sh
    $ helm version
    
    $ curl -LO https://github.com/cilium/cilium/archive/1.6.5.tar.gz
    $ tar xzvf 1.6.5.tar.gz
    $ cd cilium-1.6.5/install/kubernetes
    
    $ helm template cilium \
      --namespace cilium \
      --set global.nodeinit.enabled=true \
      --set nodeinit.reconfigureKubelet=true \
      --set nodeinit.removeCbrBridge=true \
      --set global.cni.binPath=/home/kubernetes/bin \
      > cilium.yaml
    $ kubectl create namespace cilium
    $ kubectl get namespaces
    $ kubectl create -f cilium.yaml
    
    $ kubectl delete pods -n kube-system $(kubectl get pods -n kube-system -o custom-columns=NAME:.metadata.name,HOSTNETWORK:.spec.hostNetwork --no-headers=true | grep '<none>' | awk '{ print $1 }') 
    $ kubectl -n cilium get pods
    
    $ kubectl exec -n np-test busybox-pod -- curl -kL google.com
    $ kubectl exec -n np-test busybox-pod -- curl -kL yahoo.com
    $ kubectl exec -n np-test busybox-pod -- curl -kL example.com
    $ kubectl exec -n np-test busybox-pod -- curl -kL -H 'host: example.com' example-service
    $ kubectl exec -n np-test busybox-pod -- nslookup example.com
    $ kubectl exec -n np-test busybox-pod -- nslookup example-service
    
    $ vi dns-policy.yaml
    
    apiVersion: "cilium.io/v2"
    kind: CiliumNetworkPolicy
    metadata:
      name: "dns-policy"
      namespace: np-test
    spec:
      endpointSelector:
        matchLabels: 
          namespace: np-test
      egress:
      - toFQDNs:
        - matchName: "example.com"  
      - toEndpoints:
        - matchLabels:
            "k8s:io.kubernetes.pod.namespace": kube-system
            "k8s:k8s-app": kube-dns
        toPorts:
        - ports:
          - port: "53"
            protocol: ANY
          rules:
            dns:
            - matchPattern: "*"
    
    $ kubectl create -f dns-policy.yaml
    $ kubectl get CiliumNetworkPolicy -n np-test
    
    $ kubectl exec -n np-test busybox-pod -- ping -c 3 google.com
    100% packet loss
    $ kubectl exec -n np-test busybox-pod -- ping -c 3 yahoo.com
    100% packet loss
    $ kubectl exec -n np-test busybox-pod -- ping -c 3 example-service
    0% packet loss
    $ kubectl exec -n np-test busybox-pod -- ping -c 3 example.com
    0% packet loss
    $ kubectl exec -n np-test busybox-pod -- curl -kL google.com
    Connection timed out
    $ kubectl exec -n np-test busybox-pod -- curl -kL yahoo.com
    Connection timed out
    $ kubectl exec -n np-test busybox-pod -- curl -kL example.com
    OK
    $ kubectl exec -n np-test busybox-pod -- curl -kL -H 'host: example.com' example-service
    OK