Tuesday, May 12, 2020

Minikube to deploy tomcat container from docker

To deploy the any container from the docker we need to first create the deployment.yaml file. Please refer to the below file
apiVersion: apps/v1
kind: Deployment
metadata:
name: siddhu-tomcat-deployment
spec:
selector:
matchLabels:
app: tomcat
replicas: 1
template:
metadata:
labels:
app: tomcat
spec:
containers:
- name: tomcat
image: tomcat:9.0
ports:
- containerPort: 8999
use the following command in sequence wise
1- kubectl apply -f C:\kubernetes-minikube\deployment.yaml
Image1
2- kubectl expose deployment siddhu-new-tomcat-deployment --type=NodePort
Image2
Please make sure to check the status of the container using
kubectl get pod
Image4Image5Image6Image7
Finally to check if the tomcat is running expose the above in url using below command

To install minikube use this site

https://minikube.sigs.k8s.io/docs/start/

and download the minikube installer from 

https://storage.googleapis.com/minikube/releases/latest/minikube-installer.exe

3- minikube service siddhu-new-tomcat-deployment --url

Image3
4- check if it is running using curl or browser
For deploying Apache httpd in docker using kubernetes use below deployment,yaml
'- deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd-deployment
spec:
selector:
matchLabels:
app: httpd
replicas: 1
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd:2.4.43
ports:
- containerPort: 80

Before Deployment
Image1
After Deployment
Image2
Exposing to url and cheking if the httpd is running

Image3Image4

Note: Important command to get all
kubectl get all
'- And to delete the pod or deployment use
kubectl delete pod/nameofthepod

Minikube with windows for Kubernetes

As we know we have many online site that provide us to work directly on the ready made Kubernetes Cluster created for use i.e. 1 Master node and 1 Worker nodes
Kubernetes Playground:-https://www.katacoda.com/courses/kubernetes/playground
Play with Kubernetes Classroom:- https://training.play-with-kubernetes.com/
Play with Kubernetes :- https://labs.play-with-k8s.com/
Lets go in details and try to setup our own virtual box that contain Kubernetes cluster and that can be done using minikube.
So in short minikube is the tool that work on VM machine and used to create a single kubernetes note i.e. one master and one worker nodes with docer intalled in it. So when we deploy Minikube we get our own kubernetes cluster to work on it.
As we are using windows machine we are going to follow the below given step to setup minikube
Step 1:- Install Minikube Please refer to the below link it has all the step for Windows, Linux and MacOs for minikube installation.
https://kubernetes.io/docs/tasks/tools/install-minikube/
For windows first check you have your Hyper-V Requirement and it has to be set to YES i.e. Virtualization Enabled In Firmware: Yes. You can check this using below command.
systeminfo
If the value is NO go to the bios and set the same.
Step 2:- Install Kubectl
Please refer to the below url
https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl-on-windows
You can download the exe directly from https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/windows/amd64/kubectl.exe
and add it to your path so that it can be access from any cmd prompt. Check using command
kubectl version --client
Kubectl is the tool that is used to execute command to interact with Master node of the Cluster through API server.
Other option is to use chocolatey which is similar to brewhome in MacOS.
https://chocolatey.org/
Use this command to install kubectl using chocolatey
choco install kubernetes-cli
Step 3:- Install a Hypervisor
This is important as we want to run our Minikube on virtual instance. We have two way to do it
• Hyper-V
• VirtualBox
I had used Virtual box concept as i used the same in docker exercise. Download window base virtual machine setup from https://www.virtualbox.org/wiki/Downloads
step 4:- Finally install minikube usind either by choco
choco install minikube

or direclty take the exe from below path
https://github.com/kubernetes/minikube/releases/latest/download/minikube-installer.exe
Step 5:- Start the minikube
To start the minikube we need to use the below command
minikube start
Generally it take the driver automatically but it is better to give the name explicitely so i use this command
minikube start --driver=virtualbox
Please refer to this sites
https://kubernetes.io/docs/setup/learning-environment/minikube/#specifying-the-vm-driver
https://minikube.sigs.k8s.io/docs/drivers/virtualbox/

