Table of contents
- Introduction to kubectl
- Basic commands in Kubectl
- Deploy commands in Kubectl
- Cluster management commands
- Troubleshooting and debugging commands
- Advanced commands
- Wrapping up
Introduction to kubectl
The kubectl command line tool lets you control Kubernetes clusters. For configuration, kubectl looks for a file named config in the $HOME/.kube directory. You can specify other kubeconfig files by setting the KUBECONFIG environment variable or by setting the –kubeconfig flag.
The common format of a kubectl command is:
kubectl <action> <resource> <name-resource>
This performs the specified action (like create, describe) on the specified resource (like node, container) with its name.
To know more about resource types, we can following the link.
Basic commands in Kubectl
Belows are some basic kubectl commands:
-
Check version of kubernetes clusters
If you want to check client and server’s version of Kubernetes cluster, use this command.
kubectl version
For example:
-
Create a resource for cluster
Create a resource from a file or from stdin.
For example, create a Kubernetes deployment from the file.
kubectl create <resource-name>
For example:
-
let’s deploy our app on kubernetes with the deployment name and app image location
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
This performed a few things for you:
- searched for a suitable node where an instance of the application could be run.
- scheduled the application to run on that Node.
- configured the cluster to reschedule the instance on a new Node when needed.
-
-
View the resources in the cluster
kubectl get <resource-name>
For example:
-
View the nodes in the cluster
kubectl get nodes
-
View our deployments
kubectl get deployments
Then, we have:
-
Get a list of running pods or the YAML output of a pod.
-
-
Run a particular image on the cluster
kubectl run
-
Set specific features on on objects
kubectl set
For example:
- set environment variables
- update a Docker image in a pod template
- …
-
Take a service, deployment, or pod and expose it as a new Kubernetes Service
kubectl expose
-
Get the documentation of resources
kubectl explain
For example:
- the documentation on deployments
-
Edit a resource
kubectl edit
For example:
- edit a deployment
-
Delete resources by filenames, stdin, resources, and names, or by resources and label selectors
kubectl delete
Deploy commands in Kubectl
-
Manage the rollout of a resource
kubectl rollout
-
Set a new size for a deployment, ReplicaSet, or StatefulSet
kubectl scale
-
Auto-scale a deployment, ReplicaSet, or StatefulSet
kubectl autoscale
Cluster management commands
-
Modify certificate resources
kubectl certificate
-
Display cluster information
kubectl cluster-info
-
Display resource (CPU/memory/storage) usage
kubectl top
-
Mark a node as unschedulable
kubectl cordon
-
Mark a node as schedulable
kubectl uncordon
-
Drain a node in preparation for maintenance
kubectl drain
-
Update the taints on one or more nodes
kubectl taint
Troubleshooting and debugging commands
-
Show the details of a specific resource or group of resources
kubectl describe
-
Print the logs for a container in a pod
kubectl logs
-
Attach to a running container
kubectl attach
-
Execute a command in a container
kubectl exec
-
Forward one or more local ports to a pod
kubectl port-forward
-
Run a proxy to the Kubernetes API server
kubectl proxy
-
Copy files and directories to and from containers
kubectl cp
-
Inspect authorization
kubectl auth
Advanced commands
-
Show difference of live version against a would-be applied version
kubectl diff
-
Apply a configuration to a resource by filename or stdin
kubectl apply
-
Update the fields of a resource using a strategic merge patch
kubectl patch
-
Replace a resource by filename or stdin
kubectl replace
-
Wait for a specific condition on one or many resources
kubectl wait
-
Convert config files between different API versions
kubectl convert
-
Build a customization target from a directory or a remote URL
kubectl kustomize
Wrapping up
Refer: