CKAD kubectl Cheat Sheet

0. Context Management
0.1 View Current Context
0.2 List All Contexts
0.3 Switch Context
my-context
: Name of the context to switch to.
1. Pods
1.1 Managing a Pod
Creating a Pod
--image nginx:latest
: Specifies the container image.--restart Never
: Controls the restart policy.--env VAR1=value1
: Sets environment variables.
Declarative:
- Generate YAML:
kubectl run my-pod --image=nginx:latest --restart=Never --env=VAR1=value1 --dry-run=client -o yaml > my-pod.yaml
- Apply YAML:
Getting Pods
-o wide
: Provides more detailed output.--watch
: Watches for changes in real-time.
Describing a Pod
2. Deployments
2.1 Managing Deployments
Creating a Deployment
--image nginx:latest
: Specifies the container image.--replicas 2
: Number of desired replicas.
Declarative:
- Generate YAML:
kubectl create deployment my-deployment --image=nginx:latest --replicas=2 --dry-run=client -o yaml > my-deployment.yaml
- Apply YAML:
Scaling a Deployment
--replicas 5
: Sets the number of desired replicas.
Declarative:
- Update YAML: Adjust
replicas
inmy-deployment.yaml
file. - Apply YAML:
3. Services
3.1 Creating a Service
--port 80
: Specifies the port number.--type ClusterIP
: Defines the type of service.
Declarative:
- Generate YAML:
kubectl expose deployment my-deployment --port=80 --type=ClusterIP --dry-run=client -o yaml > my-service.yaml
- Apply YAML:
4. Namespaces
4.1 Managing Namespaces
Creating a Namespace
Declarative:
- Generate YAML:
- Apply YAML:
Listing Namespaces
5. Configuration
5.1 Managing ConfigMaps and Secrets
Creating a ConfigMap
--from-literal key1=value1
: Sets a key-value pair directly.--from-file ./config-file.txt
: Creates a ConfigMap from a file.
Declarative:
- Generate YAML:
kubectl create configmap my-configmap --from-literal=key1=value1 --from-file=./config-file.txt --dry-run=client -o yaml > my-configmap.yaml
-
Apply YAML:
Creating a Secret
--from-literal key1=value1
: Sets a key-value pair for the secret.--from-file ./secret-file.txt
: Creates a Secret from a file.
Declarative:
- Generate YAML:
kubectl create secret generic my-secret --from-literal=key1=value1 --from-file=./secret-file.txt --dry-run=client -o yaml > my-secret.yaml
- Apply YAML:
6. Monitoring and Logging
6.1 Getting Logs
-f
: Follow log output in real-time.--since 1h
: Show logs since a certain time.
7. Jobs and CronJobs
7.1 Managing Jobs and CronJobs
Creating a Job
--image busybox
: Specifies the container image.
Declarative:
- Generate YAML:
- Apply YAML:
Creating a CronJob
--schedule "*/5 * * * *"
: Sets the cron schedule in cron format.
Declarative:
- Generate YAML:
kubectl create cronjob my-cronjob --schedule="*/5 * * * *" --image=busybox --dry-run=client -o yaml > my-cronjob.yaml
- Apply YAML:
8. Rolling Updates and Rollbacks
8.1 Managing Updates and Rollbacks
Updating a Deployment
Declarative:
- Update YAML: Adjust
image
inmy-deployment.yaml
. - Apply YAML:
Rolling Back a Deployment
Declarative:
- Use previous version of
my-deployment.yaml
. - Apply YAML:
9. Resource Management
9.1 Setting Resource Requests and Limits
kubectl set resources deployment/my-deployment --limits=cpu=200m,memory=512Mi --requests=cpu=100m,memory=256Mi
--limits cpu=200m,memory=512Mi
and--requests cpu=100m,memory=256Mi
: Set resource constraints.
Declarative:
- Update YAML: Adjust
resources
inmy-deployment.yaml
. - Apply YAML:
10. Debugging
10.1 Diagnosing and Fixing Issues
Executing into a Container
Declarative: Not applicable for exec command.
Port Forwarding
8080:80
: Forwards local port 8080 to the Pod's port 80.
Declarative: Not applicable for port-forward command.
Copying Files to/from a Container
Declarative: Not applicable for cp command.
11. Labels and Selectors
11.1 Managing Labels
Adding Labels to a Pod
- Adds labels
key1=value1
andkey2=value2
tomy-pod
.
Updating Labels of a Pod
- Updates the value of
key1
tovalue1
onmy-pod
, overwriting if it exists.
Removing Labels from a Pod
- Removes the label
key1
frommy-pod
.
Filtering Resources by Labels
- Lists all pods with labels
key1=value1
andkey2=value2
.
Using Labels for Resource Management
- Imperative:
- Assigning a label:
- Selecting resources:
- Declarative:
- Update YAML: Add labels under
metadata.labels
in resource definition files. - Apply YAML: