如何限制访问Kubernetes服务?

12

我正在尝试使用以下yaml创建服务。正如您所看到的,我正在尝试限制来自10.0.0.0/8范围的服务访问。

apiVersion: v1
kind: Service
metadata: 
  name: nginx-service
spec: 
  ports:
    # the port that this service should serve on
    - port: 443
      targetPort: 443
  # label keys and values that must match in order to receive traffic for this service
  selector: 
    name: nginx
  type: LoadBalancer
  loadBalancerSourceRanges:
  - 10.0.0.0/8

有几个 Kubernetes 文档(如下所列)讨论如何使用 loadBalancerSourceRanges 注释来控制服务访问。

http://kubernetes.io/docs/user-guide/services-firewalls/

然而,当我尝试创建此服务时,我遇到了以下错误:

error validating "sdp-cluster.yaml": error validating data: found invalid field loadBalancerSourceRanges for v1.ServiceSpec; if you choose to ignore these errors, turn validation off with --validate=false

我查看了 v1.ServiceSpec,也没有找到它。

我错过了什么?我该如何限制 Kubernetes 中服务的流量?

4个回答

14

现在GCE、GKE和AWS都支持此功能。如果服务提供商不支持它,它将被忽略。Kubernetes文档

apiVersion: v1
kind: Service
metadata:
    name: myapp
spec:
    ports:
    - port: 8765
        targetPort: 9376
    selector:
    app: example
    type: LoadBalancer
    loadBalancerSourceRanges:
    - 10.0.0.0/8

4

loadBalancerSourceRanges字段在1.3版本中被提升为一个字段。它一直是一个叫做https://github.com/kubernetes/kubernetes/blob/master/pkg/api/service/annotations.go#L27的注释(自1.2版以来),现在已被弃用,因为我们将其提升为一个字段。

请注意,这个注释/字段是一个白名单,但它只适用于支持的云提供商。如果您把它设置为10.0/8,您只能从kube集群内部访问端点(即负载均衡器ip的行为类似于clusterIP)。即使是同一云提供商中的集群外节点也必须进行NAT才能击中公共IP,这意味着数据包上的源IP不是10点,所以它不能通过防火墙。您可以将其设置为公共IP,只有该客户端才能访问您的服务。


感谢Prashanth B。在发布问题之前,我已经阅读了您发布的内容。然而,在1.2版本中,没有具体的说明或代码示例来正确解决它。所以我猜测了一段时间。后来我找到了解决方法。 - Sanjeet

3

如果有人遇到同样的问题,我发现一个小的补充。看起来Google Container Engine已经升级到Kubernetes 1.3,这意味着它不会再验证新的loadBalancerSourceRanges语法是否有效,但似乎它实际上并不支持新的语法,这意味着该字段将被忽略。目前仍然需要设置注释,否则负载均衡器将变为公开可用状态。


3
发现问题并进行了修复。版本1.2不支持字段“loadBalancerSourceRanges”,但是支持它作为注释。因此,将我的yaml文件按照以下方式进行了修正,并且修复后运行良好。
apiVersion: v1
kind: Service
metadata: 
  name: nginx-service
  annotations: 
    service.beta.kubernetes.io/load-balancer-source-ranges: "a.b.c.d/8, x.y.0.0/24"
spec: 
  ports:
    # the port that this service should serve on
    - port: 443
      targetPort: 443
  # label keys and values that must match in order to receive traffic for this service
  selector: 
    name: nginx
  type: LoadBalancer

目前只能限制为0.0.0.0/0。该示例无法工作。请参见代码:https://github.com/kubernetes/kubernetes/blob/f4738ff575ae625914e8dfb93f5af04dea22b1d7/pkg/cloudprovider/providers/aws/aws.go#L73 - user2707671

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接