2017-08-10 64 views
1

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

有没有办法以更自动化的方式使用Kubernetes API(从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