Project 2→Devops end to end deployment using circle ci and kubernetes

Aakib
4 min readOct 21, 2023

--

Completion steps →

  1. upload the source code to github
  2. setup circle ci and connect the github repository
  3. upload the .circleci/config.yml file to build pipeline
  4. This ci -cd pipeline build your docker-image and sent it to dockerhub
  5. setup ec2 on aws and take ssh of it
  6. setup minikube and create a sample pod.yml file
  7. start your kubernetes cluster

Step 1 → Upload code to github

Here we are not going to see how we setup if you have any issue in pushing your code to github you could checkout my previous blogs in git in my profile

check out this blog of mine for github error

  1. first you need to clone the following repository

git clone https://github.com/Aakibgithuber/Devops-end-to-end-deployment

2. Now push the exact to your github account and move forward to Circle ci step

Step 2 → setup circle ci and connect with github repository

Sign into your circle ci account and if you don’t have an account just sign up by the following steps

  1. go to circle ci sign up page
  2. enter your email and create your password

3. Choose the following options

4. Now you have to choose github and you redirect to your github accout select the repository where your code present and then click on authorize

5. now you have tocreate a project for which you have to require a key pairs

  1. go to your teminal and generate a key pairs in which public key is paste to deploy key options in github and private key on your create project page
  2. run → ssh-keygen
  3. cd /root/.ssh
  4. you will find id_rsa(private key) and id_rsa.pub(public key) just copy paste the on the location mentioned above

Now you are connected to your github repository

Step 3 →upload the .circleci/config.yml file to build pipeline

  1. copy the following code and make a folder .cricleci/config.yaml and paste into it and push it to your github repo
version: 2.1
executors:
docker-publisher:
environment:
IMAGE_NAME: aakibkhan1212/building-on-ci
docker:
- image: circleci/buildpack-deps:stretch
jobs:
build:
executor: docker-publisher
steps:
- checkout
- setup_remote_docker
- run:
name: Build Docker image
command: |
docker build -t $IMAGE_NAME:latest .
- run:
name: Archive Docker image
command: docker save -o image.tar $IMAGE_NAME
- persist_to_workspace:
root: .
paths:
- ./image.tar
publish-latest:
executor: docker-publisher
steps:
- attach_workspace:
at: /tmp/workspace
- setup_remote_docker
- run:
name: Load archived Docker image
command: docker load -i /tmp/workspace/image.tar
- run:
name: Publish Docker Image to Docker Hub
command: |
echo "$DOCKERHUB_PASSWWORD" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin
docker push $IMAGE_NAME:latest
workflows:
version: 2
build-master:
jobs:
- build:
filters:
branches:
only: master
- publish-latest:
requires:
- build
filters:
branches:
only: master

2. Add the environment variable by click on project setting option you will find Environment varible option

$DOCKERHUB_USERNAME →your dockerhub username

$DOCKERHUB_USERNAME → your dockerhub password

now it will automatically starts building your docker image and push it to your dockerhub account

Step 4 setup ec2 on aws and take ssh of it

  1. go to your aws account and launch ubuntu machine and take ssh it into your terminal

commands to run on your terminal

a. ssh -i <your key pair name > ec2-user@<public ip>

c. apt update

Step 5 setup Minikube for kubernetes

command to run on your terminal

  1. curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
  2. sudo install minikube-linux-amd64 /usr/local/bin/minikube
  3. minikube start — driver docker
  4. kubectl get po -A

Now we have to write the sample.yaml file for pod deployment →

Below is the example file just replace the image name that is present in dockerhub repo

once you created a file then you have to run a command to craete a pod from file

  1. kubectl apply -f pod sample.yaml
  2. kubectl get po -o wide

you will see your pod is created and running having a private ip which you could use in checking

  1. minikube ssh
  2. curl -L http://<your pod ip>:<your port no.>

you will see your aplication is running inside a minikube cluster change now you are free to make changes in yaml file such as creating replicas etc.

Thanks for reading ….

--

--

Aakib

Cloud computing and DevOps Engineer and to be as a fresher I am learning and gaining experiance by doing some hands on projects on DevOps and in AWS OR GCP