关于istio出口流量的细化政策

时间:2019-09-06 15:34:41

标签: kubernetes load-balancing istio mesh-network

我有安装了Istio的kubernetes集群。我有两个Pod,例如sleep1和sleep2(装有curl的容器)。我想将istio配置为允许从sleep1到www.google.com的流量,并禁止从sleep2到www.google.com的流量。

因此,我创建了ServiceEntry:

---
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
  name: google
spec:
  hosts: 
  - www.google.com
  - google.com
  ports: 
  - name: http-port
    protocol: HTTP
    number: 80
  resolution: DNS

网关

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: istio-egressgateway
spec:
  selector:
    istio: egressgateway
  servers:
  - port:
      number: 80
      name: http-port
      protocol: HTTP
    hosts:
    - "*"

两个virtualServices(网格->出口,出口-> google)

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: mesh-to-egress
spec:
  hosts: 
  - www.google.com
  - google.com
  gateways:
  - mesh
  http:
  - match:
    - gateways:
      - mesh
      port: 80
    route:
    - destination:
        host: istio-egressgateway.istio-system.svc.cluster.local
        port:
          number: 80
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: egress-to-google-int
spec:
  hosts: 
  - www.google.com
  - google.com
  gateways:
  - istio-egressgateway
  http:
  - match: 
    - gateways:
      - istio-egressgateway
      port: 80
    route:
    - destination:
        host: google.com
        port:
          number: 80
      weight: 100

结果,我可以从两个吊舱中卷曲Google。

再问一个问题:我可以允许从sleep1到www.google.com的流量,并禁止从sleep2到www.google.com的流量吗?我知道这可能与kubernetes NetworkPolicy和黑白名单(https://istio.io/docs/tasks/policy-enforcement/denial-and-list/)有关,但是两种方法都禁止(允许)特定ip的流量,或者我错过了什么?

2 个答案:

答案 0 :(得分:3)

您可以为sleep1sleep2创建不同的服务帐户。然后,您create an RBAC policy限制对istio-egressgateway策略的访问,因此sleep2将无法通过出口网关访问任何出口流量。这应该可以禁止来自集群的,不是源自出口网关的任何出口流量。参见https://istio.io/docs/tasks/traffic-management/egress/egress-gateway/#additional-security-considerations

如果要允许sleep2访问其他服务,但不允许www.google.com,则可以使用Mixer规则和处理程序,请参见this blog post。它显示了如何允许到特定服务帐户的特定URL路径。

答案 1 :(得分:2)

我认为您在拒绝选项方面可能处于正确的轨道。 它也不限于IP,因为我们可能会看到Simple DenialAttribute-based Denial

的基于属性的示例

例如,如果我们为Sleep2写一个简单的拒绝规则-> www.google.com:

apiVersion: "config.istio.io/v1alpha2"
kind: handler
metadata:
  name: denySleep2Google
spec:
  compiledAdapter: denier
  params:
    status:
      code: 7
      message: Not allowed
---
apiVersion: "config.istio.io/v1alpha2"
kind: instance
metadata:
  name: denySleep2GoogleRequest
spec:
  compiledTemplate: checknothing
---
apiVersion: "config.istio.io/v1alpha2"
kind: rule
metadata:
  name: denySleep2
spec:
  match: destination.service.host == "www.google.com" && source.labels["app"]=="sleep2"
  actions:
  - handler: denySleep2Google
    instances: [ denySleep2GoogleRequest ]

请检查是否有帮助。 同样,“规则”条目中的“匹配”字段基于属性周围的istio表达式语言。在this doc中可以找到一些词汇。