2017-08-10 83 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