Kubectl 是 Kubernetes 最重要的命令行工具。在 Flant,我们会在 Wiki 和 Slack 上相互分享 Kubectl 的妙用(其实我们还有个搜索引擎,不过那就是另外一回事了)。多年以来,我们在 kubectl 方面积累了很多技巧,现在想要将其中的部分分享给社区。
我相信很多读者对这些命令都非常熟悉;然而我还是希望读者能够从本文中有所获益,进而提高生产力。
下列内容有的是来自我们的工程师,还有的是来自互联网。我们对后者也进行了测试,并且确认其有效性。
现在开始吧。
--all-namepsaces
就可以。然而不少朋友还不知道,现在这一开关还有了 -A
的缩写。running
状态的 Pod 呢?
kubectl get pods -A --field-selector=status.phase!=Running | grep -v Complete
顺便一说,--field-selector
是个值得深入一点的参数。 kubectl get no -o json | \
jq -r '.items | sort_by(.status.capacity.memory)[]|[.metadata.name,.status.capacity.memory]| @tsv'
kubectl get po -o json --all-namespaces | \
jq '.items | group_by(.spec.nodeName) | map({"nodeName": .[0].spec.nodeName, "count": length}) | sort_by(.count)'
$ ns=my-namespace
$ pod_template=my-pod
$ kubectl get node | grep -v \"$(kubectl -n ${ns} get pod --all-namespaces -o wide | fgrep ${pod_template} | awk '{print $8}' | xargs -n 1 echo -n "\|" | sed 's/[[:space:]]*//g')\"
kubectl top
获取 Pod 列表并根据其消耗的 CPU 或 内存进行排序:
# cpu
$ kubectl top pods -A | sort --reverse --key 3 --numeric
# memory
$ kubectl top pods -A | sort --reverse --key 4 --numeric
selector
字段,用来查找 Pod。过去要打开 Service 的清单才能完成这个任务,现在使用 -o wide
参数也可以:
$ kubectl -n jaeger get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
jaeger-cassandra ClusterIP None <none> 9042/TCP 77d app=cassandracluster,cassandracluster=jaeger-cassandra,cluster=jaeger-cassandra
requests
和 limits
:
$ kubectl get pods -A -o=custom-columns='NAME:spec.containers[*].name,MEMREQ:spec.containers[*].resources.requests.memory,MEMLIM:spec.containers[*].resources.limits.memory,CPUREQ:spec.containers[*].resources.requests.cpu,CPULIM:spec.containers[*].resources.limits.cpu'
NAME MEMREQ MEMLIM CPUREQ CPULIM
coredns 70Mi 170Mi 100m <none>
coredns 70Mi 170Mi 100m <none>
...
kubectl run
(以及 create
、apply
、patch
)命令有个厉害的参数 --dry-run
,该参数让用户无需真正操作集群就能观察集群的行为,如果配合 -o yaml
,就能输出命令对应的 YAML:
$ kubectl run test --image=grafana/grafana --dry-run -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
run: test
name: test
spec:
replicas: 1
selector:
matchLabels:
run: test
简单的把输出内容保存到文件,删除无用字段就可以使用了。
1.18 开始 kubectl run
生成的是 Pod 而非 Deployment。 kubectl explain hpa
KIND: HorizontalPodAutoscaler
VERSION: autoscaling/v1
DESCRIPTION:
configuration of a horizontal pod autoscaler.
FIELDS:
apiVersion <string>
...
$ kubectl get nodes -o json | jq -r '.items[].status.addresses[]? | select (.type == "InternalIP") | .address' | \
paste -sd "\n" -
9.134.14.252
nodePort
:
$ kubectl get -A svc -o json | jq -r '.items[] | [.metadata.name,([.spec.ports[].nodePort | tostring ] | join("|"))]| @tsv'
kubernetes null
...
$ kubectl get nodes -o jsonpath='{.items[*].spec.podCIDR}' | tr " " "\n" fix-doc-azure-container-registry-config ✭
10.120.0.0/24
10.120.1.0/24
10.120.2.0/24
$ kubectl logs -f fluentbit-gke-qq9w9 -c fluentbit --timestamps
2020-09-10T13:10:49.822321364Z Fluent Bit v1.3.11
2020-09-10T13:10:49.822373900Z Copyright (C) Treasure Data
2020-09-10T13:10:49.822379743Z
2020-09-10T13:10:49.822383264Z [2020/09/10 13:10:49] [ info] Configuration:
kubectl logs -f fluentbit-gke-qq9w9 -c fluentbit --tail=10
[2020/09/10 13:10:49] [ info] ___________
[2020/09/10 13:10:49] [ info] filters:
[2020/09/10 13:10:49] [ info] parser.0
...
kubectl get secrets -o json --namespace namespace-old | \
jq '.items[].metadata.namespace = "namespace-new"' | \
kubectl create-f -
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=grafana.mysite.ru/O=MyOrganization"
kubectl -n myapp create secret tls selfsecret --key tls.key --cert tls.crt
本文没什么结论,但是可以提供一个小列表,其中包含本文相关的有用链接。
https://kubernetes.io/docs/reference/kubectl/cheatsheet/
https://linuxacademy.com/blog/containers/kubernetes-cheat-sheet/
https://www.bluematador.com/learn/kubectl-cheatsheet
https://gist.github.com/pydevops/0efd399befd960b5eb18d40adb68ef83