Introduction
Kubernetes, also known as K8s, is a powerful open-source system for automating the deployment, scaling, and management of containerized applications. Whether you are new to Kubernetes or an experienced user, mastering its commands is crucial for efficiently managing your clusters. This article provides a comprehensive guide to Kubernetes commands, organized from beginner to advanced levels.
🎯 Key Concepts
Before we dive into the commands, let's review some fundamental Kubernetes concepts:
- Pod: The smallest and simplest Kubernetes object, representing a single instance of a running process in your cluster.
- Node: A worker machine in Kubernetes, which can be a virtual or a physical machine.
- Namespace: A way to divide cluster resources between multiple users.
- Deployment: A higher-level abstraction that manages a replicated application, ensuring that a specified number of replicas are running.
- Service: An abstraction that defines a logical set of pods and a policy by which to access them.
- ConfigMap and Secret: Mechanisms to inject configuration data into your applications.
🏁 Beginner Commands
1. Cluster Information
Get Cluster Info
kubectl cluster-info
Displays the address of the Kubernetes master and services running in the cluster.
Get Nodes
kubectl get nodes
Lists all nodes in the cluster.
2. Namespace Management
List Namespaces
kubectl get namespaces
Displays all namespaces in the cluster.
Create Namespace
kubectl create namespace my-namespace
Creates a new namespace.
Delete Namespace
kubectl delete namespace my-namespace
Deletes a specified namespace.
3. Pod Management
List Pods
kubectl get pods
kubectl get pods -n my-namespace
Lists all pods in the default namespace or a specified namespace.
Describe Pod
kubectl describe pod my-pod
Displays detailed information about a specific pod.
Create Pod
kubectl run my-pod --image=nginx
Creates a new pod running the specified container image.
Delete Pod
kubectl delete pod my-pod
Deletes a specified pod.
4. Deployment Management
List Deployments
kubectl get deployments
kubectl get deployments -n my-namespace
Lists all deployments in the default namespace or a specified namespace.
Create Deployment
kubectl create deployment my-deployment --image=nginx
Creates a new deployment with the specified container image.
Delete Deployment
kubectl delete deployment my-deployment
Deletes a specified deployment.
5. Service Management
List Services
kubectl get services
kubectl get services -n my-namespace
Lists all services in the default namespace or a specified namespace.
Create Service
kubectl expose deployment my-deployment --type=LoadBalancer --name=my-service
Creates a new service to expose a deployment.
Delete Service
kubectl delete service my-service
Deletes a specified service.
🚀 Intermediate Commands
1. ConfigMap and Secret Management
List ConfigMaps
kubectl get configmaps
kubectl get configmaps -n my-namespace
Lists all ConfigMaps in the default namespace or a specified namespace.
Create ConfigMap
kubectl create configmap my-config --from-literal=key1=value1 --from-literal=key2=value2
Creates a new ConfigMap from literal values.
List Secrets
kubectl get secrets
kubectl get secrets -n my-namespace
Lists all secrets in the default namespace or a specified namespace.
Create Secret
kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=secret
Creates a new secret from literal values.
2. Scaling Applications
Scale Deployment
kubectl scale deployment my-deployment --replicas=3
Scales a deployment to the specified number of replicas.
3. Updating Applications
Update Deployment
kubectl set image deployment/my-deployment nginx=nginx:1.19.1
Updates the container image in a deployment.
Rollout Status
kubectl rollout status deployment/my-deployment
Displays the rollout status of a deployment.
Rollback Deployment
kubectl rollout undo deployment/my-deployment
Rolls back a deployment to the previous revision.
4. Monitoring and Logging
View Pod Logs
kubectl logs my-pod
Displays the logs of a specified pod.
View Previous Pod Logs
kubectl logs my-pod --previous
Displays the logs of a previous instance of a specified pod.
Get Events
kubectl get events
kubectl get events -n my-namespace
Lists all events in the default namespace or a specified namespace.
5. Resource Management
Describe Node
kubectl describe node my-node
Displays detailed information about a specified node.
Label Nodes
kubectl label nodes my-node disktype=ssd
Adds a label to a node.
Annotate Resources
kubectl annotate pod my-pod description="my example pod"
Adds an annotation to a resource.
6. Network Policies
Create Network Policy
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-nginx
namespace: my-namespace
spec:
podSelector:
matchLabels:
app: nginx
policyTypes:
- Ingress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
Apply the network policy:
kubectl apply -f network-policy.yaml
🧠 Advanced Commands
1. Advanced Pod Management
Debug Pod
kubectl debug my-pod --image=busybox --target=my-container
Debugs a running pod by creating a new debugging container in the pod.
Port Forwarding
kubectl port-forward my-pod 8080:80
Forwards a local port to a port on a pod.
2. Advanced Node Management
Cordon Node
kubectl cordon my-node
Marks a node as unschedulable.
Drain Node
kubectl drain my-node --ignore-daemonsets
Safely evicts all pods from a node before maintenance.
Uncordon Node
kubectl uncordon my-node
Marks a node as schedulable.
3. Resource Quotas and Limits
Create Resource Quota
apiVersion: v1
kind: ResourceQuota
metadata:
name: compute-resources
namespace: my-namespace
spec:
hard:
pods: "10"
requests.cpu: "4"
requests.memory: 16Gi
limits.cpu: "10"
limits.memory: 32Gi
Apply the resource quota:
kubectl apply -f resource-quota.yaml
4. Custom Resources and CRDs
Create Custom Resource Definition
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: crontabs.stable.example.com
spec:
group: stable.example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cronSpec:
type: string
image:
type: string
replicas:
type: integer
scope: Namespaced
names:
plural: crontabs
singular: crontab
kind: CronTab
shortNames:
- ct
Apply the CRD:
kubectl apply -f crd.yaml
Create Custom Resource
apiVersion: stable.example.com/v1
kind: CronTab
metadata:
name: my-new-cron-object
namespace: my-namespace
spec:
cronSpec: "* * * * */5"
image: my-cron-image
replicas: 3
Apply the custom resource:
kubectl apply -f custom-resource.yaml
5. Helm for Kubernetes Package Management
Install Helm
Follow the installation instructions for Helm from the official Helm website.
Add Helm Repository
helm repo add stable https://charts.helm.sh/stable
Install Helm Chart
helm install my-release stable/nginx
List Helm Releases
helm list
Upgrade Helm Release
helm upgrade my-release stable/nginx
Uninstall Helm Release
helm uninstall my-release
📊 Best Practices
Use Namespaces
for Isolation
- Organize resources by namespaces for better management and access control.
Label Resources
- Use labels to organize and select resources efficiently.
Monitor and Log
- Continuously monitor your cluster and collect logs for troubleshooting and performance analysis.
Automate with Scripts
- Use scripts to automate repetitive tasks and ensure consistency.
Secure Your Cluster
- Implement RBAC, network policies, and secure access controls to protect your cluster.
🚀 Conclusion
Mastering Kubernetes commands, from beginner to advanced levels, is essential for DevOps engineers to manage and troubleshoot clusters effectively. This comprehensive guide serves as a valuable reference for navigating your Kubernetes environment. By following best practices and leveraging these commands, you can ensure a robust and efficient Kubernetes setup.
Happy Clustering!🎉
Thank you for reading my blog …:)
© Copyrights: ProDevOpsGuy
Join Our Telegram Community || Follow me for more DevOps & Cloud Content