Running a Service in a Kubernetes cluster using a Docker image

I've been tinkering with Docker Swarm for container orchestration since last year. A few months ago, I watched this video about Kubernetes auto-scaling features so I was curious about how Kubernetes is now compared to Docker. Been in several discussions and looks like it is Docker Swarm vs. Kubernetes nowadays.

There are a lot of similarities in the CLI. Kubernetes has even published "kubectl for Docker Users" which basically shows the counterpart of kubectl commands to docker commands.

https://kubernetes.io/docs/user-guide/docker-cli-to-kubectl/

Pre-requisites

  1. You need minikube to run a Kubernetes cluster in your local machine. https://kubernetes.io/docs/tasks/tools/install-minikube/
  2. You need a docker image to run. You can use my docker image of a sample Spring Boot application in Dockerhub. https://hub.docker.com/r/melvindave/spring-boot-example/.

Screenshot-2017-10-15-14.06.25

So here's how you run a docker image as a service in a Kubernetes cluster.

  1. Create a deployment
kubectl run mv-spring-boot-example --image=melvindave/spring-boot-example:latest --port=8080
  1. Check if the deployment has been created
kubectl get deployments
  1. Check if the container is running
kubectl get pods
  1. Create a service so the Pod is accessible from outside the Kubernetes virtual network.
kubectl expose deployment mv-spring-boot-example --type=LoadBalancer
  1. Check if the service has been created
kubectl get services
  1. Assuming, you've run a web application which can be accessible using a web browser, execute this command. This will automatically open up a browser using a local IP address.
minikube service mv-spring-boot-example

Screenshot-2017-10-15-14.22.21

  1. Check the logs of the service
kubectl logs <pod name>

Screenshot-2017-10-15-14.23.43

  1. Optionally, you can increase your service replicas to 3 for example.
kubectl scale deployments/mv-spring-boot-example --replicas=3
  1. Clean up - Delete the service
kubectl delete service mv-spring-boot-example
  1. Clean up - Delete the deployment
kubectl delete deployment mv-spring-boot-example

Here are all the commands I've used and the usual output.

Screenshot-2017-10-15-14.29.42

It was really straightforward for someone who is familiar with Docker CLI.

Here are some references I've used:
https://kubernetes.io/docs/tasks/tools/install-minikube/
https://kubernetes.io/docs/tutorials/stateless-application/hello-minikube/
https://kubernetes.io/docs/user-guide/docker-cli-to-kubectl/
https://hub.docker.com/r/melvindave/spring-boot-example/

Hope you learned something from the article, would love to connect in LinkedIn!
https://www.linkedin.com/in/melvinvivas/

Cheers and 'til the next article!