Kubernetes Python 客户端:更新具有亲和力的部署规范
Kubernetes Python Client : update deployment spec with affinity
from kubernetes import client, config
config.load_kube_config()
api = client.AppsV1Api()
deployment = api.read_namespaced_deployment(name='foo', namespace='bar')
我试图将关联对象添加到部署规范,但我遇到了这个错误
deployment.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms = [{'nodeSelectorTerms':[{'matchExpressions':[{'key': 'kubernetes.io/hostname','operator': 'NotIn','values': ["awesome-node"]}]}]}]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'V1DeploymentSpec' object has no attribute 'affinity'
你看错地方了。当您查看部署规范 (deployment.spec.affinity
) 时,Affinity 属于 pod 模板规范 (deployment.spec.template.spec.affinity
)。
以下是完全替换现有亲和力的方法(即使它是 None):
from kubernetes import client, config
config.load_kube_config()
api = client.AppsV1Api()
# read current state
deployment = api.read_namespaced_deployment(name='foo', namespace='bar')
# check current state
#print(deployment.spec.template.spec.affinity)
# create affinity objects
terms = client.models.V1NodeSelectorTerm(
match_expressions=[
{'key': 'kubernetes.io/hostname',
'operator': 'NotIn',
'values': ["awesome-node"]}
]
)
node_selector = client.models.V1NodeSelector(node_selector_terms=[terms])
node_affinity = client.models.V1NodeAffinity(
required_during_scheduling_ignored_during_execution=node_selector
)
affinity = client.models.V1Affinity(node_affinity=node_affinity)
# replace affinity in the deployment object
deployment.spec.template.spec.affinity = affinity
# finally, push the updated deployment configuration to the API-server
api.replace_namespaced_deployment(name=deployment.metadata.name,
namespace=deployment.metadata.namespace,
body=deployment)
from kubernetes import client, config
config.load_kube_config()
api = client.AppsV1Api()
deployment = api.read_namespaced_deployment(name='foo', namespace='bar')
我试图将关联对象添加到部署规范,但我遇到了这个错误
deployment.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms = [{'nodeSelectorTerms':[{'matchExpressions':[{'key': 'kubernetes.io/hostname','operator': 'NotIn','values': ["awesome-node"]}]}]}]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'V1DeploymentSpec' object has no attribute 'affinity'
你看错地方了。当您查看部署规范 (deployment.spec.affinity
) 时,Affinity 属于 pod 模板规范 (deployment.spec.template.spec.affinity
)。
以下是完全替换现有亲和力的方法(即使它是 None):
from kubernetes import client, config
config.load_kube_config()
api = client.AppsV1Api()
# read current state
deployment = api.read_namespaced_deployment(name='foo', namespace='bar')
# check current state
#print(deployment.spec.template.spec.affinity)
# create affinity objects
terms = client.models.V1NodeSelectorTerm(
match_expressions=[
{'key': 'kubernetes.io/hostname',
'operator': 'NotIn',
'values': ["awesome-node"]}
]
)
node_selector = client.models.V1NodeSelector(node_selector_terms=[terms])
node_affinity = client.models.V1NodeAffinity(
required_during_scheduling_ignored_during_execution=node_selector
)
affinity = client.models.V1Affinity(node_affinity=node_affinity)
# replace affinity in the deployment object
deployment.spec.template.spec.affinity = affinity
# finally, push the updated deployment configuration to the API-server
api.replace_namespaced_deployment(name=deployment.metadata.name,
namespace=deployment.metadata.namespace,
body=deployment)