【クラウドネイティブ|Kubernetesをゼロから学ぶ】二十四、kubernetesコントローラ Daemonset

ここに画像の説明を挿入

DaemonSet コントローラー: 概念と原則の解釈

DaemonSet の概要

The DaemonSet controller can ensure that all nodes in the k8s cluster run the same Pod copy. ノードを k8s クラスターに追加すると、ノードも自動的にポッド コピーを作成します. ノード ノードがクラスターから削除されると、これらのポッドはは自動的に削除されます。デーモンセットを削除すると、それらが作成した Pod も削除されます。

DaemonSet の仕組み: Pod の管理方法

デーモンセットのコントローラーは、kuberntes のデーモンセット オブジェクト、ポッド オブジェクト、およびノー​​ド オブジェクトをリッスンします。これらの監視対象オブジェクトの変更により、syncLoop サイクルがトリガーされ、kubernetes クラスターがデーモンセット オブジェクトによって記述された状態に向かって進化します。

Daemonset の典型的なアプリケーション シナリオ

クラスターの各ノードで glusterd や ceph などのストレージを実行します。

各ノードで、flunentd、logstash、filebeat などのログ収集コンポーネントを実行します。

Prometheus、Node Exporter、collectd などの監視コンポーネントを各ノードで実行します。

daemonset を使用して作成および実行できます。

DaemonSet と Deployment の違い

Deployment によってデプロイされたレプリカ Pod は各ノードに分散され、各ノードは複数のレプリカを実行できます。

DaemonSets との違いは、各ノードで実行されるレプリカが最大で 1 つあることです。

DaemonSet リソース マニフェスト ファイルの作成スキル

#查看定义 Daemonset 资源需要的字段有哪些
[root@k8smaster ~]# kubectl explain ds 
KIND:     DaemonSet
VERSION:  apps/v1

DESCRIPTION:
     DaemonSet represents the configuration of a daemon set.
FIELDS: 
 apiVersion <string>    #当前资源使用的 api 版本,跟 VERSION: apps/v1 保持一致 
 kind <string> 			#资源类型,跟 KIND: DaemonSet 保持一致 
 metadata <Object> 		#元数据,定义 DaemonSet 名字的 
 spec <Object> 			#定义容器的 
 status <Object> 		#状态信息,不能改 
 
#查看 DaemonSet 的 spec 字段如何定义
[root@k8smaster ~]# kubectl explain ds.spec 
KIND:     DaemonSet
VERSION:  apps/v1

RESOURCE: spec <Object>

DESCRIPTION:
     The desired behavior of this daemon set. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

     DaemonSetSpec is the specification of a daemon set.

FIELDS: 
 minReadySeconds <integer> 			#当新的 pod 启动几秒种后,再 kill 掉旧的pod。 
 revisionHistoryLimit <integer> 	#历史版本 
 selector <Object> -required- 		#用于匹配 pod 的标签选择器 
 template <Object> -required- 		#定义 Pod 的模板,基于这个模板定义的所有 pod 是一样的 
 updateStrategy <Object> 			#daemonset 的升级策略 
 
#查看 DaemonSet 的 spec.template 字段如何定义
#对于 template 而言,其内部定义的就是 pod,pod 模板是一个独立的对象 
[root@k8smaster ~]# kubectl explain ds.spec.template 
kubectl explain ds.spec.template KIND:    DaemonSet
VERSION:  apps/v1

RESOURCE: template <Object>

FIELDS: 
 metadata <Object> 
 spec<Object>

DaemonSet ユース ケース: ログ収集コンポーネント fluentd のデプロイ

#master node1 node2 都下载 fluentd
#编写一个 DaemonSet 资源清单 daemonset 也是通过标签选择器来选择模板创建pod
[root@k8smaster ~]# mkdir ds
[root@k8smaster ~]# cd ds
[root@k8smaster ~]# kubectl explain ds.spec.template.spec.tolerations
#找到容忍度 Taints 因为没有定义value 下面定义容忍度也不用写值,写一个排斥等级就行了
[root@k8smaster ds]# vim daemonset.yaml 
apiVersion: apps/v1
kind: DaemonSet
metadata: 
  labels:
    k8s-app: fluentd-logging
  name: fluentd-elasticsearch
  namespace: kube-system
spec:
  selector:
    matchLabels:
     name: fluentd-elasticsearch
  template:
    metadata:
     name: fluentd
     labels:
       name: fluentd-elasticsearch
    spec:
     tolerations:
     - key: node-role.kubernetes.io/master
       effect: NoSchedule
     containers:
     - name:  fluentd-elasticsearch
       image: fluentd
       resources:
         limits: 
           memory: 500Mi
         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
