Kubernetes 瀛︿範21 kubernetes楂樼骇璋冨害鏂瑰紡

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kubernetes 瀛︿範21 kubernetes楂樼骇璋冨害鏂瑰紡相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/aos' title='aos'>aos   other   shu   ons   鐢熸晥   tier   搴旂敤   exp   

涓€銆佹杩?/p>

銆€銆€1銆佷笂闆嗚浜哠cheduler鍦ㄥ疄鐜拌皟搴︽椂鍒嗕笁姝ュ疄鐜拌皟搴﹁繃绋嬨€傞鍏堟槸棰勯€夛紝鍗充粠鎵€鏈夎妭鐐逛腑閫夋嫨鍩烘湰绗﹀悎閫夋嫨鏉′欢鐨勮妭鐐广€傝€屽悗鍦ㄥ熀鏈鍚堟潯浠剁殑鑺傜偣涓娇鐢ㄤ紭閫夊嚱鏁拌绠椾粬浠悇鑷殑寰楀垎骞跺姞浠ユ瘮杈冦€傚苟浠庢渶楂樺緱鍒嗙殑鑺傜偣涓殢鏈洪€夋嫨鍑轰竴涓繍琛宲od鐨勮妭鐐癸紝杩欏氨鏄垜浠殑鎺у埗骞抽潰涓璼cheduler鎵€瀹炵幇璐熻矗鐨勪富瑕佸姛鐢ㄣ€傚悓鏃跺鏋滃湪鏌愪簺璋冨害鍦烘櫙涓垜浠湡鏈涜兘澶熼€氳繃鑷繁鐨勯璁惧幓褰卞搷浠栫殑涓€浜涜皟搴︽柟寮忥紝姣斿灏辨槸鎶婃垜浠殑pod杩愯鍦ㄦ煇浜涚壒瀹氱殑鑺傜偣涔嬩笂鐨勬椂鍊欏彲浠ラ€氳繃鑷繁鐨勯璁炬搷浣滃幓褰卞搷浠栫殑棰勯€夊拰浼橀€夎繃绋嬩粠鑰屼娇寰楄皟搴︽搷浣滆兘绗﹀悎鎴戜滑鐨勬湡鏈涖€傛垜浠篃璇磋繃锛屾绫荤殑褰卞搷鏂瑰紡閫氬父鏈夊洓绉嶃€?/p>

銆€銆€銆€銆€b銆佽妭鐐逛翰鍜屾€ц皟搴︼細nodeAffinity

浜屻€佽皟搴︽柟寮?/p>

銆€銆€1銆佽妭鐐归€夋嫨鍣ㄨ皟搴︼細nodeSelector锛宯odeName锛屽nodeName鏉ヨ濡傛灉鎴戜滑鏈熸湜鎶妏od璋冨害鍒版煇涓€鐗瑰畾鑺傜偣涓婂氨鍦╬od鐨剆pec鐨刵odeName灞炴€т腑鐩存帴缁欏畾杩欎釜nodeName鐨勫悕绉板嵆鍙紝杩欎釜瀵瑰簲鐨凱od涓€瀹氬彧鑳借皟搴﹀埌杩欎釜瀵瑰簲鐨勮妭鐐逛笂锛屽鏋滄湁涓€绫昏妭鐐归兘绗﹀悎鎴戜滑鐨勮皟搴︽柟寮忥紝閭d箞寤鸿浣跨敤nodeSelector锛屾剰鎬濆氨鏄鎴戜滑鍙互缁欎竴娈佃妭鐐规墦涓婄壒鏈夋爣绛撅紝鑰屽悗鎴戜滑鍦╬od.spec鐨勫睘鎬т腑浣跨敤nodeSelector鍘诲尮閰嶈繖浜涙爣绛俱€傝妭鐐硅兘澶熼€傞厤杩欎簺鏍囩鐨勫氨鏄痯od鍙繍琛岀殑鑺傜偣涔嬩竴锛屽惁鍒欏氨涓嶆槸锛岃繖绉嶉€夋嫨鏂瑰紡鍙互鏋佸ぇ鐨勭缉灏忛閫夎寖鍥淬€備粬鐨勭敤娉曚篃绠€鍗曪紝鐩存帴缁欏畾鎴戜滑閫夊畾鐨勬爣绛惧嵆鍙€備篃鍙互缁勫悎澶氫釜鏍囩锛屼互榛樿鐨勯€昏緫鍩熺殑鏂瑰紡鏉ラ€夋嫨锛屽彧鏈夋墍鏈夋爣绛鹃兘婊¤冻鐨刾od node鎵嶄細杩愯姝od

