【问题标题】:Create Multiple Virtual envs for Multiple kubernetes contexts为多个 Kubernetes 上下文创建多个虚拟环境
【发布时间】:2022-01-04 13:47:04
【问题描述】:

到现在为止

 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 

【问题讨论】:

  • 能否更新问题状态?

标签: python python-3.x kubernetes google-cloud-platform microservices


【解决方案1】:

这可能不是一个合理的解决方案,但无论如何...有时我为不同的集群使用了不同的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 时它将使用所需的配置。

【讨论】:

    【解决方案2】:

    我会尝试按照官方客户端 repo 中的建议为集群初始化多个对象

    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() 
    

    Read more here

    您还可以使用带有 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)
    

    您也可以尝试在不同的终端中使用Environment 变量来存储不同的K8s上下文细节。

    【讨论】:

      【解决方案3】:
      1. 首先为要切换的集群上下文创建单独的配置文件
      Terminal 1:
      user@machine $ kubectl config view --minify --flatten --context=context-1 > $HOME/.kube/config-context-1
      
      Terminal 2:
      user@machine $ kubectl config view --minify --flatten --context=context-2 > $HOME/.kube/config-context-2
      
      1. 为不同的集群创建不同的虚拟环境并激活它们
      Terminal 1:
      user@machine $ python -m venv context-1
      user@machine $ . ./context-1/bin/activate
      
      Terminal 2:
      user@machine $ python -m venv context-2
      user@machine $ . ./context-2/bin/activate
      
      1. 将新的配置文件导出到各自的环境中
      Terminal 1:
      (context-1) user@machine $ export KUBECONFIG="$HOME/.kube/config-context-1"
      
      Terminal 2:
      (context-2) user@machine $ export KUBECONFIG="$HOME/.kube/config-context-2"
      

      你现在检查你的 pod,它会是不同的上下文

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-03
        • 1970-01-01
        • 2021-07-30
        • 2017-03-14
        • 1970-01-01
        • 2014-01-24
        • 2021-08-03
        相关资源
        最近更新 更多