Minikube, kubectl and local development & deployment of apps in Kubernetes

  • A Kubernetes cluster is a set of physical or virtual machines and other infrastructure resources that are needed to run your containerized applications.
  • Each machine in a Kubernetes cluster is called a node and there are 2 types of node in the cluster
    • Master node(s): this node hosts the Kubernetes control plane and manages the worker nodes and the Pods in the cluster.
    • Worker node(s): These are the nodes which run your containerized applications
  • In production environments, the control plane usually runs across multiple computers and a cluster usually runs multiple nodes, providing fault-tolerance and high availability.
  • Control plane is the brain of Kubernetes cluster as it makes all the global decision about the cluster example scheduling or detects and responds to cluster events like spinning a new pod when a deployment’s replicas field is unsatisfied
  • If the control plane is the brains of Kubernetes, where all the decisions are made, then the data plane or the worker node is its body
  • Worker nodes on the data plane carries out commands from the control plane .
  • To learn kubernetes or do some POCs or local development it is not practical to have such a setup of nodes and clusters , therefore minikube is one of the option to have kubernetes locally.
  • It is a single node cluster and the easiest way of deploying kubernetes for learning and POC purpose.
  • In minikube the master and worker processes are on the same machine and supports most of the Kubernetes feature
  • Installation details of minikube are at this link.
  • Once the installation is done, you can interact with the kubernetes cluster using kubectl.
  • It is the command line tool for communicating with a Kubernetes cluster’s control plane, details of installing kubectl are at this link
  • Once minikube is installed, you can use following commands
    • To check the status- minikube status
    • To start – minikube start
    • To stop minikube stop
  • I will now deploy a spring boot application with 3 replicas to minikube. The spring boot application is a simple Rest controller which returns a Hello Everyone message.
  • The process / steps we will follow are depicted below
  • First step of creating a spring boot service is already done and the code can be found at this link
  • To dockerize the application, we have a Dockerfile with following contents
FROM java:8-jdk-alpine
COPY ./target/hello-0.0.1-SNAPSHOT.jar app.jar
CMD [ "sh", "-c", "java -jar /app.jar" ]
  • It says to use Java 8, copy the final jar hello-0.0.1SNAPSHOT.jar which is generated by mvn install in target folder as app.jar and run it by using java -jar command
  • To dockerize the app , run below command
    • docker build -t raje/hello-world .
  • To run the application in detached mode, you can run below command which will return an id
    • docker run -d -p 8080:8080 raje/hello-world
  • To test the application you can access : http://localhost:8080/
  • To stop the container, you can use docker stop <<id>> which you got after giving the docker run command
  • Once the app is containerized and tested, let us upload this to docker registry by giving below command
    • docker push raje/hello-world
  • This will upload the image to your docker but for the above command to work you should be logged in to your docker , if you are not you can run below command and enter your credentials
    • docker login
  • You can verify if the image is uploaded by visiting your docker hub repository
  • At this stage, we have dockerized our application and uploaded it to registry. Now we need to create a Kubernetes deployment and service yaml file.
  • Deployment yaml contains details of creating copies of your application called Pods, therefore it will have details about number of replicas, image name , port on which the app runs inside a container etc.
  • Service yaml contains details of an internal load balancer that routes the traffic to Pods. Therefore it will have details on port and connections
  • Deployment file for this project is at the root with the name k8s-deployment.yml
apiVersion: apps/v1
kind: Deployment # Kubernetes resource kind we are creating
metadata:
name: hello-world-svc
spec:
replicas: 3 #Number of replicas that will be created for this deployment
selector:
matchLabels:
app: hello-world-svc
template:
metadata:
labels:
app: hello-world-svc
spec:
containers:
- name: hello-world
image: raje/hello-world # Image that will be used to containers in the cluster
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080 # The port that the container is running on in the cluster
apiVersion: v1 # Kubernetes API version
kind: Service # Kubernetes resource kind we are creating
metadata:
name: hello-world-svc
labels:
name: hello-world-svc
spec:
ports:
- port: 8080 # The port that the service is running on in the cluster
targetPort: 8080 # The port exposed by the service
protocol: TCP
nodePort: 31000
selector:
app: hello-world-svc
type: NodePort # type of the service. LoadBalancer indicates that our service will be external.
  • Now that we have the Kubernetes deployment file, we can deploy it to the cluster. Execute the command below to deploy the application to the cluster.
    • kubectl apply -f k8s-deployment.yml
  • You can check the status of the deployment on minikube dashboard by giving below command
    • minikube dashboard
  • If you click on Pods , you will see 3 pods running as we had mentioned replicas:3 in our deployment yaml file
  • Now let us create a service for the application running in 3 pods, for us to access it. To do that run the below command
    • kubectl apply -f k8s-service.yml
  • To get the url of the service to access it run the below command
    • minikube service -- hello-world-svc
  • This was a simple example to demonstrate how minikube can be used to deploy a local Kubernetes cluster
  • Thank you and do share your comments/thoughts/feedback

7 thoughts on “Minikube, kubectl and local development & deployment of apps in Kubernetes

  1. Thanks for the great content, nicely explained.
    Also, wondering which tool do you use to create the graphics? It looks visually aesthetic.

Leave a Reply to Aveek DasCancel reply