[root@k8smaster ~]# kubectl explain pod.spec.nodeSelector
KIND:     Pod
VERSION:  v1

FIELD:    nodeSelector <map[string]string>

DESCRIPTION:
     NodeSelector is a selector which must be true for the pod to fit on a node.
     Selector which must match a node鈥?/span>s labels for the pod to be scheduled on
     that node. More info:
     https://kubernetes.io/docs/concepts/configuration/assign-pod-node/

銆€銆€銆€銆€a銆佹帴涓嬫潵鎴戜滑绠€鍗曟搷浣滀竴娉?/p>

[root@k8smaster schedule]# cat pod-demo.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels: #涔熷彲浠ュ湪姝ゅ鍐欎笂app:myapp,tier:frontend浠f浛涓嬮潰涓よ
    app: myapp #搴旂敤灞傜骇鏍囩
    tier: frontend #鏋舵瀯灞傜骇鏍囩锛屽湪鍒嗗眰鏋舵瀯涓睘浜巉rontend灞?
  annotations:
    wohaoshuai.com/created-by: "cluster admin"
spec:
  containers: #鏄竴涓垪琛紝鍏蜂綋瀹氫箟鏂瑰紡濡備笅
  - name: myapp
    image: ikubernetes/myapp:v1
  nodeSelector:
    disktype: ssd
[root@k8smaster schedule]# kubectl apply -f pod-demo.yaml 
pod/pod-demo created
[root@k8smaster schedule]# kubectl get pods -o wide --show-labels 
NAME       READY     STATUS    RESTARTS   AGE       IP           NODE       LABELS
pod-demo   1/1       Running   0          25s       10.244.1.3   k8snode1   app=myapp,tier=frontend
[root@k8smaster schedule]# kubectl get nodes --show-labels
NAME        STATUS    ROLES     AGE       VERSION   LABELS
k8smaster   Ready     master    103d      v1.11.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=k8smaster,node-role.kubernetes.io/master=
k8snode1    Ready     <none>    103d      v1.11.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/hostname=k8snode1
k8snode2    Ready     <none>    103d      v1.11.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=k8snode2

銆€銆€銆€銆€b銆佸亣濡傛垜浠郴缁熶笂涓嶅瓨鍦ㄦ嫢鏈夋鏍囩鐨勮妭鐐规椂浼氬嚭鐜颁粈涔堟儏鍐靛憿锛熷彲浠ョ湅鍒皃od浼氫竴鐩村浜巔ending鐘舵€侊紝鍥犱负璋冨害鏄棤娉曟垚鍔熺殑锛岃繖涔熷氨鎰忓懗鐫€nodeSelector鏄竴绉嶅己绾︽潫锛屽彧瑕佷笉婊¤冻绗竴涓潯浠跺湪棰勯€夊叧灏辨棤娉曢€氳繃灏变笉瑕佽浼橀€変簡銆?/p>

[root@k8smaster schedule]# cat pod-demo.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels: #涔熷彲浠ュ湪姝ゅ鍐欎笂app:myapp,tier:frontend浠f浛涓嬮潰涓よ
    app: myapp #搴旂敤灞傜骇鏍囩
    tier: frontend #鏋舵瀯灞傜骇鏍囩锛屽湪鍒嗗眰鏋舵瀯涓睘浜巉rontend灞?
  annotations:
    wohaoshuai.com/created-by: "cluster admin"
