The Example in Detail
Let's look at an example of how Kubernetes can be used.
We'll cover the following
Setup #
The example is accessible at https://github.com/ewolff/microservice-kubernetes. https://github.com/ewolff/microservice-kubernetes/blob/master/HOW-TO-RUN.md explains in detail how to install the required software for running the example.
The following steps are necessary for running the example:
-
Minikube as minimal Kubernetes installation has to be installed. Instructions for this can be found at https://github.com/kubernetes/minikube#installation.
-
kubectl is a command line tool for handling Kubernetes and also has to be installed. Its installation is described at https://kubernetes.io/docs/tasks/tools/install-kubectl/.
-
The script
docker-build.sh
generates the Docker images for the microservices and uploads them into the public Docker hub. This step is optional since the images are already available on the Docker hub. It only has to be performed when changes were introduced to the code or to the configuration of the microservices. Before starting the script, the Java code has to be compiled with./mvnw clean package
(macOS, Linux) ormvnw.cmd clean package
(Windows) in directorymicroservice-kubernetes-demo
. Then the scriptdocker-build.sh
creates the images withdocker build
and withdocker tag
they receive a globally unique name anddocker push
uploads them into the Docker hub. Using the public Docker hubs spares the installation of a Docker repository and thereby facilitates the handling of the example. -
The script
kubernets-deploy.sh
deploys the images from the public Docker hub in the Kubernetes cluster and thereby generates the pods, the deployments, the replica sets, and the services. For this, the script uses the toolkubectl
.kubectl run
serves to start the image which is downloaded at the indicated URL in the Docker hub. In addition, it is defined which port the Docker container should provide. So,kubectl run
generates the deployment, which creates the replica set and thereby the pods.kubectl expose
generates the service which accesses the replica set and thus creates the IP address, node port resp. load balancer and DNS entry.
This excerpt from kubernetes-deploy.sh
shows the use of the tools
using the catalog microservice as an example.
#!/bin/sh
if [ -z "$DOCKER_ACCOUNT" ]; then
DOCKER_ACCOUNT=ewolff
fi;
...
kubectl run catalog \\
--image=docker.io/$DOCKER_ACCOUNT/microservice-kubernetes-demo-catalog:latest
\\
--port=80
kubectl expose deployment/catalog --type="LoadBalancer" --port 80
...
Get hands-on with 1400+ tech skills courses.