๐ Kubernetes Commands and Concepts#
๐ kubectl Cheat Sheet#
1. Basic Commands#
-
Cluster Info:
kubectl cluster-info- Display cluster information.kubectl version- Show client and server versions.kubectl get nodes- List all nodes in the cluster.
-
Namespace Management:
kubectl get namespaces- List all namespaces.kubectl create namespace <namespace>- Create a new namespace.kubectl delete namespace <namespace>- Delete a namespace.kubectl config set-context --current --namespace=<namespace>- Set default namespace.
-
Resource Overview:
kubectl get all- Show all resources in the default namespace.kubectl get pods- List all pods in the namespace.kubectl get services- List all services in the namespace.
2. Managing Pods#
-
Create and Delete Pods:
kubectl run <pod-name> --image=<image>- Create a pod.kubectl delete pod <pod-name>- Delete a pod.
-
Inspect Pods:
kubectl describe pod <pod-name>- Show detailed pod information.kubectl logs <pod-name>- View pod logs.kubectl exec -it <pod-name> -- <command>- Execute a command in the pod.
3. Deployments#
-
Create and Manage Deployments:
kubectl create deployment <name> --image=<image>- Create a deployment.kubectl scale deployment <name> --replicas=<number>- Scale a deployment.kubectl rollout restart deployment <name>- Restart a deployment.
-
Check Deployment Status:
kubectl get deployments- List all deployments.kubectl describe deployment <name>- Show deployment details.kubectl rollout status deployment <name>- Check rollout status.
-
Update Deployments:
kubectl set image deployment/<name> <container>=<new-image>- Update the container image.kubectl rollout undo deployment <name>- Roll back to the previous version.
4. Services and Networking#
-
Create and Manage Services:
kubectl expose pod <pod-name> --type=<type> --port=<port>- Expose a pod as a service.kubectl get services- List all services.kubectl delete service <service-name>- Delete a service.
-
Service Types:
- ClusterIP: Default; accessible only within the cluster.
- NodePort: Exposes service on a static port on each node.
- LoadBalancer: Exposes service externally via a cloud provider.
5. ConfigMaps and Secrets#
-
ConfigMaps:
kubectl create configmap <name> --from-literal=<key>=<value>- Create a ConfigMap.kubectl get configmaps- List all ConfigMaps.kubectl describe configmap <name>- View ConfigMap details.
-
Secrets:
kubectl create secret generic <name> --from-literal=<key>=<value>- Create a Secret.kubectl get secrets- List all Secrets.kubectl describe secret <name>- View Secret details.
6. Namespaces#
kubectl get namespaces- List all namespaces.kubectl create namespace <name>- Create a namespace.kubectl delete namespace <name>- Delete a namespace.
7. Debugging#
kubectl logs <pod-name>- View pod logs.kubectl describe pod <pod-name>- Detailed pod description.kubectl exec -it <pod-name> -- /bin/sh- Access a shell inside the pod.
๐ YAML Templates for Common Kubernetes Objects#
Pod#
apiVersion: v1kind: Podmetadata: name: my-pod namespace: defaultspec: containers: - name: my-container image: nginx:latest ports: - containerPort: 80