目錄
一 Kubernetes常用命令
1 獲取命令
kubectl --help
Basic Commands (Beginner):
create Create a resource from a file or from stdin.
expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
run Run a particular image on the cluster
set Set specific features on objects
Basic Commands (Intermediate):
explain Documentation of resources
get Display one or many resources
edit Edit a resource on the server
delete Delete resources by filenames, stdin, resources and names, or by resources and label selector
Deploy Commands:
rollout Manage the rollout of a resource
scale Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job
autoscale Auto-scale a Deployment, ReplicaSet, or ReplicationController
Cluster Management Commands:
certificate Modify certificate resources.
cluster-info Display cluster info
top Display Resource (CPU/Memory/Storage) usage.
cordon Mark node as unschedulable
uncordon Mark node as schedulable
drain Drain node in preparation for maintenance
taint Update the taints on one or more nodes
Troubleshooting and Debugging Commands:
describe Show details of a specific resource or group of resources
logs Print the logs for a container in a pod
attach Attach to a running container
exec Execute a command in a container
port-forward Forward one or more local ports to a pod
proxy Run a proxy to the Kubernetes API server
cp Copy files and directories to and from containers.
auth Inspect authorization
Advanced Commands:
apply Apply a configuration to a resource by filename or stdin
patch Update field(s) of a resource using strategic merge patch
replace Replace a resource by filename or stdin
wait Experimental: Wait for a specific condition on one or many resources.
convert Convert config files between different API versions
Settings Commands:
label Update the labels on a resource
annotate Update the annotations on a resource
completion Output shell completion code for the specified shell (bash or zsh)
Other Commands:
alpha Commands for features in alpha
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of "group/version"
config Modify kubeconfig files
plugin Provides utilities for interacting with plugins.
version Print the client and server version information
1.1 獲取子命令
kubectl <command> --help
kubectl create --help
2 常用命令介紹
2.1 run
創建容器鏡像
1:啟動nginx實例
kubectl run nginx --image=nginx
2:啟動nginx實例,暴露容器端口80,設置副本數2
kubectl run nginx --image=nginx --port=80 --replicas=2
2.2 expose
為Pod資源創建一個service
1:為nginx創建service,並通過Service的80端口轉發至容器的80端口上
kubectl expose deployment nginx --name=nginx --port=80 --target-port==80
2.3 create
創建一個集群資源對象(Pod,Service,job,configMap,namespace...)
1:基於命令創建資源對象
kubectl create deployment my-dep --image=busybox
kubectl create service clusterip my-cs --tcp=5678:8080
2:基於文件創建資源對象
kubectl create -f pod.yaml
kubectl create -f service.yaml
2.4 delete
刪除資源對象(Pod,Service,job,configMap,namespace...)
1:基於命令刪除資源對象
kubectl delete deployment my-dep
kubectl delete service my-cs
2:基於文件刪除資源對象
kubectl delete -f pod.yaml
kubectl delete -f service.yaml
2.5 apply
創建一個集群資源對象(Pod,Service,job,configMap,namespace...)
1:基於文件創建資源對象
kubectl apply -f pod.yaml
kubectl apply -f service.yaml
2:apply與create區別
kubectl apply -f FILENAME 可多次執行,每次修改資源清單后,使用apply可直接部署
kubectl create -f FILENAME 不可多次執行,每次修改資源清單后,需要先刪除資源后才能部署
推薦使用apply部署資源對象,delete刪除資源對象
2.6 get
獲取資源信息(Pod,Service,job,configMap,namespace...)
1:列出所有運行的Pod信息,列出kube-system命令空間中運行的Pod
kubectl get pods
kubectl get pods -n kube-system
2:列出Pod以及運行Pod節點信息
kubectl get pods -o wide
3:以JSON格式輸出一個pod信息
kubectl get pod nginx -o json
4:列出所有replication controllers,deployment和service信息
kubectl get rc
kubectl get deployment
kubectl get services
5:列出所有不同的資源對象
kubectl get all
6:列出集群組件運行狀態信息
kubectl get cs
2.7 explain
列出支持的資源的字段,用於資源清單文件編寫資料查看<.yaml>(Pod,Service,job,configMap,namespace...)
1:獲取pods/service支持的資源字段
kubectl explain pods
kubectl explain service
2:獲取pods資源下spec資源支持的字段
kubectl explain pods。spec
2.8 describe
顯示特定資源的信息
1:顯示pods nginx的詳細信息
kubectl describe pods nginx
2:顯示service nginx的詳細信息
kubectl describe svc nginx
3:顯示節點1的詳細信息
kubectl describe node k8s-node1
2.9 scale
擴縮Pod數量(擴容或縮容 Deployment、ReplicaSet、Replication Controller或 Job 中Pod數量)
1:將名為nginx的pod副本數設置為3
kubectl scale --replicas=3 deployment nginx
2:將名為nginx的pod副本數設置為1
kubectl scale --replicas=1 deployment nginx
2.10 set
配置應用資源(升級、回滾等)
1:設置名為nginx的pod運行的鏡像為:nginx:1.14-alpine
kubectl set image deployment nginx nginx=nginx:1.14-alpine
2.11 rollout
對資源進行管理
1:查看歷史版本
kubectl rollout history deployment nginx
2:暫停資源 ()只要deployment在暫停中,使用deployment更新將不會生效)
kubectl rollout pause deployment nginx
3:恢復暫停資源
kubectl rollout resume deployment nginx
4:查看資源狀態
kubectl rollout status deployment nginx
5:回滾版本
kubectl rollout undo deployment nginx
2.12 exec
在容器中執行命令
1:連接Pod終端
kubectl exec -it PODNAME -- sh
2:連接kube-system命名空間中的Pod終端
kubectl exec -it -n kube-system PODNAME -- sh
2.13 logs
打印Pod日志:
1:查看Pod日志
kubectl logs PODNAME
2.14 cp
拷貝文件
1: 從msster節點拷貝文件到Pod
kubectl cp /etc/fstab PODNAME:/tmp
2:從Pod節點拷貝文件到msster
kubectl cp PODNAME:/tmp /etc/fstab
