how to stop/pause a pod in kubernetes

170,904

Solution 1

So, like others have pointed out, Kubernetes doesn't support stop/pause of current state of pod and resume when needed. However, you can still achieve it by having no working deployments which is setting number of replicas to 0.

kubectl scale --replicas=0 deployment/<your-deployment>

see the help

# Set a new size for a Deployment, ReplicaSet, Replication Controller, or StatefulSet.
kubectl scale --help

Scale also allows users to specify one or more preconditions for the scale action.

If --current-replicas or --resource-version is specified, it is validated before the scale is attempted, and it is guaranteed that the precondition holds true when the scale is sent to the server.

Examples:

  # Scale a replicaset named 'foo' to 3.
  kubectl scale --replicas=3 rs/foo

  # Scale a resource identified by type and name specified in "foo.yaml" to 3.
  kubectl scale --replicas=3 -f foo.yaml

  # If the deployment named mysql's current size is 2, scale mysql to 3.
  kubectl scale --current-replicas=2 --replicas=3 deployment/mysql

  # Scale multiple replication controllers.
  kubectl scale --replicas=5 rc/foo rc/bar rc/baz

  # Scale statefulset named 'web' to 3.
  kubectl scale --replicas=3 statefulset/web

Solution 2

No. It is not possible to stop a pod and resume later when required. However, You can consider the below approach.

In k8s, pods are abstracted using a service. One way I can think of isolating the pod(s) is by updating the pod selector in the service definition. That way you can control the traffic to pod(s) using service definition. Whenever you want to restore the traffic update the pod selector value back to what it was in the service definition.

Solution 3

With Kubernets, it's not possible to stop/pause a Pod. However, you can delete a Pod, given the fact you have the manifest to bring that back again.

If you want to delete a Pod, you can run the following kubectl command:

kubectl delete -n default pod <your-pod-name>
Share:
170,904

Related videos on Youtube

AATHITH RAJENDRAN
Author by

AATHITH RAJENDRAN

in a Legal Services providing company located in Capital of Tamil Nadu, India. Contact me any time on LinkedIn

Updated on July 08, 2022

Comments

  • AATHITH RAJENDRAN
    AATHITH RAJENDRAN almost 2 years

    I have a MySQL pod running in my cluster.
    I need to temporarily pause the pod from working without deleting it, something similar to docker where the docker stop container-id cmd will stop the container not delete the container.
    Are there any commands available in kubernetes to pause/stop a pod?

    • AATHITH RAJENDRAN
      AATHITH RAJENDRAN about 3 years
      This question is what lands people here and get their answers, but Question received only less than half the upvotes as the accepted answer 🤔
  • Martin Kosicky
    Martin Kosicky almost 5 years
    too bad it doesnt help pods whose source is kafka/eventhub (and i wish to pause them, launch new pods and if it fails resume the old ones)
  • karlos9o
    karlos9o over 4 years
    Hi @MartinKosicky , that's exactly my use case, I have a container consuming from eventhubs trhough kafka protocol. did you find a solution? The only tworkarrounds that cames to my mind are ugly (change credentials, multiprocesses inside a container instead multiple containers in a pod, etc).
  • Martin Kosicky
    Martin Kosicky over 4 years
    @karlos9o we actually just deleted the old pods, since its eventhub/kafka sourced, the zero downtime is not so important here. But if you really want it you can kubectl apply and change some configuration (pause processing). That should trigger redeploy of the pods
  • Philippe Simo
    Philippe Simo almost 4 years
    Thanks for the tip, i think <your-pod> should be <your-deployment-name>
  • nroose
    nroose almost 4 years
    As I understand it, if there's an hpa, this is not possible.
  • sulabh chaturvedi
    sulabh chaturvedi almost 4 years
    That's correct. That's the reason I started with a statement what all have suggested and the answer is mostly in context of the question
  • AATHITH RAJENDRAN
    AATHITH RAJENDRAN over 2 years
    The question is not about deleting a pod.
  • iamnicoj
    iamnicoj over 2 years
    Services are not meant to be abstractions of pods. Services are just a way to route different types of network traffic and port mapping. Scaling the number of replicas of a Deployments, and specifically the replica set to 0 (if there is no HPA present) would actually delete all instances of the pod
  • SanjoS30
    SanjoS30 over 2 years
    I think this is a valid response. There's nothing like "stopping" a pod.
  • robotic_chaos
    robotic_chaos over 2 years
    Actually, this is the right way to stop Deployments. This option also works for StatefulSets but won't for DaemonSets. DaemonSets need to be deleted and created again.
  • Jon Watte
    Jon Watte over 2 years
    The pod will immediately come back again because the replicaset will re-create it -- that's its job. So this doesn't actually answer the question.