I have a Pod with logrotate and nginx containers:
apiVersion: v1
kind: Pod
metadata:
name: nginx-log
labels:
app: nginx-log
spec:
containers:
- name: logrotate
image: path/to/logrotate:latest
volumeMounts:
- name: logs
mountPath: /var/log
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: logs
mountPath: /var/log/nginx
volumes:
- name: logs
emptyDir: {}
The problem I have is whether mountPath for nginx is /var/log or /var/log/nginx, logrotate container always sees nginx logs in /var/log/ not in /var/log/nginx/. Is it possible to force nginx to create nginx/ folder in emptyDir and log there?
Ok, I have it working:
apiVersion: v1
kind: Pod
metadata:
name: nginx-log
labels:
app: nginx-log
spec:
containers:
- name: logrotate
image: path/to/image/logrotate:latest
volumeMounts:
- name: logs
mountPath: /var/log
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: logs
mountPath: /var/log/nginx
subPath: nginx
volumes:
- name: logs
emptyDir: {}
Logrotate side car is taken from: https://github.com/honestbee
with small modifications in logrotate.tpl.conf:
${LOGROTATE_PATTERN1}
${LOGROTATE_PATTERN2}
{
rotate ${LOGROTATE_ROTATE}
${LOGROTATE_MODE}
size ${LOGROTATE_SIZE}
missingok
nocompress
su root root
}
and in Dockerfile:
FROM alpine:3.8
ENV CRON_SCHEDULE='*/30 * * * *' \
LOGROTATE_SIZE='10M' \
LOGROTATE_MODE='copytruncate' \
LOGROTATE_PATTERN1='/var/log/*.log' \
LOGROTATE_PATTERN2='/var/log/*/*.log' \
LOGROTATE_ROTATE='5'
RUN apk --no-cache add logrotate tini gettext libintl \
&& mkdir -p /var/log \
&& chmod 775 /var/log \
&& mkdir -p /etc/logrotate.d
COPY logrotate.tpl.conf /logrotate.tpl.conf
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
CMD ["/usr/sbin/crond", "-f", "-L", "/dev/stdout"]
I think a good use here would be Init Containers, it can be used to run tasks before Pod
fully deploys.
An example would look as follows:
apiVersion: v1
kind: Pod
metadata:
name: nginx-log
spec:
containers:
- name: logrotate
image: path/to/logrotate:latest
volumeMounts:
- name: logs
mountPath: /var/log
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: logs
mountPath: /var/log/nginx
initContainers:
- name: logs
image: busybox
command: ["mkdir","-p","/var/log/nginx"]
volumeMounts:
- name: logs
mountPath: /var/log/nginx
volumes:
- name: logs
emptyDir: {}
You can also read Using InitContainers to pre-populate Volume data in Kubernetes