Java Kubernetes (4 Part Series)
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.
Check if you have the necessary tools before continue.
If you don't have one of the tools above, please go to part one, follow the instructions and return here.
Or just type:
The command above needs to be executed in the root of the project. Please clone it if you don't have it yet:
Go to java-kubernetes directory and type
make to see all
make k-setup starts minikube, enable ingress and create namespace "dev-to".
Check kubernetes folder with subdirectories app and mysql.
- Application configurations with environment variables.
apiVersion: v1 kind: ConfigMap metadata: name: myapp namespace: dev-to data: DATABASE_SERVER_NAME: "mysql" DATABASE_USER_NAME: "myapp" DATABASE_USER_PASSWORD: "myapp_pwd"
- Deployment creates one replicated pod. For more information please check documentation.
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.
- An abstract way to expose an application running on a set of Pods as a network service.
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.
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
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
Minikube gives you the host and port to which you will access.
minikube -p dev.to service -n dev-to myapp --url
Address below will be different for you!
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
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!
To create world, just copy and paste command below on terminal. Make sure that you are in the root of the project.