Image1
check the status
Image2
Above image state that all the required things in the kubernest is running properly i.e. apiserver of master nodes, kubelets of worked notes etc.
Image3
Above image show it has only one master node and service as kubernetes running but no pod in worker notes.
here at this stage if you run command to check if this container has any service or pod in side it will show empty like below
Now once the minikube gives us the cluster for paying with Kubernetes lets deploy the ready made container/pods from docker in our kubernetes.
Here we are using image named echoserver. Let’s create a Kubernetes Deployment using an existing image named echoserver, which is a simple HTTP server and expose it on port 8080 using --port.
Image4
Now once the deployment is done we need to expose it so that outer system can access it on perticular port for that we need to use below command
Image5
Once we exposed the deployment we had made our pod launced check it using this command
Image6
Finally exposed it using the URL i.e. Get the URL of the exposed Service to view the Service details:
Image7
Finally open the url in browser

Image8
Or consume using curl
Image9

Please also try to execute command like

'- Delete the hello-minikube Service:
kubectl delete services hello-minikube
'- Delete the hello-minikube Deployment:
kubectl delete deployment hello-minikube
'- Stop the local Minikube cluster:
minikube stop
'- Delete the local Minikube cluster:
minikube delete
Now also try to use one the example or container form docker using deployment.yaml and perform the above operation that will give you more details. Take deployment/container of tomcat for better and easy understanding.

Monday, May 11, 2020

Kubernetes concept

Kubernete is container management tool. This means it is used to manage container. Container manages containerized application which are deployed on them.
We have many container tool available market but the famous one is Docker. So in general Docker create container and Kubernetes manages it.
Kubernetes basically provide function that assist functionality like DSLR-MB -> deployment, scalability , load balancing, roll back, bath processing, monitoring etc.
Few of the fetures of Kubernetes as ASSS-BASH
Before understanding this we nee to understand few of the concept of Kubernetes. Kubernetes does not directly work on container it works on its functional unit called as pod which is collection of containers. Each pod as one IP address and small storage unit. This storage unit can be in local system or colud or in File system.

Auto Bin Packaging:-
Kubernetes provide the optional option to the end user to define how much CPU and Memory is required by the container to run. By this way it can adjust the available machine RAM and Memory with efficient way for scaling.
Self Healing :- In this feature Kubernetes check how the node is working. If they are alive or dead if dead then restart it. If nodes not reachable delete it. If needed transfer container and pods to another nodes.
Storage :- This is small storage used in every pods for storing the information.
Servicing and Load Balancing:- Kubernetes make a collection of PODS having similar functionality into one set and provide them a service with DNS name. By doing this it can do the load balancing properly.
Batch processing :- It support the concept of batch processing i.e. where in it create a job having many pods and execute it parallel.
Auto scaling up and scale down :- for this Kubernetes create and destroy the container as on one needed.
Secrete and Configuration :- Secrete and configuration files are maintained outside the node. Secrete store the password and userid and configuration store the configuration items details. Storing out side the nodes make it easier for deployment as we did not need to create another deployment package if the userid/password or configuration changes.
Horizontal scaling:- Looking to the current usage of CPU Kubernetes can create/replica the instance/container this is called auto controller. For that purpose it provide three things .kubectl command tool, from UI and using Auto configuration. There is controller called replica controller which will fetch replica parameter from menifest files and will create and maintain that much pods already.

Now lets discuss how the Kubernetes work. As we have team which contain manager that manage the worker in same way we have cluster=team having Master node = manager and worker nodes = worker. when we say we deploy the Kubernetes we define the deploy cluster.
in single cluster we must have at least one master and at least one worker nodes. For scalability Kubernetes handle more than one master and more than one worker nodes in cluster.

