Istio仅允许来自特定名称空间的服务传入流量

时间:2019-04-25 09:50:34

标签: istio

我们希望Istio仅允许从特定名称空间进入服务的传入流量。我们如何用Istio做到这一点?我们正在运行Istio 1.1.3版本。

更新:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: test-app-ingress
  namespace: test-ns
spec:
  podSelector:
    matchLabels:
      app: testapp
  ingress:
  - ports:
    - protocol: TCP
      port: 80
    from:
    - podSelector:
        matchLabels:
          istio: ingress

这不起作用,我也能够从其他名称空间访问该服务。接下来我尝试:

apiVersion: "rbac.istio.io/v1alpha1"
kind: ServiceRole
metadata:
  name: external-api-caller
  namespace: test-ns
spec:
  rules:
  - services: ["testapp"]
    methods: ["*"]
    constraints:
    - key: "destination.labels[version]"
      values: ["v1", "v2"]
---
apiVersion: "rbac.istio.io/v1alpha1"
kind: ServiceRoleBinding
metadata:
  name: external-api-caller
  namespace: test-ns
spec:
  subjects:
  - properties:
      source.namespace: "default"
  roleRef:
    kind: ServiceRole
    name: "external-api-caller"

我能够从所有名称空间访问该服务。我希望只能在“默认”名称空间中允许它

2 个答案:

答案 0 :(得分:0)

我不确定这是否可以用于特定的名称空间,但是它是否可以在标签上使用。

您可以在Istio中创建网络策略,这在Traffic Routing in Kubernetes via Istio and Envoy Proxy中有很好的解释。

...
  ingress:
  - from:
    - podSelector:
        matchLabels:
          zone: trusted
...

在该示例中,仅允许标签为zone: trusted的Pod建立到Pod的传入连接。

您可以阅读有关Using Network Policy with Istio的信息。

我还建议您同时阅读Security Concepts in IstioDenials and White/Black Listing

希望这对您有所帮助。

答案 1 :(得分:0)

使用k8s网络策略:是可以的。问题中发布的示例不允许使用其他名称空间。在Ingress规则中,您必须使用名称空间选择器,该选择器将用于指定您要允许其通信的名称空间。在下面的示例中,标签为'ns-group:prod-ns'的命名空间将被允许访问端口80和协议TCP上标签为'app:testapp'的Pod。

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: test-app-ingress
  namespace: test-ns
spec:
  podSelector:
    matchLabels:
      app: testapp
  ingress:
  - ports:
    - protocol: TCP
      port: 80
    from:
    - namespaceSelector:
        matchLabels:
          ns-group: prod-ns

使用Istio White上市政策:您可以通过white listing policy examplesattribute vocabulary

下面是示例

apiVersion: config.istio.io/v1alpha2
kind: handler
metadata:
  name: whitelist-namespace
spec:
  compiledAdapter: listchecker
  params:
    overrides: ["prod-ns"]
    blacklist: false
---

apiVersion: config.istio.io/v1alpha2
kind: instance
metadata:
  name: source-ns-instance
spec:
  compiledTemplate: listentry
  params:
    value: source.namespace
---

apiVersion: config.istio.io/v1alpha2
kind: rule
metadata:
  name: rule-policy-1
spec:
  match: destination.labels["app"] == "testapp" && destination.namespace == "test-ns"
  actions:
  - handler: whitelist-namespace
    instances: [ source-ns-instance ]
相关问题