Bootstrap FreeKB - OpenShift - Restart Pod using the REST API
OpenShift - Restart Pod using the REST API

Updated:   |  OpenShift articles

If you are not familiar with the oc command, refer to OpenShift - Getting Started with the oc command.

A node contains one or more pods, and each pod contains one or more containers.

 

Before deleting a pod, you may want to use the REST API to determine how many replica's the deployment has. Here is how you would list the replicas in the "foo" namespace using the curl REST API. The oc config view or oc get apiserver commands can be used to display the API Server URL (api.openshift.example.com in this example).

curl
--insecure
--request GET
--header "Accept: application/json"
--header "Authorization: Bearer sha256~0Rs__hPuXmBD3TJTXNDisC7wRBN-nrFnYTxgdBrFT-U"
--url "https://api.openshift.example.com:6443/apis/apps/v1/namespaces/foo/replicasets"

 

If the items array contains key value pairs, this means the namespace contains one or more pods. Notice in this example that the helloworld deployment has 3 replicas.

In this scenario, if the pod is deleted, 3 new pods will be created, since replicas is 3. If you want to delete the pod, the deployment replicas will need to be set to 0.

{
  "kind": "ReplicaSetList",
  "apiVersion": "apps/v1",
  "metadata": {
    "selfLink": "/apis/apps/v1/namespaces/foo/pods",
    "resourceVersion": "92368763"
  },
  "items": [
    {
      "metadata": {
        "name": "helloworld-3-2-b89cd49c8-rvg77",
        "annotations": {
          "deployment.kubernetes.io/desired-replicas": "3",
          "deployment.kubernetes.io/max-replicas": "4",
          "deployment.kubernetes.io/revision": "5"
        },
      "spec": {
        "replicas": 3,
        "selector": {
          "matchLabels": {
            "app": "helloworld"
          }
        }
      }
    }
  ]
}

 

You may also want to use the REST API to ensure the pod exists before attempting to delete the pod.

curl
--insecure
--request GET
--header "Accept: application/json"
--header "Authorization: Bearer sha256~0Rs__hPuXmBD3TJTXNDisC7wRBN-nrFnYTxgdBrFT-U"
--url "https://api.openshift.example.com:6443/api/v1/namespaces/foo/pods/example-pod"

 

If something like this is returned, this means that there are no pod named "example-pod" in the "foo" namespace.

{
  "kind": "PodList",
  "apiVersion": "v1",
  "metadata": {
    "selfLink": "/api/v1/namespaces/foo/pods",
    "resourceVersion": "92373341"
  },
  "items": []
}

 

Here is how you would delete the pods using the REST API.

curl
--insecure
--request DELETE
--header "Accept: application/json"
--header "Authorization: Bearer sha256~0Rs__hPuXmBD3TJTXNDisC7wRBN-nrFnYTxgdBrFT-U"
--url "https://api.openshift.example.com:6443/api/v1/namespaces/foo/pods/pod_name"

:

If the pod being deleted does not exist, something like this should be returned.

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {
    
  },
  "status": "Failure",
  "message": "pods \"bogus\" not found",
  "reason": "NotFound",
  "details": {
    "name": "bogus",
    "kind": "pods"
  },
  "code": 404
}

 

On the other hand, if the pod exists and was successfully deleted, a large amount of data is returned. Probably too much data. It may be best to include the --write-out --silent and --output /dev/null options.

Now, 200 should be return if the delete is successful and 404 should be returned if the delete fails.

curl
--insecure
--request DELETE
--header "Accept: application/json"
--header "Authorization: Bearer sha256~0Rs__hPuXmBD3TJTXNDisC7wRBN-nrFnYTxgdBrFT-U"
--url "https://api.openshift.example.com:6443/api/v1/namespaces/foo/pods/pod_name"
--write-out "%{http_code}"
--silent
--output /dev/null

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter b650b9 in the box below so that we can be sure you are a human.