我正在尝试使用kubernetes python客户端获取节点注释的值。
此代码为具有etcd节点的节点打印所有注释:
#!/usr/bin/python
from kubernetes import client, config
def main():
config.load_kube_config("./backup_kubeconfig_prod")
label_selector = 'node-role.kubernetes.io/etcd'
v1 = client.CoreV1Api()
print("Listing nodes with their IPs:")
ret = v1.list_node(watch=False, label_selector=label_selector)
for i in ret.items:
print(i.metadata.annotations)
if __name__ == '__main__':
main()
输出示例:
{'flannel.alpha.coreos.com/kube-subnet-manager': 'true', 'flannel.alpha.coreos.com/backend-type': 'vxlan', 'flannel.alpha.coreos.com/backend-data': '{"VtepMAC":"96:70:f6:ab:4f:30"}', 'rke.cattle.io/internal-ip': '1.2.3.4', 'volumes.kubernetes.io/controller-managed-attach-detach': 'true', 'flannel.alpha.coreos.com/public-ip': '1.2.3.4', 'rke.cattle.io/external-ip': '1.2.3.4', 'node.alpha.kubernetes.io/ttl': '0'}
例如,如何打印flannel.alpha.coreos.com/public-ip
的值?
答案 0 :(得分:2)
i.metadata.annotations
中的数据是字典类型。
您可以使用以下命令打印键flannel.alpha.coreos.com/public-ip
的值:
print(i.metadata.annotations["flannel.alpha.coreos.com/public-ip"])
答案 1 :(得分:0)
以下作品:
print("Annontation: {}".format(i.metadata.annotations['flannel.alpha.coreos.com/public-ip']))
标签和注释是字典,我们不能将其作为项目进行迭代。因此,我们必须以这种方式获取标签和注释的值。