简体   繁体   中英

Force restart of a pod inside OpenShift

Can I use OpenShift Client oc for forcing restart of a pod. I'd like to see eg 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 Dockerfile s 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.

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 eg 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

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, eg:

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

您可以尝试使用oc rollout命令

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM