gpt4 book ai didi

Kubernetes - 以编程方式更新 ConfigMap

转载 作者:行者123 更新时间:2023-12-02 12:08:16 26 4
gpt4 key购买 nike

我有一个 Kubernetes 部署,它使用 ConfigMap 和一些经常更新的配置。目前我必须手动更新这个配置,通过在我的本地机器上运行一个脚本来更新 ConfigMap kubectl .

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

最佳答案

如果你看一看,有几个 Kubernetes 客户端有多种语言版本 here .官方支持 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

关于Kubernetes - 以编程方式更新 ConfigMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45611925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com