kubernetes集群编排(6)

目录

k8s调度

nodename

nodeselector

nodeaffinity

podaffinity

podantiaffinity

Taints

cordon、drain、delete


k8s调度

nodename

[root@k8s2 node]# vim nodename.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx

spec:
  containers:
  - name: nginx
    image: nginx
  nodeName: k8s3		
[root@k8s2 node]# kubectl apply -f nodename.yaml

[root@k8s2 node]# kubectl delete -f nodename.yaml

nodeselector

NodeSelector是Kubernetes中一个用于选择节点的机制。它是一种标签选择器,可以根据选择器定义匹配节点标签,并将Pod调度到这些匹配的节点上

[root@k8s2 node]# vim nodeselector.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
  nodeSelector:
    disktype: ssd

[root@k8s2 node]# kubectl label  nodes k8s4 disktype=ssd
[root@k8s2 node]# kubectl label  nodes k8s3 disktype=ssd

[root@k8s2 node]# kubectl apply -f nodeselector.yaml

[root@k8s2 node]# kubectl delete -f nodeselector.yaml

nodeaffinity

[root@k8s2 node]# 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

[root@k8s2 node]# kubectl apply -f nodeaffinity.yaml
[root@k8s2 node]# kubectl describe  pod node-affinity

[root@k8s2 node]# kubectl delete -f nodeaffinity.yaml

podaffinity

[root@k8s2 node]# 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"

[root@k8s2 node]# kubectl apply -f podaffinity.yaml

[root@k8s2 node]# kubectl delete -f podaffinity.yaml

podantiaffinity

[root@k8s2 node]# 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"

[root@k8s2 node]# kubectl apply -f podantiaffinity.yaml

[root@k8s2 node]# kubectl delete -f podantiaffinity.yaml

Taints

[root@k8s2 node]# 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

[root@k8s2 node]# kubectl apply -f taint.yaml

设置taint

root@k8s2 pod]# kubectl taint node k8s3 k1=v1:NoSchedule
[root@k8s2 pod]# kubectl describe nodes  k8s3 |grep Tain

[root@k8s2 pod]# kubectl scale deployment web --replicas 6

[root@k8s2 pod]# kubectl taint node k8s3 k1=v1:NoExecute
[root@k8s2 pod]# kubectl describe nodes  k8s3 |grep Tain

设置 tolerations

[root@k8s2 node]# 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

[root@k8s2 node]# kubectl apply -f taint.yaml

容忍所有taints

[root@k8s2 node]# 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

[root@k8s2 node]# kubectl apply -f taint.yaml

回收、删除taints

[root@k8s2 node]# kubectl delete -f taint.yaml
[root@k8s2 node]# kubectl taint  node k8s3 k1-

cordon、drain、delete

[root@k8s2 node]# kubectl create deployment demo --image nginx --replicas 3
[root@k8s2 node]# kubectl cordon k8s3
[root@k8s2 node]# kubectl get node

[root@k8s2 node]# kubectl scale deployment demo --replicas 6

[root@k8s2 node]# kubectl drain k8s3 --ignore-daemonsets

[root@k8s2 node]# kubectl delete nodes k8s3
[root@k8s2 node]# kubectl get node

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

[root@k8s3 ~]# systemctl restart kubelet
[root@k8s2 node]# kubectl get node

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值