Bird
Raised Fist0
Azurecloud~5 mins

Kubectl for cluster management in Azure - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Managing a Kubernetes cluster requires commands to control and check the cluster's state. Kubectl is a tool that lets you talk to the cluster to create, update, and see resources easily.
When you want to start a new application by creating pods in your Kubernetes cluster.
When you need to check if your application pods are running or if they have errors.
When you want to update your app by changing its deployment settings.
When you want to delete resources that are no longer needed to keep the cluster clean.
When you want to see detailed information about a specific pod or deployment to troubleshoot.
Commands
This command creates or updates the resources defined in the example-deployment.yaml file in the cluster. It is the first step to deploy your app.
Terminal
kubectl apply -f example-deployment.yaml
Expected OutputExpected
deployment.apps/example-deployment created
-f - Specifies the file containing the resource definitions
This command lists all pods in the current namespace to check their status and see if they are running correctly.
Terminal
kubectl get pods
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-deployment-5d8f7c7d9f-abc12 1/1 Running 0 30s
This command shows detailed information about the specific pod, including events and resource usage, useful for troubleshooting.
Terminal
kubectl describe pod example-deployment-5d8f7c7d9f-abc12
Expected OutputExpected
Name: example-deployment-5d8f7c7d9f-abc12 Namespace: default Node: node1/192.168.1.10 Start Time: Thu, 01 Jun 2024 10:00:00 +0000 Labels: app=example Status: Running Containers: example-container: Image: nginx:1.21 State: Running Ready: True Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 1m default-scheduler Successfully assigned default/example-deployment-5d8f7c7d9f-abc12 to node1
This command removes the deployment and all its pods from the cluster, cleaning up resources you no longer need.
Terminal
kubectl delete deployment example-deployment
Expected OutputExpected
deployment.apps "example-deployment" deleted
Key Concept

If you remember nothing else from this pattern, remember: kubectl lets you create, check, and delete Kubernetes resources easily using simple commands.

Common Mistakes
Running kubectl commands without specifying the correct namespace.
The command may show no resources or affect the wrong ones because Kubernetes defaults to the 'default' namespace.
Use the --namespace flag or set the context namespace to target the right namespace.
Using kubectl apply with an incorrect or malformed YAML file.
The command will fail to create or update resources, causing deployment errors.
Validate the YAML file syntax before applying it using tools like kubectl apply --dry-run=client.
Deleting resources without confirming the resource name.
You might delete the wrong deployment or resource, causing downtime.
Always double-check resource names with kubectl get before deleting.
Summary
Use kubectl apply -f to create or update Kubernetes resources from a file.
Use kubectl get pods to check the status of your running pods.
Use kubectl describe pod to get detailed information for troubleshooting.
Use kubectl delete deployment to remove resources you no longer need.

Practice

(1/5)
1. What is the primary purpose of the kubectl tool in Kubernetes?
easy
A. To manage and control Kubernetes clusters
B. To create virtual machines in Azure
C. To monitor network traffic outside the cluster
D. To write application code for Kubernetes

Solution

  1. Step 1: Understand kubectl's role

    kubectl is designed to interact with Kubernetes clusters to manage resources like pods, deployments, and services.
  2. Step 2: Compare options

    Options A, B, and C describe tasks unrelated to kubectl. Only To manage and control Kubernetes clusters correctly states its purpose.
  3. Final Answer:

    To manage and control Kubernetes clusters -> Option A
  4. Quick Check:

    kubectl = cluster management [OK]
Hint: kubectl controls Kubernetes clusters directly [OK]
Common Mistakes:
  • Confusing kubectl with Azure VM tools
  • Thinking kubectl writes application code
  • Assuming kubectl monitors external network traffic
2. Which of the following is the correct syntax to list all pods in the current Kubernetes namespace using kubectl?
easy
A. kubectl get pods
B. kubectl list pods
C. kubectl show pods
D. kubectl describe pods

Solution

  1. Step 1: Recall kubectl commands for listing resources

    The command to list resources is kubectl get, followed by the resource type.
  2. Step 2: Evaluate options

    Only kubectl get pods uses the correct syntax kubectl get pods. Options A and B are invalid commands, and D shows detailed info, not a simple list.
  3. Final Answer:

    kubectl get pods -> Option A
  4. Quick Check:

    List pods = kubectl get pods [OK]
Hint: Use 'kubectl get' to list Kubernetes resources [OK]
Common Mistakes:
  • Using 'list' or 'show' instead of 'get'
  • Confusing 'describe' with listing
  • Adding extra words after 'pods'
3. Given the command kubectl get pods -o wide, what extra information will you see compared to kubectl get pods?
medium
A. Detailed pod logs
B. Extended pod information including node and IP
C. Only pod names without status
D. List of services instead of pods

Solution

  1. Step 1: Understand the '-o wide' option

    The -o wide flag shows additional columns like node name and pod IP address.
  2. Step 2: Compare output differences

    Extended pod information including node and IP correctly describes the extra info. Detailed pod logs is about logs, not shown here. Only pod names without status is incorrect as status is shown by default. List of services instead of pods is unrelated.
  3. Final Answer:

    Extended pod information including node and IP -> Option B
  4. Quick Check:

    -o wide = more pod details [OK]
Hint: Use '-o wide' to see node and IP info for pods [OK]
Common Mistakes:
  • Thinking '-o wide' shows logs
  • Assuming it hides status info
  • Confusing pods with services
4. You run kubectl get pod mypod but get an error saying the pod does not exist. What is the most likely cause?
medium
A. You are in the wrong namespace
B. The pod name is misspelled
C. The pod has already been deleted
D. All of the above

Solution

  1. Step 1: Check common reasons for pod not found error

    The error can happen if the pod name is wrong, the pod was deleted, or you are looking in the wrong namespace.
  2. Step 2: Evaluate options

    All options B, C, and D are valid causes. Therefore, All of the above which includes all is correct.
  3. Final Answer:

    All of the above -> Option D
  4. Quick Check:

    Pod not found = wrong namespace, name, or deleted [OK]
Hint: Check namespace, spelling, and pod existence [OK]
Common Mistakes:
  • Ignoring namespace context
  • Assuming pod always exists
  • Not verifying pod name spelling
5. You want to update the image of a deployment named webapp to version v2 using kubectl. Which command correctly performs this update?
hard
A. kubectl update deployment webapp --image=webapp:v2
B. kubectl edit deployment webapp image=webapp:v2
C. kubectl set image deployment/webapp webapp=webapp:v2
D. kubectl change image webapp webapp:v2

Solution

  1. Step 1: Identify the correct kubectl command to update deployment image

    The command kubectl set image is used to update container images in deployments.
  2. Step 2: Analyze each option

    kubectl set image deployment/webapp webapp=webapp:v2 uses correct syntax: kubectl set image deployment/webapp webapp=webapp:v2. Options A, C, and D use invalid or incorrect commands.
  3. Final Answer:

    kubectl set image deployment/webapp webapp=webapp:v2 -> Option C
  4. Quick Check:

    Update image = kubectl set image [OK]
Hint: Use 'kubectl set image' to update deployment containers [OK]
Common Mistakes:
  • Using 'kubectl update' which is invalid
  • Trying 'kubectl edit' without proper syntax
  • Using non-existent 'kubectl change' command