Search code examples
kubernetesservicegoogle-kubernetes-enginekubectl

How to pause and then resume a Kubernetes service in Google cloud?


I have a service named Foo that is currently running. It directs traffic it receives to a running Pod as well. Since the service is of type LoadBalancer and runs in Google Cloud - it has it's own external IP.

I'm currently doing maintenance and testing on various services and would like to temporarily STOP service Foo from working, then RESUMING it again. That is, anyone that hits the IP for service Foo would get a 404, but then later on I resume it - they would start getting answers back.

The reason why I don't just flat out delete the service then create a new one is because I wish to maintain the original IP address for the Foo service. I have tests that directly reference that IP and do not wish to have to continuously change them. i also have a few clients in production relying on that IP so I can't risk losing it.

Any indication then on how to temporarily STOP / RESUME a kubernetes service in Google cloud, while preserving it's IP?

Thanks


Solution

  • Kubernetes itself does not have mechanism to stop a service.


    When you create a Service type of LoadBalancer in GKE, it automatically creates a forwarding rule for external access. You can disable that rule (not delete!) to stop external traffic accessing your Service.

    To disable the forwarding rule:

    • Check the associated IP address with a LoadBalancer by either:
      • issuing: $ kubectl get svc
      • going to: GCP Dashboard -> Kubernetes Engine -> Services & Ingress
    • Go to GCP Dashboard -> VPC Network -> External IP addresses
    • Find your LB's IP and copy name of the forwarding rule associated with it
    • Go to GCP Dashboard -> VPC Network -> Firewall
    • Search for mentioned forwarding rule
    • Edit it
    • On the bottom of edit site you should have an option to disable it like picture below:

    GCP1


    From a GKE perspective you can create a service type of LoadBalancer with a static IP address that will be bound and available to your project as long as it's not released. Even if you delete a Service in your GKE cluster it will still be available to bound to your recreated Service.

    You can do it by either:

    Reserving static IP address before Service creation

    • Go to GCP Dashboard -> VPC Network -> External IP addresses -> Reserve Static Address
    • Create a static IP
    • Note the IP address created
    • Create a Service type of LoadBalancer with previously created IP address. Example below:
    apiVersion: v1
    kind: Service
    metadata:
      name: hello-service-lb
    spec:
      selector:
        app: hello
      ports:
        - name: hello-port
          port: 80
          targetPort: 50001
          nodePort: 30051
      type: LoadBalancer
      loadBalancerIP: PASTE_HERE_IP_ADDRESS
    

    Please take a specific look on part:

      loadBalancerIP: PASTE_HERE_IP_ADDRESS
    

    as this line is required to have previously created static IP address.

    Deleting this Service will:

    • Delete a Service in GKE
    • Delete the association between Service and IP address in GCP Dashboard
    • It will not delete the reserved static IP address

    Creating a Service before reserving static IP address

    Assuming that you have already created a Service type of LoadBalancer you can:

    • Go to GCP Dashboard -> VPC Network -> External IP addresses
    • Found the IP address associated with your LoadBalancer
    • Change type of this IP address from: Ephemeral to Static. This will ensure that this IP will not be released when Service got deleted.
    • You will need to edit your Service definition when recreating it to include:
      loadBalancerIP: PASTE_HERE_IP_ADDRESS
    

    If you changed your IP address type from Ephemeral to Static, deleting your Service will not release your Static IP address.


    Please take a look on additional documentation:

    Please let me know if you have any questions in that.