Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available. Kubernetes provides you with a framework to run distributed systems resiliently. It takes care of scaling and failover for your application, provides deployment patterns, canary deployments, and more. In this blog post, I will mention Kubernetes commands which we need for most of the use-cases.
I will list down kubectl
commands in the sections below as a quick reference to work with Kubernetes.
- List Resources
- Create Resources
- Update Resources
- Display the State of Resources
- Delete Resources
- Execute Command
- Print Container Logs
- Modify Kubeconfig Files
List Resources
- Get list of all namespaces
kubectl get namespaces
- Get list of all pods
kubectl get pods
- Get list of all pods with detailed information like IP, Node Name etc...
kubectl get pods -o wide
- Get list of all pods running on a particular node server
kubectl get pods --field-selector=spec.nodeName=[server-name]
- Get list of all replication controllers and services
kubectl get replicationcontroller,services
- Get list of daemonsets
kubectl get daemonset
Create Resources
- Create a new namespace
kubectl create namespace [namespace-name]
- Create a new namespace from JSON or YAML file.
kubectl create –f [filename]
Update Resources
To apply or update a resource use the kubectl apply
command.
- Create a new service with the definition contained in [service-config].yaml
kubectl apply -f [service-config].yaml
- Create a new replication controller with the definition contained in [controller-config].yaml
kubectl apply -f [controller-config].yaml
- Create the objects defined in any .yaml, .yml, or .json file in a directory
kubectl apply -f [yaml-file/directory-name]
- Edit a service config
kubectl edit svc/[service-name]
Above command opens the file in your default editor. To choose another editor, specify it in front of the command:
KUBE_EDITOR=”[editor-name]” kubectl edit svc/[service-name]
Display the State of Resources
- Get Details about Particular Node
kubectl describe nodes [node-name]
- Get Details about a Particular pod
kubectl describe pods [pod-name]
- Get Details about a Particular pod whose name and type are listed in
pod.json
kubectl describe –f pod.json
- Get Details about a Particular pod managed by a specific replication controller
kubectl describe pods [replication-controller-name]
- Get Details about all pods
kubectl describe pods
Delete Resources
- Delete a pod using the name and type mentioned in pod.yaml
kubectl delete -f pod.yaml
- Delete all pods and services with a specific label
kubectl delete pods,services -l [label-key]=[label-value]
- Delete all pods
kubectl delete pods --all
Execute Command
- Get output from a command run on the first container in a pod
kubectl exec [pod-name] -- [command]
- Get output from a command run on a specific container in a pod
kubectl exec [pod-name] -c [container-name] -- [command]
- Run /bin/bash from a specific pod. The received output comes from the first container
kubectl exec -ti [pod-name] -- /bin/bash
Print Container Logs
- Print Logs from Pod
kubectl logs [pod-name]
- Stream Logs from Pod
kubectl logs -f [pod-name]
- Tail Logs from Pod (Print last 200 logs from pod)
kubectl logs --tail=200 [pod-name]
Modify Kubeconfig Files
kubectl config
command lets you view and modify kubeconfig files.
- Get Current Context
kubectl config current-context
- Set cluster entry in kubeconfig
kubectl config set-cluster [cluster-name] --server=[server-name]
- Unset an entry in kubeconfig
kubectl config unset [property-name]
Thank you for reading
Hope you find these resources useful. If you like what you read and want to see more about system design, microservices, and other technology-related stuff... You can follow me on
- Twitter - Follow @vishnuchi
- Subscribe to my weekly newsletter here.
Top comments (0)