Restart Kubernetes Pod several ways

Foreword

In use docker in the process, we can use docker restart {container_id} to restart the container, but did not restart command in kubernetes in (no kubectl restart {podname}), sometimes we appear Bug Pod terminated unexpectedly, leads us need to restart Pod, but not a good way, especially in the absence of yaml case file, so I summarized the following several ways to restart the Pod.

method 1

The latest yaml file.

In the case where there yaml document can be used directly kubectl replace --force -f xxxx.yaml to forcibly replace the API objects Pod, to achieve the purpose of the restart. As shown below:

[root@test-129-70 viua]# kubectl replace --force -f viua.yml
namespace "viua" deleted
service "viua-app-cms" deleted
deployment.apps "viua-app-cms" deleted
service "viua-app-command" deleted
deployment.apps "viua-app-command" deleted
service "viua-show-service" deleted
deployment.apps "viua-show-service" deleted
service "viua-skills-service" deleted
deployment.apps "viua-skills-service" deleted
namespace/viua replaced
secret/xa-harbor-ca replaced
service/viua-app-cms replaced
deployment.apps/viua-app-cms replaced
service/viua-app-command replaced
deployment.apps/viua-app-command replaced
service/viua-show-service replaced
deployment.apps/viua-show-service replaced
service/viua-skills-service replaced
deployment.apps/viua-skills-service replaced
Method 2

No yaml file, but using the Deployment objects.

kubectl scale deploy viua-app-cms --replicas=0 -n viua
kubectl scale deploy {deploy对象} --replicas=0 -n {namespace}


[root@test-129-70 pvd]# kubectl get deploy -n viua
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
viua-app-cms          1/1     1            1           48m
viua-app-command      1/1     1            1           48m
viua-show-service     1/1     1            1           48m
viua-skills-service   1/1     1            1           48m
[root@test-129-70 pvd]# kubectl scale deploy viua-app-cms --replicas=0 -n viua
deployment.apps/viua-app-cms scaled
[root@test-129-70 pvd]# kubectl get deploy -n viua
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
viua-app-cms          0/0     0            0           49m
viua-app-command      1/1     1            1           49m
viua-show-service     1/1     1            1           49m
viua-skills-service   1/1     1            1           49m
[root@test-129-70 pvd]# kubectl get po -n viua
NAME                                   READY   STATUS    RESTARTS   AGE
viua-app-command-95f7b6f7f-rb7mh       1/1     Running   0          49m
viua-show-service-85565b9dcf-ss8qp     1/1     Running   0          49m
viua-skills-service-65447f9b94-fhqhr   1/1     Running   0          49m

Because Pod Deployment objects are not the object of direct manipulation, but manipulation ReplicaSet object, and the object is ReplicaSet by the number of copies of the template definition and composition of the Pod. So this command are to scale the number of ReplicaSet to 0, and then scale to 1, then the Pod will restart.

Method 3

Yaml also did not file, but using the Deployment objects.

Use the command kubectl delete pod {podname} -n { namespace}
This method is very simple and crude, and directly to the Pod deleted because Kubernetes is declarative API, so then deleted, Pod API object is inconsistent with expectations, so will automatically re-create the Pod remain consistent with expectations, but if replicaSet management Pod many objects, then to manually remove one, will be very troublesome, it can be used kubectl delete replicaset {rs_name} -n { namespace} command to delete replicaSet

Method 4

No yaml file, Pod objects directly.

Use the command kubectl get pod {podname} -n {namespace} -o yaml | kubectl replace --force -f -

In this case, since there is no yaml file and start a Pod objects, it is not directly delete or scale to 0, but can be restarted by the above command. The meaning of this command is to get yaml statement pod of currently running, and redirect the output to the standard input pipe kubectl replace command, so as to achieve the purpose of restarting.

to sum up

We can restart the objects through a variety of ways, in general, the most recommended way is to use kubectl get pod {podname} -n {namespace} -o yaml | kubectl replace --force -f - this way, as applicable a variety of objects. In addition, the restart does not fix the bug Pod run the program, you want to solve the unexpected termination of the program, eventually have to fix bug.

Published 33 original articles · won praise 0 · Views 3909

Guess you like

Origin blog.csdn.net/erhaiou2008/article/details/104063100