How to restart a pod without a deployment in K8S?

Viewed 4890

I deployed an elasticsearch cluster on K8S using this command helm install elasticsearch elastic/elasticsearch.

I can see the pod is running:

$ kubectl get pods
NAME                             READY   STATUS    RESTARTS   AGE
elasticsearch-master-0           0/1     Running   0          4m30s
kibana-kibana-5697fc485b-qtzzl   0/1     Running   0          130m

The service looks good as well:

$ kubectl get services
NAME                            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)             AGE
elasticsearch-master            ClusterIP   10.105.59.248   <none>        9200/TCP,9300/TCP   4m50s
elasticsearch-master-headless   ClusterIP   None            <none>        9200/TCP,9300/TCP   4m50s
kibana-kibana                   ClusterIP   10.104.31.124   <none>        5601/TCP            6d7h
kubernetes                      ClusterIP   10.96.0.1       <none>        443/TCP             10d

But there is no deployment for the elasticsearch:

$ kubectl get deployments
NAME            READY   UP-TO-DATE   AVAILABLE   AGE
kibana-kibana   0/1     1            0           6d7h

I'd like to restart the elasticsearch pod and I have searched that people say to use kubectl scale deployment --replicas=0 to terminate the pod. But there is no deployment for the elasticsearch cluster, In this case, how can I restart the elasticsearch pod?

2 Answers

The elasticsearch-master-0 rise up with a statefulsets.apps resource in k8s.

statefulsets apps is like Deployment object but different in the naming for pod. You should delete the pod and the statefulsets recreate the pod.

    kubectl delete pods elasticsearch-master-0

I have a trick which may not be the right way but it works.
You can simply edit the running pod's configuration just for the sake of restarting it and then you can replace the older configuration.
Let me explain through an example:
Here I have a busybox pod running:

$ kubectl get pods
NAME    READY  STATUS   RESTARTS  AGE
busybox  1/1   Running     0      30s

Now, I'll try to edit the configuration of the running pod:

kubectl edit pod busybox

This command will open up the configuration data in a editable mode, and I'll simply go to the spec section and lets say I just update the image name as depicted below:
Earlier:

spec:
  containers:
  - command:
    - sleep 1000
    image: busybox
    imagePullPolicy: Always

After updating image name from busybox to busybox:latest :
Just enter i to enter into insert mode and make changes and then ESC and :wq same way as we use a vi/vim editor.

spec:
  containers:
  - command:
    - sleep 1000
    image: busybox:latest
    imagePullPolicy: Always

The output should be:

$ kubectl edit pod busybox
pod/busybox edited

Now to see the change you can just enter the following command see the Events:

$ kubectl describe pod busybox

In the events you can see: Container busybox definition changed, will be restarted

You can check the restart count:

$ kubectl get pods
 NAME    READY  STATUS   RESTARTS  AGE
busybox   1/1   Running     1      14m

You can see that the restart count is 1, you can now replace with the orginal image name by performing the same edit operation.
Hope that helps!

Related