kubernetes之容器

1.1、容器内存资源限制

apiVersion: v1
kind: Pod
metadata:
  name: memory-demo
  namespace: mem-example
spec:
  containers:
  - name: memory-demo-ctr
    image: polinux/stress
    resources:
      limits:
        memory: "200Mi"
      requests:
        memory: "100Mi"

初始内存为50M,最大100M,超过最大限制时,容器可能正在运行或被杀死

资源单位:E,P,T,G,M,K,Ei,Pi,Ti,Gi,Mi,Ki

如果未对pod指定资源限制,则容器对其使用的内存量没有上限

1.2、容器cpu资源限制

apiVersion: v1
kind: Pod
metadata:
  name: cpu-demo
  namespace: cpu-example
spec:
  containers:
  - name: cpu-demo-ctr
    image: vish/stress
    resources:
      limits:
        cpu: "1"
      requests:
        cpu: "0.5"

资源单位:0.5CPU = 500mCPU = 500milliCPU

2.1、emptyDir存储卷

apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: redis
    volumeMounts:
    - name: redis-storage
      mountPath: /data/redis
  volumes:
  - name: redis-storage
    emptyDir: {}

其生命周期为Pod整个生命周期

2.2、PersistentVolume存储卷

apiVersion: v1
kind: PersistentVolume
metadata:
  name: task-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

定义PersistentVolume 的StorageClassName名称manual用于将PersistentVolumeClaim请求绑定到此PersistentVolume

apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

清除数据

kubectl delete pod task-pv-pod
kubectl delete pvc task-pv-claim
kubectl delete pv task-pv-volume

sudo rm -rf /mnt/data

 3、调度器

3.1、节点亲和性

pod.spec.affinity.nodeAffinity
    requiredDuringSchedulingIgnoredDuringExecution: 硬策略
    preferredDuringSchedulingIgnoredDuringExecution: 软策略
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - kubernetes-node1
                - kubernetes-node3
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 1
            preference:
              matchExpressions:
              - key: kubernetes.io/hostname
                operator: NotIn
                values:
                - kubernetes-node2
      containers:
      - name: nginx-server
        image: nginx:latest
kubectl get node --show-labels        # 查看node节点的标签
# label 的值可选的操作符
In:                    label 的值在某个列表中
NotIn:                 label 的值不在某个列表中
Exists:                某个 label 存在
DoesNotExist:           某个 label 不存在
Gt:                    label 的值大于某个值(字符串比较)
Lt:                    label 的值小于某个值(字符串比较)        

注:matchExpressions:匹配的是node节点的标签(label)

3.2、pod亲和性和非亲和性

node.spec.affinity.podAntiAffinity/podAffinity
    requiredDuringSchedulingIgnoredDuringExecution: 硬策略
    preferredDuringSchedulingIgnoredDuringExecution: 软策略
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      nodeSelector:
        app: nginx
      affinity:  
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - nginx
              topologyKey: kubernetes.io/hostname
      containers:
      - name: nginx
        image: nginx
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: nginx-server
        image: nginx:latest
# label 的值可选的操作符
In:                    label 的值在某个列表中
NotIn:                 label 的值不在某个列表中
Exists:                某个 label 存在
DoesNotExist:           某个 label 不存在

注:matchExpressions: 匹配的是pod节点的标签(label)

 

 

posted @ 2019-11-25 23:09  张界  阅读(412)  评论(1编辑  收藏  举报