简体   繁体   English

如何限制 IP 可以访问 kubernetes 服务的地址?

[英]How to limit IP Addresses that have access to kubernetes service?

Is there any way to limit the access to Kubernetes Service of type LoadBalancer from outside the cluster?有什么办法可以限制从集群外部访问 LoadBalancer 类型的 Kubernetes 服务?

I would like to expose my database's pod to the Internet using the LoadBalancer service that would be accessible only for my external IP address.我想使用 LoadBalancer 服务将我的数据库的 pod 公开到 Internet,该服务只能用于我的外部 IP 地址。

My Kubernetes cluster runs on GKE.我的 Kubernetes 集群在 GKE 上运行。

Yes, you can achieve that on Kubernetes level with a native Kubernetes Network Policy .是的,您可以使用本机 Kubernetes Network Policy在 Kubernetes 级别上实现这一点。 There you can limit the Ingress traffic to your Kubernetes Service by specifying policies for the Ingress type.在那里,您可以通过为Ingress类型指定策略来将 Ingress 流量限制到您的 Kubernetes 服务。 An example could be:一个例子可能是:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: test-network-policy
  namespace: default
spec:
  podSelector:
    matchLabels:
      role: db
  policyTypes:
  - Ingress
  ingress:
  - from:
    - ipBlock:
        cidr: 172.17.0.0/16
        except:
        - 172.17.1.0/24
    ports:
    - protocol: TCP
      port: 6379

More information can be found in the official documentation .更多信息可以在官方文档中找到。

If you already want to block traffic from unwanted IP addresses on Load Balancer level, you have to define firewall rules and apply them on your GCP load balancer.如果您已经想在负载均衡器级别阻止来自不需要的 IP 地址的流量,则必须定义防火墙规则并将它们应用于您的 GCP 负载均衡器。 More information regarding the GCP firewall rules can also be found in the documentation .有关 GCP 防火墙规则的更多信息也可以在文档中找到。

You can use loadBalancerSourceRanges to filter load balanced traffic as mentioned here .您可以使用loadBalancerSourceRanges来过滤负载平衡的流量,如此所述。

Here is the simple example of Service in front of Nginx Ingress controllers:下面是 Nginx Ingress 控制器前面的 Service 的简单示例:

apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: external
    app.kubernetes.io/name: ingress-nginx
  name: external-ingress-nginx-controller
  namespace: kube-ingress
spec:
  loadBalancerSourceRanges:
  - <YOUR_IP_1>
  - <YOUR_IP_2>
  - <YOUR_IP_3>
  ports:
  - name: https
    nodePort: 32293
    port: 443
    targetPort: https
  selector:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: external
    app.kubernetes.io/name: ingress-nginx
  type: LoadBalancer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM