[kubernetes]-k8s定时清理文件并通过钉钉通知
导语:希望通过daemonset执行某个命令来清理磁盘。
·
导语:希望通过daemonset执行某个命令来清理磁盘。记录一下测试过程
Dockerfile
# 1
FROM centos:7
RUN yum -y install crontabs
USER root
WORKDIR /beta/logs/
RUN echo "Asia/Shanghai" > /etc/timezone
CMD ["/usr/sbin/crond","-n"] # -i 后台运行
docker build -t harbor.betawm.com/tools/clearlog:v7 -f 1 .
1.yaml
apiVersion: v1
data:
ClearLog.sh: |-
#!/bin/bash
webhook='https://oapi.dingtalk.com/robot/send?access_token=84031e85e408a7a8cf9e7dec957c43eb7b13907325ba51aff2e9643324591520'
Date=$(date "+%Y-%m-%d_%H%S%M")
Date1=$(date "+%Y-%m-%d_%H%M%S")
Dir='/tmp/'
ip=$HOST_IP #pod里边定义
du -sh /beta/logs/ | awk '{print $1}' > $Dir$Date
#删除三天以前的日志文件
find /beta/logs/ -name '*.log' -type f -mtime +3 -print -exec ls -l {} \;
#删除三天以前的空目录
find /beta/logs/ -maxdepth 2 -type d -empty -print -exec ls -l {} \;
Date1=$(date "+%Y-%m-%d_%H%M%S")
du -sh /beta/logs/ | awk '{print $1}' > $Dir$Date1
curl $webhook -H 'Content-Type: application/json' -d "
{
'msgtype': 'text',
'text': {
'content': '[比心][比心]生产环境k8s空间清理[比心][比心]
\n ++++++[对勾]${HOST_IP}清理前空间[对勾]+++++++
/beta/logs: `cat $Dir$Date`
\n++++++[对勾]${HOST_IP}清理后空间[对勾]+++++++
/beta/logs: `cat $Dir$Date1`\n'
},
'at': {
'isAtAll': true
}
}"
kind: ConfigMap
metadata:
name: clearlogconf
namespace: default
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
annotations:
deprecated.daemonset.template.generation: '13'
labels:
app: clearlog
name: clearlog
namespace: default
resourceVersion: '10612247'
spec:
minReadySeconds: 60
revisionHistoryLimit: 10
selector:
matchLabels:
app: clearlog
template:
metadata:
annotations:
kubectl.kubernetes.io/restartedAt: '2021-09-29T18:14:54+08:00'
creationTimestamp: null
labels:
app: clearlog
spec:
containers:
- env:
- name: HOST_IP #赋值给指定变量,在pod中调用此环境变量返回的就是当前物理机的IP地址。
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: status.hostIP #获取当前节点的物理地址
image: 'harbor.betawm.com/tools/clearlog:v7'
imagePullPolicy: IfNotPresent
lifecycle:
postStart:
exec:
command:
- /bin/bash
- '-c'
- ' echo "* * * * * /bin/bash /tmp/ClearLog.sh >> /tmp/tmp.txt" >> conf && crontab conf && rm -f conf '
name: clear-client
resources:
limits:
cpu: 500m
memory: 2Gi
requests:
cpu: 200m
memory: 500Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /beta/logs
name: hostlog
- mountPath: /tmp/ClearLog.sh
name: clear-log-config
subPath: ClearLog.sh
dnsConfig: {}
dnsPolicy: ClusterFirst
hostNetwork: true
imagePullSecrets:
- name: betasecret
restartPolicy: Always
schedulerName: default-scheduler
securityContext:
seLinuxOptions: {}
terminationGracePeriodSeconds: 30
volumes:
- hostPath:
path: /beta/logs
type: Directory
name: hostlog
- configMap:
defaultMode: 420
name: clearlogconf
name: clear-log-config
updateStrategy:
rollingUpdate:
maxUnavailable: 1
type: RollingUpdate
https://www.cnblogs.com/nf01/articles/15353573.html
Dockerfile
# 2
FROM docker:dind
COPY cleaner.sh /bin/
RUN chmod +x /bin/cleaner.sh
#CMD ["bash", "/bin/cleaner.sh"]
docker build . -t cleaner:v1 -f 2
docker-cleanup.yaml
# 定时任务的执行时间调整一下
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: docker-cleanup
spec:
jobTemplate:
spec:
completions: 1
template:
spec:
restartPolicy: Never
volumes:
- name: local-time
hostPath:
path: /etc/localtime
- hostPath:
path: /var/run/docker.sock
type: Socket
name: dockersocks
containers:
- name: mysqlcleanup-container
image: cleaner:v1
volumeMounts:
- name: local-time
mountPath: /etc/localtime
- mountPath: /var/run/docker.sock
name: dockersocks
command: ["sh","-c","/bin/cleaner.sh"]
schedule: "*/5 * * * *"
https://openkruise.io/zh/docs/best-practices/acronjob+broadcastjob/
更多推荐
已为社区贡献84条内容
所有评论(0)