K8s common command collection

Posted by [e]r!k on Wed, 05 Jan 2022 07:21:02 +0100

The purpose of this article is to record the CLI command set commonly used in the use of K8s. The following is a brief classification of the related commands for different operations of various resource objects in K8s cluster.

1. Create resources

Generally, there are two ways to create resources: through files or commands.

# Create a Deployment from a file
kubectl create -f /path/to/deployment.yaml
cat /path/to/deployment.yaml | kubectl create -f -
# However, the following commands may be more commonly used to create resources
kubectl apply -f /path/to/deployment.yaml

# Create directly through kubectl command
kubectl run nginx_app --image=nginx:1.9.1 --replicas=3 

Kubectl also provides commands to update resources, such as kubectl edit, kubectl patch, and kubectl replace.

# kubectl edit: it is equivalent to using get to obtain resources, then updating them, and finally apply ing the updated resources
kubectl edit deployment/nginx_app

# kubectl patch: use the patch to modify and update the fields of a resource, such as updating a node
kubectl patch node/node-0 -p '{"spec":{"unschedulable":true}}'
kubectl patch -f node-0.json -p '{"spec": {"unschedulable": "true"}}'

# kubectl replace: use the configuration file to replace the resource
kubectl replace -f /path/to/new_nginx_app.yaml

2. View resources

Get information about different kinds of resources.

# The format of general commands is as follows:
kubectl get <resource_type>
# For example, get the pod information under the K8s cluster
kubectl get pod
# More detailed information
kubectl get pod -o wide
# Specify the information of the resource, format: kubectl get < resource_ type>/<resource_ Name >, such as obtaining deployment nginx_app information
kubectl get deployment/nginx_app -o wide
# You can also format and output the specified resources, such as json, yaml, etc
kubectl get deployment/nginx_app -o json
# You can also customize the output results. For example, only the container name and image name can be output for the pod
kubectl get pod httpd-app-5bc589d9f7-rnhj7 -o custom-columns=CONTAINER:.spec.containers[0].name,IMAGE:.spec.containers[0].image
# The value of a specific key can also be obtained by entering the following command. This directory refers to the usage of go template, and the '\ n' at the end of the command is to wrap the output result
kubectl get pod httpd-app-5bc589d9f7-rnhj7 -o template --template='{{(index spec.containers 0).name}}{{"\n"}}'
# There are also some options to filter the results, which are not listed here. If you are interested, please refer to kubectl get --help description

3. Deploy command set

Deployment commands include resource operation management commands, capacity expansion and reduction commands, and automatic capacity expansion and reduction commands.

3.1 rollout command

Manage the operation of resources, such as d eployment, daemon, StatefulSet, etc.

  • View deployment status: for example, update deployment / nginx_ View the update status of the container after it is mirrored in the app.
kubectl set image deployment/nginx_app nginx=nginx:1.9.1
kubectl rollout status deployment/nginx_app
  • Pause and resume of resources: pause a Deployment before issuing one or more updates, and then resume it, so that multiple repairs can be carried out during the pause of Deployment without issuing unnecessary rollout s.
# suspend
kubectl rollout pause deployment/nginx_app
# Restore after all update operation commands are completed
kubectl rollout resume deployment/nginx_app
  • Rollback: the image of a Deployment is updated as above, but it can be rolled back in case of update failure or wrong update.
# View the historical version information before rollback
kubectl rollout history deployment/nginx_app
# RollBACK 
kubectl rollout undo deployment/nginx_app
# Of course, you can also specify the version number to roll back to the specified version
kubectl rollout undo deployment/nginx_app --to-revision=<version_index>

3.2 scale command

Expand / shrink a Deployment, RS, StatefulSet.

# Capacity expansion
kubectl scale deployment/nginx_app --replicas=5
# If the volume is reduced, set the corresponding number of copies to be smaller than the current number of copies
# In addition, you can also restrict the current number of copies. For example, if the current number of copies is 5, shrink to 3
kubectl scale --current-replicas=5 --replicas=3 deployment/nginx_app

3.3 autoscale command

By creating an autoscaler, you can automatically select and set the number of pods in the K8s cluster.

# Create 3-10 pod s based on CPU utilization
kubectl autoscale deployment/nginx_app --min=3 --max=10 --cpu_percent=80

4. Cluster management command

4.1 cordon & uncordon command

Set whether the pod can be scheduled to this node.

# Non schedulable
kubectl cordon node-0

# When a node needs maintenance, all pods on the node can be expelled (the pods on the node will be deleted and automatically set through the above command)
# This node is not schedulable, and then restart pods on other available nodes)
kubectl drain node-0
# After the maintenance is completed, the node can be set as schedulable
kubectl uncordon node-0

4.2 taint command

At present, it can only be used on node resources. Generally, this command will be used in combination with the tolerances field of the pod. The pod without the corresponding tolerance will not be scheduled to the node with the taint, so as to avoid the pod being scheduled to an inappropriate node. The taint of a node generally includes key, value and effect(effect can only be taken in noschedule, prefernoschedule and noexecute).

# Set taint
kubecl taint nodes node-0 key1=value1:NoSchedule
# Remove taint
kubecl taint nodes node-0 key1:NoSchedule-

If the pod wants to be dispatched to node-0 with taint set above, it needs to be set in the descriptions field of the spec of the Pod:

tolerations:
- key: "key1"
  operator: "Equal"
  value: "value1"
  effect: "NoSchedule"

# perhaps
tolerations:
- key: "key1"
  operator: "Exists"
  effect: "NoSchedule"

5. Others

# Mapping ports allow external access
kubectl expose deployment/nginx_app --type='NodePort' --port=80
# Then view the randomly mapped ports through kubectl get services -o wide
# In this way, the nginx service can be accessed through the external IP and port of the node

# Forward the application service that accesses the Pod on the local port
kubectl port-forward nginx_app_pod_0 8090:80
# In this way, you can access: curl -i localhost:8090 locally

# If the expected results are not achieved when creating or starting some resources, you can use the following command to simply locate the fault first
kubectl describe deployment/nginx_app
kubectl logs nginx_pods
kubectl exec nginx_pod -c nginx-app <command>

# The internal call interface of the cluster (such as curl command) can be used as a proxy, and the returned ip and port can be used as the base URL
kubectl proxy &

# View the complete list of resources supported by K8s
kubectl api-resources

# View the api versions supported by K8s
kubectl api-versions

Topics: Cyber Security Cloud Native