Helpful Kubernetes ‘kubectl’ Aliases

Add these to ~/.bashrc on your linux workstation and save time from repetitive typing. After you’ve saved these to your ~/.bashrc, activate then via either:

  • closing the terminal and opening a new one, or
  • Reload ~/.bashrc in current terminal:
    • $ source ~/.bashrc
# ########################
# add these to your ~/.bashrc  #
#########################
# alias for using kind cluster context
alias kkc='kubectl config use-context kind-kind'

# aliases
# cg: git fetch -- scans your ~/git subfolders and fetches
# git fetcher
alias gf="python3 -c \"\"\"
import os, sys, subprocess
queue = []
errors = []
for root, dirs, files in os.walk(sys.argv[1], topdown=False):
    for d in dirs:
        if d == '.git':
            # print(root, d)
            try:
                cmd = f'cd {root}; git -c color.status=always fetch origin'
                returned_output = subprocess.check_output(cmd,shell=True).decode('utf-8')
                if 'nothing to commit' not in returned_output:
                    #queue.append(root)
                    print(f'{root}\n{returned_output}')
            except Exception as e:
                errors.append(root)
                print(f'!!!!!!!!!!!!!!!!!!! {root}\n{e}')
                

# print('\n\nSummary:')
# for q in queue:
#     print(q)

print('\n\nNot Fetched:')
for q in errors:
    print(q)

\"\"\" ~/git"
# cg: check git -- scans your ~/git subfolders looking for modified source
# git checker
alias cg="python3 -c \"\"\"
import os, sys, subprocess
queue = []
for root, dirs, files in os.walk(sys.argv[1], topdown=False):
    for d in dirs:
        if d == '.git':
            # print(root, d)
            cmd = f'cd {root}; git -c color.status=always status'
            returned_output = subprocess.check_output(cmd,shell=True).decode('utf-8')
            if 'nothing to commit' not in returned_output:
                queue.append(root)
                print(f'{root}\n{returned_output}')

print('\n\nSummary:')
for q in queue:
    print(q)
\"\"\" ~/git"

alias kdp='kubectl describe pod'
alias k='kubectl'
alias kgp='kubectl get pods'
alias kgs='kubectl get svc'
alias kkc='kubectl config use-context kind-kind'
alias kccc='kubectl config current-context'
alias kcgc='kubectl config get-contexts'
alias kcuc='kubectl config use-context'

Use these to select and use a cluster config in case you have multiple clusters:

#!/bin/bash

# get list of contexts from your config
kubectl config get-contexts
# pick a context to use
kubectl config use-context <context>
# to remove contexts:
kubectl config unset contexts.<context-name>
Scroll to Top