KUBERNETES之Kubernetes Workloads(Kubernetes 作業管理)四

2.5 DaemonSet(Daemon 作業)

在 Kubernetes 集羣裏,運行一個 Daemon Pod。

 

2.5.1 三個特徵
  • 這個 Pod 運行在 Kubernetes 集羣裏的每一個節點(Node)上
  • 每個節點上只有一個這樣的 Pod 實例
  • 當有新的節點加入 Kubernetes 集羣后,該 Pod 會自動地在新節點上被創建出來;而當舊節點被刪除後,它上面的 Pod 也相應地會被回收掉。

 

2.5.2 應用案例
  • 各種網絡插件的 Agent 組件,都必須運行在每一個節點上,用來處理這個節點上的容器網絡;
  • 各種存儲插件的 Agent 組件,也必須運行在每一個節點上,用來在這個節點上掛載遠程存儲目錄,操作容器的 Volume 目錄;
  • 各種監控組件和日誌組件,也必須運行在每一個節點上,負責這個節點上的監控信息和日誌蒐集。

 

2.5.3 工作原理

1.api對象格式

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd-elasticsearch
  namespace: kube-system
  labels:
    k8s-app: fluentd-logging
spec:
  selector:
    matchLabels:
      name: fluentd-elasticsearch
  template:
    metadata:
      labels:
        name: fluentd-elasticsearch
    spec:
      tolerations:
      - key: node-role.kubernetes.io/master
        effect: NoSchedule
      containers:
      - name: fluentd-elasticsearch
        image: k8s.gcr.io/fluentd-elasticsearch:1.20
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
        volumeMounts:
        - name: varlog
          mountPath: /var/log
        - name: varlibdockercontainers
          mountPath: /var/lib/docker/containers
          readOnly: true
      terminationGracePeriodSeconds: 30
      volumes:
      - name: varlog
        hostPath:
          path: /var/log
      - name: varlibdockercontainers
        hostPath:
          path: /var/lib/docker/containers

這個DaemonSet,管理的是一個 fluentd-elasticsearch 鏡像的 Pod。這個鏡像的功能非常實用:通過 fluentd 將 Docker 容器裏的日誌轉發到 ElasticSearch 中。

 

 selector:
    matchLabels:
      name: fluentd-elasticsearch

使用 selector 選擇管理所有攜帶了 name=fluentd-elasticsearch 標籤的 Pod

 

 volumeMounts:
        - name: varlog
          mountPath: /var/log
        - name: varlibdockercontainers
          mountPath: /var/lib/docker/containers
          readOnly: true
      terminationGracePeriodSeconds: 30
      volumes:
      - name: varlog
        hostPath:
          path: /var/log
      - name: varlibdockercontainers
        hostPath:
          path: /var/lib/docker/containers

這個容器掛載了兩個 hostPath 類型的 Volume,分別對應宿主機的 /var/log 目錄和 /var/lib/docker/containers 目錄。

Docker 容器裏應用的日誌,默認會保存在宿主機的 /var/lib/docker/containers/{{. 容器 ID}}/{{. 容器 ID}}-json.log 文件裏,所以這個目錄正是 fluentd 的蒐集目標。

 

 

2.DS如何保證每個 Node 上有且只有一個被管理的 Pod 呢?

DaemonSet Controller,首先從 Etcd 裏獲取所有的 Node 列表,然後遍歷所有的 Node。可能有三種情況

 
1)沒有這種 Pod,那麼就意味着要在這個 Node 上創建這樣一個 Pod;

nodeSelector 將要廢棄的字段,會被nodeAffinity替換

nodeAffinity

apiVersion: v1
kind: Pod
metadata:
  name: with-node-affinity
spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: metadata.name
            operator: In
            values:
            - node-pl

requiredDuringSchedulingIgnoredDuringExecution

這個 nodeAffinity 必須在每次調度的時候予以考慮。同時,這也意味着你可以設置在某些情況下不考慮這個 nodeAffinity;

這個 Pod,將來只允許運行在“metadata.name”是“node-pl”的節點上。

In 部分匹配

Equal 完全匹配
 

 

2)有這種 Pod,但是數量大於 1,那就說明要把多餘的 Pod 從這個 Node 上刪除掉;

3)正好只有一個這種 Pod,那說明這個節點是正常的。

 

