Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gonoph/0e2ee64704615ba78c2f0613ab3a8e05 to your computer and use it in GitHub Desktop.
Save gonoph/0e2ee64704615ba78c2f0613ab3a8e05 to your computer and use it in GitHub Desktop.
Basic Docker, Kubernetes, and OpenShift commands for management

Helpful commands for management in Docker, Kubernetes, and OpenShift

Clean up old podman images

cleanpodman()
{
    # clean up any containers that have Exited
    for i in $(podman ps -qa);
    do
        # this does it in parallel
        podman rm $i &
    done;
    # clean up any images that aren't tagged properly
    for i in $(podman images -qf dangling=true);
    do
        # this does it in parallel
        podman rmi $i &
    done;
    # wait for all those forks to finish
    wait
}

Clean up old docker images

clean ()
{
    # clean up any containers that have Exited
    for i in $(docker ps -qa);
    do
        # this does it in parallel
        docker rm $i &
    done;
    # clean up any images that aren't tagged properly
    for i in $(docker images -qf dangling=true);
    do
        # this does it in parallel
        docker rmi $i &
    done;
    # wait for all those forks to finish
    wait
}

Print the current docker container stats on the current node

dockerstats() {
  local TT=$(mktemp)
  local TTT=$(mktemp)
  # get list of containers
  docker ps | grep -v "k8s_POD." > $TT &&
  # calculate length of header row
  MAX=`cat $TT | head -n 1 | wc -c` &&
  # calculate length of NAMES column
  MIN=`echo NAMES | wc -c`
  # how much to cut
  CUT=$[ $MAX - $MIN ] &&
  # cut everything out but the names of the containers
  cut -b ${CUT}- $TT | tail -n +2 > $TTT &&
  # only show the stats using the names
  docker stats `cat $TTT`
  # clean up
  rm -f $TT $TT
}

Get OCP stats for all pods

ocpodstats ()
{
  # this syntax is fixed in OCP 3.7
  # list all the pod stats (replace "oc adm" with "kubectl" for the raw kube command)
  oc adm top pod --heapster-namespace=openshift-infra --heapster-scheme=https --all-namespaces |
  # KLUDGE the NAME row so it's sorted at the top
  sed 's/^NA/0A/' | sort | sed 's/^0A/NA/'
}

Get OCP stats for all nodes

ocnodestats ()
{
  # this syntax is fixed in OCP 3.7
  # list all the pod stats (replace "oc adm" with "kubectl" for the raw kube command)
  oc adm top node --heapster-namespace=openshift-infra --heapster-scheme=https
}

Watch OCP stats using the two oc*stats functions above

ocstats ()
{
  # declare the bash functions inside the watch command as it starts in a different shell
  watch -n 3 "$(declare -f ocnodestats)" \; "$(declare -f ocpodstats)" \; ocnodestats \; ocpodstats
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment