k8s-学习笔记18-用DaemonSet部署cron

创建一个dockerfile

FROM centos:7
RUN yum install crontabs -y
CMD [ "/usr/sbin/crond", "-n" ]

  

创建一个清理和压缩容器日志的脚本,配置为cm。

kind: ConfigMap
apiVersion: v1
metadata:
  name: crontab
  namespace: monitoring
data:
  k8s-cron: |
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root

    38 18 * * * root bash /root/logclean.sh
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: logclean
  namespace: monitoring
data:
  logclean.sh: >
    Date=`date -d "3 days ago" +"%Y-%m-%d"`

    find /var/lib/docker/overlay2/*/merged/opt/logs -name "*`date -d "3 days
    ago" +"%Y-%m-%d"`*log"|while read LogName

    do

    [ "X$LogName" == "X" ] && exit

    gzip $LogName

    done

    find /var/lib/docker/overlay2/*/merged/opt/logs -mtime +5 -type f |xargs rm
    -f
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: cronbox
  namespace: monitoring
  labels:
    k8s-app: cronbox
spec:
  selector:
    matchLabels:
      k8s-app: cronbox
  template:
    metadata:
      labels:
        k8s-app: cronbox
    spec:
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirstWithHostNet
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: eks.amazonaws.com/nodegroup
                    operator: NotIn
                    values:
                      - system
      containers:
      - name: cronbox
        image: cronbox:v1
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 100Mi
        volumeMounts:
        - name: logclean
          mountPath: /root
        - name: crontab
          mountPath: /etc/cron.d
        - name: docker
          mountPath: /var/lib/docker          
      volumes:
      - name: logclean
        configMap:
          defaultMode: 0750
          name: logclean
      - name: crontab
        configMap:
          name: crontab
      - name: docker
        hostPath:
          path: /var/lib/docker

  

posted @ 2021-07-14 16:05  jabbok  阅读(182)  评论(0编辑  收藏  举报