Kubernetes has a lot of different and helpful tools. In this short tutorial we look into the kuberc feature which allows us to create aliases directly in Kubernetes without the need of a bashrc file. This feature was added with the KEP-3104 to Kubernetes and is currently in beta testing. More about KEP (Kubernetes Enhancement Proposals) can be found here.
The following code was tested with Kubernetes version v1.36, but it should work with v1.34+.
Enable kuberc
To enable this feature be sure that the environment variable KUBERC is not set and KUBECTL_KUBERC is set to true.
unset KUBERC
export KUBECTL_KUBERC=trueIf everything is setup correctly you should be able to use the kuberc subcommand.
dominik@cp:~$ kubectl kuberc
Manage user preferences (kuberc) file.
The kuberc file allows you to customize your kubectl experience.Before starting with a deep dive into the functionality we start with two simple examples. First we look into setting up an alias and the using the override function. After this we start with the deep dive.
Create aliases
An alias in kuberc works the same as in bash. So we define a shorthand command which will be evaluated to a valid longer command.
For the example we first create a Pod with the following command.
kubectl run mypod --image nginxWe can now list the running Pod and show its labels.
kubectl get pod --show-labelsAs we can see the Pod has the labels run=mypod.
dominik@cp:~$ kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
mypod 1/1 Running 0 14s run=mypodNow we create an alias which uses labels to show the Pod. Below is an example for a kuberc file which creates a new alias called customapp. This customapp alias is evaluated to kubectl get pod –selector run=mypod.
cat << 'EOF' > ~/.kube/kuberc
apiVersion: kubectl.config.k8s.io/v1beta1
kind: Preference
aliases:
- name: customapp # alias name
command: get # kubectl get
prependArgs: # kubectl get pod
- pod
options: # use label selector
- name: selector # kubectl get pod --selector run=mypod
default: run=mypod
EOFThe new alias is now available with kubectl.
kubectl customappWhen the command is executed we list the previously created Pod by its label.
dominik@cp:~$ kubectl customapp
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 2m47sCreate defaults
Now we look into the second option, the defaults. A default allows us to override an existing command and set options. In the example below we append to each delete command the –interactive=true which always asks for confirmation when deleting.
cat << 'EOF' > ~/.kube/kuberc
apiVersion: kubectl.config.k8s.io/v1beta1
kind: Preference
aliases:
- name: customapp # alias name
command: get # kubectl get
prependArgs: # kubectl get pod
- pod
options: # use label selector
- name: selector # kubectl get pod --selector run=mypod
default: run=mypod
defaults:
- command: delete # add --interactive=true
options:
- name: interactive
default: "true"
EOFWe can test this new behavior with the previously created Pod.
kubectl delete pod mypod
# evaluates to
# kubectl delete pod mypod --interactive=trueWhen everything was done correctly a confirmation is needed to delete the Pod.
dominik@cp:~$ kubectl delete pod mypod
You are about to delete the following 1 resource(s):
pod/mypod
Do you want to continue? (y/N):
deletion is cancelledDeep Dive
In the following we will to a deep dive and look into some more complex setups. The reference for all possible options can be found on the official Kubernetes website here.
Precedence
When using configuration file the precedence is always important. The kuberc file is evaluated in the following order:
--kubercflagKUBERCenvironment variable$HOME/.kube/kuberc(default)
Aliases
Below is a reference how the alias is expanded into a valid kubectl command.
kubectl <alias-name>
# evaluates to
kubectl <command> <prependArgs> <options> <appendArgs>- alias-name: Name of the alias
- command: A single command optional with subcommands (run, create, rollout status)
- prependArgs: Arguments which are placed after the command from the kuberc
- options: Add options for a command without the dashes. Only the long form is valid.
- appendArgs: Arguments which are placed at the end of the whole command. Helpful for executing commands like in kubectl exec mypod –namespace default — hostname
Helpful aliases
Here are some helpful aliases for your kuberc file.
Create a simple Pod yaml
apiVersion: kubectl.config.k8s.io/v1beta1
kind: Preference
aliases:
- name: dpod
command: run
options:
- name: namespace
default: "default"
- name: image
default: "nginx"
- name: output
default: "yaml"
- name: dry-run
default: "client"You can use it with kubectl dpod mypod which evaluates to kubectl run mypod –namespace=default –image=nginx –output yaml –dry-run=client. Important: after the dpod a name for the Pod is needed!
dominik@cp:~$ kubectl dpod mypod
apiVersion: v1
kind: Pod
metadata:
labels:
run: mypod
name: mypod
namespace: default
spec:
containers:
- image: nginx
name: mypod
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}Open a bash
apiVersion: kubectl.config.k8s.io/v1beta1
kind: Preference
aliases:
- name: bash
command: exec
options:
- name: stdin
default: "true"
- name: tty
default: "true"
appendArgs:
- --
- /bin/bashYou can use it with kubectl bash <pod name>.
dominik@cp:~$ kubectl bash mypod
root@mypod:/# Defaults
The defaults allow to override exiting command options with a new default.
kubectl <command> <options>- command: A single command optional with subcommands (run, create, rollout status)
- options: Add a new default for options. Only the long form is valid.
Helpful defaults
Here are some helpful defaults for your kuberc file.
Always ask for confirmation when deleting
apiVersion: kubectl.config.k8s.io/v1beta1
kind: Preference
defaults:
- command: delete
options:
- name: interactive
default: "true"After calling the delete command a confirmation is needed.
dominik@cp:~$ kubectl delete pod mypod
You are about to delete the following 1 resource(s):
pod/mypod
Do you want to continue? (y/N):Use the server side apply
apiVersion: kubectl.config.k8s.io/v1beta1
kind: Preference
defaults:
- command: apply
options:
- name: server-side
default: "true"All applys will use the server-side option. More about this server-side can be found here.
Next Article
This sums up all the important things about kuberc. If you are interested in more Kubernetes related articles take a look at here.
Contact
For further questions contact me at: blog [@] dominiklandau.de
