容器化进阶之路

kubectl操作指南

2020-03-28  本文已影响0人  hyperjiang

概述

kubectl是控制k8s集群的命令行工具,它默认会从~/.kube/config读取配置。

使用格式:

kubectl [command] [TYPE] [NAME] [flags]
kubectl get pod pod1
kubectl get pods pod1
kubectl get po pod1

在多个资源上执行操作时,可以通过类型和名称指定每个资源,也可以指定一个或多个文件,比如:

# kubectl [command] TYPE1 name1 name2 name<#>
kubectl get pod example-pod1 example-pod2

# kubectl [command] TYPE1/name1 TYPE1/name2 TYPE2/name3 TYPE<#>/name<#>
kubectl get pod/example-pod1 replicationcontroller/example-rc1

# kubectl [command] [TYPE] -f file1 -f file2 -f file<#>
kubectl get pod -f ./pod1.yaml -f ./pod2.yaml

详细命令说明参考:https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands

常用命令

# 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>
# 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. 
kubectl get rc <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
# 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
# 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 pods, including uninitialized ones.
kubectl delete pods --all
# 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
# 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>
# Diff resources included in "pod.json".
kubectl diff -f pod.json

# Diff file read from stdin.
cat service.yaml | kubectl diff -f -
上一篇 下一篇

猜你喜欢

热点阅读