rookie099
rookie099

Reputation: 2607

Force restart of a pod inside OpenShift

Can I use OpenShift Client oc for forcing restart of a pod. I'd like to see e.g. what details in oc describe $pod change, and in particular whether hostname as displayed inside the pod changes?

For instance, is killing the process started in a Dockerfiles ENTRYPOINT script (postgres -D $PGDATA in my case) from a shell executed inside the container (oc exec -it $pod bash) appropriate?

I am on an OpenShift (OpenShift Container Platform) 3.9 cluster w/o admin access rights.

Upvotes: 2

Views: 8096

Answers (3)

eljeko
eljeko

Reputation: 426

You could try with oc rollout command

Upvotes: 0

Cromax
Cromax

Reputation: 2042

AFAIK there's no way to simply restart the pod with something like oc restart pod mypod for example, but one can use scale subcommand to achieve that effect—first you scale your deployment to 0, then scale it back to previous number of replicas. As it is tedious to check manually for declared replica count, it seems useful to define short function for that purpose (and possibly place it in .bash_functions, if bash is your shell). Consider this:

function oc-rescale-deployment {
    local -r deployment="${1:?Usage: ${FUNCNAME[0]} <deployment>}";
    local -r target="deployment/${deployment}";
    #-- Find the number of declared replicas.
    local -ri replicas=$(oc get "${target}" \
      -o go-template='{{range $key, $val := .spec}}{{if eq $key "replicas"}}{{$val}}{{"\n"}}{{end}}{{end}}');

    oc scale --replicas=0 "${target}" \
    && sleep 0.5s \
    && oc scale --replicas=${replicas} "${target}" \
    && oc wait pod \
      --selector app.kubernetes.io/name="${deployment}" \
      --for=condition=Ready;
}

alias oc-restart='oc-rescale-deployment';

Whether you want to wait for the pod to be ready after it's rescaled is up to you, but I added it here, so you can chain the invocations, e.g.:

oc-restart myapp && curl http://myapp.in.the.cloud/hello

Upvotes: 0

rookie099
rookie099

Reputation: 2607

I've by now empirically confirmed that killing the container's "root" process (as described in the question) apparently serves the purpose. One can observe e.g. these effects from restarting a pod in this manner: (This assumes as single container inside the pod.)

  • oc get pod/$pod -o jsonpath='{$.status.startTime}: remains the same
  • oc exec -it $pod hostname: remains the same (equals pod ID)
  • oc get pod/$pod -o jsonpath='{$.status.containerStatuses[0].state.running.startedAt}': changes (increases)
  • oc get pod/$pod -o jsonpath='{$.status.containerStatuses[0].restartCount}': increments

Upvotes: 3

Related Questions