Run jekins setup script
cat /var/lib/jenkins/secrets/initialAdminPassword
Click new item > Name: Hello world > select freestyle project > Build Steps > Select Execute on shell > echo "Hello World!" && uptime > Save.
Click on Build now > Check build history > See output.
cat /var/lib/jenkins/workspace/
echo "jenkins-server" > /etc/hostname
yum install git -y & git -v
Click Dashboard > manage jenkins > manage pulgins > Avaliable > Search for github > check and click on install without restart.
Click Dashboard > manage jenkins > click tools > go for git > name git > Path to Git executable: /usr/bin/git (Exceute cmd whereis git to get path)> save
Click Dashboard > new item > Name: PUllcodefromgitRepo > source code management > select git > Enter git url name > credential none > click save.
Click on build now > check the build history successfull.
cd /var/lib/jenkins/workspace/PullcodefromGitRepo
Search maven install & maven download > copy link from the maven download Binary tar.gz archive.
cd /opt
wget https://dlcdn.apache.org/maven/maven-3/3.9.1/binaries/apache-maven-3.9.1-bin.tar.gz
tar xzvf apache-maven-3.9.1-bin.tar.gz
mv apache-maven-3.9.1 maven
/opt/maven/bin/mvn -v
M2_HOME=/opt/maven
M2=/opt/maven/bin
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.18.0.10-3.el9.x86_64
find / -name java-11*
OUTPUT= /usr/lib/jvm/java-11-openjdk-11.0.18.0.10-3.el9.x86_64
Export the variable to .bash_profile (check before executing file there should not be any path entry)
echo -e "M2_HOME=/opt/maven\nM2=/opt/maven/bin\nJAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.18.0.10-3.el9.x86_64\nPATH=b\$PATH:b\$HOME:bin:b\$JAVA_HOME:b\$M2_HOME:b\$M2\nexport PATH" >> .bash_profile && source .bash_profile
echo $PATH
mvn -v
Click Dashboard > manage jenkins> plugins > Available > search maven > check maven integration > install withour restart
Click Dashboard > manage jenkins> TOOLS >
- jdk > Name: Java-11 > JAVA_HOME: /usr/lib/jvm/java-11-openjdk-11.0.18.0.10-3.el9.x86_64 > MAVEN ADD > Name: Maven-3.9.1 MAVEN_HOME: /opt/maven > apply & save.
- Click Dashboard > new item > Name:Build_Maven_Artifact > select maven project > Source_code_management:check Git,
- repo url:https://github.com/Pruthvi360/ci-cd-hello-world.git
- Build > Root POM: pom.xml> Goals and options: clean install (Check maven lifecycle)
- Apply & save
follow the steps in the tomcatinstall.md to complete the installation. Run the tomcat-setup.sh
Click Dashboard > manage jenkins > plugins > available > Search: Deploy to continer > check and click install withour restart.
- Click Dashboard > manage jenkins > manage credentials > system > global > select usernam & password > specify username & password >
- name: tomacat-deployer > description:tomcat-deployer.
- Click Dashboard > new item > Name:Deploy-to-container > Source_code_management: check git and git url > Build > Root POM: pom.xml > Goals and options: clean install
- Post build action > select deploy to continer > specify war file path webapp/target/webapp.war or **/*.war
- Containers > Select Tomatcat 9 > Give credentials select from previously created > Tomcat URL: http://35.231.215.116:8080/ > click Apply and save
Modify the existing job and Select
git clone https://github.com/Pruthvi360/ci-cd-hello-world.git
Edit index.jsp
git status > It should be **modified** state
git add . > In the current directory state should be **staging**
git commit -m "Auto Tigger to CI/CD" > In the state **commited** ready to push
git remote -v > used to show the remotes mapped to git remote repository
git branch > Check the branch
git push origin master > push the commited changes to the repository
Watch the Build history in jenkins and validate the changes in the tomcat server.
Refer the Docker-install.md file
FROM tomcat:latest
RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps
COPY ./*.war /usr/local/tomcat/webapps
docker build -t mytomcat .
docker run -d --name tomcat -p 8082:8080 mytomcat
useradd dockeradmin
passwd
usermod -aG docker dockeradmin
usermod -aG sudo dockeradmin
sudo usermod -s /bin/bash dockeradmin
mkdir /home/dockeradmin
sudo chown dockeradmin:dockeradmin dockeradmin
visudo
%dockeradmin ALL=(ALL:ALL) NOPASSWD:ALL
nano /etc/ssh/sshd_config
passwordAuthentication yes
Click Dashboard > manage jenkins > plugins > available > Search Publish over SSH > install without restart
Click Dashboard > manage jenkins > click system > scroll down > publish over ssh > add > enter ip (public/private) > dockeradmin: password apply and save
sudo echo -e "FROM tomcat:latest
RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps
COPY ./*.war /usr/local/tomcat/webapps" > Dockerfile
docker build -t app:v1 .
docker run -d --name myfirstapp -p 8087:8080 app:v1
Click Dashboard > manage jenkins > click system > scroll down > publish over ssh > add > enter hostname (public/private) > username: root: password apply and save
- Click Dashboard > New Item > Name: Build_and_deploy_container > Description: Build code with help of maven and deploy it on tomcat docker container.
- Source code manangement > Check Git > git url: > Check it is master > Build Triggers: Poll SCM : * * * * * >
- Build > Root POM: pom.xml > Goal and Options: clean install
- Post Build Action: Name: root > Transfers: webapp/target/*.war > Remove prefix: webapp/target > Remote Directory: //opt//docker
- Exec command : echo -e "FROM tomcat:latest RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps COPY ./*.war /usr/local/tomcat/webapps" > Dockerfile
docker build -t app:v2 . docker run -d --name myfirstapp -p 8087:8080 app:v2
docker container prune
docker images prune -a
Edit Exec commads in the Post Build Actions:
echo -e "FROM tomcat:latest
RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps
COPY ./*.war /usr/local/tomcat/webapps" > Dockerfile
CONTAINER=myfirstapp
docker stop $CONTAINER
docker rm $CONTAINER
docker rmi app:v1
docker build -t app:v1 .
docker run -d --name $CONTAINER -p 8087:8080 app:v1
Refer to ansible/ansible.tf file
ansible --version
python3 --version
ansible -m ping localhost
tree /etc/ansible/
cat /etc/ansible/hosts
useradd ansibleadmin
passwd ansibleadmin
sed -i '/%wheel/a ansibleadmin ALL=(ALL) NOPASSWD: ALL' /etc/sudoers
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
sed -i 's/PermitRootLogin no/PermitRootLogin yes/g' /etc/ssh/sshd_config
sed -i 's/PubkeyAuthentication no/PubkeyAuthentication yes/g' /etc/ssh/sshd_config
step1
sudo nano /etc/default/locale
step2
LANG="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
step3
sudo update-locale LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8
step4
Finally restart your machine
su - root
ssh-keygen
ssh-copy-id <docker-host-ip private or public ip if both are in same VPC>
echo -e "docker-host-ip" > /etc/ansible/hosts
ansible all -m ping
ansible all -m command -a uptime
dokcer is install in the same ansible server
add **ansibleadmin** user to the **docker** group
update hosts file
[localhost]
127.0.0.1 ansible_user=root ansible_ssh_pass=1996
1. Create a docker account
2. create a repo
3. retag the image with
** docker tag <local-image:tag> <docker-username>/<repo>:tag**
4. Push the docker image to docker hub
5. Create playbook
---
- hosts: localhost
become: true
tasks:
- name: Log into DockerHub
community.docker.docker_login:
username: <>
password: <>
- name: stop if we have old docker container
command: docker stop myfirstapp
ignore_errors: yes
- name: remove stopped docker container
command: docker rm myfirstapp
ignore_errors: yes
- name: remove current docker image
command: docker rmi app:v1
ignore_errors: yes
- name: building docker image
command: docker build -t app:v1 .
args:
chdir: /home/ansibleadmin
- name: creating docker image
command: docker run -d --name myfirstapp -p 8080:8080 app:v1
- name: tag the created docker image to push into docker hub
command: docker tag app:v1 pruthvidevops/deveops:app-v1
- name: push the built image to docker hub
command: docker push pruthvidevops/deveops:app-v1
- Run the docker playbook
ansible-playbook /etc/ansible/docker.yaml
Copy the previous the jenkins job and create new job.
Replace the Exec commands with
echo -e "FROM tomcat:latest RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps COPY ./*.war /usr/local/tomcat/webapps" > Dockerfile ;
ansible-playbook /etc/ansible/docker.yaml
sudo snap install google-cloud-cli --classic
gcloud auth login
gcloud config set project qwiklabs-gcp-02-ea4ca0d66fa5
sudo snap install google-cloud-sdk --classic
sudo apt-get install apt-transport-https ca-certificates gnupg -y
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
sudo apt-get update && sudo apt-get install google-cloud-cli
sudo apt-get install google-cloud-cli-gke-gcloud-auth-plugin
gcloud init
gcloud components install kubectl
gcloud components install gke-gcloud-auth-plugin
sudo apt-get install google-cloud-cli-gke-gcloud-auth-plugin
git clone https://github.com/Pruthvi360/ci-cd-build-kubernetes.git
cd ci-cd-build-kubernetes/terraform-gke
Create service account in the gcloud account and give
1. kubernetes admin privilege
2. compute admin privilege
3. service account user privilege
Download the json key and keep in the terraform dir
sudo snap install terraform --classic
terraform init
terraform plan -var "project_id=<your-project-id>"
terraform apply -var "project_id=<your-project-id>" -auto-approve
gcloud container clusters get-credentials <gke-cluster-name> --region us-central1 --project <project_id>
kubectl get nodes
kubectl get pods
terraform destroy -var "project_id=<your-project-id>" -auto-approve
gcloud beta container --project "<Project-id>" clusters create "ci-cd" --zone "us-central1-c" --no-enable-basic-auth --cluster-version "1.25.7-gke.1000" --release-channel "regular" --machine-type "e2-medium" --image-type "COS_CONTAINERD" --disk-type "pd-balanced" --disk-size "100" --metadata disable-legacy-endpoints=true --scopes "https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" --max-pods-per-node "110" --num-nodes "3" --logging=SYSTEM,WORKLOAD --monitoring=SYSTEM --enable-ip-alias --network "projects/qwiklabs-gcp-03-b499196e43ab/global/networks/default" --subnetwork "projects/qwiklabs-gcp-03-b499196e43ab/regions/us-central1/subnetworks/default" --no-enable-intra-node-visibility --default-max-pods-per-node "110" --no-enable-master-authorized-networks --addons HorizontalPodAutoscaling,HttpLoadBalancing,GcePersistentDiskCsiDriver --enable-autoupgrade --enable-autorepair --max-surge-upgrade 1 --max-unavailable-upgrade 0 --enable-shielded-nodes --node-locations "us-central1-c"
gcloud container clusters get-credentials ci-cd --zone us-central1-c --project <project-id>
nano deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: ci-cd
labels:
app: myfirst-app
spec:
replicas: 2
selector:
matchLabels:
app: myfirst-app
template:
metadata:
labels:
app: myfirst-app
spec:
containers:
- name: myfirst-app
image: pruthvidevops/deveops:app-v1
imagePullPolicy: Always
ports:
- containerPort: 8080
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
nano service-expose.yaml
apiVersion: v1
kind: Service
metadata:
name: ci-cd-service
labels:
app: myfirst-app
spec:
selector:
app: myfirst-app
ports:
- port: 8080
targetPort: 8080
type: LoadBalancer
kubectl apply -f deployment.yaml
watch kubectl get pods
kubectl apply -f service-expose.yaml
watch kubectl get svc
- Create and new job with by copying the previous docker build and push JOB
- add EXEC commands
echo -e "FROM tomcat:latest
RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps
COPY ./*.war /usr/local/tomcat/webapps" > Dockerfile ;
ansible-playbook /etc/ansible/docker.yaml
gcloud container clusters get-credentials ci-cd --zone us-central1-c --project qwiklabs-gcp-02-701d60ba3040
kubectl delete deployment ci-cd
kubectl apply -f deployment.yaml
kubectl apply -f service-expose.yaml
git init
git pull
git add .
git commit -m "Deploying to kubernetes"
git push
Do Some changes in the index.jsp and perform the above steps
kubectl get svc
http:<LoadBalaner-ip>:8080/webapp/
##-------------NOW YOU SUCCESSFULLY ESTABLISHED THE CI-CD PIPELINE FOR THE ENTIRE DEPLOYMENT--------------##
##-------------------------------------THANKS FOR WATCHING----------------------------------------------##