python - Kubernetes REST API call using python
问题描述
I'm looking for a way to find a pod by name, and run REST call using python. I thought of using port forwarding and kubernetes client
Can someone share a code sample or any other way to do it?
Here is what I started to do:
from kubernetes import client, config
config.load_kube_config(config_file="my file") client = client.CoreV1Api()
pods = client.list_namespaced_pod(namespace="my namespace") # loop pods to
#find my pod
Next I thought of using:
stream(client.connect_get_namespaced_pod_portforward_with_http_info ...
In kubectl command line tool I do the following: 1. List pods 2. Open port forward 3. Use curl to perform the REST call
I want to do the same in python
解决方案
列出所有 pod:
from kubernetes import client, config
# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
然后在上面的 for 循环中,您可以检查您的 pod name ,如果匹配则返回。
推荐阅读
- ms-access - 有没有办法在访问中将多个值添加到 1 个 ID
- c# - 如何使用 asp.net 核心从 appsettings.json 获取配置数据并做出反应
- python - 设置类变量不会更新原始类的实例
- mysql - ERD:仅当实体 1 具有从主键实体 2 标识的外键时,两个实体之间的关系才会存在吗?
- ios - 传递给另一个 ViewController 时重置变量
- python - 运行更改名称的功能时如何修复文件被删除
- php - php 7.2 上的连续命令 ssh2
- php - 在 PHP 中将 MySQL 输出显示为行而不是列
- php - Laravel 5.8,PHP中的validate()和validated()函数有什么区别?
- machine-learning - 在真实主题输入/输出上训练神经网络,使其行为与主题相似