Search code examples
kubernetesnetwork-programminggoogle-kubernetes-engineload-balancingkubernetes-ingress

Kubernetes open port to server on same subnet


I am launching a service in a Kubernetes pod that I would like to be available to servers on the same subnet only.

I have created a service with a LoadBalancer opening the desired ports. I can connect to these ports through other pods on the cluster, but I cannot connect from virtual machines I have running on the same subnet.

So far my best solution has been to assign a loadBalancerIP and restrict it with loadBalancerSourceRanges, however this still feels too public.

The virtual machines I am attempting to connect to my service are ephemeral, and have a wide range of public IPs assigned, so my loadBalancerSourceRanges feels too broad.

My understanding was that I could connect to the internal LoadBalancer cluster-ip from servers that were on that same subnet, however this does not seem to be the case.

Is there another solution to limit this service to connections from internal IPs that I am missing?

This is all running on GKE.

Any help would be really appreciated.


Solution

  • i think you are right here a little bit but not sure why you mentioned the cluster-ip

    My understanding was that I could connect to the internal LoadBalancer cluster-ip from servers that were on that same subnet, however this does not seem to be the case.

    Now if you have deployment running on GKE and you have exposed it with service type LoadBalancer and have internal LB you will be able to access to internal LB across same VPC.

    apiVersion: v1
    kind: Service
    metadata:
      name: internal-svc
      annotations:
        networking.gke.io/load-balancer-type: "Internal"
    spec:
      type: LoadBalancer
      externalTrafficPolicy: Cluster
      selector:
        app: internal-svcinternal-svc
      ports:
      - name: tcp-port
        protocol: TCP
        port: 8080
        targetPort: 8080
    

    once your changes are applied check the status using

    kubectl get service internal-svc --output yaml
    

    In YAML output check at last section for

    status:
      loadBalancer:
        ingress:
        - ip: 10.127.40.241
    

    that's your actual IP you can use to connect with service from other VMs in subnet.

    Doc ref