Welcome back! This warmup is designed to help you review some essential kubectl commands. Some experienced Kubernetes folks might find this elementary, but if you're new to Kubernetes, these are critical skills worth practicing!
Your goal is to understand and get hands-on practice with the following commands:
kubectl applykubectl getkubectl describekubectl delete
Let's get to it! 🧑💻
Our environment already has a pod manifest available. Before we start, let's review what the manifest looks like:
student@bchd~$ batcat ~/mycode/yaml/my_first_pod.yaml
You'll see something like this:
apiVersion: v1
kind: Pod
metadata:
name: myfirstpod
spec:
containers:
- name: webster
image: registry.gitlab.com/alta3/webby:latestThis manifest defines a pod named myfirstpod that runs a container using the image webby:latest.
First, let's create (or update) the pod using the apply command. This will create the pod based on the manifest.
student@bchd~$ kubectl apply -f ~/mycode/yaml/my_first_pod.yaml
-
If you’ve already applied this manifest before, you might see something like this:
pod/myfirstpod unchangedThat’s okay! This means the pod is already running, and the manifest hasn’t changed.
Next, let's check the status of your pod using the get command. This will give you a quick overview of your pod's state.
student@bchd~$ kubectl get pods
You should see something like this:
NAME READY STATUS RESTARTS AGE
myfirstpod 1/1 Running 0 1m- STATUS: This tells you whether the pod is running, pending, or failed.
- RESTARTS: Number of times the pod has restarted (due to failures, etc.).
To see a detailed breakdown of what's happening with your pod, use the describe command.
student@bchd~$ kubectl describe pod myfirstpod
This will give you loads of info, including:
- Pod events
- Container statuses
- Resource usage
Look for the Events section to see how Kubernetes is managing your pod.
To view the logs produced by the container inside your pod, use the logs command:
student@bchd~$ kubectl logs myfirstpod
This will display any output the container has generated. Super handy for debugging!
Let’s clean up our environment by deleting the pod using the delete command:
student@bchd~$ kubectl delete pod myfirstpod
You should see something like:
pod "myfirstpod" deletedThis removes the pod, but don't worry—you can always reapply the manifest if you need to bring it back!
Here are a couple of other useful commands you should practice:
student@bchd~$ kubectl get all
This shows all resources (pods, services, deployments, etc.) running in the current namespace.
If you ever need a quick refresher on what a resource does, use explain!
student@bchd~$ kubectl explain pod
This will give you a summary of what a pod is and what fields you can define in its manifest.
Boom! 💥 You've just gone through the basics of working with kubectl. Whether you're applying, getting, describing, or deleting, mastering these commands is key to managing Kubernetes like a pro!