Lets understand few of the architect of Kubernets
Master Nodes:- It has following part
1- Scheduler :- USe to schedule the pods and nodes.
2- API Server :- USe to communicate with different component of Master i.e. Controller and Schedule.
3- Controller manager:- It main function is to maintain the health of cluster. It check if the pods/container is live or not. If not it either try to restart it or transfer them to another nodes. It check the current status of the cluster with the configuration done in etcd and take appropriate action if needed.
It has controller like
service account :- To maintain account of the user.
End point joint controller :- Deal with communication with service that handle the pods and container.
Replica controller :- Use to maintain minimum number of pods and container.
Node controller: check the status of the pods and container.
4- ETCD - Database to store the information . It is key value data base provided by CoreOS with open source. It store the information that is required for by the Kubernetes to handle the cluster.

Worker nodes:- It has following part
1- Proxy:- This is used to expose the nodes to the out side environment.
2- Kubelet:-This is use to interact with master node using API server.
3- container:- This is the original tool machine on which our container run
Generally Kubernetes allowed to have 5000 notes in a cluster with maximum 150000 pods and 300000 container in a single cluster. Along with this a single pod cannot have more than 100 container .
Few of the diagram to understand it better taken from the source
https://blog.newrelic.com/engineering/what-is-kubernetes/
https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/
https://medium.com/faun/kubernetes-architecture-85ad2999882a
https://blog.sensu.io/how-kubernetes-works
https://automationstepbystep.com/

Image1Image2Image3Image4Image5Image6

Wednesday, May 06, 2020

React Formik with Yup for Validation

Generally in any web application we have an HTML form that contains all the component that is required to be displayed to the user with a purpose of getting some information and capturing the same after validation on screen to send it to the back end system using form submission. ReactJS is a view part of Front end application. In reactJs also we use the form and as usual we do show the user some info and after validation submit the form. But in traditional fashion we have to write huge code with large test case for form submission and validation. React come with 3rd party library called as Formik created by Jared Palmer. You can visit the site https://jaredpalmer.com/formik/
So the question here is why we should use this library when we already have HTML form and submit functionality on button along with validation. I agree but if the same thing can be done with already tested library with less code and easy way ... then why to take hard path ....
Three main reason to use formik is
1- It mangae the state of the data in form.
2- It manage the submission action
3- It elivate and ease the validation process.

So lets start with the implementation first step to install formik in react application. Use below command

npm install formik --save
Lets say we have one normal html form having two textfield as name and email id. Now in general ReactJS we need to maintain their state using state object in class or using useState hook in function. Additional if we want to implement onchange or onBlur event handler we need to all method on that specific text field component. With using Formik it keeps track of your form's state along with few reusable methods and event handlers (handleChange, handleBlur, and handleSubmit) to your form via props. handleChange and handleBlur use a name or id attribute to figure out which field to update.

As show below first we need to import Formik from formik to use it. Then we need to bring our whole form inside the formik tag and in formik tag we can define initialValue, validate and onSubmit this represent the above three aspect i.e. manage the state of the data in form , elevate and ease the validation process. and manage the submission action.
One more thing we need to add our html form inside formic using function i.e. {(event handlers) =>(html form)}
All the event handler for formik is mapped with the html methods.
Image1Image2Image3Image4
In addition Formik also give us some additional functionality that can reduce our boilerplate. i.e. we explicitly write code for events such as onChange -> handleChange, onBlur -> handleBlur, and so on. Formik comes with a few extra components to do the same i.e. <Form />, <Field />, and <ErrorMessage />. They use React context to hook into the parent <Formik /> state/methods.
Image5Image6Image7
Now in above both the example you have seen that form internal component state, submission and validation are done using formik library. But still validation is done using the old fashion. Developer has to write the code and test it properly. How if we get he ready made library the do the already tested validation for us and in yes .. for this we are using yup package. So lets first install it using command
npm install yup --save
Lets this time use the useFormik as hook in functional component to introduce formik in the form rather than Formik as an component/render-prop in class that we had done in above two examples.
Image8Image9Image10
Note: Get the running code from Git
https://github.com/shdhumale/reactjsformikyup