Overview
kubectl is a command line tool for kubernetes clusters. Through kubectl, the cluster itself can be managed, and containerized applications can be installed and deployed on the cluster.
Command format
The format is as follows
kubectl [command] [type] [name] [flags]
parameter
command : Specify the operation to be performed on the resource, such as create, get, describe, delete
type : Specify the type of the resource. The resource type is case-sensitive. Developers can use singular, plural, and abbreviated forms. E.g:
kubectl get pod pod1
kubectl get pods pod1
kubectl get po pod1
name : The command to specify the resource. The name is also case-sensitive. If omitted, all resources will be displayed. The same resource type in the same namespace cannot have duplicate name
flags : specify optional parameters. For example, the -s or -server parameter specifies the address and port of the Kunernetes API server
Common commands
kubectl help for more information
Through the help command, you can get help information
#获取kubectl命令
kubectl -h
#获取某个命令的介绍和使用
kubectl get --help
Basic commands
Common basic commands
command | Introduction |
---|---|
create | Create resources from documented or standard input |
expose | Expose a resource as a Service |
run | Run a specific image in the cluster |
set | Specific functions set on the object |
explain | View resource references |
edit | Edit a resource using the default editor |
delete | Delete resources by file name, standard input, resource name or tag |
Deployment command
command | Introduction |
---|---|
rollout | Manage the release of resources |
rolling | Rolling updates to the given replication controller |
scale | Expand or shrink the number of Pods, Deployment, RS, RC or Job |
autoscale | Create an automatic selection of expansion or shrinkage and set the number of copies of the Pod |
Cluster management commands
command | Introduction |
---|---|
certificate | Modify certificate resources |
cluster-info | Display cluster information |
top | Display resources (CPU/Memory/Storage) |
cordon | Mark node is not schedulable |
uncordon | Marked nodes can be scheduled |
drain | Evict the application on the node and prepare to go offline for maintenance |
taint | Modify the stain of the node |
Fault and debug commands
command | Introduction |
---|---|
describe | Display detailed information about a specific resource or resource group |
logs | Print a container log in a Pod. If the Pod has multiple containers, the container command is optional |
attach | Attach to a running container |
exec | Execute command to container |
port-forwrd | Forward one or more local ports to pod |
proxy | Run a proxy to the kubernetes API server |
cp | Copy files or directories to the container, and copy files or directories from the container to the machine |
auth | Check authorization |
Other commands
command | Introduction |
---|---|
diff | Compare the changes of old and new resources |
apply | Apply configuration to resources through files |
patch | Use patches to modify and update resource fields |
replace | Replace a resource by file name or standard input |
convert | Convert configuration files between different API versions |
label | Update the label on the resource |
annotate | Update the comment on the resource |
completion | Used to realize automatic completion of kubectl tool |
api-versions | Print the supported API version |
config | Modify the kubeconfig file (used to access the API, such as configuring authentication information) |
help | All commands help |
plugin | Run a command line plugin |
version | Print client and service version information |
Names commonly used at present
#创建一个nginx镜像
kubectl create deployment nginx --image=nginx
#对外暴露端口
kubectl expose deployment nginx --port=80 --type=NodePort
#查看资源
kubectl get pod,svc
#通过文件删除一个资源
kubectl delete -f nginx.yaml