Skip to main content

Nomad Setup

Step-by-Step Guide

  1. Install Nomad on your EC2 Instance: Follow the official Nomad installation guide to install Nomad on your EC2 instance.

  2. Nomad Agent Configuration: Ensure the Nomad agent is configured properly. Create a nomad.hcl configuration file. Here’s an example of a basic configuration:

    data_dir  = "/opt/nomad/data"
    bind_addr = "0.0.0.0"

    server {
    enabled = true
    bootstrap_expect = 1
    }

    client {
    enabled = true
    network_interface = "eth0"
    }

    advertise {
    http = "18.130.193.213:4646"
    rpc = "18.130.193.213:4647"
    serf = "18.130.193.213:4648"
    }
  3. Create a Nomad Job Specification: Create a Nomad job file (job.nomad) to define how the Docker image will be deployed. Here’s an example based on your setup:

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

    group "web" {
    count = 1

    network {
    port "http" {
    static = 13012
    }
    }

    task "docserver" {
    driver = "docker"

    config {
    image = "cajameshewitt/doc-server:dev"
    ports = ["http"]
    }

    env {
    LOCATION = "DEV"
    PORT = "80"
    }

    resources {
    cpu = 500
    memory = 512
    }
    }
    }
    }
  4. Deploy with Nomad: Use the Nomad CLI to deploy the job. First, start the Nomad agent:

    nomad agent -config=nomad.hcl

    Then, run the job file:

    nomad run job.nomad
  5. Continuous Deployment with GitHub Actions: Update your GitHub Actions workflow to deploy to Nomad after pushing the Docker image to Docker Hub. Here's an example of how you can update your .github/workflows/deploy.yml file:

    name: CI/CD Pipeline

    on:
    push:
    branches:
    - main

    jobs:
    build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
    uses: actions/checkout@v2

    - name: Set up Docker Buildx
    uses: docker/setup-buildx-action@v1

    - name: Login to Docker Hub
    uses: docker/login-action@v1
    with:
    username: ${{ secrets.DOCKER_USERNAME }}
    password: ${{ secrets.DOCKER_PASSWORD }}

    - name: Build and push Docker image
    run: |
    docker build --tag cajameshewitt/doc-server:dev .
    docker push cajameshewitt/doc-server:dev

    deploy:
    needs: build
    runs-on: ubuntu-latest

    steps:
    - name: Deploy to Nomad
    uses: hashicorp/setup-nomad@v1
    with:
    version: 1.1.4

    - name: Apply Nomad job
    run: nomad run job.nomad
    env:
    NOMAD_ADDR: http://your-ec2-instance-public-ip:4646

Additional Information Required

  • Security and Access: Ensure your EC2 instance security group allows Nomad communication ports (4646, 4647, 4648) and that your GitHub Actions runner can access your EC2 instance.
  • Environment Variables and Secrets: Store sensitive information like Docker Hub credentials and Nomad API tokens in GitHub Secrets for secure access.
  • Monitoring and Logging: Implement logging and monitoring for your Nomad jobs to track deployments and troubleshoot issues.