Imagine cloud computing as a giant toolbox, full of ways to connect to different services via the internet. This makes it a pretty smart choice for folks looking to set up and run a process that continuously improves and delivers their software, also known as a CI/CD server.
One of the most loved in this arena is a free tool called Jenkins. Now, Digital Ocean provides a wide playground for running web apps that speak the language of Java, making it an ideal home for Jenkins. So, it's no wonder that a bunch of tech whizzes who specialize in development and operations - we call them DevOps experts - love to run Jenkins on Digital Ocean.
What is Terraform?
I wonder if you ever heard about Infrastructure As Code(IAC). If you haven't, don't worry we will discuss here everything about IAC and Terraform. Terraform is an infrastructure as a code tool that lets you define the cloud & on-premise resources in human-readable configuration files using Hashicorp configuration language(HCL). We don't have to learn a new language for this, it's very similar to YAML😌
Below is an example.
Seems easy, isn't it?😅😅
Why use Terraform?🤔
Using terraform has many benefits such as:
Version control: By storing infrastructure configurations as code, you can keep them in version control systems (e.g., Git) and track changes over time. We will see that in a moment.
Collaboration: Teams can collaborate on infrastructure management by sharing, reviewing, and updating configurations through established software development practices. Very useful for enterprises and pair programming
Reusability: Terraform allows you to create reusable modules, which can be shared across different projects, reducing code duplication and enabling consistency.
Predictability: Terraform uses a planning phase, allowing you to preview changes before applying them, reducing the risk of unintended consequences. This is an important use case for production deployment.
Multi-cloud support: Terraform supports multiple cloud providers, making it easier to manage resources across different platforms in a consistent way.
Don't worry we will learn all the basics of Terraform by implementing later.
Configure Jenkins & Digital Ocean🔩
If you're already into DevOps then you must be familiar with Jenkins. Jenkins is an open-source automation server primarily used for Continuous Integration(CI) & Continuous Deployment(CD) pipelines. It is written in Java and has a vast ecosystem of plugins that extend its functionality, making it a versatile and widely used tool for building, testing, deploying, and automating software projects.
Jenkins is widely used in various industries and organizations, from small startups to large enterprises, to automate software development processes and improve overall software quality, consistency, and delivery speed. Since it is an open-source project you can contribute to it too, if you are new to open-source you can get started about it here.
Digital Ocean, I assume you have already heard about the digital ocean. DigitalOcean is a cloud infrastructure provider that offers cloud computing resources and services to developers, startups, and businesses. Founded in 2011, DigitalOcean has become popular for its simplicity, developer-friendly features, and cost-effective pricing.
Note: This tutorial is all about Jenkins deployment using Terraform, if you are not familiar with Jenkins & Digital Ocean then head over to their docs for getting started.
Ready set go!🚀
Don't worry we will discuss everything in the below steps. Keep your hands ready on your keyboard and follow with me.
Installing Prerequisites
Install Digital Ocean CLI(Optional, works without installing)
Configure Digital Ocean API access🔐
- Create a Digital Ocean account if you don't have one
Note: If you haven't installed the Digital Ocean CLI(doctl) then head over here to install & authenticate it with a digital ocean account.
- Now generate API tokens from the digital ocean here. Please keep it safe since it is highly confidential, with this key one can do anything with your digital ocean account, deploy servers, instances, clusters, etc. Also, we will be using this to configure the digital ocean using Terraform.
Create Terraform Configuration file
All you have to do is create a Terraform directory(eg. terraform-demo) for the sake of this tutorial. You have to create terraform file in your project repository for configuring the terraform.
- Now, create main.tf (you can name anything) file. Add the following lines of code inside the file.
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}
This code is Terraform configuration which selects the providers and their source location and version. In this case, it's Digital Ocean.
source
: This attribute specifies the source address of the provider. In this case, it is set to"digitalocean/digitalocean"
which refers to the DigitalOcean provider available on the Terraform Registry.version
: This attribute defines the version constraint for the provider. In this case, it is set to"~> 2.0"
, which means Terraform will use any version of the DigitalOcean provider that is compatible with version 2.0. The tilde arrow (~>
) is a shorthand that allows for flexible versioning while ensuring that the major version remains the same.
Now append the following lines of code:
resource "digitalocean_droplet" "jenkins" {
image = "ubuntu-20-04-x64"
name = "jenkins-server"
region = "nyc3"
size = "s-1vcpu-1gb"
ssh_keys = [
var.ssh_key_fingerprint
]
user_data = <<-EOT
#!/bin/bash
apt-get update
apt-get install -y openjdk-11-jdk apt-transport-https wget
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | apt-key add -
sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
apt-get update
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
sudo apt-get install jenkins
systemctl start jenkins
systemctl enable jenkins
EOT
}
Here, we are using the Digital Ocean droplet for deploying our Jenkins server. Droplets are light Linux virtual machines(VMs) that run on virtualization hardware. For the sake of this tutorial we are using the Droplets, you can also use the Kubernetes cluster or compute engines for deployment, since droplets are better for small programs. Regarding Kubernetes/container deployment, we will have a separate article.
user_data = <<-EOT
#!/bin/bash
...
EOT
These are the bash commands to run your Jenkins instance in the digital ocean droplet, terraform allows the running of bash commands in their configuration file, which is what I like about Terraform.
Initialize Terraform and set variables
Firstly, let's get your PC ssh key(a pair(public & private) of cryptographic keys for a secure connection between client and server) for accessing the droplet instance. Run the below script to generate the public ssh key.
ssh-keygen
Now you will find your ssh key in the location "/Users/username/.ssh/id_rsa". Run the script to view the ssh key.
cat ~/.ssh/id_rsa
Now you have to copy your public ssh key and paste it into the digital ocean account by heading over to "/settings/security" and clicking on "Add SSH Key". Copy the fingerprint of your ssh.
Then, create a terraform.tfvars
file with your DigitalOcean API token and your SSH key fingerprint, which you just copied.
do_token = "your_digitalocean_api_token"
ssh_fingerprint = "your_ssh_key_fingerprint"
You will find your digital ocean API token in the API section.
Run
terraform init
to initialize your Terraform project.
Review your Terraform Plan📖
Congratulations🎉 You have successfully completed the coding part, now it's time to see the magic of Terraform 🪄. Till now you might have understood how simple and easy is terraform configuration language(HCL).
Let's continue...🏃🏻
Review and apply your Terraform plan. Copy the code below and paste into the terminal.
terraform plan
terraform apply
The Terraform configuration language is very simple and easy to understand.
terraform plan
- with this command, you can see all the changes that will occur with this terraform configurationterraform apply
- this will apply the terraform configuration
You will see below if everything works fine.
After the apply
command you will see the Jenkins IP address, if you don't see any IP address, it's possible that the bash commands didn't work properly 😔 but your droplet in Digital Ocean has been created.😃
Don't worry, follow the below steps to get your Jenkins IP address.
- Head over to your digital ocean dashboard and copy the IP address of the droplet💧
Open your terminal and try connecting the droplet using the command
ssh root@<ip address>
You will see result like this:
Now, you're inside your Jenkins-server droplet copy all the bash scripts and paste them into the terminal.
apt-get update apt-get install -y openjdk-11-jdk apt-transport-https wget wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | apt-key add - sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list' apt-get update 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 sudo apt-get install jenkins systemctl start jenkins systemctl enable jenkins
You will see this:
Great🔥 you have your Jenkins instance running in port 8080 inside the droplet.
Opening Jenkins in Browser🚀
Once you have your Jenkins instance running inside the droplet, you are ready to set up your Jenkins configuration. Follow the below steps to configure your Jenkins instance:
Open your Digital Ocean droplet and copy the public IPv4 address.
Open your browser and paste the IPv4 address with the port number of your Jenkins instance
<Your ipv4 address>:8080
, since Jenkins runs at port 8080 by default.When you configure Jenkins for the first time, you will have a default password for login which you will find in the location
/var/lib/jenkins/secrets/initialAdminPassword
. Copy the location and paste it into the terminal using thecat
command to view the password.cat /var/lib/jenkins/secrets/initialAdminPassword
Which may look like this:🎉
Note: The password is randomly generated and may vary from device to device, please do not copy others.
- Copy and paste the password into the password box and then you're ready to go.
You have successfully created a Jenkins instance in the Digital Ocean droplet using Terraform🎉🥳
P.S. If you want the whole code and files, click on this Github repo link.
Conclusion🚩
We've been making great progress together, haven't we? And truly, this journey of discovery and learning has been both challenging and rewarding, and we've achieved remarkable progress. Kudos to us all!👊
As we move forward, our next adventure involves Jenkins, a widely-used automation tool that's integral for setting up a Continuous Integration/Continuous Deployment (CI/CD) pipeline. We'll be diving deep into how we can connect Jenkins with GitHub, one of the most popular platforms for version control and collaboration. This is a fantastic chance for us to explore the nitty-gritty of real-world DevOps practices.
Moreover, we'll be stepping into an essential aspect of Jenkins - creating a Jenkinsfile. This is a configuration file that contains the definition of a Jenkins Pipeline and is checked into a project's source control repository. It's the heartbeat of any Jenkins setup, and understanding it is vital to getting the most out of this powerful tool🛠️
To make the most of our next session, it might be beneficial to brush up on some basic Git commands and familiarize yourself with the concept of version control if you haven't already. Also, a quick overview of Jenkins wouldn't hurt. There are plenty of resources available online that can aid you in this.
So gear up, because our next chapter promises to be an exciting one, full of hands-on learning and valuable insights. And remember, no question is too small or too big - feel free to reach out if you need any clarifications or have any ideas you'd like to share. After all, we're on this journey together!✌️
Looking forward to our next step in mastering Jenkins together. Until then, happy learning! ❤️❤️❤️