Kubernetes - 编程方式更新 ConfigMap

5
我有一个使用 ConfigMap 的 Kubernetes 部署,其中包含一些经常更新的配置。目前我必须手动更新此配置,通过在本地机器上运行一个脚本来通过 kubectl 更新 ConfigMap。
是否有一种更自动化的方式可以使用 Kubernetes API(从 Kubernetes 内部或外部)来完成这个过程?
2个回答

6

如果你查看这里,会发现有许多语言的Kubernetes客户端可供使用,Python和Go是官方支持的。您可以通过调用客户端自动化步骤。

如果您了解Python,可以参考以下示例

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。特别是这个线程解释了如何从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

1
您提供的客户端库链接已失效。这是新链接:https://kubernetes.io/docs/reference/using-api/client-libraries - Quentin Brosse

0
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。但要注意令牌权限(服务帐户)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接