php-fpm pod日志如何清理?

背景:我在阿里云的k8s集群上有一个pod,这个pod有2个容器,它们共享emptyDir卷,php-fpm容器会向emptyDir卷打印很多份业务日志,logtail容器会将日志收集到阿里云的sls日志服务里。

问题:如果pod状态正常那emptyDir卷里的日志会一直增长,我现在想要实现自动清理emptyDir卷里的日志,有什么好的方法吗?

想过的处理方法:
1.php程序自身滚动日志-----和开发人员沟通过,没有办法,他们一般是配合logrotate工具进行日志滚动清理。
2.在pod里集成crontab和logrotate ---- 感觉可行,但觉得crontab这个不好配置。

  1. 将日志目录挂载到宿主机上,在配合logrotate或其他工具处理 ---- 感觉麻烦了
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-fpm
  labels: 
    app: php-fpm
spec:
  replicas: 1
  selector:
    matchLabels:
      app: php-fpm
  template:
    metadata:
      labels:
        app: php-fpm
    spec:  
      containers:
      - name: php-fpm
        image: php-demo:testing-2023
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9000
          protocol: TCP
          name: fastcgi
        volumeMounts:
        - name: logs
          mountPath: /data/logs
      - name: logtail
        image: registry.cn-hangzhou.aliyuncs.com/log-service/logtail:latest
        command:
        - sh
        - -c
        - /usr/local/ilogtail/run_logtail.sh 15
        - 
        volumeMounts:
        - name: logs
          mountPath: /data/logs

      volumes:
      - name: logs
        emptyDir: {}
阅读 1.5k
1 个回答

你可以安装一下 logrotate 和 cron 工具,然后配置 Cron 然后让它定期清理日志就好了:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-fpm
  labels: 
    app: php-fpm
spec:
  replicas: 1
  selector:
    matchLabels:
      app: php-fpm
  template:
    metadata:
      labels:
        app: php-fpm
    spec:
      initContainers:
      - name: logrotate-setup
        image: alpine
        command:
        - sh
        - -c
        - |
          apk add logrotate cron
          echo '*/5 * * * * /usr/sbin/logrotate /etc/logrotate.conf' > /etc/crontabs/root
          echo '/data/logs/*.log {
                rotate 5
                size 10M
                copytruncate
                compress
                delaycompress
                missingok
          }' > /etc/logrotate.conf
          crond -f
        volumeMounts:
        - name: logs
          mountPath: /data/logs

      containers:
      - name: php-fpm
        image: php-demo:testing-2023
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9000
          protocol: TCP
          name: fastcgi
        volumeMounts:
        - name: logs
          mountPath: /data/logs
      - name: logtail
        image: registry.cn-hangzhou.aliyuncs.com/log-service/logtail:latest
        command:
        - sh
        - -c
        - /usr/local/ilogtail/run_logtail.sh 15
        volumeMounts:
        - name: logs
          mountPath: /data/logs

      volumes:
      - name: logs
        emptyDir: {}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进