【问题标题】:GKE deny statement with network policy + calico带有网络策略 + calico 的 GKE 拒绝语句
【发布时间】:2019-11-08 05:32:26
【问题描述】:

我正在使用 Google Cloud 运行托管集群,因此它可以选择启用 NetworkPolicy,并且在后端它使用 calico。我遇到的问题,看来我只能使用 api 版本networking.k8s.io/v1

我正在尝试创建策略来禁用来自 pod 的任何内部出口 traefik,并允许任何进出外部网络的入口 + 出口。

使用 calico API,它看起来像这样:

apiVersion: projectcalico.org/v3
kind: NetworkPolicy
metadata:
  name: policy-name
  namespace: namespace-name
spec:
  selector: label == value
  types:
  - Ingress
  - Egress
ingress:
  - action: Allow
    notProtocol: UDP
    destination:
      ports:
      - 53
  - action: Allow
    notProtocol: TCP
    destination:
      ports:
      - 53
  egress:
  - action: Deny
    protocol: UDP
    destination:
      ports:
      - 53
  - action: Deny
    protocol: TCP
    destination:
      ports:
      - 53

或以下政策的否定版本:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: policy-name
  namespace: namespace-name
spec:
  podSelector:
    matchLabels:
      label: value
  policyTypes:
  - Egress
  egress:
  - ports:
    - port: 53
      protocol: UDP
    - port: 53
      protocol: TCP
  - to:
    - namespaceSelector: {}

所以我有两个问题: 1. 是否可以使用networking.k8s.io/v1 API 重现上述规则? 2. 我能否在托管 GKE 集群上启用projectcalico.org/v3 API?

【问题讨论】:

  • 如果在后端使用 calico,您是否有理由要在 apiVersion 字段中明确指定 calico?
  • 我不能肯定,但这似乎不太可能。 GKE 上的 CNI 实现完全由 Google 控制。
  • @Prashant 不同的语法。使用networking.k8s.io/v1 api,我不能使用否定或否认语句,但可以使用projectcalico.org/v3
  • 语法没问题。但是有什么东西只能通过calico而不是gke API来实现吗?
  • @Prashant 这就是我要问的问题)

标签: networking kubernetes calico gke-networking


【解决方案1】:

终于花了 2 天。似乎要从 API 'projectcalico.org/v3' 应用配置,您必须首先安装或部署到您的集群 CLI 工具 calicoctl。然后,您可以使用 calicoctl apply -f ./policy.yml 应用您的策略,或者如果它已部署到集群,则使用别名 alias calicoctl="kubectl exec -i -n kube-system calicoctl /calicoctl -- " + cat ./policy.yml | calicoctl apply -f -

下面的工作政策将禁止出口到专用网络,并且只允许公共:

apiVersion: projectcalico.org/v3
kind: NetworkPolicy
metadata:
  name: policy-name
  namespace: namespace-name
spec:
  selector: label == value
  types:
  - Egress
  egress:
  - action: Allow
    protocol: UDP
    destination:
      ports: [53]
  - action: Allow
    protocol: TCP
    destination:
      ports: [53]
  - action: Deny
    destination:
      nets:
      - 10.0.0.0/8
      - 172.16.0.0/12
      - 192.168.0.0/16
  - action: Allow

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多