Search code examples
kubernetesgoogle-kubernetes-engineload-balancingip-address

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?

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.

My Kubernetes cluster runs on GKE.


Solution

  • You can use loadBalancerSourceRanges to filter load balanced traffic as mentioned here.

    Here is the simple example of Service in front of Nginx Ingress controllers:

    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