How to run the binary job in HashiCrop nomad

nomad
Reading Time: 4 minutes

Prerequisite

  • Install Hashicrop Nomad
  • Nomad must be running

What is the job in HashiCrop nomad

Job is the declarative way to define the task that the nomad has to run. Job defines the schema for nomad jobs. To write jobs we have to use HashiCorp Configuration Language which tries to balance between human-readable and editable, and machine-friendly.

Let’s understand the basic hierarchy of job

job
  \_group
       \_task

Every job file starts with a job keyword where we specify the name of the job and each job file has only a single. We can have more the one group for example Frontend and Backend and Databases. Similarly, we can have multiple tasks in one group.

We are going to use the http-echo server which is a small go binary for this demonstration. You can use any binary step that is going to the same. Let’s download and compile.

Download and Compile http-echo sever

We need to clone this git repository https://github.com/hashicorp/http-echo.git

git clone https://github.com/hashicorp/http-echo.git 
job in HashiCrop nomad

Now we need to build this code. First, make sure you have Go in the system. To check whether Go is installed or not. If you don’t have Go u can install it.

sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt update
sudo apt install golang-go

Check installation is successful or not.

go version
go version

Now we can build our code using a simple command. Let’s look at the following picture there is no binary right now.

NO binary in code

Now run the following command to build this code.

go build 
binary build

After running go build we can see there is a new file added. We need to move this binary to the bin folder.

mv http-echo /bin/

We can test now whether this is binary or not. This binary two-parameter -listen for a port like in which port you want to listen and -text any text you want to see on the server.

-listen=:9090 -text="Hello from knoldus "

You can simply run this command on your terminal and hit your browser.

http-echo -listen=:9090 -text="Hello from knoldus "

Run the binary job in HashiCrop nomad

Up your nomad cluster and run the following job file

job "binary" {   
  datacenters = ["dc1"]

 
  group "binary-example" {
    network {
      port "http" {
        static = "9090"
      }
    }

    task "server" {
      driver = "exec"

      config {
        command = "/bin/http-echo"

        args = [
          "-listen",
          ":9090",
          "-text",
          "Hello",
        ]
      }
    }
  }
}

Firstly we have to define job “binary” as the name of the job. We also need to define datacenter without a datacenter our job will throw an error and by default, dc1 is a datacenter. then we have a group we can have multiple groups within one job file we need to name the group. I defined network static it will run on my localhost:9090.

The “task” stanza creates an individual unit of work, such as a Docker container, web application, or batch processing. we can have more than one task within a group. we also specify the drivers could be docker, exec, or java, based on your requirement. we are using exec just to execute the http-echo server binary.

The “config” stanza specifies the driver configuration, which is passed directly to the driver to start the task. The details of configurations are specific to each drive.

Run job

example: nomad run jobfilename.hcl or jobfilename.nomad

nomad run ex1.hcl
binary deployemnet

Hit http://localhost:9090/ you will see “hello “

localhost:9090 msg

We are able to orchestrate our binary file without dockerized it. Let’s try to scale up binary

Scale up the binary job

job "binary1" {
  datacenters = ["dc1"]

 
  group "example" {
    count = 3
    network {
      port "http" {}
    }

    task "server" {
      driver = "exec"

      config {
        command = "/bin/http-echo"

        args = [
          "-listen",
          ":${NOMAD_PORT_http}",
          "-text",
          "hello and welcome to ${NOMAD_IP_http} running on port ${NOMAD_PORT_http} ",
        ]
      }
    }
  }
}

The above code is similar to the previous one, we just added the count = 3 we will have 3 replicas of the same binary. we removed static from networks because we want dynamic IP for each binary

nomad run binary.hcl
succesfuly deploy our job

Our status Desired 3 number of replicas we want is 3 and desired is 3. status is successful.

scale 3

To get the dynamic IP of every binary just click on any of them in the center.

In the center down we can see the IP. if we hit on the IP address we are able to see the output.

dyanmic ip

Conclusion

In this blog, we have covered how to orchestrate a binary without dockerized it. we have seen the job and how It works. if you want to install and learn what is nomad do click on this link. https://blog.knoldus.com/what-is-hashicorp-nomad/

Written by 

Shivam Pateriya is a DevOps Engineer at Knoldus. He likes to learn about emerging technologies. His keen interest in Python, Cloud, and Automation.