On the road again

In this note I will collect useful K8s commands with descriptions.

List all non-master nodes:
$kubectl get nodes --selector='!node-role.kubernetes.io/master'
Find pod by label:
kubectl get pods -l application=mariadb,component=ingress
"Live" tail of pod logs:
kubectl logs <pod_name> --follow
List only names of non-master nodes:
$kubectl get nodes --selector='!node-role.kubernetes.io/master' -o=jsonpath='{.items[*].metadata.name}'

Bash function to re-schedule failed job. Invoke by running "job_rerun <namespace> <job_name> "

function job_rerun {
NS=$1
JOB=$2
if command -v jq; then
  JOB_JSON_ORIGINAL=$(mktemp --suffix=".json")
  kubectl get -n "$NS" jobs "$JOB" -o=json > "$JOB_JSON_ORIGINAL"
  JOB_JSON_RE_RUN=$(mktemp --suffix=".json")
  jq 'del(.status) | del(.metadata.creationTimestamp) | del(.metadata.labels."controller-uid") | del(.metadata.resourceVersion) | del(.metadata.selfLink) | del(.metadata.uid) | del(.spec.selector) | del(.spec.template.metadata.creationTimestamp) | del(.spec.template.metadata.labels."controller-uid" )' "$JOB_JSON_ORIGINAL" > "$JOB_JSON_RE_RUN"
  cat "$JOB_JSON_ORIGINAL" | kubectl delete -f -
  cat "$JOB_JSON_RE_RUN" | kubectl create -f -
else
  echo "JQ not installed"
fi
}

Get list of containers inside the given pod:

kubectl get pods [pod-name-here] -n [namespace] -o jsonpath='{.spec.containers[*].name}*'

Connect to specific container inside given pod:

kubectl exec -it vault-0 --container vault-unsealer -n openstack -- /bin/sh

Set default namespace:

kubectl config set-context --current --namespace=openstack
Add comment