Skip to content

Instantly share code, notes, and snippets.

@iamkominn
Created March 26, 2025 02:13
Show Gist options
  • Save iamkominn/87b1d3255fb61167a100a2df20cfc4b2 to your computer and use it in GitHub Desktop.
Save iamkominn/87b1d3255fb61167a100a2df20cfc4b2 to your computer and use it in GitHub Desktop.
List of Kubectl commands for CKA exam

Kubernetes CLI Commands for CKA Exam

This a comprehensive list of kubectl commands you'll likely use during the CKA exam, categorized by function:

Cluster Management

Check cluster info

kubectl cluster-info

Shows the master and service endpoints.

Kubernetes control plane is running at https://kubernetes.docker.internal:6443
CoreDNS is running at https://kubernetes.docker.internal:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

Get API resources

kubectl api-resources

Lists all API resources available in the cluster.

NAME                              SHORTNAMES   APIVERSION                             NAMESPACED   KIND
bindings                                       v1                                     true         Binding
componentstatuses                 cs           v1                                     false        ComponentStatus
configmaps                        cm           v1                                     true         ConfigMap
...

Get component status

kubectl get componentstatuses

Check health of control plane components.

NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-0               Healthy   {"health":"true"}   

Node Management

List nodes

kubectl get nodes

Shows all nodes in the cluster.

NAME       STATUS   ROLES           AGE     VERSION
minikube   Ready    control-plane   5d20h   v1.26.3

Node details

kubectl describe node <node-name>

Shows detailed information about a specific node.

Name:               minikube
Roles:              control-plane
Labels:             beta.kubernetes.io/arch=amd64
                    beta.kubernetes.io/os=linux
                    kubernetes.io/arch=amd64
...

Pod Management

List pods

kubectl get pods [-n namespace] [-o wide]

Lists all pods in the default or specified namespace.

NAME                          READY   STATUS    RESTARTS   AGE
nginx-deploy-6c9f56c8-7pvzl   1/1     Running   0          15m

Create pod

kubectl run nginx --image=nginx

Creates a pod named nginx using the nginx image.

pod/nginx created

Pod details

kubectl describe pod <pod-name> [-n namespace]

Shows detailed information about a specific pod.

Name:         nginx
Namespace:    default
Priority:     0
Node:         minikube/192.168.49.2
Start Time:   Wed, Mar 26, 2025 10:15:32 -0400
...

Edit pod

kubectl edit pod <pod-name> [-n namespace]

Opens the pod manifest in an editor to make changes.

Delete pod

kubectl delete pod <pod-name> [-n namespace]

Deletes a pod.

pod "nginx" deleted

Execute command in pod

kubectl exec -it <pod-name> [-n namespace] -- <command>

Runs a command inside a pod container.

kubectl exec -it nginx -- /bin/bash
root@nginx:/#

Pod logs

kubectl logs <pod-name> [-c container-name] [-n namespace] [--previous]

Shows logs from a pod container.

10.244.0.1 - - [26/Mar/2025:14:28:43 +0000] "GET / HTTP/1.1" 200 615 "-" "Mozilla/5.0" "-"

Deployment Management

Create deployment

kubectl create deployment nginx --image=nginx --replicas=3

Creates a deployment with specified replicas.

deployment.apps/nginx created

List deployments

kubectl get deployments [-n namespace]

Lists all deployments.

NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   3/3     3            3           45s

Scale deployment

kubectl scale deployment <deployment-name> --replicas=<count>

Changes the number of replicas.

deployment.apps/nginx scaled

Deployment rollout status

kubectl rollout status deployment/<deployment-name>

Checks the rollout status of a deployment.

deployment "nginx" successfully rolled out

Rollback deployment

kubectl rollout undo deployment/<deployment-name> [--to-revision=<number>]

Rolls back to a previous deployment version.

deployment.apps/nginx rolled back

Service Management

List services

kubectl get services [-n namespace]

Lists all services.

NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        5d20h
nginx        NodePort    10.106.145.116   <none>        80:30080/TCP   2m

Create service

kubectl expose deployment <deployment-name> --port=<port> --type=NodePort

Creates a service for a deployment.

service/nginx exposed

ConfigMap and Secret Management

Create ConfigMap

kubectl create configmap <name> --from-literal=key1=value1 --from-literal=key2=value2

Creates a ConfigMap from literal values.

configmap/app-config created

Create ConfigMap from file

kubectl create configmap <name> --from-file=<path-to-file>