spec:
  containers: #鏄竴涓垪琛紝鍏蜂綋瀹氫箟鏂瑰紡濡備笅
  - name: myapp
    image: ikubernetes/myapp:v1
  nodeSelector:
    disktype: hirddisk
[root@k8smaster schedule]# kubectl apply -f pod-demo.yaml 
pod/pod-demo created
[root@k8smaster schedule]# kubectl get pods -o wide
NAME       READY     STATUS    RESTARTS   AGE       IP        NODE
pod-demo   0/1       Pending   0          21s       <none>    <none>
[root@k8smaster schedule]# kubectl describe pods pod-demo
Name:               pod-demo
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               <none>
Labels:             app=myapp
                    tier=frontend
Annotations:        kubectl.kubernetes.io/last-applied-configuration="apiVersion":"v1","kind":"Pod","metadata":"annotations":"wohaoshuai.com/created-by":"cluster admin","labels":"app":
"myapp","tier":"frontend","nam...                    wohaoshuai.com/created-by=cluster admin
Status:             Pending
IP:                 
Containers:
  myapp:
    Image:        ikubernetes/myapp:v1
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-jvtl7 (ro)
Conditions:
  Type           Status
  PodScheduled   False 
Volumes:
  default-token-jvtl7:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-jvtl7
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  disktype=hirddisk
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason            Age                From               Message
  ----     ------            ----               ----               -------
  Warning  FailedScheduling  2s (x16 over 45s)  default-scheduler  0/3 nodes are available: 3 node(s) didn鈥?/span>t match node selector.

銆€銆€銆€銆€銆€銆€鎴戜滑鐜板湪鎵嬪姩缁欎竴鑺傜偣鎵撲笂璇ユ爣绛句粬灏变細绔嬪嵆鐢熸晥浜?/p>

[root@k8smaster schedule]# kubectl label nodes k8snode2 disktype=hirddisk --overwrite
node/k8snode2 labeled
[root@k8smaster schedule]# kubectl get pods -o wide
NAME       READY     STATUS    RESTARTS   AGE       IP           NODE
pod-demo   1/1       Running   0          6m        10.244.2.3   k8snode2

銆€銆€2銆佽妭鐐逛翰鍜屾€ц皟搴odeAffinity锛屽湪鎴戜滑pod.spec鍐呭祵鏈変竴涓睘鎬у氨鍙獳ffinity锛屼粬鐨勫€兼槸涓€涓璞★紝杩欎釜瀵硅薄涓垜浠湁涓嬮潰涓夌锛屼箣鎵€浠ュ彨affinity鏄洜涓哄叾鍐呭祵鐨勬墍璋撶殑鑺傜偣浜插拰鎬э紝pod浜插拰鎬э紝鍜孭od鍙嶄翰鍜屾€ч兘鍦ㄨ繖鍎垮畾涔夛紝姝ゅ鎴戜滑姝ゅ璇寸殑鏄痭odeAffinity

[root@k8smaster schedule]# kubectl explain pods.spec.affinity
KIND:     Pod
VERSION:  v1

RESOURCE: affinity <Object>

DESCRIPTION:
     If specified, the pod鈥?/span>s scheduling constraints

     Affinity is a group of affinity scheduling rules.

FIELDS:
   nodeAffinity    <Object>
     Describes node affinity scheduling rules for the pod.

   podAffinity    <Object>
     Describes pod affinity scheduling rules (e.g. co-locate this pod in the
     same node, zone, etc. as some other pod(s)).

   podAntiAffinity    <Object>
     Describes pod anti-affinity scheduling rules (e.g. avoid putting this pod
     in the same node, zone, etc. as some other pod(s)).

 

以上是关于Kubernetes 瀛︿範21 kubernetes楂樼骇璋冨害鏂瑰紡的主要内容,如果未能解决你的问题,请参考以下文章

python瀛︿範鍩虹涓€

python瀛︿範--bisect妯″潡

MacOS涓婰ucene瀛︿範

docker瀛︿範涔嬭矾

瀛︿範Java鐨勭浜屽ぉ

python瀛︿範绡?5 - 瀛楀吀 闆嗗悎