Nomad Tutorials Part-2: Creating a Nomad job

4 min to read.

Welcome to the second part of our Nomad tutorial series! In this post, "Nomad Tutorials Part-2: Creating a Nomad Job", we continue our journey into the fascinating world of HashiCorp Nomad orchestration. After focussing on installing Nomad on an Ubuntu server in the first part, we will now dive deeper into the practical application. Nomad not only enables the efficient management of containers, but also makes it possible to orchestrate complex workloads across clusters. In this tutorial, we will learn how to create a Nomad job to launch and manage applications. Step by step, we will walk through the processes to make sure you understand the basics of Nomad job definitions and are able to apply them in your own environment. Let's discover together how Nomad can simplify the orchestration of your applications!

Preparation of the project folder

Before we start creating our Nomad job, a clean project structure is crucial. To do this, open Visual Studio Code and create a new folder for your Nomad project. In this folder, we will create a subfolder called "Certificates" to manage the authentication of Nomad. If you did not create any certificates in the first part, you can skip this or do it again here.

bash
# Create the main folder for the Nomad project
mkdir NomadProjekt

# Navigate to the project folder
cd NomadProjekt

# Create the folder for the certificates
mkdir Zertifikate

Inside the "NomadProject" folder, create a file called nomad.env where we will store our environment variables. This file will be used to load environment variables through the source command. Open the file with your favourite text editor and insert the relevant environment variables:

bash
export NOMAD_ADDR=<http/https-Adresse des Servers>:4646
export NOMAD_CACERT=/pfad/zu/Zertifikate/nomad-agent-ca.pem
export NOMAD_CLIENT_CERT=/pfad/zu/Zertifikate/global-server-nomad.pem
export NOMAD_CLIENT_KEY=/pfad/zu/Zertifikate/global-server-nomad-key.pem

Replace /path/to/certificates/ and the Nomad address with the actual values. These environment variables are crucial for executing Nomad commands and interacting with the Nomad cluster.
With these preparations, your project folder is now ready to create and manage Nomad jobs!
To enable Nomad to use the values, execute the following command on the console.

bash
source nomad.env

Structures in a job

This example code creates a service that returns "Hello World!" when addressed via port 8080. Save the job as demo-job.nomad.

hcl
job "http-echo" {
  group "echo" {
    network {
      port "http" {
        static = 8080
      }
    }

    task "server" {
      driver = "docker"

      config {
        image = "hashicorp/http-echo:latest"
        args  = [
          "-listen", ":8080",
          "-text", "Hello World!",
        ]
      }
    }
  }
}

In HashiCorp Nomad, a "job" represents an abstract unit that describes a group of tasks to be executed on the Nomad cluster. A job is therefore an orchestration unit that defines one or more applications, services or processes to be executed on the distributed cluster. In the code excerpt above, this is the "http-echo" job.

Groups in Nomad

The next sub-organisation unit in the job is the "echo" group.
In Nomad, a "group" represents a collection of tasks that are deployed and scaled together. For example, on the same server. Groups make it possible to define multiple instances of the same application or service that are managed and monitored together. In the code example above, for example, the communication settings are also made here, such as which ports should be released. Each group within a Nomad job defines an isolated environment for the tasks it contains and their resource requirements. Groups are particularly useful when it comes to ensuring scalability, load balancing and high availability.

Tasks in Nomad

The "server" task is located under the group.
A "task" in Nomad represents a single instance of an application, service or process within a group. Each task is started and executed on a separate Nomad agent in the cluster. Tasks are the smallest executable units in a Nomad job and can be configured as containers, processes or other forms of executable applications. Nomad takes care of the efficient distribution of tasks across the cluster and ensures that the defined resource requirements are met.

Start a job

We can start a job with the command nomad run. To do this, execute the following:

bash
nomad run demo-job.nomad

Now you should see something like this in the output:

bash
==> 2023-12-10T15:57:46+01:00: Monitoring evaluation "1cd4aa7d"
    2023-12-10T15:57:46+01:00: Evaluation triggered by job "http-echo"
    2023-12-10T15:57:46+01:00: Evaluation within deployment: "90e82e55"
    2023-12-10T15:57:46+01:00: Allocation "c99ab72a" created: node "07d3e6d8", group "echo"
    2023-12-10T15:57:46+01:00: Evaluation status changed: "pending" -> "complete"
==> 2023-12-10T15:57:46+01:00: Evaluation "1cd4aa7d" finished with status "complete"
==> 2023-12-10T15:57:46+01:00: Monitoring deployment "90e82e55"
  â ¦ Deployment "90e82e55" in progress...
    
    2023-12-10T15:57:47+01:00
    ID          = 90e82e55
    Job ID      = http-echo
    Job Version = 0
    Status      = running
    Description = Deployment is running
    
    Deployed
    Task Group    Desired  Placed  Healthy  Unhealthy  Progress Deadline
    echo  1        1       0        0          2023-12-10T16:07:46+01:00

As soon as the service has been successfully started, it can be called via the :8080 in the web browser.

Congratulations, you have rolled out your first application, in the next part we will deal with services and service discorvery in Nomad.