Creates a ConfigMap from a file.

configmap/app-config created

List ConfigMaps

kubectl get configmaps [-n namespace]

Lists all ConfigMaps.

NAME               DATA   AGE
app-config         2      30s
kube-root-ca.crt   1      5d20h

Create Secret

kubectl create secret generic <name> --from-literal=key1=value1

Creates a Secret from literal values.

secret/db-creds created

List Secrets

kubectl get secrets [-n namespace]

Lists all Secrets.

NAME                  TYPE                                  DATA   AGE
db-creds              Opaque                                1      25s
default-token-abcde   kubernetes.io/service-account-token   3      5d20h

Namespace Management

List namespaces

kubectl get namespaces

Lists all namespaces.

NAME              STATUS   AGE
default           Active   5d20h
kube-node-lease   Active   5d20h
kube-public       Active   5d20h
kube-system       Active   5d20h

Create namespace

kubectl create namespace <name>

Creates a new namespace.

namespace/dev created

RBAC Management

List roles

kubectl get roles [-n namespace]

Lists all roles in a namespace.

NAME        CREATED AT
pod-reader  2025-03-26T14:32:10Z

List cluster roles

kubectl get clusterroles

Lists all cluster roles.

NAME                                                                   CREATED AT
admin                                                                  2025-03-20T15:12:47Z
cluster-admin                                                          2025-03-20T15:12:47Z
...

List role bindings

kubectl get rolebindings [-n namespace]

Lists all role bindings.

NAME           ROLE              AGE
read-pods      Role/pod-reader   5m

Resource Management

Resource quota

kubectl get resourcequota [-n namespace]

Lists resource quotas.

NAME          AGE   REQUEST                                           LIMIT
mem-cpu-demo  10s   requests.cpu: 0/1, requests.memory: 0/1Gi   limits.cpu: 0/2, limits.memory: 0/2Gi

Limit ranges

kubectl get limitrange [-n namespace]

Lists limit ranges.

NAME       CREATED AT
mem-limit  2025-03-26T14:40:10Z

Storage Management

List PersistentVolumes

kubectl get pv

Lists all persistent volumes.

NAME    CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv001   1Gi        RWO            Retain           Available           standard                5m

List PersistentVolumeClaims

kubectl get pvc [-n namespace]

Lists all persistent volume claims.

NAME        STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
data-pvc    Bound    pv001    1Gi        RWO            standard       2m

List StorageClasses

kubectl get sc

Lists all storage classes.

NAME                 PROVISIONER                RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
standard (default)   k8s.io/minikube-hostpath   Delete          Immediate           false                  5d20h

Configuration and Context

View kubeconfig

kubectl config view

Shows the current kubeconfig.

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://kubernetes.docker.internal:6443
  name: docker-desktop
...

Change context

kubectl config use-context <context-name>

Switches to a different context.

Switched to context "docker-desktop".

List contexts

kubectl config get-contexts

Lists all available contexts.

CURRENT   NAME                 CLUSTER          AUTHINFO         NAMESPACE
*         docker-desktop       docker-desktop   docker-desktop   
          minikube             minikube         minikube         default

Network Policy

List NetworkPolicies

kubectl get networkpolicies [-n namespace]

Lists all network policies.

NAME             POD-SELECTOR   AGE
allow-frontend   app=frontend   45s

Troubleshooting

Check DNS resolution

kubectl run -it --rm dns-test --image=busybox:1.28 -- nslookup kubernetes.default

Tests DNS resolution within the cluster.

Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      kubernetes.default
Address 1: 10.96.0.1 kubernetes.default.svc.cluster.local

Check service connectivity

kubectl run -it --rm curl-test --image=curlimages/curl -- curl <service-name>.<namespace>.svc.cluster.local

Tests connectivity to a service.

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...

Create temporary debug pod

kubectl run -it --rm debug --image=busybox -- sh

Creates a temporary pod for debugging.

/ # 

Output Formatting

Output in YAML

kubectl get pod <pod-name> -o yaml

Gets pod configuration in YAML format.

Output in JSON

kubectl get pod <pod-name> -o json

Gets pod configuration in JSON format.

Custom columns

kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase

Displays specific fields in columns.

NAME                          STATUS
nginx-deploy-6c9f56c8-7pvzl   Running

Apply YAML

kubectl apply -f <file-name.yaml>

Creates or updates resources defined in a YAML file.

deployment.apps/nginx created
service/nginx created
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment