# Access nomad from within nomad

**URL:** <https://discuss.hashicorp.com/t/access-nomad-from-within-nomad/30026>\
**Category:** Nomad\
**Created:** [September 28, 2021, 9:41pm UTC](https://discuss.hashicorp.com/t/access-nomad-from-within-nomad/30026 "2021-09-28T21:41:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![joh4n](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/joh4n/32/22651_2.png) [@joh4n](https://discuss.hashicorp.com/u/joh4n)\
**Post date:** [September 28, 2021, 9:41pm UTC](https://discuss.hashicorp.com/t/access-nomad-from-within-nomad/30026/1 "2021-09-28T21:41:53Z")

</div>

I’m looking in to setting up a micro service that can create nomad jobs from within nomad. I can’t find any information of how one can set that up. Is there a minimal example of how to do it? Is it even possible?

Similar to how one can use docker if one mounts the docker socket in to the container, i.e. `docker run -it -v /var/run/docker.sock:/var/run/docker.sock ubuntu:latest sh -c "apt-get update ; apt-get install docker.io -y ; bash", and `rbac`and`ClusterRole` in k8s to be able to control the cluster from inside the cluster.

If the cluster has a public ip one can of course call it on that url, but it would be nice not requiring a public ip such one can use it for local development etc.

---

<div class="post-metadata">

**Author:** ![fhemberger](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/fhemberger/32/6262_2.png) [@fhemberger](https://discuss.hashicorp.com/u/fhemberger)\
**Post date:** [September 29, 2021, 9:04am UTC](https://discuss.hashicorp.com/t/access-nomad-from-within-nomad/30026/2 "2021-09-29T09:04:36Z")

</div>

Have you tried Nomad’s HTTP API yet?

> **[Jobs - HTTP API | Nomad by HashiCorp](https://www.nomadproject.io/api-docs/jobs#create-job)**
>
> The /jobs endpoints are used to query for and interact with jobs.

… or one of the SDKs?

> **[Libraries and SDKs - HTTP API | Nomad by HashiCorp](https://www.nomadproject.io/api-docs/libraries-and-sdks)**
>
> There are many third-party libraries for interacting with Nomad's HTTP API.
> This page lists the HashiCorp and community-maintained Nomad HTTP API client
> libraries.

And you **definitively** don’t want to expose such a service via public IP. 😉

---

<div class="post-metadata">

**Author:** ![DerekStrickland](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/derekstrickland/32/7715_2.png) [@DerekStrickland](https://discuss.hashicorp.com/u/DerekStrickland)\
**Post date:** [September 29, 2021, 12:40pm UTC](https://discuss.hashicorp.com/t/access-nomad-from-within-nomad/30026/3 "2021-09-29T12:40:38Z")

</div>

Hi @joh4n,

Thanks for using Nomad!

This has come up before. We’re currently exploring options for making this a first-class feature and where to prioritize it, but in the meantime, you could write your own job controller using the HTTP API. You should be able to inject the client IP using env { HOST\_IP = “${attr.unique.network.ip-address}” } and then access it using the HTTP API.

If you are programming in go, you can check out the `api` package in the main Nomad repository for a fully baked API Client implementation. Also, we have a new experimental [openapi](https://github.com/hashicorp/nomad-openapi) project that is under active development. It doesn’t cover all endpoints yet, but it has Jobs, and it has multiple language targets. If you don’t see your language of choice you can even add your own. Take a look at the `Makefile` and the `README` for instructions and examples of how to generate clients for other languages. Also, feel free to submit a PR if you do add another language or add coverage for other endpoints. We’re definitely looking for contributors.

I hope that helps.

Cheers,

@DerekStrickland and the Nomad Team

---

<div class="post-metadata">

**Author:** ![joh4n](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/joh4n/32/22651_2.png) [@joh4n](https://discuss.hashicorp.com/u/joh4n)\
**Post date:** [September 29, 2021, 7:00pm UTC](https://discuss.hashicorp.com/t/access-nomad-from-within-nomad/30026/4 "2021-09-29T19:00:02Z")

</div>

@DerekStrickland Thx that should work, I just could not figure out how to get that ip. I was thinking of using the HTTP API or [Home - python-nomad](https://python-nomad.readthedocs.io/en/latest/) for now, as I probably will do a quick setup of this in python for now.

Is there a way to monitor a nomad cluster for events, jobs completing/failing etc? I found [GitHub - seatgeek/nomad-firehose: Firehose all nomad job, allocation, nodes and evaluations changes to rabbitmq, kinesis or stdout](https://github.com/seatgeek/nomad-firehose) but it does not look to be under active development. Ideally using amqp such I can just consume it from rabbitmq and not have to ping and api every 10s.

@fhemberger The plan is to use the HTTP API or simmilar sdk. But my question is how to get that ip to call from within the cluster. That was the ip that could be public assuming one enables auth for nomad not the service itself. Sorry if that was unclear.

---

<div class="post-metadata">

**Author:** ![DerekStrickland](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/derekstrickland/32/7715_2.png) [@DerekStrickland](https://discuss.hashicorp.com/u/DerekStrickland)\
**Post date:** [September 30, 2021, 4:30pm UTC](https://discuss.hashicorp.com/t/access-nomad-from-within-nomad/30026/5 "2021-09-30T16:30:36Z")

</div>

@joh4n

There is a way! Check out the [Events API](https://www.nomadproject.io/api-docs/events). Also, [this project](https://github.com/hashicorp/damon) has some example code for how to use it.

- Derek

---

<div class="post-metadata">

**Author:** ![jrxFive](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jrxfive/32/22759_2.png) [@jrxFive](https://discuss.hashicorp.com/u/jrxFive)\
**Post date:** [October 1, 2021, 9:16pm UTC](https://discuss.hashicorp.com/t/access-nomad-from-within-nomad/30026/6 "2021-10-01T21:16:56Z")

</div>

Hi @joh4n, I’m the creator of python-nomad and also a maintainer. While it took us a bit to add event stream support. You can now in the 1.4.1 version create a listener against the event stream to send results to a more established message broker/pub sub. It’s not exactly like firehose but give you the capability to integrate into your stack upon your discretion.

With Hashicorp adding an official openapi specification I imagine this will be the way to to go to generate client libraries once it has full compatibility with all of Nomads routes. python-nomad also doesn’t have 100% route support as well, but hoping to bridge that gap till the openapi spec is.

> <https://github.com/jrxFive/python-nomad/blob/master/docs/api/event.md>

---

<div class="post-metadata">

**Author:** ![joh4n](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/joh4n/32/22651_2.png) [@joh4n](https://discuss.hashicorp.com/u/joh4n)\
**Post date:** [October 3, 2021, 7:50pm UTC](https://discuss.hashicorp.com/t/access-nomad-from-within-nomad/30026/7 "2021-10-03T19:50:25Z")

</div>

The events api seems like exactly what I was looking for 🙂 I should probably read trough the `https://www.nomadproject.io/api-doc` I have just been reading on [Documentation | Nomad by HashiCorp](https://www.nomadproject.io/docs)

---

<div class="post-metadata">

**Author:** ![mr-karan](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/mr-karan/32/22763_2.png) [@mr-karan](https://discuss.hashicorp.com/u/mr-karan)\
**Post date:** [November 10, 2021, 3:36pm UTC](https://discuss.hashicorp.com/t/access-nomad-from-within-nomad/30026/8 "2021-11-10T15:36:54Z")

</div>

I think Advertise Address is better suited here:

```auto
      env {
        // Ensure this interface is where Nomad advertises it's HTTP port.
        NOMAD_ADDR = "${attr.nomad.advertise.address}"
      }

```

I was running a Nomad agent in `dev` mode locally on localhost, so `attr.unique.network.ip-address` was coming as `127.0.0.1`.

Running with `-bind 0.0.0.0` or explicitly setting advertise address in config and reading that value should make the HTTP API accessible.
