kubernetes集群编排——k8s调度-CSDN博客

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

nodename

vim nodename.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx

spec:
  containers:
  - name: nginx
    image: nginx
  nodeName: k8s2

  nodeName: k8s2 #找不到节点pod会出现pending优先级最高

kubectl apply -f nodename.yaml

kubectl get pod -o wide

回收

kubectl delete -f nodename.yaml

nodeselector

vim nodeselector.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  nodeSelector:
    disktype: ssd
kubectl label  nodes k8s4 disktype=ssd

kubectl label  nodes k8s3 disktype=ssd
kubectl apply -f nodeselector.yaml

回收

kubectl delete -f nodeselector.yaml

nodeaffinity

vim nodeaffinity.yaml
apiVersion: v1
kind: Pod
metadata:
  name: node-affinity
spec:
  containers:
  - name: nginx
    image: nginx
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
           nodeSelectorTerms:
           - matchExpressions:
             - key: disktype
               operator: In
               values:
                 - ssd
                 - fc

      preferredDuringSchedulingIgnoredDuringExecution:
      - weight: 1
        preference:
          matchExpressions:
          - key: kubernetes.io/hostname
            operator: NotIn
            values:
            - k8s3
kubectl apply -f nodeaffinity.yaml
kubectl describe  pod node-affinity

回收

kubectl delete -f nodeaffinity.yaml

podaffinity

vim podaffinity.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx
            topologyKey: "kubernetes.io/hostname"
kubectl apply -f podaffinity.yaml

kubectl get pod -o wide

回收

kubectl delete -f podaffinity.yaml

podantiaffinity

vim podantiaffinity.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx
            topologyKey: "kubernetes.io/hostname"
kubectl apply -f podantiaffinity.yaml

kubectl get pod -o wide

回收

kubectl delete -f podantiaffinity.yaml

pod反亲和倾向满足

vim poda.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: node-affinity
spec:
 replicas: 3
 selector:
  matchLabels:
   app: nginx
 template:
  metadata:
   labels:
    app: nginx
  spec:
   tolerations:
   - effect: NoSchedule
     operator: Exists
   - effect: NoExecute
     operator: Exists
   containers:
   - name: nginx
     image: nginx
   affinity:
     podAntiAffinity:
       preferredDuringSchedulingIgnoredDuringExecution:
       - weight: 100
         podAffinityTerm:
           labelSelector:
             matchExpressions:
             - key: app
               operator: In
               values:
               - nginx
           topologyKey: kubernetes.io/hostname

     nodeAffinity:
       requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: disktype
                operator: In
                values:
                  - ssd
                  - sata
kubectl apply -f poda.yaml

kubectl get pod -o wide

回收

kubectl delete -f poda.yaml

Taints

vim taint.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - image: nginx
        name: nginx
kubectl apply -f taint.yaml

kubectl get pod -o wide

设置taint

kubectl taint node k8s3 k1=v1:NoSchedule

kubectl describe nodes  k8s3 |grep Tain

kubectl scale deployment web --replicas 6

kubectl get pod -o wide

kubectl taint node k8s3 k1=v1:NoExecute

kubectl describe nodes  k8s3 |grep Tain

kubectl get pod -o wide

回收

kubectl delete  -f taint.yaml

设置 tolerations

vim taint.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
  name: web
spec:
  replicas: 6
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      tolerations:
      - operator: Exists
        effect: NoSchedule
      containers:
      - image: nginx
        name: nginx
kubectl apply -f taint.yaml

kubectl get pod -o wide

回收

kubectl delete -f taint.yaml

容忍所有taints

vim taint.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
  name: web
spec:
  replicas: 6
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      tolerations:
      - operator: Exists
      containers:
      - image: nginx
        name: nginx
kubectl apply -f taint.yaml

kubectl get pod -o wide

回收

kubectl delete -f taint.yaml

删除taints

kubectl taint  node k8s3 k1-

cordon、drain、delete

kubectl create deployment demo --image nginx --replicas 3

kubectl get pod -o wide

kubectl cordon k8s3

kubectl get node

kubectl scale deployment demo --replicas 6

kubectl get pod -o wide

kubectl drain k8s3 --ignore-daemonsets

kubectl get pod -o wide

kubectl delete nodes k8s3

kubectl get node

k8s3节点重启kubelet服务重新加入集群

[root@k8s3 ~]# systemctl restart kubelet

[root@k8s2 node]# kubectl get node

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: k8s