HashiCorp Nomad Introduction
Tags: nomad, orchestrator, container
Published: 2020-04-01 11:42:42

### Oh, yeah???
{% twitter 1172961505144377350 %}

P/s: I'm not telling to use [HashiCorpNomad](https://nomadproject.io) and abandoned K8S, but at least, take a look at it, you may think God is not abandoned you, no more!

## TL;DR
- Nomad is the project from HashiCorp, a company that help me, and a lot of other guys, working with Cloud more easily.
- It was a workload orchestrator.
- It support container application, legacy application, Java, Batch Job, QEMU.
- It's easy to operate and maintain.
- It had less magic than k8s, actually, no magics.
- It's may be what you want from the very beginning but other guys just bark about K8S and told you to use K8S.


## Use cases
- You're working with legacy system, which is not ready to one-step move to Cloud.
- You're not familiar with Orchestrator and Containerized application.
- You're not familiar with Cloud, or you don't want to learn K8S.
- You cannot use K8S because you don't have an OPs team, and your country don't have a K8S provider which you trusted.
- You want to try out something differences than K8S.

## Facts
In facts, some company combine K8S and Nomad together to handle their workload. Learn about it [here](https://github.com/kelseyhightower/nomad-on-kubernetes)

### Terms

#### Nomad Server
This one will operate and checking the state of system. For example:
"Is this app in state that user defined?" If not, reschedule it!
"I need 1GB of RAM for my app, can you handle it?" Agent-X have enough memory, Ok, I will run your app on it!

To start a nomad server:
`nomad agent -server -bind=0.0.0.0 -data-dir=/tmp/nomad-server`

#### Nomad Agent
This one will listen to Server request and reporting the state of application to Server, if the state of the application is abnormal, Server will request Agent to stop/start/restart the application.

To start a nomad agent:
`nomad agent -agent -data-dir=/tmp/nomad-agent`

#### *Notes*:
On local machine, I usually use `nomad agent -dev` to bootstrap nomad cluster. Think about K8S 🙃

#### Job
Is the configuration which is defined the `expected state`, which you want your app into.

A job file usually have following structure:
```
job
  \_ group
        \_ task
```

Example:
{% gist https://gist.github.com/liemle3893/fcb67882cfba3a7739c9ade4bd73453a %}

#### Group
Is a collection of application which will be scheduled on the same Agent.
Application in the same group can be shared volume and network (using Consul Connect).
*Pro tips*: Using `group` as your unit of scaling.
*Bonus* For people come from K8S, using group as your `Pod` will be good enough. Combine group with difference type of Job will provide things similar to Deployment, DaemonSet.
Example:
{% gist https://gist.github.com/liemle3893/40c6b887c7dce541ab031eed5b6855b5 %}
#
#### Task
Is a unit of work which was your application. Say container, binary, Java app.
*Pro tips*: Naming your task follow your job will help you debug and managing/monitoring application more easily and friendly. Eg: Your job name is "example", then your nginx should named "example-nginx".
Example:
{% gist https://gist.github.com/liemle3893/94ce497603ee623d0fd6212a7b56c1ad %}

### Service
*Note*: This is a bit out of Nomad, becuz you will need Consul to let this work. But, who use Nomad without Consul?
After you start your application, say, an API and you want to populate its address to other services over Consul, you can use this stanza instead of self register inside your code.
Example:
{% gist https://gist.github.com/liemle3893/0c8e3cc8dffde3255e72863e90838f1a %}

### Conclusion:
That enough for the introduction. As a developer, you can get use of Nomad right away.
In the next post, I will show you how to create a website using Docker, Traefik and Nomad.

### For people who dont have patience. Come [here](https://github.com/liemle3893/nomad-getting-started) with a full source code how to deploy a web using Nomad, Traefik and Docker as well as how to bootstrap Nomad Cluster using Ansible.
https://github.com/liemle3893/nomad-getting-started