K8S脈絡整理(008)-DaemonSet


1、典型應用場景

2、兩個 k8s 自己的 DaemonSet:kube-flannel-ds 和 kube-proxy

3、以 Prometheus Node Exporter 為例演示如何運行自己的 DaemonSet

Kubernetes 集群中每個當前運行的資源(deployment daemonset replicaset pod)都可以通過 kubectl edit 查看和編輯其配置和運行狀態。

比如 kubectl edit deployment nginx-deployment
使用kubespary安裝的k8s1.9 中,kube-proxy不再是daemonset,而是以pod存在各節點;

查看pod配置方式:kubectl edit pod kube-proxy-master --namespace=kube-system


 

1、典型應用場景

Deployment 部署的副本 Pod 會分布在各個 Node 上,每個 Node 都可能運行好幾個副本。

DaemonSet 的不同之處在於:每個 Node 上最多只能運行一個副本

DaemonSet 的典型應用場景有:

  1. 在集群的每個節點上運行存儲 Daemon,比如 glusterd 或 ceph。

  2. 在每個節點上運行日志收集 Daemon,比如 flunentd 或 logstash。

  3. 在每個節點上運行監控 Daemon,比如 Prometheus Node Exporter 或 collectd。

其實 Kubernetes 自己就在用 DaemonSet 運行系統組件。執行如下命令:

kubectl get daemonset --namespace=kube-system
daweij@master:~$ kubectl get daemonset --namespace=kube-system
NAME          DESIRED   CURRENT   READY     UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
calico-node   5         5         5         5            5           <none>          33d

DaemonSet calico-node負責在每個節點上運行 calico網絡。

daweij@master:~$ kubectl get pod --namespace=kube-system -o wide
NAME                                    READY     STATUS    RESTARTS   AGE       IP               NODE
calico-node-4gm72                       1/1       Running   53         33d       172.28.2.212     node2
calico-node-8fkfk                       1/1       Running   0          33d       172.28.2.210     master
calico-node-fqdwj                       1/1       Running   53         33d       172.28.2.213     node3
calico-node-lpdtx                       1/1       Running   48         33d       172.28.2.211     node1
calico-node-nq8l2                       1/1       Running   42         33d       172.28.2.214     node4
kube-apiserver-master                   1/1       Running   0          33d       172.28.2.210     master
kube-apiserver-node4                    1/1       Running   224        18d       172.28.2.214     node4
kube-controller-manager-master          1/1       Running   0          33d       172.28.2.210     master
kube-controller-manager-node4           1/1       Running   5          18d       172.28.2.214     node4
kube-dns-79d99cdcd5-6vvrw               3/3       Running   0          18d       10.233.102.131   node1
kube-dns-79d99cdcd5-rkpf2               3/3       Running   0          18d       10.233.71.5      node3
kube-proxy-master                       1/1       Running   0          33d       172.28.2.210     master
kube-proxy-node1                        1/1       Running   0          33d       172.28.2.211     node1
kube-proxy-node2                        1/1       Running   0          18d       172.28.2.212     node2
kube-proxy-node3                        1/1       Running   0          33d       172.28.2.213     node3
kube-proxy-node4                        1/1       Running   0          18d       172.28.2.214     node4
kube-scheduler-master                   1/1       Running   0          33d       172.28.2.210     master
kube-scheduler-node4                    1/1       Running   3          18d       172.28.2.214     node4
kubedns-autoscaler-5564b5585f-7z62x     1/1       Running   0          18d       10.233.71.4      node3
kubernetes-dashboard-6bbb86ffc4-zmmc2   1/1       Running   0          18d       10.233.75.5      node2
nginx-proxy-node1                       1/1       Running   0          33d       172.28.2.211     node1
nginx-proxy-node2                       1/1       Running   0          18d       172.28.2.212     node2
nginx-proxy-node3                       1/1       Running   0          33d       172.28.2.213     node3

因為 calico 屬於系統組件,需要在命令行中通過 --namespace=kube-system 指定 namespace kube-system

如果不指定則只返回默認 namespace default 中的資源。

下節詳細分析 calico和 kube-proxy 這兩個 DaemonSet。

calico-node的ymal文件模板參考

vim /root/kubespray/roles/network_plugin/calico/templates/calico-node.yml.j2

或執行下述命令查看配置

kubectl edit daemonset calico-node --namespace=kube-system

kube-proxy參考

vim  /root/kubespray/roles/kubernetes/node/templates/kube-proxy-kubeconfig.yaml.j2

 


 

