k8s亲和性

节点选择器:NodeSelector
如果我们期望把Pod调度到某一个特定的节点上,可以通过设定Pod.spec.nodeName给定node名称实现。我们可以给一部分node打上特有标签,在pod.spec.nodeSelector中匹配这些标签

# 查看node的label命令
kubectl get nodes --show-labels

# 给一个节点增加标签
kubectl    label  node  k8s-node-1   effect=mysql

之后我们可以采用软亲和或者硬亲和的策略将pod调度到刚才标签的node上

  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:  # 硬策略
        nodeSelectorTerms:
        - matchExpressions:
          - key: effect
            operator: NotIn
            values:
            - mysql
      preferredDuringSchedulingIgnoredDuringExecution:  # 软策略
      - weight: 1
        preference:
          matchExpressions:
          - key: effect
            operator: In
            values:
            - mysql

实际案例

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-muban
  namespace: mysql
  labels:
    app: mysql-muban
spec:
  replicas: 1
  revisionHistoryLimit: 3
  selector:
    matchLabels:
      app: mysql-muban
  template:
    metadata:
      labels:
        app: mysql-muban
    spec:
      containers:
      - name: mysql-muban
        image: mysql:v5.7.22
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: mysqldb-muban
        - mountPath: /etc/mysql
          name: mysqlconf-muban
        env:
        - name: TZ
          value: Asia/Shanghai
        - name: MYSQL_ROOT_PASSWORD
          value: Root123+
        ports:
        - containerPort: 3306
          name: mysql-muban
        resources:
          requests:
            cpu: 1
            memory: 1G
          limits:
            cpu: 4
            memory: 4G
      volumes:
      - name: mysqldb-muban
        hostPath:
          path: /data/mysql/muban/mysqldb
          type: Directory
      - name: mysqlconf-muban
        hostPath:
          path: /data/mysql/muban/conf
          type: Directory
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: effect
                operator: In
                values:
                - mysql
      tolerations:     
        - key: "key"  
          operator: "Equal"  
          value: "value"  
          effect: NoExecute
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-muban
  namespace: mysql
  labels:
    app: mysql-muban
spec:
  type: NodePort
  selector:
    app: mysql-muban
  ports:
  - port: 3306
    nodePort: 33307

参考文章:
https://www.cnblogs.com/sxgaofeng/p/13188723.html

https://www.cnblogs.com/wangxu01/articles/11672175.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值