Build & Deploy Series | Part-3 | Build and Deploy of React Application in Kubernetes(microk8s) hosted on AWS EC2
Highly Recommend using repo here -Day3to practise this project instead of code snippets, In case of confusion, Please do watch video that is explained in English, the code here in the blog is not changed to keep screenshots intact
Also please do note that English video also consists of another session clubbed that is kubernetes imperative commands
cost charges may occur on t3.medium, make sure to turn off the instance
#Note: AWS Free Tier is only for 750 Hours per year, so make sure you stop instance once you are done with the project
Now let us Dockerize our React application,We will install docker on our AWS EC2
Now before we start ,we recommend you to watch
Part-2 as Docker images plays a major part in this project
As we have already created docker image in Part-2 Project, we will use the
same docker image here
We will make an account in
Dockerhub first. and
then we will push our image to dockerhub.
By default we get one public repositories free from Dockerhub,we will make
use of it.
once you created, click on create repository inside docker hub
you will be able to find two commands at right corner of the screen
docker tag local-image:tagname new-repo:tagname
docker push new-repo:tagname
Now let us sign into AWS EC2,at which we created docker image as part of our
last project
sudo docker login
once logged in, now let us use the commands that we have used copied from
docker hub
and change commands according to our need
check the docker images to make sure
sudo docker images
Now let us make changes for docker hub commands we copied accordingly
our local-image name is react-docker and tagname can be latest,our new repo
name will be username/repo-name, our username is testdemo880 for docker hub
and repo-name can be react-docker-hub. you can give it any name. dockerhub
creates repo if repo does not exist
so our command modifies as follows
sudo docker tag react-docker:latest testdemo880/react-docker-hub:latest
sudo docker push testdemo880/react-docker-hub:latest
Once docker image is pushed, you will be able to see our docker image in
docker hub
We can now logout of our AWS EC2 server in which we have our docker
image
Kubernetes(microk8s)
Now let us launch a new AWS EC2 instance install
microk8s on by following
instructions from official documentation.
We are proceeding with microk8s for this sample project as kubernetes setup
is bit complex
We will use alias command to get the feel just like we work in
kubernetes
make sure to sign out and sign in from ubuntu user for the next commands to work
sudo su - root
sudo su - ubuntu
let us check microk8s pods by
microk8s kubectl get all --all-namespaces
Now let us use alias command to work like kubernetes
alias kubectl="microk8s kubectl"
Deployment of React in Kubernetes
Now to start with Deployment of kubernetes, we need pods to deploy our
application,service to access our application, And Ingress to access our
application through ingress.
Now to start with Deployment of kubernetes, we need pods to deploy our
application,service to access our application, And Ingress to access our
application through ingress,so we will create three manifest files,
react-deployment.yaml, react-service.yaml and react-ingress.yaml
let us create one by one
Before that , let us create a namespace
kubectl create ns react-microk8s
you can give any name for namespace of your choice, i have given name as
react-microk8s
As you could see in image, i have used our dockerhub repo name
by default it takes latest tag, if you have specified any other tag apart
from latest then update image name to be
testdemo880/react-docker-hub:1.2 , here 1.2 can be referred as
tagname
let us create deployment by command and also i have exposed our pod at port
80
kubectl apply -f deployment.yaml
Now check pods created by deployment with command
kubectl get pods -n react-microk8s
Now overview of our setup looks like this
Now let us create a service,Service and deployment must be in the same
namespace
Service and deployment connects together through labels
TargetPort must be the Port number that you opened on our pod, in our case
it is 80,We matched our deployment and service using labels as you can see
in see in selector,labels,specified are same as that mentioned in
deployment
lets start our service by command
kubectl apply -f react-service.yaml
Now check the service by command
kubectl get svc -n react-microk8s
r
By default service creates ClusterIp, there are different services like
NodePort and Loadbalancer,each service having its own significance,since
service is up,we will be able to access our website internally,you can use
curl http://{CLUSTER-IP}:{SERVICE-PORT}
the above screenshot shows our website is accessible,Now our setup looks
like this
Now since clusterip can only be accessed internally, let us go to create
ingress to access by our domain. Ingress must be in the same namespace where
service and deployment are available,but before creating ingress, we need
ingress controller pod to be created.
since kubernetes has its own nginx controller pod, we need to enable
it
since we are using microk8s, enable ingress by command
your blog is easy to understand DevOps easily. Thankyou
ReplyDelete