Step 1: Set up Google Cloud Platform (GCP) Project
Create a GCP Account: If you don't have a Google Cloud Platform account, sign up for one.
Create a Project: Create a new project in the Google Cloud Console.
Enable Billing: Make sure to enable billing for your project.
Enable the Kubernetes Engine API: In the Google Cloud Console, navigate to the "APIs & Services" -> "Dashboard." Enable the "Kubernetes Engine API."
Step 2: Enable CloudShell
Step 3: Set Up gcloud
Configurations
Run the following commands to configure gcloud
with your GCP project:
$gcloud auth login
$gcloud config set project ashok-198510
Replace PROJECT_ID
with your GCP project ID.
Step 4: Pull Nginx image, Copy your web-app html file to docker and push to Google Container Registry
First pull the nginx image following command.
$docker pull nginx:latest
Then run the nginx image
$docker run -d -p 80:80 nginx:latest
second download or create any sample index.html page. Here i cloned from my git hub repo in that repo only copy the index.html and ashok.png.
a. This is repo link https://github.com/Ashoksana/explorewithashok.git
Next copy the index.html and ashok.png for deploying my webpage to internet.
$docker cp index.html <container-id>:/usr/share/nginx/html/
Now check the docker container web interface.
you will get web inerface like this
Then update the image using docker commit command
docker commit <containerid> explorewithashok/web:version
$docker commit <container-id> /web:version1
Tag the image for push this image to Google container registry using following commands.
Before this you have to enable the container registry api.
$gcloud services enable containerregistry.googleapis.com $docker tag explorewithashok/web:version1 gcr.io/ashok-198510/explorewithashok:version1
Now push the image to Google Container Registry
$docker push gcr.io/ashok-198510/explorewithashok:version1
this error means you should give docker auth permission to gcloud using following command.
gcloud auth configure-docker
After above command issue will reslove.
Afer image pushed into GCR now go to the Container Registry console.
these are image details like zone, version, iamge name
In above it clearly shown we can directly deploy our application using deploy option but we are devops engineer best practice is deploy through Gcloud cli.
Know more about GCR follow this document https://cloud.google.com/container-registry/docs/pushing-and-pulling?_ga=2.111514180.-173018589.1570384092
Step 5: Setup the zone for Cluster Create a GKE Cluster
Run the following command to create a GKE cluster:
$gcloud config set compute/zone us-central1-a
$gcloud container clusters create <CLUSTER_NAME> --num-nodes=3
Replace CLUSTER_NAME
with the desired name for your cluster. Eg: gcloud container clusters create ashoksana --num-nodes=3
Step 5: Configure kubectl
Run the following command to configure kubectl
to use your GKE cluster:
$gcloud container clusters get-credentials CLUSTER_NAME
Step 6: Deploy a Sample Website
For deploy the sample website in GKE run following comand:
$kubectl create deployment web-app --image=gcr.io/ashok-198510/explorewithashok:version1
$kubectl expose deployment web-app --type LoadBalancer --port 80 --target-port 80
# To get pods
$kubectl get pods
# To get service
$Kubectl get services
Now you want to host application in internet copy the external ip and pppaste it in brower of your desktop.
Congratulations! You've successfully created a GKE cluster and deployed a sample website. Keep in mind that this is a basic setup, and in a production environment, you would likely want to use a more complex configuration and consider security best practices.
After completion of your please delete GKE cluster using following command
gcloud container clusters delete <cluster-name>
I hope you people like this blog.
If you like this blog please follow these below Links, You will get more content like this in that links.
WhatsApp Group:- https://chat.whatsapp.com/Ii2xKz9vuW93AWt07m4AYj
Telegram:- https://t.me/ExplorewithAshok
LinkedIn: https://www.linkedin.com/in/ashok-sana
Instagram:- https://instagram.com/explorewithashok?igshid=OGQ5ZDc2ODk2ZA==
Linktree:- https://linktr.ee/ashoksana