3.DS網絡插件pod沒有啓動,不允許調度pod?

1)Toleration

這個字段意味着這個 Pod,會“容忍”(Toleration)某些 Node 的“污點”(Taint)。

apiVersion: v1
kind: Pod
metadata:
  name: with-toleration
spec:
  tolerations:
  - key: node.kubernetes.io/unschedulable
    operator: Exists
    effect: NoSchedule
  • 這個 Toleration 的含義是:“容忍”所有被標記爲 unschedulable“污點”的 Node;“容忍”的效果是允許調度。

  • operator

    Exists 表示key是否存在,此時無需定義value

    Equal(默認) 表示key是否等於value,默認

默認k8s會給剛加入的節點打上污點node.kubernetes.io/network-unavailable

kubectl get ds weave-net -n kube-system -o yaml

weave網絡組件本身就是通過這種機制讓所有pod上運行weave pod的

 

 

2)Taint

kubectl taint nodes node1 key=value:NoSchedule

Effect

  • NoSchedule 表示不允許調度,已調度的不影響
  • NoExecute 表示不允許調度,已調度的在tolerationSeconds(定義在Tolerations上)後刪除
  • PreferNoSchedule 表示儘量不調度

 

2.5.4 實戰

1.在所有節點上下載鏡像

docker pull anjia0532/google-containers.fluentd-elasticsearch:1.20
docker tag anjia0532/google-containers.fluentd-elasticsearch:1.20 k8s.gcr.io/fluentd-elasticsearch:1.20
docker rmi anjia0532/google-containers.fluentd-elasticsearch:1.20

查看

docker images
REPOSITORY                              TAG                 IMAGE ID            CREATED             SIZE
k8s.gcr.io/fluentd-elasticsearch        1.20                c264dff3420b        3 years ago         301MB

 

2.創建ds

kubectl create -f fluentd-elasticsearch.yaml

在YAML文件內容上面工作原理處。

 

3.查看對象

kubectl get ds -n kube-system fluentd-elasticsearch

查看

在這裏插入圖片描述

 

4.升級鏡像

kubectl set image ds/fluentd-elasticsearch fluentd-elasticsearch=k8s.gcr.io/fluentd-elasticsearch:v2.2.0 --record -n=kube-system

拉取鏡像

docker pull anjia0532/google-containers.fluentd-elasticsearch:v2.2.0
docker tag anjia0532/google-containers.fluentd-elasticsearch:v2.2.0 k8s.gcr.io/fluentd-elasticsearch:v2.2.0
docker rmi anjia0532/google-containers.fluentd-elasticsearch:v2.2.0

觸發滾動更新

kubectl get pod -w -n kube-system

在這裏插入圖片描述

 

5.查看滾動歷史

kubectl rollout history daemonset fluentd-elasticsearch -n kube-system

 

2.6 Job(一次性任務)&CronJob(定時任務)

 

2.6.1 作業分類
  • 在線作業Long Running Task

    運行後除非出錯或停止,它的容器一直保持在Running狀態

  • 離線作業Batch Job

 

2.6.2 Job API
apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  template:
    spec:
      containers:
      - name: pi
        image: resouer/ubuntu-bc 
        command: ["sh", "-c", "echo 'scale=10000; 4*a(1)' | bc -l "]
      restartPolicy: Never
  backoffLimit: 4

restartPolicy

  • Never
  • OnFailure

kubectl describe jobs/pi

kubectl logs

1)如果這個離線作業失敗了要怎麼辦?

spec.backoffLimit 重試次數限制

2)並行job

apiVersion: batch/v1
kind: Job
metadata:
  name: pi
spec:
  parallelism: 2
  completions: 4
  template:
    spec:
      containers:
      - name: pi
        image: resouer/ubuntu-bc
        command: ["sh", "-c", "echo 'scale=5000; 4*a(1)' | bc -l "]
      restartPolicy: Never
  backoffLimit: 4
  • spec.parallelism 它定義的是一個 Job 在任意時間最多可以啓動多少個 Pod 同時運行
  • spec.completions 它定義的是 Job 至少要完成的 Pod 數目,即 Job 的最小完成數

 

2.6.3 CronJob

1)api對象

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

schedule: “*/1 * * * *”

 

2)查看

kubectl get cronjob NAME

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章