我有我的kubernetes集群,它部署在云中,我有一个本地代理,我应该使用它来从桌面连接k8s集群。我可以使用kubectl通过运行代理访问我的集群。现在,我想看看是否可以通过单独使用python客户机实用程序来消除kubectl命令行实用程序。在
所以在我的第一个需求中,我消除了kubectl,我可以使用下面的代码访问我的集群。问题是,我的访问令牌只在15分钟内有效,我必须用kubectl运行我的代理,以使我的更新令牌在我的kubeconfig文件中更新,这样我就可以在接下来的15分钟内使用了。在
所以我的问题和建议, 1有没有什么方法可以完全消除kubectl,只需使用kubernetespython客户机和本地代理。 2如何自动获取刷新的令牌,这样我就不必每隔15分钟运行一次代理。在
有什么想法/医生/例子吗。在
from kubernetes import client, config
def main():
try:
#config.load_kube_config('kubeconfig',persist_config=True)
config.load_kube_config('kubeconfig')
kube_host = config.kube_config.Configuration._default.host+":443"
kube_api_key = config.kube_config.Configuration._default.api_key
aConfiguration = client.Configuration()
# Specify the endpoint of your Kube cluster
aConfiguration.host = kube_host
aConfiguration.verify_ssl = False
aConfiguration.api_key = kube_api_key
aApiClient = client.ApiClient(aConfiguration)
# Do calls
v1 = client.CoreV1Api(aApiClient)
api_response = v1.list_namespaced_pod('default')
print(api_response)
except Exception as e:
print("Exception when calling CoreV1Api->list_namespaced_pod: %s\n" % e)
if __name__ == '__main__':
main()
答案是:https://stackoverflow.com/a/48377444/5936468
您可以创建服务帐户并使用其令牌进行身份验证
相关问题 更多 >
编程相关推荐