Introduction
This is a Continuation of this article related to Docker. Kubernetes
is a Container Orchestration Tool. It is used when our Application is
distributed in Multiple Containers. Kubernetes Job is to Monitor, Scale,
Restart Containers automatically even if containers are spread across multiple
nodes.
This Article targets
the below areas in explaining how to use Kubernetes to Deploy the docker image
with simple Steps. To create Docker image, refer this link, - What is Kubernetes
- Features of Kubernetes
- Kubernetes Functions
- How Kubernetes Works
- Kubernetes (Service & POD Interaction)
- Ingress (High Level)
- To Set up Kubernetes Dashboard
- To Set up the Metric Server
- Kubernetes Commands
Prerequisites
1.
Should have knowledge in developing Service API
using .Net Core (or) .Net Framework. I have used .Net core to create the
service. 2.
Should have knowledge in Docker and its
Commands. 3.
Docker and Kubernetes should be Installed and
Running on Your Machine.
Before learning Kubernetes, checkout the below topics for better
understanding on the need of Docker and Kubernetes.
1.
Limitations of Monolithic Application, refer here. 2.
Why Micro Service and its Limitation, refer here. 3.
Why Docker and Kubernetes is needed, refer here.
Below are few challenges in using Docker Deployment, - Monitoring - Each Service’s Monitoring is difficult. Monitoring is needed to ensure there is 100% availability of Service.
- Scaling - With Docker Deployment we can’t scale the deployment automatically. Auto Scaling is needed if the container (i.e Service) is getting more load on it.
- Manual Intervention - If Kubernetes is not present, we need to manage it manually and it is difficult & challenging Process.
- Managing Containers - Managing Container on multiple Servers become difficult.
1. What is Kubernetes
Kubernetes is a Container Orchestration Tool. It is used when our Application is distributed in Multiple Containers. Kubernetes Job is to Monitor, Scale, Restart Containers automatically even if containers are spread across multiple nodes.
2. Features of Kubernetes
3. Kubernetes Functions
4. How Kubernetes Works
Kubernetes is a Cluster. Master have several nodes inside it.
K - Master - Schedule container on the nodes.
- Monitor the Nodes
- Monitor the Container Running inside the Nodes.
- It keeps the Track of the Logs of what operation is performed on each container.
K – Nodes (Worker Node) - Processing the Request
- Sending API Calls
Kubernetes will run both on the Master and the Node and interacting with each other as its one System. It’s easy to add more nodes to the existing Cluster
4. Kubernetes (Service & POD Interaction)
Deployment will handle the POD deployment by specifying an image and number for replicas Image specified during the deployment will be present in the each POD.
Service is an internal Load balancer for all the PODs which is running inside the cluster. It caters the user request to the PODs
5. Ingress (High Level)
Ingress - If an Application is deployed in Multiple Services, Ingress will direct to the Service based on the Configuration done inside it.
7. To Set up Kubernetes Dashboard
Use the below command to setup the Dashboard, kubectl apply –f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml To deploy and View Dashboard. Kubectl proxy Above command will deploy the dashboard on your system and
will be able to access the link like given below. http://127.0.0.1:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#!/login There are two ways to login into the Dashboard,
1.
Using Config
2.
Using Token
Use the below commands
to log in into the above Link. PS C:\WINDOWS\system32> $TOKEN=((kubectl -n kube-system describe secret default | Select-String
"token:") -split " +")[1] PS C:\WINDOWS\system32> kubectl config set-credentials docker-for-desktop
--token="${TOKEN}" User "docker-for-desktop" set. à Response of the above Commands Above command will update the .config file with the Token Information which is present in the .kube Folder. You can either upload the “Config”
file (or) Token to enter into the Kubernetes Dashboard.
8. To Set up the Metric Server
git clone
https://github.com/kubernetes-incubator/metrics-server.git
cd metrics-server
kubectl apply -f deploy/1.8+/
Refer this Article for brief understanding on the Metric
Server
Once it is installed, use the below command to get the metrics of the deployment.
kubectl top node
NAME
|
CPU(cores)
|
CPU%
|
MEMORY(bytes)
|
MEMORY%
|
docker-for-desktop
|
292m
|
14%
|
1265Mi
|
67%
|
9. Kubernetes Commands
Mycurrentserviceapi à Consider this is a Service API image Name
·
Using the below command, we can deploy docker
image using Kubernetes Command. kubectl run mycurrentserviceapikube --image=mycurrentserviceapi --port=80
--requests=cpu=200m --image-pull-policy=IfNotPresent ·
To expose the deployment, use the below command
to expose the Service. kubectl expose deployment mycurrentserviceapikube
--type=NodePort ·
To get the details of the Service, use the below
command. kubectl get svc mycurrentserviceapikube ·
To describe the Service Deployment, use the
below command. kubectl describe
services mycurrentserviceapikube ·
To enable Auto Scale on the Deployment kubectl autoscale deployment mycurrentserviceapikube
--cpu-percent=50 --min=1 --max=10
Below are the Main Commands which will be used to manage the
deployment of Docker Image using Kubernetes Commands.
◦
To view all nodes running on a System
kubectl get nodes
◦
To view container PODs
kubectl get pods
◦
To view container PODs
kubectl
get pods -o wide
◦
To View all PODs running on a System
kubectl get pods --all-namespaces
◦
To View where PODs are running
kubectl get pods --all-namespaces
-o wide
◦
To Run Service
kubectl run mycurrentserviceapikube
--image=mycurrentserviceapi
--port=80 --requests=cpu=200m --image-pull-policy=IfNotPresent
◦
To expose Service
kubectl expose deployment mycurrentserviceapikube
--type=NodePort
◦
To get HPA
kubectl get hpa
◦
To view all Services
kubectl get svc
◦
To get Service Information
kubectl get svc mycurrentserviceapikube
◦
To describe Service
kubectl describe services
mycurrentserviceapikube
◦
To get Deployment
kubectl get deployments
◦
To Scale Container
kubectl scale deploy mycurrentserviceapikube
--replicas=2
◦
To Auto Scale
kubectl autoscale deployment mycurrentserviceapikube
--cpu-percent=50 --min=1 --max=10
◦
To Delete AutoScale HPA
kubectl delete hpa
mycurrentserviceapikube
◦
To Delete Deployment
kubectl delete deployment
mycurrentserviceapikube --namespace=default
◦
To Delete Service
kubectl delete svc
mycurrentserviceapikube
That's it! Hope this article given an overview of Kubernetes and its uses. To become Proficient in Docker & Kubernetes, it needs Practical Implementation using Docker, converting Application code into Docker Image and Deploy it using Kubernetes. Happy Learning! |