[root@k8smaster ds]# kubectl apply -f daemonset.yaml 
daemonset.apps/fluentd-elasticsearch created
[root@k8smaster ds]# kubectl get ds -n kube-system 
NAME                    DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
fluentd-elasticsearch   3         3         3       3            0           <none>                   9s
kube-flannel-ds         3         3         3       3            3           <none>                   18d
kube-proxy              3         3         3       3            3           kubernetes.io/os=linux   18d
[root@k8smaster ds]# kubectl get pods -n kube-system -o wide -l name=fluentd-elasticsearch
NAME                          READY   STATUS    RESTARTS   AGE   IP           NODE        NOMINATED NODE  
fluentd-elasticsearch-n5jj7   1/1     Running   0          59s   10.244.1.4   k8snode2    <none>           
fluentd-elasticsearch-qxgmc   1/1     Running   0          59s   10.244.0.2   k8smaster   <none>           
fluentd-elasticsearch-rhhv8   1/1     Running   0          59s   10.244.2.6   k8snode     <none>           
#通过上面可以看到在 k8s 的三个节点均创建了 fluentd 这个 pod 
#pod 的名字是由控制器的名字-随机数组成的 

#资源清单详细说明 
apiVersion: apps/v1 			#DaemonSet 使用的 api 版本 
kind: DaemonSet 				# 资源类型 
metadata: 
name: fluentd-elasticsearch 	#资源的名字 
namespace: kube-system 			#资源所在的名称空间 
labels: 
k8s-app: fluentd-logging 		#资源具有的标签 
spec: 
selector: 						#标签选择器 
matchLabels: 
name: fluentd-elasticsearch 
template: 
metadata: 
labels: 						#基于这回模板定义的 pod 具有的标签 
name: fluentd-elasticsearch 
spec: 
tolerations: 	#定义容忍度 
- key: node-role.kubernetes.io/master 
effect: NoSchedule 				#只会影响新调度的pod
containers: 	#定义容器 
- name: fluentd-elasticsearch 
image: xianchao/fluentd:v2.5.1 
resources: 		#资源配额 
limits: 		#最大资源
memory: 200Mi 
requests: 		#最小资源
cpu: 100m 
memory: 200Mi 
volumeMounts: 
- name: varlog 
mountPath: /var/log 			#把本地/var/log 目录挂载到容器 
- name: varlibdockercontainers 
mountPath: /var/lib/docker/containers 		#把/var/lib/docker/containers/挂载到容器里 
readOnly: true 					#挂载目录是只读权限 
terminationGracePeriodSeconds: 30 #优雅的关闭服务 
volumes: 
- name: varlog 
hostPath: 
path: /var/log 					#基于本地目录创建一个卷 会去采集这个目录下的日志
- name: varlibdockercontainers 
hostPath: 
path: /var/lib/docker/containers #基于本地目录创建一个卷 同上

Daemonset は Pod を管理します: ローリング更新

#DaemonSet 实现 pod 的滚动更新 
#查看 daemonset 的滚动更新策略 
[root@k8smaster ds]# kubectl explain ds.spec.updateStrategy 
KIND:     DaemonSet
VERSION:  apps/v1

RESOURCE: updateStrategy <Object>

DESCRIPTION:
     An update strategy to replace existing DaemonSet pods with new pods.

     DaemonSetUpdateStrategy is a struct used to control the update strategy for
     a DaemonSet.
     
FIELDS:
   rollingUpdate	<Object>
     Rolling update config params. Present only if type = "RollingUpdate".

   type	<string>
     Type of daemon set update. Can be "RollingUpdate" or "OnDelete". Default is
     RollingUpdate.

#查看 rollingUpdate 支持的更新策略 
[root@k8smaster ds]# kubectl explain ds.spec.updateStrategy.rollingUpdate 
KIND:     DaemonSet
VERSION:  apps/v1

RESOURCE: rollingUpdate <Object>

DESCRIPTION:
     Rolling update config params. Present only if type = "RollingUpdate".

     Spec to control the desired behavior of daemon set rolling update.

FIELDS:
   maxUnavailable	<string>
#上面表示 rollingUpdate 更新策略只支持 maxUnavailabe,先删除在更新;因为我们不支持一个节点运行两个 pod,因此需要先删除一个,在更新一个。 

#更新镜像版本,可以按照如下方法: 
[root@k8smaster ds]# kubectl set image daemonsets fluentd-elasticsearch fluentd-elasticsearch=nginx -n kube-system
daemonset.apps/fluentd-elasticsearch image updated
#这个镜像启动 pod 会有问题,主要是演示 daemonset 如何在命令行更新 pod 平时不用 都是改yaml
[root@k8smaster ds]# kubectl set image daemonsets fluentd-elasticsearch fluentd-elasticsearch=fluentd -n kube-system
daemonset.apps/fluentd-elasticsearch image updated
#改回来
[root@k8smaster ds]# kubectl get pods -n kube-system
NAME                                READY   STATUS    RESTARTS   AGE
fluentd-elasticsearch-874qw         1/1     Running   0          23s
fluentd-elasticsearch-lmvjq         1/1     Running   0          26s
fluentd-elasticsearch-qxgmc         1/1     Running   0          8m29s

最後に書く

作成するのは簡単ではありません。コンテンツが役立つと思われる場合は、3 つのリンクをフォローしてサポートしてください。間違いがあればコメントで指摘していただければ修正します!
現在更新中のシリーズ:ゼロからk8sを学ぶ ご覧
いただきありがとうございます 記事には個人的な理解が混在しています 誤りがある場合は、私に連絡して指摘してください〜

おすすめ

転載: blog.csdn.net/qq_45400861/article/details/127104998