This is the 17th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Basic commands
create
Create resources from files or standard input (STDIN).
Create a resource
kubectl create -f ./nginx.yaml
Create all yamL resources in the current directory
kubectl create -f .
Create resources using multiple files
kubectl create -f ./nginx1.yaml -f ./mysql2.yaml
Use all the manifest files in the directory to create the resource
kubectl create -f ./dir
Use urls to create resources
kubectl create -f xxx.git.io/vxxzso.yaml
run
Run a specific image in the cluster.
Create pod with terminal
kubectl run -i –tty busybox –image=busybox
Start an instance of nginx
kubectl run nginx –image=nginx
Start multiple POD instances
kubectl run mybusybox –image=busybox –replicas=5
explain
Get the documentation for the resource.
Get documentation for POD and SVC
kubectl explain pods,svc
get
Displays one or more resources.
Viewing nodes
kubectl get nodes
Query resources using yamL files
kubectl get -f xxx.yaml
Query resources
kubectl get resourcequota
Query endpoints
kubectl get endpoints
To view the pods
Viewing a specified spacekube-system
The pods
kubectl get po -n kube-system
View pods details for all Spaces
kubectl get pods -o wide –all-namespaces
View details about the Pods of the specified space kube-system
kubectl get pod -o wide –namespace=kube-system
Access to the service (SVC)
View services in all command Spaces
kubectl get svc –all-namespaces
See other ways of writing service for all command Spaces
kubectl get services –all-namespaces
Query events
kubectl get events –all-namespaces
View events under a namespace
kubectl get events -n kube-system
View events in a namespace and filter by keyword
kubectl get events -n kube-system | grep “name”
Use lable to query information
kubectl get pods -l app=nginx -o yaml|grep podIP
edit
Edit resources on the server side.
Edit the service named docker-Registry
kubectl edit svc/docker-registry
delete
Delete resources based on file, standard input (STDIN), resource name, or resource label.
Delete the pod of type and name defined in the pod.json file
kubectl delete -f ./pod.json
Delete pod named “baz” and service named “foo”
kubectl delete pod,service baz foo
Delete pod and Serivce with the name=myLabel label
kubectl delete pods,services -l name=myLabel
Delete pods and Services with the name=myLabel label, including those that have not been initialized
kubectl delete pods,services -l name=myLabel –include-uninitialized
Delete all pods and serivce in the my-ns namespace, including uninitialized ones
kubectl -n my-ns delete po,svc –all
Forcibly remove pod(e.g., Prometheus-7fCFCB9F89-qKKf7)
kubectl delete pods prometheus-7fcfcb9f89-qkkf7 –grace-period=0 –force
The deployment command
rollout
View the history of the revised version
kubectl rollout history deployment nginx-deployment
Rollback to a specified version
If you do not add –to-revision= version number, by default, you are reverted to the previous version.
kubectl rollout undo deployment nginx-deployment –to-revision=1
scale
The copy of the application expands or shrinks as the load increases or shrinks.
Expand the number of copies to 4
Kubectl scale rc rC-nginx-3 — replicas=4
Reduce the number of copies again to 2
Kubectl scale rc rC-nginx-3 — replicas=2
autoscale
Although scale can be very convenient to expand or reduce the number of copies, but still need manual intervention, can not be real-time automatic expansion and reduction of copies according to the system load.
The autoscale command provides the ability to automatically scale copies of a POD based on its load.
The autoscale command assigns a range of copies to an RC. Pods are automatically expanded or shrunk within the range based on the load of programs running in the POD.
As with the nginx created earlier, you can use the following command to specify the replica range from 1 to 4.
Kubectl autoscale rc rc-nginx-3 — min=1 — Max =4
Cluster Management Commands
cluster-info
Viewing Cluster Information
kubectl cluster-info
View more detailed cluster information.
kubectl cluster-info dump
top
Displays the CPU, memory, and storage resource usage.
Displays the resource usage of a K8S-node
kubectl top node k8s-node
Displays resource usage of all nodes in the cluster
kubectl top node
Displays resource usage for pods in the specified namespace (for example, logging)
kubectl top pod -n logging
cordon
Flag the node is not schedulable.
Flag k8S-node is unschedulable
kubectl cordon k8s-node
uncordon
Tag nodes are schedulable.
Flag k8S-node is schedulable
kubectl uncordon k8s-node
drain
A node is excluded and ready for maintenance
The K8S-node is excluded and ready for maintenance
kubectl drain k8s-node
Troubleshooting and diagnostic commands
describe
Displays detailed information about a particular resource or resource group.
Show a description of a POD when we find that it is too late to create one
kubectl describe pod xxx
Displays the CPU, GPU, and memory usage of cluster nodes.
kubectl describe nodes
Displays the resource usage of a node (for example, Node-work-2) in the cluster.
kubectl describe node node-work-2
logs
Prints the log of a container in Pod.
View the logs of the specified Pod
kubectl logs -f kube-dns-699984412-vz1q6 -n kube-system
View the last 10 lines of the log for the specified POD
kubectl logs –tail=10 nginx
Specify one of the containers of the Pod to view the logs
kubectl logs kube-dns-699984412-n5zkz -c kubedns –namespace=kube-system
View the scroll log for the last 10 lines of the container specified by the specified Pod
kubectl logs -f –tail=10 kube-dns-699984412-vz1q6 -c manager -n kube-system
exec
The exec command is also similar to the docker exec command. To execute a shell command in an already running container, if there are multiple containers in a POD container, use the -c option to specify the container.
Into the container
kubectl exec -it codeleak-github-cron-1567581840-zsqpc /bin/bash
Execute commands in containers where the specified namespace (e.g., ns) already exists (if there is only one container)
kubectl exec nginx-pod -n ns — ls /
Execute commands in existing containers (which container needs to be specified if there are multiple containers in pod)
kubectl exec nginx-pod -c my-container — ls /
Note: before the shell command, add –; otherwise, the parameters in the shell command will not be recognized
Senior command
apply
The configuration is applied to the resource by filename or standard input (stdin)
Update the resource
kubectl apply -f rc-nginx.yaml
Apply the JSON configuration entered from the console to the Pod
cat pod.json | kubectl apply -f –
Set the command
label
Update (add, modify, or delete) labels on resources.
The label must start with a letter or number and can contain up to 63 letters, digits, hyphens, periods, and underscores.
If –overwrite is true, existing labels can be overwritten, otherwise an attempt to overwrite a label will result in an error.
If –resource-version is specified, the update will use this resource version, otherwise the existing resource version will be used.
Add a lable for a node
Kubectl Label Nodes 10.126.72.31 points=test
View all nodes and lable
kubectl get nodes –show-labels
annotate
Update the Annotations information for the resource.
Update pod(e.g., foo) to set its annotation ‘description’ to ‘my frontend’.
kubectl annotate pods foo description=’my frontend’
Note: If the same annotation is assigned more than once, only the value set last time is saved.
Other commands
version
View the version information about the client and server.
kubectl version
api-versions
Output the API version supported by the server in group/version format.
kubectl api-versions
api-resources
Output the resource types supported by the server API.
kubectl api-resources
Reference documentation
- kubectl-commands
- kubectl