0
0
Azurecloud~5 mins

Kubectl for cluster management in Azure - Commands & Configuration

Choose your learning style9 modes available
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.