我有一个 Kubernetes 部署,它使用 ConfigMap 和一些经常更新的配置。目前,我必须通过在本地计算机上运行脚本来手动更新此配置,该脚本通过kubectl.
有没有办法使用 Kubernetes API(从 Kubernetes 内部或外部)以更自动化的方式执行此操作?
如果您看一下这里,有许多语言的 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)
Run Code Online (Sandbox Code Playgroud)
关于在内部和外部使用 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5611 次 |
| 最近记录: |