Kubectl 常用命令复习
通用公式
kubectl
+ [command]
+ [type]
+ [name]
+[flags]
command:指定要对一个或多个资源执行的操作,例如create、get、describe、delete等。(增删改查)
type:指定资源类型。资源类型不区分大小写,可以指定单数、复数或缩写形式。
name:指定资源的名称。名称区分大小写。如果省略名称,则显示所有资源的详细信息:kubectl get pods
。
flags: 指定可选的参数。例如,可以使用-s或-server参数指定 Kubernetes API服务器的地址和端口。
注意事项说明:
从命令行指定的参数会覆盖默认值和任何相应的环境变量
命令汇总
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| get
describe
create
edit
delete
log
rolling-update
exec
port-forward
proxy
run
expose
label
config
cluster-info
api-versions
version
help
ingress-nginx
|
kubectl get - 显示一个或者多个资源信息
点我查看
常用可选参数
1 2 3 4 5 6
| -o wide/yaml/json (列表格式/yaml格式/json格式)
-l key=value
-n 命名空间 -A 所有命名空间
|
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| kubectl get componentstatuses (简写cs)
kubectl get namespace
kubectl get pods -A
kubectl get pods -o wide
kubectl get replicationcontroller mysql-default-0
kubectl get -o json pod mysql-default-0
kubectl get -f pod.yaml -o json
kubectl get -o template pod/mysql-default-0 --template {{.status.phase}}
kubectl get rc,services
kubectl get rc/web service/frontend pods/mysql-default-0
kubectl get cm -n namespace
kubectl edit cm -n namespace cm名称
kubectl get deploy -n namespace
kubectl edit deploy -n namespace deploy名称
kubectl get secrets -n namespace
kubectl edit secrets -n namespace secret名称
kubectl get all
|
kubectl apply- 基于文件名或标准输入,将新的配置应用到资源上
点我查看
kubectl apply
命令用于以声明方式创建和更新对象
nginx.yaml
1 2 3 4 5 6 7 8 9 10 11
| apiVersion: v1 kind: Pod metadata: name: fl-nginx labels: app: fl-nginx type: front-end-service spec: containers: - name: nginx-container image: nginx:1.18
|
1
| kubectl apply -f nginx.yaml
|

示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| kubectl apply -f ./pod.json
cat pod.json | kubectl apply -f -
kubectl apply -f ./my-manifest.yaml
kubectl apply -f ./my1.yaml -f ./my2.yaml
kubectl apply -f ./dir
kubectl apply -f https://git.io/vPieo
|
kubectl describe - 显示某个资源或某组资源的详细信息
点我查看
支持的资源类型包括但不限于:pods (po)、services (svc)、 replicationcontrollers (rc)、nodes (no)、events (ev)、componentstatuses (cs)、 limitranges (limits)、persistentvolumes (pv)、persistentvolumeclaims (pvc)、 resourcequotas (quota)和secrets。
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| kubectl describe nodes ulgqv45xaqziqeri -n defalut
kubectl describe pods/kube-apiserver-db1 -n kube-system
kubectl describe -f nginx.yaml
kubectl describe pods --all-namespaces
kubectl describe po -l k8s-app=calico-kube-controllers --all-namespaces
|
kubectl create - 通过文件或标准输入来创建资源
点我查看
示例
通过配置文件名或 stdin
创建一个集群资源对象。
支持 JSON 和 YAML 格式的文件
1
| kubectl create -f ./nginx.yaml
|
通过stdin的JSON创建一个pod
1
| cat pod.json | kubectl create -f -
|
kubectl delete - 通过文件名、标准输入、资源和名字删除资源,或者通过资源和标签选择算符来删除资源
点我查看
示例
1 2 3 4 5 6 7 8 9 10 11
| kubectl delete -f pod.yaml
kubectl delete pods,services -l name=<label-name>
kubectl delete pods,services -l name=<label-name> --include-uninitialized
kubectl delete pods --all
|
kubectl edit - 修改服务器上的某资源
点我查看
示例
1 2 3 4 5 6 7 8 9 10 11
| kubectl edit svc/mysql
KUBE_EDITOR="nano" kubectl edit svc/docker-registry
kubectl edit job.v1.batch/myjob -o json
kubectl edit deployment/mydeployment -o yaml --save-config
|
kubectl logs - 输出 Pod 中某容器的日志
点我查看
1
| kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l 'app in (ks-install, ks-installer)' -o jsonpath='{.items[0].metadata.name}') -f
|
解析一下这行代码
-n 指定命名空间
-f 持续输出 类似与 tail -f
的方式
$() 包裹其他命令
1
| kubectl logs -f <pod_name>
|
kubectl exec - 在容器中执行相关命令
点我查看
1 2 3 4 5 6 7 8 9 10 11 12
| kubectl exec[POD名称] -- bash -c'[需要执行的命令]'
kubectl exec my-pod --bash -c 'ps -ef| grep hello'
kubectl get pods -o name -n your-namespace |grep -v "demo\|hello" | xargs -I{} kubectl -n your-namespace exec {} -- bash -c 'ps ux|grep ng'
kubectl exec -it <pod-name> -n <name-space> bash
|
示例
1
| kubectl exec "$(kubectl get pod -l app=ratings -o jsonpath='{.items[0].metadata.name}')" -c ratings -- curl -sS productpage:9080/productpage | grep -o "<title>.*</title>"
|
kubectl run - 在集群中使用指定镜像启动容器
点我查看
格式:
1
| kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...]
|
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| kubectl run nginx --image=nginx
kubectl run hazelcast --image=hazelcast --port=5701
kubectl run hazelcast --image=hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"
kubectl run nginx --image=nginx --replicas=5
kubectl run nginx --image=nginx --dry-run
kubectl run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }'
kubectl run -i --tty busybox --image=busybox --restart=Never
kubectl run nginx --image=nginx -- <arg1> <arg2> ... <argN>
kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>
kubectl run pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'
|