Kubectl
What is kubectl ?
Kubectl is a command line tool that allows us to interact with kubernetes. Kubectl uses the kubernetes API to communicate with the cluster and carry out our command
We can use kubectl to deploy applications, insepct and manage cluster resources, and view logs.
Kubectl get
use kubectl get to list objects in the Kubernetes cluster
kubectl get <object type> <object name> -o <output> --sort-by <JSONPATH> -- selector <selector>
-o = Set output format
--sort-by = Sort output using a JSONPath expression.
--selector - Filter results by label.
kubectl get pods // its to give to you the list of pods
kubectl describe
We can get detailed information about kubernetes objects using kubectl describe.
Kubectl describe <object type> <object name>
kubectl describe pods my-pod // to detail information of my-pod
kubectl create
kubectl create to create objects.
Supply a yaml file with -f to create an object form a yaml descriptor stored in the file
if we attempt to create an object that already exists, an error will occur.
kubectl create -f <file name>
kubectl apply
kubectl apply is similar to kubectl create.
However, if you use kubectl apply on an object that already exists, it will modify the existing object, if possible.
kubectl apply -f <file name>
kubectl delete
Use kubectl delete to delete objects from the cluster.
kubectl delete <object type > <object name>
kubectl exec
kubectl exec can be used to run commands inside containers. Keep in mind that, in order for a command to succeed, the necessary software must exist within the container to run it.
For pods with multiple containers, specify the container name with -c.
kubectl exec <pod name> -c <container name> -- <command>
kubectl api-resources // list all shortcuts name of resources
Kubectl Tips
Declarative
Define objects using data structures such as YAML or JSON
Imperative
Define objects using kubectl commands and flags. Some people find imperative commands faster.
Experimnet and see what works for you!
kubectl create deployment my-deployment --image=nginx
Quick Sample YAML
use the --dry-run flag to run an imperative command without creating an object. Combine it with -o yaml to quickly obtain a sample yaml file we can manipulate !
kubectl create deployment my-deploymnet --image=nginx --dry-run -o yaml
Record a Command
Use the --record flag to record the command that was used to make a change.
Comments
Post a Comment