often abbreviated as “K8s” (because there are 8 letters between the “K” & “s” in Kubernetes), orchestrates containerized applications to run on a cluster of hosts. K8s also allocates storage and persistent volumes to running containers, provides automatic scaling, and works continuously to maintain the desired state of applications, providing resiliency
Jenkins is a self-contained, open source automation server used to automate tasks associated with building, testing, and delivering/deploying software. Jenkins Pipeline implements continuous deliver pipelines into Jenkins through use of plugins and a Jenkinsfile. The Jenkinsfile can be Declarative or Scripted and contains a list of steps for the pipeline to follow
- AWS Account
- GitHub Account
- AWS CLI
- Preferred IDE (I used VScode)
To begin, ensure that you have the necessary tools installed. These tools include:
- Terraform
- An s3 bucket created on AWS for the terraform backend.
- AWS CLI installed and configured on your IDE
First, clone my GitHub repository:
git clone <https://github.com/Bukola-Testimony/DevOps-Project>
Change into the directory to the folder shown below:
cd DevOps-Project/Terraform/jenkins/
Here we are going to create our Jenkins Server. The terraform configuration files to create the server are in this path.
This configuration creates a VPC along with subnets and security groups needed to create the server. Also a script is included to install the following at startup :
- Jenkins
- Terraform
- git
- kubectl
All these dependencies are needed for this project.
The first step is to initialize the terraform backend by using the following command:terraform init
Next, you will run the terraform plan command to evaluate the Terraform configuration.
terraform plan
Finally, you will run the command terraform apply to apply the configuration.
terraform plan
Once everything runs successfully, you’ll see an output of the ec2-instance IP
Next, you will navigate over to the AWS console and you will be able to see the following:
- Edit the inbound security group
Note: this has already been included in the terraform security group configuration. - Type: CustomTCP - Port range: 8080
- Next, copy the public IP of the instance to your browser and append port 8080
e.g 192.34.54.20:8080
- This should open the Jenkins Getting started page.
-
SSH into your jenkins server either through your IDE or directly from aws console.
-
To unlock, copy the code on the jenkins dashboard to your terminal.
-
On the terminal, run:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
#Example Output: 95c29cd1d4a54943a8c9c05edcf13a8c
#Note: remember to include "sudo cat" before the unlock code
-
enter the code in the text box and click on continue
-
On the next page, click on suggested pluggings. Allow the selected pluggins to run.
-
On the next page enter your user details and password. and viola! jenkins server is ready for use.
-
Configure jenkins server with all the necesary credentials both for Github and AWS.
-
Now, Set up the environment variables needed for the pipeline. This require your aws credentials and github credentials. See example below.
AWS_ACCESS_KEY_ID: AKIANDPAW23PAWEXAMPLE
AWS_SECRET_ACCESS_KEY:Osie3yutegyu5vjnbn455Example
AWS_DEFAULT_REGION: us-east-1
Github Username and password
Configure Jenkins Pipeline to use the Terraform configuration file and the AWS CLI to provision the Kubernetes cluster. Click on new item.
- Give your project a name.
Choose pipeline and click
OK
at the bottom of the page.
- Add the github repository url to the pipeline. Indicate the Jenkinsfile and the Github branch. Save the job.
- Push your code to a repository on Github.
-
Back to your IDE, create a terraform file for deploying kubernetes clusters to AWS EKS. In the git repository, you will find the terraform file I created for this purpose in the path:
Terraform/eks.
-
This file will create an eks cluster with 1 node. The size of the instance is t3.large. You may customise it to your preferences.
- Next, push your code to Github.
- Run the pipeline using the Jenkinsfile. You will find the file at the root of the folder in this repository. All the stages in th pipeline has been defined in the Jenkinsfile.
- The configuration in the jenkinsfile will run the terraform file to build the eks clusters, deploy microservices application and my web application on the cluster using the manifests in the kubernete directory. It will also Expose the services of the applications and attach load balancers to each application. Finally It will output the load balancers' endpoints.
Here is an example of what the jenkins file looks like:
#!/usr/bin/env groovy
pipeline {
agent any
environment {
AWS_ACCESS_KEY_ID = credentials('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY')
AWS_DEFAULT_REGION = "us-east-1"
}
stages {
stage("Create an EKS Cluster") {
steps {
script {
dir('Terraform/eks') {
sh "terraform init"
sh "terraform apply -auto-approve"
}
}
}
}
stage("Deploy to EKS") {
steps {
script {
dir('kubernetes') {
sh "aws eks --region us-east-1 update-kubeconfig --name Eks-cluster"
sh "kubectl apply -f complete-demo.yaml"
sh "kubectl apply -f web-deployment.yml"
sh "kubectl apply -f manifests-monitoring"
}
}
}
}
}
}
Click on Build now
. As the project rus you will see a stage view as shown below. You will also see the build number at the bottom under the Build history
section.
To see the console output and other information, click on the build number.
- How to deploy EKS with terraform
- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eks_cluster
- https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eks_node_group
- Deploy EKS Cluster to AWS using Terraform
- CICD Pipeline to deploy Kubernetes Applications using Terraform, EKS, and Jenkins
- https://github.com/frankisinfotech/terraform
- Website - Bukola Testimony
- Twitter - @BukolaTestimony