how to link and run mysql database and tomcat server on kubernetes clusture?

5/8/2019

How to link my tomcat container having war file in webapps folder to another container having mysql database.currently if i start my war file in tomcat manager app i get error saying could not start application at contextpath /data-core-0.0.1-SNAPSHOT catalina lifecycle exception.

I think my tomcat container cant access the mysql database container!!

statefulset.yaml

serviceName: mysql
 template:
  metadata:
   labels:
    name: mysql
  spec:
   containers:
    - name: mysql
      image: suji165475/vignesh:latest
      ports:
       - containerPort: 3306
      volumeMounts:
       - name: mysql-initdb
         mountPath: /docker-entrypoint-initdb.d
   volumes:
    - name: mysql-initdb
      persistentVolumeClaim:
       claimName: mysql-initdb-pv-claim

persistantvolumeclaim.yaml

kind: PersistentVolume
apiVersion: v1
metadata:
  name: mysql-initdb-pv-volume
  labels:
    type: local
    app: mysql
spec:
  storageClassName: manual
  capacity:
    storage: 1Mi
  accessModes:
    - ReadOnlyMany
  hostPath:
    path: "/tmp/data"

---

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-initdb-pv-claim
  labels:
    app: mysql
spec:
  storageClassName: manual
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 1Mi

tomcatdeployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment
  labels:
    app: tomcat
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat
  template:
    metadata:
      labels:
        app: tomcat
    spec:
      containers:
      - name: tomcat
        image: suji165475/vignesh:tomcatserver
        ports:
        - containerPort: 8080
-- Swetha Swaminathan
docker
docker-compose
kubernetes

1 Answer

5/8/2019

You have to crate service for mysql and consume that service name in tomcat service so tomcat pod will be able to talk with mysql server.

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: <value>
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pv-claim

Now deployment for tomcat :

apiVersion: v1
kind: Service
metadata:
  name: tomcat
  labels:
    app: tomcat
spec:
  ports:
  - name: http
    port: 80
    targetPort: 80
  - name: https
    port: 443
    targetPort: 443
  selector:
    app: tomcat
    tier: frontend
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat
  labels:
    app: tomcat
spec:
  selector:
    matchLabels:
      app: tomcat
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: tomcat
        tier: frontend
    spec:
      containers:
      - image: <image location>
        name: tomcat
        env:
        - name: DB_PORT_3306_TCP_ADDR
          value: mysql                  #service name of mysql
        - name: DB_ENV_MYSQL_PASSWORD
          value: <Mysql password>
        - name: DB_ENV_MYSQL_USER
          value: <mysql user>
        - name: DB_ENV_MYSQL_DATABASE
          value: <database name>
        - name: DB_ENV_MYSQL_PASSWORD
          value: <Mysql password>
        ports:
        - containerPort: 80
          name: http
        - containerPort: 443
          name: https
        volumeMounts:
        - name: tomcat-persistent-storage
          mountPath: /var/data
      volumes:
      - name: tomcat-persistent-storage
        persistentVolumeClaim:
          claimName: tomcat-pv-claim
-- Harsh Manvar
Source: StackOverflow