How to run multiple containers within the same pod in kubernetes clusture?

5/6/2019

I have 2 docker containers. one containing tomcat server and the other containing MySQL database. I want to run those containers on a single pod since tomcat has to access the MySQL database.

What I have tried:

pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: tomcat-mysql-pod
  labels:
  app: tomcat-mysql-pod
spec:
containers:
- name: kubernetestomcatcontainer
  image: suji165475/vignesh:tomcatserver
  ports:
- containerPort: 8080
containers:
- name: kubernetesmysqlcontainer
  image: suji165475/vignesh:latest
  ports:
- containerPort: 3306

I ran this pod using

kubectl apply -f pod.yaml

then I exposed the pod using

kubectl expose pod tomcat-mysql-pod --type=NodePort --port=8080

Currently, I can only access the tomcat homepage using https://serveripaddress:nodeport but not my spring boot app using https://serveripaddress:nodeport/data-core-0.0.1-SNAPSHOT???

NOTE: The app works and ran perfectly when running the containers using docker-compose.yaml and then the docker-compose up command. But when I run this on kubernetes I get Catalina lifecycle exception in tomcat's manager app while starting my war file.

-- Vignesh Swaminathan
docker
docker-compose
kubernetes

2 Answers

5/6/2019

That containers: line in your YAML snippet is the name of a collection and items in it should be enumerated using dashes indented at the same column.

You should try something like:

apiVersion: v1
kind: Pod
metadata:
  name: tomcat-mysql-pod
  labels:
  app: tomcat-mysql-pod
spec:
  containers:
  - name: kubernetestomcatcontainer
    image: suji165475/vignesh:tomcatserver
    ports:
      containerPort: 8080
  - name: kubernetesmysqlcontainer
    image: suji165475/vignesh:latest
    ports:
      containerPort: 3306
-- Zsolt Katona
Source: StackOverflow

5/6/2019

It is not recommended to run both ui and database services in the same pod. It would enable hard coupling between ui and db.

Better to run ui and db in a separate pods. Ui can communicate mysql database using service dns. Consider running mysql In stateful set to maintain the application state

-- P Ekambaram
Source: StackOverflow