Find complete steps here in my blog DevSecOps: Deploying the 2048 Game on Docker and Kubernetes with Jenkins CI/CD
- Launch an Ubuntu(20.04) AWS t2.large / GCP e2-highcpu-8
- Install Jenkins, Docker and Trivy. Create a Sonarqube Container using Docker.
- Install Plugins like JDK, Sonarqube Scanner, Nodejs, and OWASP Dependency Check.
- Create a Pipeline Project in Jenkins using a Declarative Pipeline
- Install OWASP Dependency Check Plugins
- Docker Image Build and Push
- Deploy the image using Docker
- Kubernetes master and slave setup on Ubuntu (20.04) t2.medium/e2-medium
- Access the Game on Browser.
- On Main server
sudo apt install openjdk-11-jre
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update -y
sudo apt-get install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins
sudo apt-get update -y
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo usermod -aG docker $USER
newgrp docker
sudo chmod 666 /var/run/docker.sock
sudo systemctl restart docker
sudo apt-get install wget apt-transport-https gnupg lsb-release
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | sudo tee /usr/share/keyrings/trivy.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install trivy -y
-
Once Jenkins is installed, you will need to go to your AWS EC2 Security Group and open Inbound Port 8080, since Jenkins works on Port 8080.
-
Install Plugins like JDK, Sonarqube Scanner, NodeJS, OWASP Dependency Check, kubernetes Go to Manage Jenkins βPlugins β Available Plugins β
-
Install below plugins
1 β Eclipse Temurin Installer (Install without restart)
2 β Sonarqube Scanner (Install without restart)
3 β NodeJS Plugin (Install Without restart)
4 β OWASP Dependency-Check (Install Without restart)
5 β Search for Docker and install these plugins (Install Without restart)
Docker Docker Commons Docker Pipeline Docker API docker-build-step
6 β Kubernetes (Install Without restart)
-
After the docker installation, we create a Sonarqube container (Remember to add 9000 ports in the security group).
docker run -d --name sonar -p 9000:9000 sonarqube:lts-community
Now our Sonarqube is up and running
Take Two Ubuntu 20.04 instances one for k8s master and the other one for worker.
-
Install Kubectl on Jenkins machine also.
-
Kubectl is to be installed on Jenkins also
sudo apt update sudo apt install curl -y curl -LO https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl kubectl version --client
-
Part 1 β β β β β Master Node β β β β β β
sudo hostnamectl set-hostname K8s-Master
-
β β β β β Worker Node β β β β β β
sudo hostnamectl set-hostname K8s-Worker
-
Part 2 β β β β β β Both Master & Node β β β β β
sudo apt-get update sudo apt-get install -y docker.io sudo usermod βaG docker Ubuntu newgrp docker sudo chmod 777 /var/run/docker.sock sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - sudo tee /etc/apt/sources.list.d/kubernetes.list <<EOF deb https://apt.kubernetes.io/ kubernetes-xenial main # 3lines same command EOF sudo apt-get update sudo apt-get install -y kubelet kubeadm kubectl sudo snap install kube-apiserver
-
Part 3 β β β β β β β β Master β β β β β β β -
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
-
β β β β β Worker Node β β β β β β
sudo kubeadm join <master-node-ip>:<master-node-port> --token <token> --discovery-token-ca-cert-hash <hash>
Copy the config file to Jenkins master or the local file manager and save it copy it and save it in documents or another folder save it as secret-file.txt
Note: create a secret-file.txt in your file explorer save the config in it and use this at the Kubernetes credential section.