Skip to content

Advanced Kubectl Commands: jq, Plugins & Context Management

DodaTech 3 min read

In this tutorial, you'll learn about Advanced Kubectl Commands: jq, Plugins & Context Management. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Kubectl is the Kubernetes command-line tool, and mastering its advanced features transforms cluster management from tedious to efficient through JSON Parsing, custom plugins, and context management.

What You'll Learn

This tutorial covers advanced kubectl usage including JSONPath and jq for output Parsing, kubectl plugins, context and namespace management, port forwarding, and ephemeral containers for debugging.

Why It Matters

Senior Kubernetes engineers spend significant time interacting with clusters through kubectl. Advanced techniques reduce query time from minutes to seconds and enable automation that would otherwise require custom scripts.

Real-World Use

Platform engineering teams at companies like Shopify and GitHub use kubectl plugins and custom output formatting to build internal developer tools that standardize cluster access across hundreds of engineers.

JSON Output with jq

Raw kubectl output is hard to parse in scripts. Using JSON output with jq enables precise data extraction.

# Get pod names and IPs
kubectl get pods -o json | jq '.items[] | {name: .metadata.name, ip: .status.podIP}'

# Get all images running in a namespace
kubectl get pods -o json | jq '[.items[].spec.containers[].image] | unique'

Expected output shows a filtered list of pod names with their IP addresses or unique container images.

JSONPath for Simpler Queries

JSONPath works without installing jq and is built into kubectl.

# Get pod names only
kubectl get pods -o jsonpath='{.items[*].metadata.name}'

# Custom column output
kubectl get pods -o custom-columns=NAME:.metadata.name,IP:.status.podIP,NODE:.spec.nodeName

Custom Columns and Sorting

# Sort pods by restart count
kubectl get pods --sort-by=.status.containerStatuses[0].restartCount

# Show pods with node and status
kubectl get pods -o wide --all-namespaces

Kubectl Plugins

Plugins extend kubectl with custom commands. Use krew, the plugin manager, to install them.

# Install krew
(
  set -x; cd "$(mktemp -d)" &&
  OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
  ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/arm.*$/arm/')" &&
  curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/krew.tar.gz" &&
  tar zxvf krew.tar.gz &&
  ./krew install krew
)

# Install useful plugins
kubectl krew install ctx ns sniff tail access-matrix

Using Plugins

# Switch context with ctx plugin
kubectl ctx

# Switch namespace with ns plugin
kubectl ns production

# View access matrix for a user
kubectl access-matrix --user alice

Context and Namespace Management

Without plugins, manage contexts manually.

# List contexts
kubectl config get-contexts

# Switch context
kubectl config use-context production-cluster

# Set default namespace for current context
kubectl config set-context --current --namespace=production

# View current context
kubectl config current-context

Port Forwarding for Debugging

Forward local ports to pod ports for testing internal services.

# Forward single port
kubectl port-forward pod/nginx 8080:80

# Forward multiple ports
kubectl port-forward deployment/api 8080:80 8443:443

# Forward to a service
kubectl port-forward service/database 5432:5432

Ephemeral Containers for Debugging

Ephemeral containers let you add debugging containers to running pods without restarting them.

# Add debug container to a running pod
kubectl debug my-pod -it --image=nicolaka/netshoot -- /bin/bash

# Debug a node by creating a temporary pod
kubectl debug node/worker-1 -it --image=ubuntu -- /bin/bash

Copying Files

# Copy file to pod
kubectl cp ./config.yaml my-pod:/etc/config/

# Copy file from pod
kubectl cp my-pod:/var/log/app.log ./app.log

Practice Questions

  1. How do you extract all pod names using JSONPath? kubectl get pods -o jsonpath='{.items[*].metadata.name}'

  2. What is krew and what does it do? Krew is the kubectl plugin manager that helps discover and install community-built plugins.

  3. How do you switch between multiple Kubernetes clusters? Use kubectl config use-context or the ctx plugin to switch cluster contexts.

  4. What is the purpose of ephemeral containers? They add temporary debugging containers to running pods without restarting the pod.

  5. How do you forward a database port for local development? kubectl port-forward service/database 5432:5432 maps local port 5432 to the database service.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro