到目前為止我做
kubectl --context <cluster context> get pod -A
在特定集群中獲取 pod
是否有 python 方法為虛擬環境設定 kubernetes 背景關系,因此我們可以同時使用多個背景關系示例:
Terminal 1:
(cluster context1) user@machine #
Terminal 2:
(cluster context2) user@machine #
這應該相當于
Terminal 1:
user@machine # kubectl --context <cluster context1> get pod -A
Terminal 2:
user@machine # kubectl --context <cluster context1> get pod -A
uj5u.com熱心網友回復:
這可能不是一個合理的解決方案,但無論如何......有時我kubectl為不同的集群使用了不同的版本,我想出了一個類似 venv 的解決方案來在它們之間切換。我寫了這樣的文本檔案:
export KUBECONFIG="/path/to/kubeconfig"
export PATH="/path/including/the/right/kubectl"
并以與 venv: 相同的方式激活它們source the_file。如果您可以將背景關系拆分為單獨的檔案,則可以添加export KUBECONFIG="/path/to/kubeconfig"到您的檔案中,venv/bin/activate當您激活venv.
uj5u.com熱心網友回復:
我會嘗試按照官方客戶端存盤庫中的建議為集群初始化多個物件
from pick import pick # install pick using `pip install pick`
from kubernetes import client, config
from kubernetes.client import configuration
def main():
contexts, active_context = config.list_kube_config_contexts()
if not contexts:
print("Cannot find any context in kube-config file.")
return
contexts = [context['name'] for context in contexts]
active_index = contexts.index(active_context['name'])
cluster1, first_index = pick(contexts, title="Pick the first context",
default_index=active_index)
cluster2, _ = pick(contexts, title="Pick the second context",
default_index=first_index)
client1 = client.CoreV1Api(
api_client=config.new_client_from_config(context=cluster1))
client2 = client.CoreV1Api(
api_client=config.new_client_from_config(context=cluster2))
print("\nList of pods on %s:" % cluster1)
for i in client1.list_pod_for_all_namespaces().items:
print("%s\t%s\t%s" %
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
print("\n\nList of pods on %s:" % cluster2)
for i in client2.list_pod_for_all_namespaces().items:
print("%s\t%s\t%s" %
(i.status.pod_ip, i.metadata.namespace, i.metadata.name))
if __name__ == '__main__':
main()
在這里閱讀更多
您還可以使用帶有pick 的python來獲取背景關系
from pick import pick # `pip install pick`
from kubernetes import client, config
from kubernetes.client import configuration
def main():
contexts, active_context = config.list_kube_config_contexts()
if not contexts:
print("Cannot find any context in kube-config file.")
return
contexts = [context['name'] for context in contexts]
active_index = contexts.index(active_context['name'])
option, _ = pick(contexts, title="Pick the context to load",
default_index=active_index)
# Configs can be set in Configuration class directly or using helper
# utility
config.load_kube_config(context=option)
print("Active host is %s" % configuration.Configuration().host)
您也可以嘗試在不同終端中使用環境 變數來存盤不同的 K8s 背景關系詳細資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/367690.html
標籤:Python 蟒蛇-3.x Kubernetes 谷歌云平台 微服务
