Kubernetes编程—— 使用自定义资源 —— 服务发现信息
使用自定义资源 —— 服务发现信息
我们看看 kubectl 使用 API 服务器所提供的服务发现信息来找到新的资源。让我们一起来深入理解一下这个发现机制。
为 kubectl 启用更高级别的日志,我们便能从日志中了解到 kubectl 是如何发现这种新的资源类型的:
[root@JumperServer:zuoyang] # kubectl get deployment -n pc-official -v=7 // kubectl从/root/.kube/config文件加载配置信息。 I0630 17:39:43.903576 4128802 loader.go:373] Config loaded from file: /root/.kube/config // kubectl发送一个GET请求到 https://rancher.zuoyangs.com/k8s/clusters/local/api?timeout=32s,用于获取集群的API信息。这个请求是用来发现API组的。 I0630 17:39:43.904933 4128802 round_trippers.go:463] GET https://rancher.zuoyangs.com/k8s/clusters/local/api?timeout=32s // 这行显示了下面几行是请求头信息。 I0630 17:39:43.904944 4128802 round_trippers.go:469] Request Headers: // 请求头中指定了 Accept 头部,要求接收 application/json 格式的响应,带有 g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList 参数,这是为了查询 API 组列表。 I0630 17:39:43.904956 4128802 round_trippers.go:473] Accept: application/json;g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList,application/json // 请求头中指定了 User-Agent 头部,这个头部用于标识发送请求的客户端。在这里,客户端是 kubectl/v1.27.1 (linux/amd64) kubernetes/4c94112。 I0630 17:39:43.904968 4128802 round_trippers.go:473] User-Agent: kubectl/v1.27.1 (linux/amd64) kubernetes/4c94112 // 请求头中包含 Authorization 头部,这个头部用于进行身份验证。授权标头被屏蔽掉了,以保护安全。 I0630 17:39:43.904979 4128802 round_trippers.go:473] Authorization: Bearer <masked> // 获取 API 组的 GET 请求响应返回了HTTP状态码200,表示成功。耗时22毫秒。 I0630 17:39:43.927882 4128802 round_trippers.go:574] Response Status: 200 OK in 22 milliseconds // kubectl 发送了另一个 GET 请求到 https://rancher.zuoyangs.com/k8s/clusters/local/apis?timeout=32s, 用于获取所有的 API。 I0630 17:39:43.928033 4128802 round_trippers.go:463] GET https://rancher.zuoyangs.com/k8s/clusters/local/apis?timeout=32s I0630 17:39:43.928063 4128802 round_trippers.go:469] Request Headers: I0630 17:39:43.928088 4128802 round_trippers.go:473] Accept: application/json;g=apidiscovery.k8s.io;v=v2beta1;as=APIGroupDiscoveryList,application/json I0630 17:39:43.928117 4128802 round_trippers.go:473] User-Agent: kubectl/v1.27.1 (linux/amd64) kubernetes/4c94112 I0630 17:39:43.928139 4128802 round_trippers.go:473] Authorization: Bearer <masked> I0630 17:39:43.935887 4128802 round_trippers.go:574] Response Status: 200 OK in 7 milliseconds I0630 17:39:43.936351 4128802 round_trippers.go:463] GET https://rancher.zuoyangs.com/k8s/clusters/local/apis/events.k8s.io/v1?timeout=32s I0630 17:39:43.936375 4128802 round_trippers.go:469] Request Headers: I0630 17:39:43.936387 4128802 round_trippers.go:473] Accept: application/json, */* I0630 17:39:43.936397 4128802 round_trippers.go:473] User-Agent: kubectl/v1.27.1 (linux/amd64) kubernetes/4c94112 I0630 17:39:43.936412 4128802 round_trippers.go:473] Authorization: Bearer <masked> I0630 17:39:43.936673 4128802 round_trippers.go:463] GET https://rancher.zuoyangs.com/k8s/clusters/local/apis/apiregistration.k8s.io/v1?timeout=32s