Kubernetes – Kubectl

Kubectl是一个命令行工具,用于在Kubernetes集群上执行命令。

kubectl 语法

使用以下语法从终端窗口运行kubectl命令:

kubectl [command] [TYPE] [NAME] [flags]
  • command 指定希望对一个或多个资源执行的操作,例如create, get, describe, delete
  • TYPE 资源类型, 例如pod,可以是单复数形式,或或者简写形式。例如:
    kubectl get pod pod1
    kubectl get pods pod1
    kubectl get po pod1
    
  • NAME 资源的名称,例如kubectl describe pod example-pod1, example-pod1是名称
  • flags 选项标志,例如可以使用-s--server标志来指定Kubernetes API服务器的地址和端口。

常用命令

kubectl apply

从文件应用或更新资源。

# Create a service using the definition in example-service.yaml.
kubectl apply -f example-service.yaml

# Create a replication controller using the definition in example-controller.yaml.
kubectl apply -f example-controller.yaml

# Create the objects that are defined in any .yaml, .yml, or .json file within the <directory> directory.
kubectl apply -f <directory>

kubectl get

列出资源。

# List all pods in plain-text output format.
kubectl get pods

# List all pods in plain-text output format and include additional information (such as node name).
kubectl get pods -o wide

# List the replication controller with the specified name in plain-text output format. Tip: You can shorten and replace the 'replicationcontroller' resource type with the alias 'rc'.
kubectl get replicationcontroller <rc-name>

# List all replication controllers and services together in plain-text output format.
kubectl get rc,services

# List all daemon sets in plain-text output format.
kubectl get ds

# List all pods running on node server01
kubectl get pods --field-selector=spec.nodeName=server01

kubectl describe

获取资源详情。

# Display the details of the node with name <node-name>.
kubectl describe nodes <node-name>

# Display the details of the pod with name <pod-name>.
kubectl describe pods/<pod-name>

# Display the details of all the pods that are managed by the replication controller named <rc-name>.
# Remember: Any pods that are created by the replication controller get prefixed with the name of the replication controller.
kubectl describe pods <rc-name>

# Describe all pods
kubectl describe pods

kubectl delete

删除资源。

# Delete a pod using the type and name specified in the pod.yaml file.
kubectl delete -f pod.yaml

# Delete all the pods and services that have the label name=<label-name>.
kubectl delete pods,services -l name=<label-name>

# Delete all the pods and services that have the label name=<label-name>.
kubectl delete pods,services -l name=<label-name>

# Delete all pods, including uninitialized ones.
kubectl delete pods --all

kubectl exec

对pod中的容器执行命令。

# Get output from running 'date' from pod <pod-name>. By default, output is from the first container.
kubectl exec <pod-name> date

# Get output from running 'date' in container <container-name> of pod <pod-name>.
kubectl exec <pod-name> -c <container-name> date

# Get an interactive TTY and run /bin/bash from pod <pod-name>. By default, output is from the first container.
kubectl exec -ti <pod-name> /bin/bash

kubectl logs

打印pod日志。

# Return a snapshot of the logs from pod <pod-name>.
kubectl logs <pod-name>

# Start streaming the logs from pod <pod-name>. This is similar to the 'tail -f' Linux command.
kubectl logs -f <pod-name>


浙ICP备17015664号 浙公网安备 33011002012336号 联系我们 网站地图  
@2019 qikegu.com 版权所有,禁止转载