AATHITH RAJENDRAN
AATHITH RAJENDRAN

Reputation: 5366

how to stop/pause a pod in kubernetes

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?

Upvotes: 180

Views: 350359

Answers (5)

P Ekambaram
P Ekambaram

Reputation: 17615

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 the service definition. Whenever you want to restore the traffic, update the pod selector value back to what it was in the service definition.

Upvotes: 14

Anjana Silva
Anjana Silva

Reputation: 9181

With Kubernetes, 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.

However, if you want to delete a pod, knowing that it will immediately be launched again by the cluster, run the following kubectl command.

kubectl delete -n default pod <your-pod-name>

Upvotes: 13

Noushad
Noushad

Reputation: 512

Stop a Single deployment

kubectl --namespace <ns> scale deployment my-deployment --replicas 0

Stop a statefulset

kubectl --namespace <ns> scale statefulset --replicas 0

Stop all the pods in a NS

kubectl --namespace <ns> scale deployment $(kubectl --namespace <ns> get deployment | awk '{print $1}') --replicas 0

To stop all Kubernetes stateful sets

kubectl --namespace <ns> scale statefulset --replicas 0 $(kubectl --namespace <ns> get statefulset  | awk '{print $1}')

Delete All Resources

kubectl delete all --all --namespace <ns>

Upvotes: 3

Riho
Riho

Reputation: 4593

For me it worked when I scaled the pods down to 0 in Helm's DeploymentConfig details in Openshift Console.

Upvotes: 2

sulabh chaturvedi
sulabh chaturvedi

Reputation: 3984

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

Upvotes: 317

Related Questions