Namespaces
List all resources within a namespace
NS=some-namespace
kubectl api-resources --verbs=list --namespaced -o name \
| xargs -n 1 kubectl get --show-kind --ignore-not-found -n $NS
List all resources within a namespace
NS=some-namespace
kubectl api-resources --verbs=list --namespaced -o name \
| xargs -n 1 kubectl get --show-kind --ignore-not-found -n $NS
#!/usr/bin/python | |
from __future__ import print_function | |
import sys | |
def usage(argv): | |
print('Usage: %s [commands][resources][options][parameters]' % argv[0], file=sys.stderr) | |
print('\nCommands:', file=sys.stderr) | |
for s, l in commands: | |
print(' %s (%s)' % (s, l), file=sys.stderr) |
#!/bin/bash | |
if [ $# -lt 3 ]; then | |
echo "Usage: $0 pod-name-suffix source-pvc-name destination-pvc-name [rsync options...]" | |
exit 1 | |
fi | |
POD_SUFFIX=${1} | |
PVC_FROM=${2} | |
PVC_TO=${3} |
Let's look at some basic kubectl output options.
Our intention is to list nodes (with their AWS InstanceId) and Pods (sorted by node).
We can start with:
kubectl get no
This example shows how to read options and positional arguments from a bash script (same principle can be applied for other shells).
# some global var we want to overwrite with options
force=false
help=false
log=info
ARGS=() ### this array holds any positional arguments, i.e., arguments not started with dash
while [ $# -gt 0 ]; do
## Ever had to add something to your shell's config files (i.e. .bashrc) | |
## and open a new shell? Well, that may be fine, but you can achieve the | |
## same result, plus without having to open a new window/tab | |
## or execute a child process. | |
## | |
## Idea taken from https://learn.hashicorp.com/vault/getting-started/install | |
## lets check if any command `hello` exists | |
$ hello | |
bash: hello: command not found |
$ function spread_parameter { | |
local p="$1" | |
shift | |
local IFS="," | |
eval "echo $(echo "$p\ {$*}")" | |
} | |
$ spread_parameter --include '*.jpg' '*.gif' '*.svg' | |
--include *.jpg --include *.gif --include *.svg |
# Switch kubernetes namespaces withou touching ~/.kube/config | |
# Example: | |
# | |
# $ on kube-system | |
# NAME STATUS AGE | |
# kube-system Active 1y | |
# $ k get pod | |
# | |
on() |
#!/usr/bin/env python | |
## If you see messages like this on your kubelet journal: | |
## | |
## Mar 25 22:36:44 ip-10-0-3-67.ec2.internal dockerd-current[28612]: time="2018-03-25T22:36:44.419126265Z" level=error msg="Handler for GET /v1.24/containers/60532fa8184bdf41e52788194faa1253f1168e3ad4f54f7c159192fe66c4bb1d/json returned error: No such container: 60532fa8184bdf41e52788194faa1253f1168e3ad4f54f7c159192fe66c4bb1d" | |
## | |
## use this script to remove dockershim container files from /var/lib/dockershim/sandbox | |
import os, glob, subprocess |
#!/bin/bash | |
set -eu | |
NAMESPACE=$1 | |
PVCNAME=$2 | |
TARGETZONE=$3 | |
DEPLOYMENTOBJ=$4 | |
PVNAME=$(oc -n $NAMESPACE get pvc $PVCNAME --template={{.spec.volumeName}}) | |
VOLUMEID=$(oc -n $NAMESPACE get pv $PVNAME --template={{.spec.awsElasticBlockStore.volumeID}} | cut -d/ -f 4) |