We will learn how to automate springboot microservices builds using Jenkins pipeline and Deploy into AWS EKS Cluster with help of Kubernetes CLI plug-in.
We will use Springboot Microservices based Java application. I have already created a repo with source code + Dockerfile. The repo also have Jenkinsfile for automating the following:
- Automating builds using Jenkins - Automating Docker image creation - Automating Docker image upload into AWS ECR - Automating Docker Containers Deployments to Kubernetes Cluster
Step # 1 - Create Maven3 variable under Global tool configuration in Jenkins
Make sure you create Maven3 variable under Global tool configuration.
Step #2 - Create Credentials for connecting to Kubernetes Cluster using kubeconfig
Click on Add Credentials, use Kubernetes configuration from drop down.
use secret file from drop down.
execute the below command to login as jenkins user.
sudo su - jenkins
you should see the nodes running in EKS cluster.
kubectl get nodes
Execute the below command to get kubeconfig info, copy the entire content of the file:
cat /var/lib/jenkins/.kube/config
Open your text editor or notepad, copy and paste the entire content and save in a file.
We will upload this file.
Enter ID as K8S and choose File and upload the file and save.
Enter ID as K8S and choose enter directly and paste the above file content and save.
Step # 3 - Create a pipeline in Jenkins
Create a new pipeline job.
Step # 4 - Copy the pipeline code from below Make sure you change red highlighted values below as per your settings: Your docker user id should be updated. your registry credentials ID from Jenkins from step # 1 should be copied
Provides out-of-the-box monitoring capabilities for the Kubernetes container orchestration platform. It can monitor servers and databases as well.
Collects and stores metrics as time-series data, recording information with a timestamp
It is based on pull and collects metrics from targets by scraping metrics HTTP endpoints.
What is Grafana?
Grafana is an open source visualization and analytics software.
It allows you to query, visualize, alert on, and explore your metrics no matter where they are stored.
Please Watch this steps in YouTube Channel:
Prometheus Architecture
Key components:
1. Prometheus server - Processes and stores metrics data
2. Alert Manager - Sends alerts to any systems/channels
3. Grafana - Visualize scraped data in UI
Installation Method
The are are many ways you can setup Prometheus and Grafana. You can install in following ways:
1. Create all configuration files of both Prometheus and Grafana and execute them in right order.
2. Prometheus Operator - to simplify and automate the configuration and management of the Prometheus monitoring stack running on a Kubernetes cluster
3. Helm chart (Recommended) - Using helm to install Prometheus Operator including Grafana
Why to use Helm?
Helm is a package manager for Kubernetes. Helm simplifies the installation of all components in one command. Install using Helm is recommended as you will not be missing any configuration steps and very efficient.
Prometheus and grafana helm chart moved to kube prometheus stack
Create Prometheus namespace
kubectl create namespace prometheus
Below is helm command to install kube-prometheus-stack. The helm repo kube-stack-prometheus (formerly prometheus-operator) comes with a grafana deployment embedded.
Amazon EKS is a fully managed container orchestration service. EKS allows you to quickly deploy a production ready Kubernetes cluster in AWS, deploy and manage containerized applications more easily with a fully managed Kubernetes service.
EKS takes care of master node/control plane. We need to create worker nodes.
EKS cluster can be created in following different ways
1. AWS console
2. AWS CLI
3. eksctl command
4. using Terraform
We will create EKS cluster using eksctl command line tool.
Please watch the steps in YouTube channel:
Pre-requistes:
This Lab is using Jenkins EC2 instance. Jenkins EC2 instance needs to have following configured:
Install AWS CLI– Command line tools for working with AWS services, including Amazon EKS.
Install eksctl– A command line tool for working with EKS clusters that automates many individual tasks.
Install kubectl – A command line tool for working with Kubernetes clusters.
Create IAM Role with Administrator Access
You need to create an IAM role with AdministratorAccess policy. Go to AWS console, IAM, click on Roles. create a role
Select AWS services, Click EC2, Click on Next permissions.
Now search for AdministratorAccess policy and click
Skip on create tag. Now give a role name and create it.
Assign the role to EC2 instance Go to AWS console, click on EC2, select EC2 instance, Choose Security. Click on Modify IAM Role
Choose the role you have created from the dropdown. Select the role and click on Apply.
Switch to Jenkins user
sudo su - jenkins
Create EKS Cluster with two worker nodes using eksctl
the above command should create a EKS cluster in AWS, it might take 15 to 20 mins. The eksctl tool uses CloudFormation under the hood, creating one stack for the EKS master control plane and another stack for the worker nodes.
Once EKS cluster is created, kubeconfig file be created under /var/lib/jenkins/.kube folder.
you can view the kubeconfig file by entering the below command:
cat /var/lib/jenkins/.kube/config
Connect to EKS cluster using kubectl commands
To view the list of worker nodes as part of EKS cluster.
kubectl get nodes
kubectl get ns
Deploy Nginx on a Kubernetes Cluster Let us run some apps to make sure they are deployed to Kubernetes cluster. The below command will create deployment: