Kubernetes ConfigMap File Volume Mount

app.xml would be copied as path#app.xml to /usr/local/tomcat/conf/Catalina/localhost of target pod

app.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="${catalina.base}/webapps/app" debug="0" reloadable="false" useHttpOnly="true"
    clearReferencesHttpClientKeepAliveThread="true"
    clearReferencesStopThreads="true" clearReferencesStopTimerThreads="true">
  
 
  
</Context>

lets create configmap from above app.xml

D:\practices\kubernetes\demo>kubectl create configmap app-config --from-file=app.xml
configmap "app-config" created

app.yml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: tomcat
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      run: tomcat
  template:
    metadata:
      labels:
        run: tomcat
    spec:
      containers:
      - image: tomcat:8.5
        name: tomcat
        volumeMounts:
        - name: my-config
          mountPath: /usr/local/tomcat/conf/Catalina/localhost/path#app.xml
          subPath: app.xml
      volumes:
      - name: my-config
        configMap:
         name: app-config

Lets apply the above deployment

D:\practices\kubernetes\demo>kubectl apply -f app.yml
deployment.extensions "tomcat" created

pods created

D:\practices\kubernetes\demo>kubectl get pods
NAME                         READY     STATUS    RESTARTS   AGE
tomcat-65886c9fd5-l2zjq      1/1       Running   0          32s

lets see if the file is copied correctly

D:\practices\kubernetes\demo>kubectl exec -it tomcat-65886c9fd5-l2zjq bash
root@tomcat-65886c9fd5-l2zjq:/usr/local/tomcat# cd conf/Catalina/localhost/
root@tomcat-65886c9fd5-l2zjq:/usr/local/tomcat/conf/Catalina/localhost# ls
path#app.xml
root@tomcat-65886c9fd5-l2zjq:/usr/local/tomcat/conf/Catalina/localhost# cat path#app.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="${catalina.base}/webapps/app" debug="0" reloadable="false" useHttpOnly="true"
    clearReferencesHttpClientKeepAliveThread="true"
    clearReferencesStopThreads="true" clearReferencesStopTimerThreads="true">
 
 
 
</Context>
root@tomcat-65886c9fd5-l2zjq:/usr/local/tomcat/conf/Catalina/localhost# exit

References

Tomcat Context File And Environment Variables

We should be able to read environment variables in Tomcat context.xml files, for example

<Context>
    <Environment name="app" type="java.lang.String" value="${app.name}" />
    :
    :
</Context>

However it does not work out of the box, to make it possible you have to do the following. Create setenv.sh and add the following content

#!/usr/bin/env bash

export APP_NAME=DEVApp

export JAVA_OPTS="$JAVA_OPTS -Dhost.name=$HOSTNAME -Dapp.name=$APP_NAME"

Keep the file under $CATALINA_HOME/bin

Example

Fork the project and execute the following command.

mvn clean package docker:build docker:run

Mocking Web/Rest Services Made Easy

Lets create webserver.sh

#!/usr/bin/env bash
RESPONSE="HTTP/1.1 ${3:-200} ${4:-OK}\r\nConnection: keep-alive\r\n\r\n${2:-"OK"}\r\n"
while { echo -en "$RESPONSE"; } | nc -l "${1:-8080}"; do
  echo "================================================"
done

webserver.sh has got 4 paramaters

sh webserver.sh port response responseCode responseMessage
 
 
# Examples
sh webserver.sh 8087
sh webserver.sh 8087 "Error" 500 "Internal Server Error"
sh webserver.sh 8087 "You are not Authorized" 403 "Un Authorized"

Port

Any number defaults to 8080

Response

Any String default to OK

Response Code

Any Number default to 200

Response Message

Any String default to OK

Example #1 403

Lets start the server

[admin@host ~]$ sh webserver.sh 8087 "You are not Authorized" 403 "Un Authorized"

Example #2 500

Lets start the server

[admin@host ~]sh webserver.sh 8087 "Error" 500 "Internal Server Error"

Possibilities

You can run multiple webservers on different ports on same machine.

Post data using CURL

[admin@clienthost ~]$ curl -H "Content-type:application/json" -X POST -u C2214CAB-41B4-42BA-983F-617E427B4220: "mockserver:8087" -d '{"Id":"123","orgCode":"ABC","jobId":"321","applicationStatus":"cancel"}'
OK
[admin@clienthost ~]$

Post data using WGET

[admin@clienthost ~]$ wget --user-agent=Mozilla/5.0  --post-data '{"Id":"123","orgCode":"ABC","jobId":"321","applicationStatus":"cancel"}' --no-check-certificate mockhost:8087 --header="Content-Type: application/json"

Tools

Following tools would be useful, if the services are not available at the time it is required. Just mock the service and go ahead.