因所安裝網絡和cloudman的不同,下述以cloudman blog為准。

參考:http://www.cnblogs.com/CloudMan6/p/8449209.html

本節詳細分析兩個 k8s 自己的 DaemonSet:kube-flannel-ds 和 kube-proxy 。

kube-flannel-ds

下面我們通過分析 kube-flannel-ds 來學習 DaemonSet。

還記得之前是如何部署 flannel 網絡的嗎?我們執行了如下兩個命令:

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

flannel 的 DaemonSet 就定義在 kube-flannel.yml 中:

注:配置文件的完整內容要復雜些,為了更好地學習 DaemonSet,這里只保留了最重要的內容。

① DaemonSet 配置文件的語法和結構與 Deployment 幾乎完全一樣,只是將 kind 設為 DaemonSet

hostNetwork 指定 Pod 直接使用的是 Node 的網絡,相當於 docker run --network=host。考慮到 flannel 需要為集群提供網絡連接,這個要求是合理的。

③ containers 定義了運行 flannel 服務的兩個容器

我們再來分析另一個 DaemonSet kube-proxy

kube-proxy

由於無法拿到 kube-proxy 的 YAML 文件,只能運行如下命令查看其配置:  

kubectl edit daemonset kube-proxy --namespace=kube-system

 

同樣為了便於理解,這里只保留了最重要的信息。

① kind: DaemonSet 指定這是一個 DaemonSet 類型的資源。

② containers 定義了 kube-proxy 的容器。

③ status 是當前 DaemonSet 的運行時狀態,這個部分是 kubectl edit特有的。

其實 Kubernetes 集群中每個當前運行的資源都可以通過 kubectl edit 查看其配置和運行狀態,

比如 kubectl edit deployment nginx-deployment

首先查看deployment  kubectl get deployment --namespace=kube-system

使用kubespary安裝的k8s1.9 中,kube-proxy不再是daemonset,而是以pod存在各節點,

查看配置方式:kubectl edit pod kube-proxy-master --namespace=kube-system

apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubernetes.io/config.hash: d9ac7a0726731fe5e9bb4d0047ee6840
    kubernetes.io/config.mirror: d9ac7a0726731fe5e9bb4d0047ee6840
    kubernetes.io/config.seen: 2018-02-01T19:02:59.015440759+08:00
    kubernetes.io/config.source: file
    kubespray.kube-proxy-cert/serial: C943FB9988696E0A
  creationTimestamp: 2018-02-01T11:04:11Z
  labels:
    k8s-app: kube-proxy
  name: kube-proxy-master
  namespace: kube-system
  resourceVersion: "310"
  selfLink: /api/v1/namespaces/kube-system/pods/kube-proxy-master
  uid: a22c8545-073f-11e8-b7fb-005056a90262
spec:
  containers:
  - command:
    - /hyperkube
    - proxy
    - --v=2
    - --kubeconfig=/etc/kubernetes/kube-proxy-kubeconfig.yaml
    - --bind-address=172.28.2.210
    - --cluster-cidr=10.233.64.0/18
    - --proxy-mode=iptables
    image: quay.io/coreos/hyperkube:v1.9.0_coreos.0
    imagePullPolicy: IfNotPresent
    name: kube-proxy
    resources:
      limits:
................

 


 

 

運行自己的 DaemonSet。 

本節以 Prometheus Node Exporter 為例演示如何運行自己的 DaemonSet。

Prometheus 是流行的系統監控方案,Node Exporter 是 Prometheus 的 agent,以 Daemon 的形式運行在每個被監控節點上。

如果是直接在 Docker 中運行 Node Exporter 容器,命令為:

docker run -d \
-v "/proc:/host/proc" \
-v "/sys:/host/sys" \
-v "/:/rootfs" \
--net=host \ prom/node-exporter \
--path.procfs /host/proc \
--path.sysfs /host/sys \
--collector.filesystem.ignored-mount-points "^/(sys|proc|dev|host|etc)($|/)"

將其轉換為 DaemonSet 的 YAML 配置文件 node_exporter.yml:

① 直接使用 Host 的網絡。
② 設置容器啟動命令。
③ 通過 Volume 將 Host 路徑 /proc/sys 和 / 映射到容器中。我們將在后面詳細討論 Volume。

執行 kubectl apply -f node_exporter.yml

DaemonSet node-exporter-daemonset 部署成功,k8s-node1 和 k8s-node2 上分別運行了一個 node exporter Pod。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM