how to link tomcat with mysql db container in kubernetes

5/8/2019

My tomcat and mysql containers are not connecting.so how can I link them so that my war file can run succesfully.

I built my tomcat image using docker file

FROM picoded/tomcat7
COPY data-core-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/data-core-0.0.1-SNAPSHOT.war

mysql.yaml

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
        imagePullPolicy: "IfNotPresent"
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: root
        - name: MYSQL_DATABASE
          value: data-core  
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /docker-entrypoint-initdb.d
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-initdb-pv-claim

mysqlpersistantvolume.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: "/home/vignesh/stackoverflow/tmp/data"  //this is the path were my 
                                                    sql init script is placed.
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql-initdb-pv-claim
  labels:
    app: mysql
spec:
  storageClassName: manual
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 1Mi

tomcat.yaml

apiVersion: v1
kind: Service
metadata:
  name: tomcat
  labels:
    app: tomcat
spec:
  type: NodePort     
  ports:
  - name: http
    port: 8080
    targetPort: 8080

  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: suji165475/vignesh:tomcatserver
        name: tomcat
        env:
        - name: DB_PORT_3306_TCP_ADDR
          value: mysql                  #service name of mysql


        - name: DB_ENV_MYSQL_DATABASE
          value: data-core
        - name: DB_ENV_MYSQL_ROOT_PASSWORD
          value: root
        ports:
        - containerPort: 8080
          name: http


        volumeMounts:
        - name: tomcat-persistent-storage
          mountPath: /var/data
      volumes:
      - name: tomcat-persistent-storage
        persistentVolumeClaim:
          claimName: tomcat-pv-claim

tomcatpersistantvolume.yaml

kind: PersistentVolume
apiVersion: v1
metadata:
  name: tomcat-pv
  labels:
    type: local
    app: mysql
spec:
  storageClassName: manual
  capacity:
    storage: 1Mi
  accessModes:
    - ReadOnlyMany
  hostPath:
    path: "/app"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: tomcat-pv-claim
  labels:
    app: mysql
spec:
  storageClassName: manual
  accessModes:
    - ReadOnlyMany
  resources:
    requests:
      storage: 1Mi

currently using type:Nodeport for tomcat service. Do I have to use Nodeport for mysql also?? If so then should i give the same nodeport or different??

Note: Iam running all of this on a server using putty terminal

-- Swetha Swaminathan
docker
kubernetes

1 Answer

11/15/2019

When kubetnetes start service, it adds env variables for host, port etc. Try using environment variable MYSQL_SERVICE_HOST

-- Santosh Sawant
Source: StackOverflow