Skip to content

Kubectl

kubectl may not be expected to appear in the tooling section as it's, well, the basic tool.

But maybe there's some commands and tricks that are worth mentioning to use it at its full potential ?

So here are some cool usage of kubectl:

Create a vanilla resource

k create deployment sample_app \
  --image=alpine \
  --dry-run=client \
  --output yaml > sample_deployment.yaml

Which will result in the following deployment to be saved:

sample_app.yaml
cat > /tmp/toto <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: sample_app
  name: sample_app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sample_app
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: sample_app
    spec:
      containers:
      - image: alpine
        name: alpine
        resources: {}
status: {}
EOF

Then it's easy to edit the yaml file and fill the voides.

Edit a resource in the cluster

The command kubectl edit can be used to edit a resource directly in the cluster.

While it's better to manage your resources as code, versioned in a git repo (#Gitops), it's sometimes faster to directly edit the resource. Such case is changing the replicas value of a deployment or updating a label, or removing a Finalizer so the resource can be deleted.

Let's edit our simple-deployment and scale it to 3:

kubectl edit deployment gowebapp

Then go to the line with replicas: 1 and change it to replicas: 2.

By default, kubectl is using vi as an editor. This can be changed by setting a value to the KUBE_EDITOR env variable. You can add it to your .zshrc or .bashrc to make it permanent.

If you're using vi, highlight the number 1, type r then 2 then :wq

If you made a mistake, just exit without saving using :q!

If you prefer to change the editor, set the KUBE_EDITOR variable:

Use SublimeText
export KUBE_EDITOR="/Applications/Sublime.app/Contents/SharedSupport/bin/subl -w"
Use VsCode
export KUBE_EDITOR="code --wait"

export KUBE_EDITOR="ed"
KUBE_EDITOR=code -w

Of course, there is a kubectl command to change the number of replicas:

kubectl scale --replicas=2 deployment/gowebapp
kubectl get pods
output
NAME                                 READY   STATUS             RESTARTS       AGE
gowebapp-57d9ccc7f8-2m97j            0/1     CrashLoopBackOff   7 (73s ago)    12m
gowebapp-57d9ccc7f8-bm8jp            0/1     CrashLoopBackOff   5 (119s ago)   4m48s

I'm still bored to use kubectl

And this is right. We added a ton of stuff in our shell to be faster. Let's revisit what we did previously:

  • List the pods

    kgp
    
    output
    NAME                                 READY   STATUS             RESTARTS       AGE
    simple-deployment-57d9ccc7f8-2m97j   0/1     CrashLoopBackOff   7 (73s ago)    12m
    simple-deployment-57d9ccc7f8-bm8jp   0/1     CrashLoopBackOff   5 (119s ago)   4m48s
    simple-deployment-57d9ccc7f8-bqnxs   0/1     CrashLoopBackOff   9 (5m ago)     26m
    simple-pod                           1/1     Running            0              26m
    

  • change the replicas of the deployment

    ksd --replicas=2 simple-deployment
    
    output
    deployment.apps/simple-deployment scaled
    

Next

Continue to KubeColor