每期一个小窍门: k8s容器亲和性的小例子

Posted 李昊轩的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每期一个小窍门: k8s容器亲和性的小例子相关的知识,希望对你有一定的参考价值。

很多情况下 我们希望某些pod 禁止运行到一些worker节点上, 可以利用容器亲和性来实现
例如下面 设置此pod调度在非ip为10.0.40.3的节点上, 而其他节点都可以

    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: kubernetes.io/hostname
                    operator: NotIn
                    values:
                      - 10.0.40.3

完整demo文件 安装了一个metabase

apiVersion: apps/v1
kind: Deployment
metadata:
  name: metabase3
  namespace: demo
  labels:
    app: metabase3
    version: "0.1"
spec:
  replicas: 1
  selector:
    matchLabels:
      app: metabase3
      version: "0.1"
  template:
    metadata:
      labels:
        app: metabase3
        version: "0.1"
        production: metabase3
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: kubernetes.io/hostname
                    operator: NotIn
                    values:
                      - 10.0.40.3
      containers:
        - name: metabase3
          image: metabase/metabase:v0.41.5
          env:
            - name: MB_DB_TYPE
              value: "mysql"
            - name: MB_DB_DBNAME
              value: "db"
            - name: MB_DB_PORT
              value: "3306"
            - name: MB_DB_USER
              value: "root"
            - name: MB_DB_HOST
              value: "10.0.40.3"
            - name: MB_DB_PASS
              value: "!"
          resources:
            requests:
              cpu: 10m
              memory: 10Mi
            limits:
              cpu: 4000m
              memory: 3000Mi
          command: [ "java","-Xms256M","-Xmx3000M","-Dlogfile.path=target/log","-XX:ParallelGCThreads=5","-server","-jar","/app/metabase.jar" ]
          ports:
            - containerPort: 3000

以上是关于每期一个小窍门: k8s容器亲和性的小例子的主要内容,如果未能解决你的问题,请参考以下文章

每期一个小窍门: k8s容器亲和性的小例子

每期一个小窍门: 灰度重启 mongodb 复制集

每期一个小窍门: 灰度重启 mongodb 复制集

每期一个小窍门: 灰度重启 mongodb 复制集

每期一个小窍门(006): k8s创建顶级权限cluster role

每期一个小窍门(006): k8s创建顶级权限cluster role