# DaemonSet

DaemonSet会在集群的每一个Node上创建唯一的一个Pod，通常用来运行一些需要执行系统级别、与基础结构相关的操作的Pod。例如，希望在每个Node上运行日志收集器和资源监控器。

在`kube-system`命名空间中，Kubnernetes有一个名为`kube-proxy`的DaemonSet，其创建并接管了一个名为`kube-proxy-xxxx`的Pod。

## 创建DaemonSet

默认情况下，DaemonSet会在集群中的所有Node上都部署Pod，但是可以配合Pod的`spec.nodeSelector`将Pod仅部署到具有特定标签的Node上。

{% hint style="info" %}
**Tips:** Node是可以被设置为不可调度的，用以防止Pod被部署到该Node上。但是DaemonSet管理的Pod可以**完全绕过调度器**，也就是说即使被定义为不可调度的Node，也是可以在上面部署Pod的。
{% endhint %}

下面展示了一个具体的DaemonSet的配置文件格式：

```yaml
apiVersion: apps/v1    # 注意DaemonSet资源所属的API组
kind: DaemonSet
metadata:
  name: node-exporter-daemonset
spec:    # DaemonSet的规格
  selector:    # （必须）匹配Pod的标签。可以使用matchLabels或matchExpressions
    matchLabels: 
      app: prometheus
  template:    # Pod的模板
    metadata:
      labels:    # 设定Pod的labels
        app: prometheus
    spec:
      nodeSelector:    # Pod的Node选择器，只在具有该标签的Node上部署Pod；若没有该选择器，则会在集群中的所有Node上部署
        gpu: "true"
      hostNetwork: true        # 设定网络模式，hostNetwork表示与主机相同的协议栈
      containers:
      - name: node-exporter
        image: prom/node-exporter
        imagePullPolicy: IfNotPresent    # 镜像拉取规则
        command:    # 镜像执行的命令
        - /bin/node_exporter
        - --path.procfs
        - /host/proc
        - --path.sysfs
        - /host/sys
        - --collector.filesystem.ignored-mount-points
        - ^/(sys|proc|dev|host|etc)($|/)
        volumeMounts:
        - name: proc
          mountPath: /host/proc
        - name: sys
          mountPath: /host/sys
        - name: root
          mountPath: /rootfs
      volumes:
      - name: proc
        hostPath:
          path: /proc
      - name: sys
        hostPath:
          path: /sys
      - name: root
        hostPath:
          path: /
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yangsijie151104.gitbook.io/k8s-note/kong-zhi-qi-controllers/controllers/daemonset.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
