Skip to the content.

Sugar for kubectl - Some aliases to make life easier

I will only pick a very limited selection of the shortcuts which I need to use most frequenlty and the shortcuts are flexible enough.

Share some small command line tools/tricks in using kubectl.

oh-my-zsh plugin for kubectl:

If you use Zsh and oh-my-zsh, this plugin is very helpful.

Just add kubectl to the plugins= line in your ~/.zshrc. If you do not use Zsh, I think some alternatives could be found or you could just borrow the aliases and functions definitions to your own bash rc file.

The most useful part is alias k=kubectl. It makes you be able to type only one single character to use kubectl. Another one is kca, which apply the following command to all-namespaces.

% kca get svc
NAMESPACE     NAME    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)             AGE
analytics     pod0    ClusterIP   172.20.xxx.xx    <none>        80/TCP              262d
analytics     pod1    ClusterIP   172.20.xxx.xx    <none>        80/TCP              4y190d
cache         pod2    ClusterIP   172.20.xxx.xx    <none>        6379/TCP            2y333d
cache         pod3    ClusterIP   None             <none>        6379/TCP,16379/TCP  2y333d

If you have colorizers like jq, fx, yh installed, you could use shortcuts kj, ky to run kubectl commands and show the output in the corresponding format with colorized.

output_with_jq

output_with_yh

This oh-my-zsh plugin provids the shortcuts of almost all kubectl commands (and subcommands). If you are a full-time dev-ops person and need to deal with many different kubectl commands everyday, the shortcuts would be super valuable. But for me, I will only pick a very limited selection of the shortcuts which I need to use most frequenlty and the shortcuts are flexible enough.

A selection of aliases:

Personally I don’t like to use alias like kgp=kubectl get pods, which lacks the flexibility thus I need to memorize many aliases. Here is the list of alias I use:

  • kg=”k get”
  • kd=”k describe”
  • kgy=”k get -o yaml”
  • kgw=”k get -o wide”

    I like to set alias to the level to kubectl command (get and describe). So I could type it with 2 letters and combine it with the remaining parts of the command.

    Use kg to get a list of the resources of one type; Use kgw to list the resources with wider information; Use kgy or kd to show the details of one certain resource.

    % kgw -n kube-system pod
    NAME          READY   STATUS    RESTARTS AGE   IP          NODE                        NOMINATED NODE   READINE
    SS GATES
    aws-node-xxx  1/1     Running   0        33d   10.0.xx.xx  ip-10-0-xx-xx.ec2.internal  <none>           <none>
    aws-node-ooo  1/1     Running   0        2d7h  10.0.xx.xx  ip-10-0-xx-xx.ec2.internal  <none>           <none>
    aws-node-yyy  1/1     Running   0        33d   10.0.xx.xx  ip-10-0-xx-xx.ec2.internal  <none>           <none>
    
    % kgy -n glow pod/my-pod-aaaaaaa-66666
    apiVersion: v1
    kind: Pod
    metadata:
      annotations:
        cliRestartAt: "1565324980"
        cluster-autoscaler.kubernetes.io/safe-to-evict: "true"
        fluentbit.io/exclude: "true"
        kubectl.kubernetes.io/restartedAt: "2023-10-10T21:51:25+08:00"
        kubernetes.io/psp: eks.privileged
      creationTimestamp: "2023-11-11T02:35:30Z"
      generateName: zoe-www-aaaaaaa-
      labels:
        app.kubernetes.io/instance: my-pod
        app.kubernetes.io/name: my-pod
    ... ...
    
  • kt=”k taint node”

    I need to use taint frequently to manipulate nodes in k8s cluster when do cluster upgrading.

  • ktop=”k top”

    To use k top to check CPU/memory usage of nodes and pods is also common for me.

  • kl=”k logs”

    This alias is provided by the oh-my-zsh plugin, as well as some related shortcuts which are useful as well, like klf(k logs -f), kl1m(k logs –since 1m).

Search with (partial) pod name:

function kfindpod {
    name=$1
    shift
    kg --all-namespaces pod $@ | grep $name
}

Sometimes the namespace is forgotten but only part of the pod’s name is remembered, this function can list all pods containing the name. e.g. once I just cannot recall the namespace of our aws load balancer controller, so I use this function to retrieve:

aws:(prod-eks) k8s:(prod-eks)  % kfindpod lb
ingress  aws-lb-controller-xxx-yyy  1/1  Running     0  34d
ingress  aws-lb-controller-xxx-yyy  1/1  Running     0  34d

No-Schedule node tainting:

function ktnos {
    for n in $*
    do
        k taint node $n eks-node-rolling:NoSchedule
    done
}
function ktnos- {
    for n in $*
    do
        k taint node $n eks-node-rolling:NoSchedule-
    done
}

For me I need to do tainting and untainting on multiple nodes in k8s cluster very frequently when do k8s upgrade, so I have these 2 function to make it easier. Notice the implementation, if you are not using EKS, remember to change the taint key value.

List pods and the nodes they are running on:

function kpodnode {
    ns=$1
    shift
    if [[ $ns =~ "allns" || $ns =~ "all"  ]]; then
      kg --all-namespaces pod -o=custom-columns=NAME:.metadata.name,NODE:.spec.nodeName,NS:.metadata.namespace,STATUS:.status.phase $@
    else
      kg -n $ns pod -o=custom-columns=NAME:.metadata.name,NODE:.spec.nodeName,STATUS:.status.phase $@
    fi
}
alias knodepod="kpodnode"

This function selects the pods under a certain namespace (or all pods if all is passed as the 1st parameter), and shows the node names they are running on. Useful in upgrading worker node. It works great with -w option:

aws:(prod-eks) k8s:(prod-eks)  % kpodnode analytics -w
NAME                NODE                          STATUS
metabase2023-xxxx   ip-10-0-xx-oo.ec2.internal    Running
superset-yyy        ip-10-0-xx-oo.ec2.internal    Running

If you use node group lable to organize the nodes in your k8s cluster, you might often need to list nodes with node group name, or search nodes with a certain node group label:

function kgnode {
    kubectl get node -o=custom-columns=NAME:.metadata.name,NODEGROUP:.metadata.labels.node-group,VERSION:.status.nodeInfo.kubeletVersion $@
}

function knodegroup {
    kubectl get node -l node-group=$@
}

⚠️ notice that the label name (node-group) could be different in your cluster, modify them to the label name you use.

Make sops smarter:

If you use sops to encrypt/decrypt credentials in your helm-var, it’s a good idea to make the command shorter. Additionally, I have 2 k8s environments (2 kube configs) so it’s annoying to specifiy different kms and different secrets yaml files every time. I have the 2 functions below to solve the problem:

function sopsd {
  if [[ $KUBECONFIG =~ "prod-eks$" ]]; then
    sops -d -i helm_vars/prod/secrets.yaml
  elif [[ $KUBECONFIG =~ "sandbox-eks$" ]]; then
    sops -d -i helm_vars/sandbox/secrets.yaml
  fi
}
function sopse {
  if [[ $KUBECONFIG =~ "prod-eks$" ]]; then
    sops -e -k arn:aws:kms:us-east-1:xxxx:key/ooo -i helm_vars/prod/secrets.yaml
  elif [[ $KUBECONFIG =~ "sandbox-eks$" ]]; then
    sops -e -k arn:aws:kms:us-east-1:yyyy:key/iii -i helm_vars/sandbox/secrets.yaml
  fi
}

Remember in your case, you need to have env var to indicate your k8s enviroment. And the key in sops -e -k and the secrets yaml file paths could be different. Customize them for your own case.

Written on November 12, 2023