Pod containers share Volume

Keywords: Linux Tomcat Apache

Project background

In the following example, Pod contains two containers: tomcat and busybox. Volume "app-logs" is set at the Pod level for tomcat to write log files to and busybox to read log files.

# cat pod-volume-applogs.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: volume-pod
spec:
  containers:
  - name: tomcat
   image: tomcat
   ports:
   - containerPort: 8080
   volumeMounts:
   - name: app-logs
     mountPath: /usr/local/tomcat/logs
  - name: busybox
   image: busybox
   command: ["sh","-c","tail -f /logs/catalina*.log"]
   volumeMounts:
   - name: app-logs
     mountPath: /logs
  volumes:
  - name: app-logs
   emptyDir: {}

The Volume set up here is called app-logs, of type emptyDir (or other types), mounted to the / usr/local/tomcat/logs directory in the tomcat container, and mounted to the / logs directory in the logreader container. The tomcat container writes files to the / usr/log/tomcat/logs directory after booting, and the logreader container can read the files.

The output of the logreader container can be viewed through the kubectl logs command:

# find / -name app-logs
[root@master other]# kubectl logs volume-pod -c busybox
26-Jul-2019 18:06:22.126 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.w
ebresources26-Jul-2019 18:06:22.126 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UM
ASK=002726-Jul-2019 18:06:22.144 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
26-Jul-2019 18:06:22.144 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
26-Jul-2019 18:06:22.144 INFO [main] 
......

Log in to the tomcat container for viewing:

# kubectl exec -it volume-pod -c tomcat -- ls /usr/local/tomcat/logs
catalina.2019-07-26.log     localhost_access_log.2019-07-26.txt
host-manager.2019-07-26.log  manager.2019-07-26.log
localhost.2019-07-26.log

# kubectl exec -it volume-pod -c tomcat -- tail /usr/local/tomcat/logs/catalina.2019-07-26.log
26-Jul-2019 18:06:29.935 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tom
cat/webapps/docs] has finished in [388] ms26-Jul-2019 18:06:29.936 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/
webapps/examples]26-Jul-2019 18:06:32.777 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat

Posted by kshyju on Sun, 13 Oct 2019 10:33:33 -0700