Kubernetes for Java Developers - Deploy Application
Tags: java, docker, kubernetes, containers
Published: 2019-07-07 13:33:41

---
title: Kubernetes for Java Developers - Deploy Application
published: true
description: Kubernetes for Java Developers - Deploy Application
tags: java, docker, kubernetes, containers
series: Java Kubernetes
cover_image: https://thepracticaldev.s3.amazonaws.com/i/efa0toi8knmzcbvbiuj2.png
---
##Hello Java Dev

This is the third part of the series Kubernetes for Java Developers. Before continue, check the [first](https://dev.to/sandrogiacom/kubernetes-for-java-developers-setup-41nk) and [second](https://dev.to/sandrogiacom/kubernetes-for-java-developers-creating-a-docker-image-1g36) part.

In this tutorial, we learn about to building and run application inside a kubernetes cluster.

At this point, we have a **Java application** running in **docker** that uses a **mysql** database running on docker too.

##Prepare the environment for the big moment

Check if you have the necessary tools before continue.

Just check:

`make --version`

`minikube version`

`kubectl version`

`vboxmanage --version`

If you don't have one of the tools above, please go to part one, follow the instructions and return here.

Or just type:

`make check`

The command above needs to be executed in the root of the project. Please clone it if you don't have it yet:

https://github.com/sandrogiacom/java-kubernetes


##Prepare demo application

Go to java-kubernetes directory and type `make` to see all `make` options.

###Create minikube machine

`make k-setup` starts **minikube**, enable **ingress** and create **namespace** "dev-to".

##Create kubernetes resources

Check kubernetes folder with subdirectories **app** and **mysql**.

![](https://thepracticaldev.s3.amazonaws.com/i/gik8s3f53uotkamw9hna.png)

**app-configmap.yaml**
* Application configurations with environment variables.

```yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: myapp
  namespace: dev-to
data:
  DATABASE_SERVER_NAME: "mysql"
  DATABASE_USER_NAME: "myapp"
  DATABASE_USER_PASSWORD: "myapp_pwd"
```

**app-deployment.yaml**
* Deployment creates one replicated pod. For more information please check [documentation](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/).

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  namespace: dev-to
  labels:
    app: myapp
spec:
  selector:
    matchLabels:
      app: myapp
  template:
      metadata:
        labels:
          app: myapp
      spec:
        containers:
          - name: myapp
            image: java-k8s:latest
            imagePullPolicy: Never
            ports:
              - containerPort: 8080
                name: http
            envFrom:
              - configMapRef:
                  name: myapp
```

The important part here is the **template** that defines the name of docker image, http port and source of environment variables.
 
**app-service.yaml**
* An abstract way to expose an application running on a set of Pods as a network service.

```yaml
kind: Service
apiVersion: v1
metadata:
  name: myapp
  namespace: dev-to
spec:
  selector:
    app: myapp
  type: NodePort
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
``` 

In **mysql** folder, we have only deployment and service files, because environment variables are in the same file.


###Deploy database

`make k-deploy-db` create mysql **deployment** and **service**.

Check if the service is running:

`kubectl get pods -n dev-to`

Access database in localhost:

`kubectl port-forward -n dev-to <pod_name> 3306:3306`

###Build application and docker image

`make k-build-app` **build app and create docker image inside minikube machine**.

`make k-deploy-app` **deploy app on cluster**.

Check if services is running:

`kubectl get pods -n dev-to`

## Check app url

Minikube gives you the host and port to which you will access.

Type:
`minikube -p dev.to service -n dev-to myapp --url`

Address below will be different for you!

`http://192.168.99.133:30343`

`curl -X GET http://192.168.99.133:30343/persons`

To open app directly in web browser:
`minikube -p dev.to service -n dev-to myapp`

##Conclusion

In this part, we learned about how to build and deploy application inside a local kubernetes cluster.

In the next part, we will see how to debug our application and some good practices for microservices.

See you soon!

##Bonus

### Create all in one step

To create world, just copy and paste command below on terminal. Make sure that you are in the root of the project.

`make k-all`


[![](https://thepracticaldev.s3.amazonaws.com/i/gmrz82bjwhej1f1iqb1e.png)](https://twitter.com/sandrogiacom)