Daemonset不支持使用kubectl create获取YAML模板,所以只能照葫芦画瓢了,参考Deployment的YAML编写,其实Daemonset和Deployment的差异很小,除了Kind不一样,还需要去掉replica配置。

DaemonSet示例说明:

1、创建YAML文件

$ vim  ds-demo.yaml

apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels:
    app: ds-demo
  name: ds-demo
  namespace: aming
spec:
  selector:
    matchLabels:
      app: ds-demo
  template:
    metadata:
      labels:
        app: ds-demo
    spec:
      containers:
        - name: ds-demo
          image: nginx:1.21.6
          ports:
          - name: mysql-port
            containerPort: 80

2、使用YAML创建ds

$ k apply -f ds-demo.yaml

3、查看

$ k get ds,po -n aming

NAME                     DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
daemonset.apps/ds-demo   5         5         5       5            5           <none>          4s

NAME                READY   STATUS    RESTARTS   AGE
pod/ds-demo-5x76n   1/1     Running   0          4s
pod/ds-demo-77dv5   1/1     Running   0          4s
pod/ds-demo-ccc5b   1/1     Running   0          4s
pod/ds-demo-k2lh8   1/1     Running   0          4s
pod/ds-demo-vpxcp   1/1     Running   0          4s

如果只在node节点上启动了pod,没有在master上启动,这是因为默认master有限制。

$ k describe node k8s01 |grep -i 'taint'
Taints:             node-role.kubernetes.io/control-plane:NoSchedule

说明:Taint叫做污点,如果某一个节点上有污点,则不会被调度运行pod。 但是这个还得取决于Pod自己的一个属性:toleration(容忍),即这个Pod是否能够容忍目标节点是否有污点。

为了解决此问题,有两种方法进行解决:

方法一:在Pod上增加toleration属性

下面改一下YAML配置:

$ vim  ds-demo.yaml

apiVersion: apps/v1
kind: DaemonSet
metadata:
  labels:
    app: ds-demo
  name: ds-demo
  namespace: aming
spec:
  selector:
    matchLabels:
      app: ds-demo
  template:
    metadata:
      labels:
        app: ds-demo
    spec:
      tolerations:
        - key: node-role.kubernetes.io/control-plane
          effect: NoSchedule
      containers:
        - name: ds-demo
          image: nginx:1.21.6
          ports:
          - name: mysql-port
            containerPort: 80

再次应用此YAML,此时所有ds都会重启

$ k apply -f ds-demo.yaml

方法二:去除Master节点上的污点

k label node k8s01 node-role.kubernetes.io/control-plane-