how to deploy 2 docker container that has to be linked together using yaml on kubernetes?

4/11/2019

I have 2 containers (tomcat server and mysql db).These containers have to be linked and deployed using yaml files over kubernetes using kubectl -f apply command.The problem is I dont know how the yaml file for deployment should look like when I have to link the 2 containers for deployment.

the dockerfile i have used to build the tomcat images is

FROM tomcat

COPY app.war /usr/local/tomcat/webapps/

I tried using the --link attribute of the docker run command but I want to do this using kubernetes ie using yaml files.so kindly tell what changes I have to make for my deployment.yaml and service.yaml files in order to link the containers(tomcat and mysql) and deploy them on the root node master.

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-pod
spec:
  selector:
    matchLabels:
      run: tomcat-pod
  replicas: 1
  template:
    metadata:
      labels:
        run: tomcat-pod
    spec:
      containers:
      - name: tomcat
        image: tomcat:warfile
        ports:
        - containerPort: 8080

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: tomcat-pod
  labels:
    run: tomcat-pod
spec:
  type: NodePort
  ports:
  - port: 8080
    targetPort: 8080
  selector:
    run: tomcat-pod

the tomcat container which has the war file should be able to interact with the mysql container and fetch the data from database that has to be displayed after deployment but currently I am able to only see the tomcat homepage and not the output of running the war file presnt in the webapps folder.

-- Vignesh Swaminathan
kubernetes

1 Answer

4/12/2019

If you mean to communicate two apps with link you have to create Kubernetes Service object for those apps.

For example i have simpleservice in my cluster:

argela@etcd1:~$ kubectl get svc
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes      ClusterIP   10.96.0.1       <none>        443/TCP   5h23m
simpleservice   ClusterIP   10.96.219.103   <none>        80/TCP    179m

To reach that app from a pod i have to use (assume it is a http service) http://simpleservice/

This tutorial explains the service concept with examples.

-- Yavuz Sert
Source: StackOverflow