An overview of the
Kubectl is the command line tool of K8S cluster, through Kubectl can manage the cluster itself, and can install and deploy containerization applications on the cluster.
kubectl [command] [type] [name] [flags]
Copy the code
commad
: Specifies the operations to be performed on the resource, such as create, GET, Describe, and DELETEtype
: Specifies the resource type. The resource type is case sensitive.name
: Specifies the name of the resource, which is also case-sensitiveflags
: Specifies optional parameters. For example, the -s or -server arguments can be used to specify the address and port of the Kubernetes API Server.
Kubectl main command
- Basic commands
operation | instructions |
---|---|
create | Create resources using file names or standard input |
expose | Expose a resource as a new Service |
run | Run a specific image in the cluster |
set | Set specific functions on objects |
get | Displays one or more resources |
explain | Documentation References |
edit | Edit a resource using the default editor |
delete | Delete a resource by filename, standard input, resource name, or label selector |
2. Deploy commands
operation | instructions |
---|---|
rollout | Manage the release of resources |
rollout-update | Roll updates to the given replication controller |
scale | Expand or reduce the number of PODS, Deployment, ReplicaSet, RC, or Jobs |
autoscale | Create an automatic capacity expansion or reduction option and set the number of PODS |
- Cluster Management Commands
operation | instructions |
---|---|
certificate | Modifying certificate Resources |
cluster-info | Displaying Cluster Information |
top | Display resources (CPU/Memory/Storage) use, need to run Heapster |
cordon | Flag the node is not schedulable |
uncordon | Tag nodes are schedulable |
drain | Delete applications from the node and prepare for offline maintenance |
taint | Modify the node taint flag |
4. Fault diagnosis and debugging commands
operation | instructions |
---|---|
describe | Displays detailed information about a particular resource or resource group |
logs | Print a container log in a POD. If the POD has only one container, the container name is optional |
attach | Attach to a running container |
exec | Execute commands to containers |
port-forward | Forward one or more local ports to a POD |
proxy | Run a proxy into Kubernetes API Server |
cp | Copy files or directories to containers |
auth | Check the authorization |
- Other commands
operation | instructions |
---|---|
apply | Apply configuration to resources by file name or standard input |
patch | Use patches to modify and update resource fields |
replace | Replace a resource with a filename or standard input |
convert | Convert configuration files between different API versions |
label | Update labels on resources |
annotate | Update comments on resources |
completion | Used to achieve kubectl command automatic completion |
api-versions | Prints supported API versions |
config | Kubeconfig file (for accessing apis, such as configuring authentication information) |
help | Print command help |
plugin | Run a command line plug-in |
version | Print client and service version information |
Gets the specific syntax of the command
kubectl <command> --help
Copy the code
Gets all resource types currently supported by the server
kubectl api-resources
Copy the code
Prints all the optional flags currently supported
kubectl options
Copy the code
For example, -s can specify the Kubernetes API server address, -n can specify the namespace, –kubeconfig can specify the Kubeconfig configuration file
(Figure source network)
Format output command
By default, the default output format of all Kubectl commands is readable plain text. To output details to a terminal window in a specific format, we need to use “-o” or multiple “-output” flags.
Command format:
kubectl [command] [TYPE] [NAME] -o=<output_format>
Copy the code
Common output formats
The output format | describe |
---|---|
-o=custom-columns=<spec> | Enter the specified comma-separated list of column names to print the table. |
-o=custom-columns-file=<filename> | Use the custom column template in the file to print the table. |
-o=json | Outputs an API object in JSON format. |
-o=jsonpath=<template> | Print the fields defined in the JsonPath expression |
-o=jsonpath-file=<filename> | Prints the fields defined by the JSONPath expression in the file. |
-o=name | Only the resource name is printed. |
-o=wide | Output any additional information in plain text format. For pods, include node names. |
-o=yaml | Output YAML API objects. |
Examples of Common Commands
- Create a resource
#According to the YAML file to create the corresponding resource, with the record parameter will record the update history, can be added or not added
kubectl apply -f nginx.yaml --record
Copy the code
- See the resources
#View resources in the specified namespace
kubectl get pod -n default
Copy the code
#Viewing All Resources
kubectl get pod -A
Copy the code
#Viewing POD Details
kubectl get pod nginxdeployment-585449566-h6hng -n default -o wide
Copy the code
- Delete the resource
kubectl delete pod nginxdeployment-585449566-h6hng -n default -o wide
#or
kubectl delete -f nginx.yaml
Copy the code
- Manually expand or shrink the capacity
#If the replicas is larger than the current NUMBER of PODS, it is expanded; if the replicas is smaller, it is reduced
kubectl scale deployment nginxdeployment --replicas=4
Copy the code
- The rollback
#Rollback (back to the last revision in the record)
kubectl rollout undo deployment nginxdeployment
#See the history
kubectl rollout history deployment nginxdeployment
Copy the code
6. Edit and update resources
#Use the default editor to edit the corresponding resource file. After saving the file, the resource attributes will be updated
kubectl edit -f nginx.yaml
Copy the code
The original two pods were changed to one pod by modifying the nginx.yaml file
Everyday the debug
Take pod resources as an example
- Check whether the corresponding POD exists first. If yes, check whether the status is normal
kubectl get pod coredns-f9fd979d6-kvpmn -n kube-system -o wide
Copy the code
- View details about the POD that needs to be rectified. Note that namespace must be followed
3. If the cause cannot be found in the previous step, print related logs
#Refresh print logs in real time, with namespace followed by n and podName
kubectl logs -f -n kube-system etcd-minikube
#Displays the last 50 lines of the log
kubectl logs --tail=50 -n kube-system etcd-minikube
#Displays logs generated in the past 2h
kubectl logs --since=2h -n kube-system etcd-minikube
#Specifying a configuration file
kubectl --kubeconfig="log-watcher.kubeconfig" logs -f etcd-minikube -n kube-system
Copy the code
- Unable to solve the problem, go inside the container to find the cause
kubectl exec -it -n kube-system etcd-minikube -- sh
Copy the code