Kubernetes Tools Part 1 – kuberc

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.

Bash
unset KUBERC

export KUBECTL_KUBERC=true

If everything is setup correctly you should be able to use the kuberc subcommand.

Bash
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.

Bash
kubectl run mypod --image nginx

We can now list the running Pod and show its labels.

Bash
kubectl get pod --show-labels

As we can see the Pod has the labels run=mypod.

Bash
dominik@cp:~$ kubectl get pod --show-labels
NAME    READY   STATUS    RESTARTS   AGE   LABELS
mypod   1/1     Running   0          14s   run=mypod

Now 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.

Bash
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
EOF

The new alias is now available with kubectl.

Bash
kubectl customapp

When the command is executed we list the previously created Pod by its label.

Bash
dominik@cp:~$ kubectl customapp
NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          2m47s

Create 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.

Bash
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"
EOF

We can test this new behavior with the previously created Pod.

Bash
kubectl delete pod mypod
# evaluates to
# kubectl delete pod mypod --interactive=true

When everything was done correctly a confirmation is needed to delete the Pod.

Bash
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 cancelled

Deep 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:

  1. --kuberc flag
  2. KUBERC environment variable
  3. $HOME/.kube/kuberc (default)

Aliases

Below is a reference how the alias is expanded into a valid kubectl command.

Bash
kubectl <alias-name> 
# evaluates to
kubectl <command> <prependArgs> <options> <appendArgs>
  1. alias-name: Name of the alias
  2. command: A single command optional with subcommands (run, create, rollout status)
  3. prependArgs: Arguments which are placed after the command from the kuberc
  4. options: Add options for a command without the dashes. Only the long form is valid.
  5. 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

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!

Bash
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

YAML
apiVersion: kubectl.config.k8s.io/v1beta1
kind: Preference
aliases:
- name: bash
  command: exec
  options:
  - name: stdin
    default: "true"
  - name: tty
    default: "true"
  appendArgs:
  - --
  - /bin/bash

You can use it with kubectl bash <pod name>.

Bash
dominik@cp:~$ kubectl bash mypod
root@mypod:/# 

Defaults

The defaults allow to override exiting command options with a new default.

Bash
kubectl <command> <options>
  1. command: A single command optional with subcommands (run, create, rollout status)
  2. 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

YAML
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.

Bash
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

YAML
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