- kubernetes学习系列 快捷连接
本文介绍kubectl的几个常用命令,kubconfig文件基本属性,并开启kubectl debug日志分析其背后基本原理
- 结论写在前面:
- kubectl就是一个和kube-apiserver交互的命令行工具,通过将命令转换成对应的get、delete等请求发送给api-server,实现与kubernetes的交互
- kubeconfig文件为kubectl提供集群地址、集群鉴权cert、token等信息
1.kubectl 做了什么
- 执行kubectl命令的时候,添加 -v 9,即可开启debug日志,可以看到kubectl到底干了什么
- 可以看到,kubectl命令首先加载了 /root/.kube/config 文件,获取到了当前要连接的集群信息,及user认证信息cert、key
- 然后向apiserver发送get请求
2.kubeconfig 内容解释
- kubectl从其中获取当前正在使用的上下文,并使用 该上下文中 指定的user 连接 指定的cluster
- kubeconfig 默认路径:/root/.kube/config,当然你可以通过设置 KUBECONFIG 环境变量或者设置 --kubeconfig参数来指定其他 kubeconfig 文件
- 官方文档:https://kubernetes.io/zh-cn/docs/concepts/configuration/organize-cluster-access-kubeconfig/
3.kubectl get 命令
- 使用-oyaml和-w实时查看 default ns 的yaml变化
[root@VM-226-235-tencentos ~]# kubectl get ns default -oyaml -w apiVersion: v1 kind: Namespace metadata: creationTimestamp: "2024-04-17T08:00:47Z" name: default resourceVersion: "146" selfLink: /api/v1/namespaces/default uid: da7c3a89-3ac2-411b-a7fa-f985f5fb24ec spec: finalizers: - kubernetes status: phase: Active
- 开启kubectl debug日志,可以看出
- kubectl会首先发送get请求,获取default ns的yaml
- 然后又发送了一次get请求,不过这个get请求的watch参数置为了true,和apiserver建立了一个长连接
- 当ns有变化的时候,apiserver就会把事件推送过来,我们的控制台就可以打印最新ns的yaml了
[root@VM-226-235-tencentos ~]# kubectl get ns default -oyaml -w -v 9 I1207 17:13:12.017346 6316 loader.go:375] Config loaded from file: /root/.kube/config ...... I1207 17:13:12.057355 6316 round_trippers.go:424] curl -k -v -XGET -H "Accept: application/json" -H "User-Agent: kubectl/v1.19.16 (linux/amd64) kubernetes/e37e4ab" 'https://9.135.226.235:6443/api/v1/namespaces/default' I1207 17:13:12.059146 6316 round_trippers.go:444] GET https://9.135.226.235:6443/api/v1/namespaces/default 200 OK in 1 milliseconds I1207 17:13:12.059162 6316 round_trippers.go:450] Response Headers: I1207 17:13:12.059166 6316 round_trippers.go:453] Cache-Control: no-cache, private I1207 17:13:12.059175 6316 round_trippers.go:453] Content-Type: application/json I1207 17:13:12.059181 6316 round_trippers.go:453] Content-Length: 286 I1207 17:13:12.059194 6316 round_trippers.go:453] Date: Sat, 07 Dec 2024 09:13:12 GMT I1207 17:13:12.059222 6316 request.go:1097] Response Body: {"kind":"Namespace","apiVersion":"v1","metadata":{"name":"default","selfLink":"/api/v1/namespaces/default","uid":"da7c3a89-3ac2-411b-a7fa-f985f5fb24ec","resourceVersion":"146","creationTimestamp":"2024-04-17T08:00:47Z"},"spec":{"finalizers":["kubernetes"]},"status":{"phase":"Active"}} I1207 17:13:12.059373 6316 table_printer.go:45] Unable to decode server response into a Table. Falling back to hardcoded types: attempt to decode non-Table object apiVersion: v1 kind: Namespace metadata: creationTimestamp: "2024-04-17T08:00:47Z" name: default resourceVersion: "146" selfLink: /api/v1/namespaces/default uid: da7c3a89-3ac2-411b-a7fa-f985f5fb24ec spec: finalizers: - kubernetes status: phase: Active I1207 17:13:12.059734 6316 round_trippers.go:424] curl -k -v -XGET -H "Accept: application/json" -H "User-Agent: kubectl/v1.19.16 (linux/amd64) kubernetes/e37e4ab" 'https://9.135.226.235:6443/api/v1/namespaces?fieldSelector=metadata.name%3Ddefault&resourceVersion=0&watch=true' I1207 17:13:12.060448 6316 round_trippers.go:444] GET https://9.135.226.235:6443/api/v1/namespaces?fieldSelector=metadata.name%3Ddefault&resourceVersion=0&watch=true 200 OK in 0 milliseconds I1207 17:13:12.060474 6316 round_trippers.go:450] Response Headers: I1207 17:13:12.060481 6316 round_trippers.go:453] Content-Type: application/json I1207 17:13:12.060493 6316 round_trippers.go:453] Date: Sat, 07 Dec 2024 09:13:12 GMT I1207 17:13:12.060506 6316 round_trippers.go:453] Cache-Control: no-cache, private I1207 17:13:12.060780 6316 table_printer.go:45] Unable to decode server response into a Table. Falling back to hardcoded types: attempt to decode non-Table object
- 为default ns添加label,长连接查看输出
4.kubectl describe 命令
- 最重要的用途是 查看event,用于问题排查
5.kubectl exec 进入容器
kubectl exec -it pod-xxx -n ns-xxx -- bash
--
:前面是指令,后面是在容器中执行的命令【非必须,但建议写,语义更明确】bash
:执行容器的bash命令,会进入容器的bash命令行,可以做一些debug操作
- 比如也可以直接使用 – 指定查看容器内文件
kubectl exec -it pod-xxx -n ns-xxx -- tail -f /var/app.log
6.kubectl logs 命令
kubectl logs
查看的是标准输出日志kubectl logs pod-xxx -n ns-xxx
:查看pod 标准输出日志,使用-f
参数可以实时输出kubectl logs pod-xxx -n ns-xxx | head -n 10
:查看pod前10行 标准输出日志kubectl logs pod-xxx -n ns-xxx | grep "user-xxxx"
:检索 标准输出日志 中指定关键字
- 如果你的应用会把日志写入到某个指定路径下,可以使用exec查看容器的指定文件
kubectl exec -it pod-xxx -n ns-xxx -- tail -f /var/app.log
。