以编程方式更新kubernetes清单文件的属性子集(例如,类似nodeselector)



我有几个daemonSets在我的k8s集群上运行。我想使用powershell以编程方式在所有daemonSets的pod模板规范中添加一个附加属性。

具体地说;添加";以下到几个守护程序集:

spec:
template:
spec:
tolerations:
- key: node-role.kubernetes.io/master
operator: Exists
effect: NoSchedule

容器、卷、标签选择器等现有的东西应该是已经存在的。我如何通过编程实现这一点?

您可以首先更新YAML文件,并使用K8s客户端的"代码"或"程序"应用YAML文件更改。

Python客户端

示例

from os import path
import yaml
from kubernetes import client, config

def main():
# Configs can be set in Configuration class directly or using helper
# utility. If no argument provided, the config will be loaded from
# default location.
config.load_kube_config()
with open(path.join(path.dirname(__file__), "daemon-deployment.yaml")) as f:
dep = yaml.safe_load(f)
k8s_beta = client.ExtensionsV1beta1Api()
resp = k8s_beta.create_namespaced_deployment(
body=dep, namespace="default")
print("Deployment created. status='%s'" % str(resp.status))

if __name__ == '__main__':
main()

最新更新