文章目录
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