In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the realization method of K8s nodeSelector and affinity scheduling affinity". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "K8s nodeSelector and affinity scheduling affinity implementation method"!
NodeSelector
1. Method of assigning pod to node
There are two ways to constrain pod to run to a specified node through node label selector, nodeSelector and affinity
2.nodeSelector is a node selector implementation provided by k8s early riser
1) first type the corresponding label for nodes
Kubectl label nodes master disktype=ssd
2) create yaml file, nginx-pod.yaml
ApiVersion: v1kind: Podmetadata: name: nginx labels: env: testspec: containers:-name: nginx image: nginx
Create pod
Kubectl create-f nginx-pod.yaml
Node default label
Kubectl get nodes-o yaml can see the label of the default node, and you can also use these label for constraints
Alpha.kubernetes.io/fluentd-ds-ready: "true" beta.kubernetes.io/arch: amd64beta.kubernetes.io/os: linuxdisktype: ssdkubeadm.alpha.kubernetes.io/role: masterkubernetes.io/hostname: master Affinity and Anti-compatibility
According to the type
NodeAffinity (host affinity)
PodAffinity (POD affinity)
PodAntiAffinity (POD anti-compatibility).
The comparison of the three affinity and anti-compatibility strategies is shown in the following table:
Operators supported by policy name matching target support topology domain design target nodeAffinity host label In,NotIn,Exists,DoesNotExist,Gt,Lt does not support deciding on which hosts Pod can be deployed podAffinityPod tag In,NotIn,Exists,DoesNotExist supports determining which Pod and which Pod can be deployed in the same topology domain PodAntiAffinityPod tag In,NotIn,Exists,DoesNotExist supports determining which Pod and which Pod cannot be deployed in the same topology domain
For affinity and anti-compatibility, there are three rules that can be set:
RequiredDuringSchedulingRequiredDuringExecution: the affinity or anti-affinity rules are required during scheduling. If the rules are not met, the POD cannot be scheduled to the corresponding host. Later in the run, if the rule is not satisfied for some reason (such as modifying the label), the system will try to remove the POD from the host (not supported by the current version).
RequiredDuringSchedulingIgnoredDuringExecution: the affinity or anti-affinity rules are required during scheduling. If the rules are not met, the POD cannot be scheduled to the corresponding host. During later runs, the system will no longer check whether these rules are met.
PreferredDuringSchedulingIgnoredDuringExecution: try to meet the affinity or anti-affinity rules during the scheduling period. If the rules are not met, POD may also be dispatched to the corresponding host. During later runs, the system will no longer check whether these rules are met.
Introduction to the usage scenario
NodeAffinity usage scenarios:
Deploy all Pod of the S1 service to the specified host that complies with the labeling rules.
Deploy all Pod of S1 service to other hosts except some hosts.
PodAffinity usage scenarios:
Deploy the pod of a particular service in the same topology domain without specifying a specific topology domain.
If the S1 service uses the S2 service, to reduce the network latency between them (or for other reasons), deploy the POD of the S1 service and the pod of the S2 service in the same topology domain.
PodAntiAffinity uses the scene:
Distribute the POD of a service in different hosts or topology domains to improve the stability of the service itself.
Give POD exclusive access to a node to ensure resource isolation and ensure that there are no other pod to share node resources.
Spread the POD of services that may interact with each other on different hosts.
NodeAffinity
Reference: https://k8smeetup.github.io/docs/concepts/configuration/assign-pod-node/#node- affinity beta- characteristics
Example of requiredDuringSchedulingIgnoredDuringExecution
The following example uses nodeAffinity to deploy POD to hosts mesos-slave1 and mesos-slave2
ApiVersion: v1kind: Podmetadata: name: with-node-affinity annotations: scheduler.alpha.kubernetes.io/affinity: > {"nodeAffinity": {"requiredDuringSchedulingIgnoredDuringExecution": {"nodeSelectorTerms": [{"matchExpressions": [{"key": "kubernetes.io/hostname" "operator": "In", "values": ["mesos-slave1" "mesos-slave2"]} another-annotation-key: another-annotation-valuespec: containers:-name: with-node-affinity image: gcr.io/google_containers/pause:2.0
We can see that NodeSelectorTerms can have multiple yes or relationships, satisfying any one can be satisfied, MatchExpressions can also have multiple ones, and the relationship between them must be satisfied.
PreferredDuringSchedulingIgnoredDuringExecution example apiVersion: v1kind: Podmetadata: name: with-labels annotations: scheduler.alpha.kubernetes.io/affinity: > {"nodeAffinity": {"preferredDuringSchedulingIgnoredDuringExecution": [{"weight": 1 "preference": {"matchExpressions": [{"key": "disktype", "operator": "In" "values": ["ssd"]}}]}} another-annotation-key: another-annotation-valuespec: containers:-name: test-affinity env:- Name: MYSQL_ROOT_PASSWORD value: pass image: mysql
The SecretredDuringSchedulingIgnoredDuringExecution value is a list, and the order is determined according to the weight.
MatchExpressions values are list relations and must be satisfied
Inter-pod affinity and anti-affinity
Inter-pod affinity determines the running node of the scheduled pod based on the label of the pod already running on the node rather than the label of the node. Because the pod runs on the specified namespace, you need to specify the namesapce on which to run the pod.
Anti-affinity is the opposite of Inter-pod affinity
In the following example, an Pod affinity and a Pod anti-affinity rule are defined. Among them, the Pod affinity rule must be satisfied, and the Pod anti-affinity rule is satisfied by reference.
The meaning of the Pod affinity rule is that Pod must be deployed on a node where there is at least one running Pod. The security attribute of this Pod is "S1", and the deployed node is required to be in the same cloud service area as the running Pod node, that is, "topologyKey:failure-domain.beta.kubernetes.io/zone". In other words, if something goes wrong in an area, we want these Pod to be migrated to the same area again.
The meaning of the Pod anti-compatibility rule is that Pod cannot be deployed on a node, the security attribute in the running Pod on this node is "S2", and the newly deployed Pod cannot be on the same host as the Pod whose security attribute is "S2", that is, "topologyKey: kubernetes.io/hostname".
The values of operator in matchExpressions include In, NotIn, Exists, DoesNotExist, Gt and Lt. The values of topologyKey include:
Kubernetes.io/hostname the same host
one
Failure-domain.beta.kubernetes.io/zone same cloud service area
one
Failure-domain.beta.kubernetes.io/region same area
one
ApiVersion: v1kind: Podmetadata: name: with-pod-affinity annotations: scheduler.alpha.kubernetes.io/affinity: > {"podAffinity": {"requiredDuringSchedulingIgnoredDuringExecution": [{"labelSelector": {"matchExpressions": [{"key": "security" "operator": "In", "values": ["S1"]}}, "topologyKey": "failure-domain.beta.kubernetes.io/zone"}]} "podAntiAffinity": {"requiredDuringSchedulingIgnoredDuringExecution": [{"labelSelector": {"matchExpressions": [{"key": "security", "operator": "In" "values": ["S2"]}} "topologyKey": "kubernetes.io/hostname"}} spec: containers:-name: with-pod-affinity image: gcr.io/google_containers/pause:2.0 so far I believe you have a deeper understanding of "the implementation of K8s nodeSelector and affinity scheduling affinity", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.