kubernetes 从容器中获取Pod自身的详细信息(不使用downwardAPI或env变量方法)

snz8szmq  于 2023-04-20  发布在  Kubernetes
关注(0)|答案(1)|浏览(129)

在pod中运行的应用程序是否可以获取关于其自己的pod的详细信息/元数据。我知道这可以使用downwardAPIenv variables轻松实现。
有第三条路吗?
我想不使用这两种方法来找到pod的service帐号和namespace,可以吗?

7uhlpewt

7uhlpewt1#

有什么特别的原因要解耦容器细节吗?请记住,向下的API旨在获取服务帐户等细节。现在,针对您的问题,我设想了两种可能的方法来实现您的目标,kubectl和API Server。我详细介绍了kubectl方法:

Kubernetes客户端(kubectl)

  • 获取有关Kubernetes secret对象的信息

kubectl get secret --namespace={namespace}
以下是一个示例输出:

NAME                  TYPE                                  DATA      AGE
admin.registrykey     kubernetes.io/dockercfg               1         1h
default-token-2mfqv   kubernetes.io/service-account-token   3         1h
  • 获取服务帐户令牌的详细信息

kubectl get secret default-token-2mfqv --namespace={namespace} -o yaml
以下是一个示例输出,注意注解部分

apiVersion: v1
data:
 ca.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURVRENDQWppZ0F3SUJBZ0lKQUxSSHNCazBtblM4TUEwR0NTcUdTSWIzRFFFQkN3VUFNQjh4SFRBYkJnTlYKQkFNTUZERXlOeTR3TGpBdU1VQXhORGczTWprNU1EZ3lNQjRYRFRFM01ESXhOekF5TXpnd01sb1hEVEkzTURJeApOVEF5TXpnd01sb3dIekVkTUJzR0ExVUVBd3dVTVRJM0xqQXVNQzR4UURFME9EY3lPVGt3T0RJd2dnRWlNQTBHCkNTcUdTSWIzRFFFQkFRVUFBNElCRHdBd2dnRUtBb0lCQVFDMTUwZWxjRXZXUDBMVFZZK09jNTl4ZG9PUCtXb08Kd3BGNGRxaGpDSDdyZGtUcGVKSE1zeW0raU4wMWxBSjNsc2UvYjB0V2h5L1A5MVZpZmpjazFpaDBldDg0eUZLawpuQWFaNVF6clJxQjk2WGZ3VVVyUElZc0RjRlpzbnAwZUlZU0xJ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blBKM2tFb0dzX3hoTVVsaDc3OEkweTMwV3FwYW9uOHBLS1I1NjIzMjd6eTdXNGY0UnJhc3VPSGZwUGE3SVE5cU1ub21fcWxBcWxDQ3lXVEkyV3dxQ09xdnNHUmdNUHJjemc3WnYzLWlXRktBaVc3ZU5VYnVR
kind: Secret
metadata:
 annotations:
   kubernetes.io/service-account.name: default
   kubernetes.io/service-account.uid: df441c69-f4ba-11e6-8157-525400225b53
 creationTimestamp: 2017-02-17T02:43:33Z
 name: default-token-2mfqv
 namespace: default
 resourceVersion: "37"
 selfLink: /api/v1/namespaces/default/secrets/default-token-2mfqv
 uid: df5f1109-f4ba-11e6-8157-525400225b53
type: kubernetes.io/service-account-token

有一个完整的文档here

相关问题