Kubernetes Commands Alias

It’s very tedious to type full command when you operate on K8s cluster, this blog is the summary of alias I adopt in daily work.

Put these alias to $HOME/.bashrc or $HOME/.zshrc, then sourcing it or next time when you login they will take effect.

Some quick commands used to create resources:

1
2
3
4
5
6
7
8
9
10
11
12
13
## create nodeport svc to expose access
## --type default is ClusterIP
## --target-port default is --port
kubectl expose pod <podname> [--type=NodePort] --port=80 [--target-port=80] --name=<svcname>

## run command in pod
kubectl exec -n <namespace> <podname> -- sh -c "commands"

## check log
kubectl logs -f <pod name>

## scale up/down
kubectl scale deploy <deploy name> --replicas=5

These are some aliases used to run test:

1
2
3
4
## exit will delete the pod automatically
## --restart=Never: create a pod instead of deployment
## praqma/network-multitool: network tools image
alias kbt='kubectl run testpod -it --rm --restart=Never --image=praqma/network-multitool -- /bin/sh'

很多都是当时在IBM 时候的命令了,留着做个纪念吧.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
## docker shortcut
alias di='docker images'
alias dp='docker ps -a'
alias dri='docker rmi -f'
alias drp='docker rm -f'

alias k='kubectl'
alias kbn='kubectl get nodes'
## can replace with your working namespace
## pods
alias kbp='kubectl get pods --all-namespaces'
## deployments
alias kbd='kubectl get deploy -n zen | grep -E "xmeta|services"'
## statefulsets
alias kbsts='kubectl get sts -n zen | grep -E "conductor|compute"'
## services
alias kbs='kubectl get svc -n test-1'


## get into pods
kbl()
{
pod=$1
## get namepace
ns=$(kubectl get pod --all-namespaces | grep $pod | awk {'print $1'})
kubectl exec -it $pod sh -n $ns
}
### for fixed pod name
alias gocond='kubectl exec -it is-en-conductor-0 bash -n test-1'
alias gocomp0='kubectl exec -it is-engine-compute-0 bash -n test-1'
### for dynamic pod name
goxmeta()
{
isxmetadockerpod=`kubectl get pods --field-selector=status.phase=Running -n test-1 | grep is-xmetadocker-pod | awk {'print $1'}`
kubectl exec -it ${isxmetadockerpod} bash -n test-1
}

gosvc()
{
isservicesdockerpod=`kubectl get pods --field-selector=status.phase=Running -n test-1 | grep is-servicesdocker-pod | awk {'print $1'}`
kubectl exec -it ${isservicesdockerpod} bash -n test-1
}

## clean pods
alias rmxmeta='kubectl delete svc is-xmetadocker -n test-1; kubectl delete deploy is-xmetadocker-pod -n test-1; rm -rf /mnt/IIS_test-1/Repository/*'
alias rmsvc='kubectl delete svc is-servicesdocker -n test-1; kubectl delete deploy is-servicesdocker-pod -n test-1; rm -rf /mnt/IIS_test-1/Services/*'
alias rmcond='kubectl delete svc is-en-conductor-0 -n test-1; kubectl delete svc en-cond -n test-1; kubectl delete statefulset is-en-conductor -n test-1; rm -rf /mnt/IIS_test-1/Engine/test-1/is-en-conductor-0/'
alias rmcomp='kbc delete svc conductor-0 -n test-1; kbc delete statefulset is-engine-compute -n test-1; rm -rf /mnt/IIS_test-1/Engine/test-1/is-engine-compute*'
0%