【问题标题】:Kubernetes - update ConfigMap programmaticallyKubernetes - 以编程方式更新 ConfigMap
【发布时间】:2018-01-18 14:20:25
【问题描述】:

我有一个 Kubernetes 部署,它使用带有一些经常更新的配置的 ConfigMap。目前,我必须手动更新此配置,方法是在我的本地机器上运行一个脚本,该脚本通过kubectl 更新 ConfigMap。

有没有办法使用 Kubernetes API(从 Kubernetes 内部或外部)以更自动化的方式执行此操作?

【问题讨论】:

    标签: kubernetes


    【解决方案1】:

    如果你看一下here,有几个 Kubernetes 客户端可以使用多种语言。官方支持 Python 和 Go。您可以通过调用客户端来自动执行这些步骤。

    如果你了解 Python,可以参考下面的sample

    from __future__ import print_statement 
    import time 
    import kubernetes.client from kubernetes.client.rest 
    import ApiException from pprint import pprint
    
    # Configure API key authorization: BearerToken 
    kubernetes.client.configuration.api_key['authorization'] = 'YOUR_API_KEY'
    # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
    # kubernetes.client.configuration.api_key_prefix['authorization'] = 'Bearer'
    
    # create an instance of the API class api_instance = 
    kubernetes.client.CoreV1Api() 
    name = 'name_example' # str | name of the ConfigMap 
    namespace = 'namespace_example' # str | object name and auth scope, such as for teams and projects 
    body = NULL # object |  
    pretty = 'pretty_example' # str | If 'true', then the output is pretty printed. (optional)
    
    try: 
        api_response = api_instance.patch_namespaced_config_map(name, namespace, body, pretty=pretty)
        pprint(api_response) 
    except ApiException as e:
        print("Exception when calling CoreV1Api->patch_namespaced_config_map: %s\n" % e)
    

    关于内部和外部使用 API,您可以查看wiki。特别是这个thread 解释了如何从 pod 访问 API。

    KUBE_TOKEN=$(</var/run/secrets/kubernetes.io/serviceaccount/token)
    curl -sSk -H "Authorization: Bearer $KUBE_TOKEN" https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/iot2cloud/configmaps
    

    【讨论】:

      【解决方案2】:
      def updateConfigMap(token):
         print(token)
         token = "Bearer {}".format(token)
         headers = {"Content-Type": "application/merge-patch+json", "authorization":token}
      
      r = requests.patch("{}/api/v1/namespaces/default/configmaps/CONFIMAPNAME".format(KUBERNETES_MASTER), verify=False, headers=headers, json=configData)
      return r.content
      

      我之前遇到了一些问题,但是在更改 PATCH 请求的标头时。我可以更新我的 configmaps 。但注意token权限(Service Account)

      【讨论】:

        猜你喜欢
        • 2021-06-22
        • 2019-02-10
        • 2023-04-09
        • 2018-12-20
        • 2020-09-30
        • 1970-01-01
        • 2018-11-25
        • 2015-06-30
        • 1970-01-01
        相关资源
        最近更新 更多