Hello World

By | 2022년 8월 24일
Table of Contents

Hello World

Job 을 테스트하고,
Load Balancing 의 health check point 를 제공하기 위해,
hello_world job 을 생성합니다.

vi hello_world.nomad
-----------------------------
job "hello_world" {
  datacenters = ["dc1"]

  group "hello_world" {
    count = 1

    network {
      port "http" { to = 80 }
    }

    task "hello_world" {
      driver = "docker"

      config {
        image = "nginxdemos/hello"
        ports = ["http"]

        auth_soft_fail = true
      }

      resources {
        cpu    = 500
        memory = 128
      }

      service {
        name = "hello-world"
        port = "http"

        check {
          type     = "http"
          path     = "/health"
          interval = "10s"
          timeout  = "2s"
        }
      }
    }
  }
}
-----------------------------

haproxy 연동

  • 고정 포트 할당 :

    port "hello_world" { static = 2390 }

  • frontend 설정 :

    NOMAD_PORT_포트명

  • backend 설정 :

    _hello-world._tcp.service.consul 에서 hello-world 가 job 서비스명
    service { name = "hello-world" }

job "haproxy" {
  datacenters = ["dc1"]
  type = "system"         # 모든 노드에 자동설치

  group "haproxy" {
    count = 1

    network {
      port "haproxy_ui" {
        static = 4936
      }

      port "prometheus_ui" {
        static = 9090
      }

      port "hello_world" {
        static = 2390
      }

      port "haproxy_exporter" {}
    }

    task "haproxy" {
      driver = "docker"

      config {
        image = "haproxy:2.4.4"
        ports = ["haproxy_ui"]

        auth_soft_fail = true

        # host 로 설정했으므로 127.0.0.1 는 호스트를 가르킨다.
        network_mode = "host"

        volumes = [
          "local/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg",
          "/ssl/:/ssl/",
        ]
      }

      template {
        data = <<EOF
global
   maxconn 8192
defaults
   mode http
   timeout client 10s
   timeout connect 5s
   timeout server 10s
   timeout http-request 10s
   option forwardfor

frontend stats
   bind *:{{ env "NOMAD_PORT_haproxy_ui" }}
   stats uri /
   stats show-legends
   no log

frontend prometheus_ui_front
   bind *:{{ env "NOMAD_PORT_prometheus_ui" }}
   default_backend prometheus_ui_back

backend prometheus_ui_back
   balance roundrobin
   server-template prometheus_ui 5 _prometheus._tcp.service.consul resolvers consul resolve-opts allow-dup-ip resolve-prefer ipv4 check

frontend hello_world_front
   bind *:{{ env "NOMAD_PORT_hello_world" }}
   default_backend hello_world_back

backend hello_world_back
   balance roundrobin
   server-template hello_world 5 _hello-world._tcp.service.consul resolvers consul resolve-opts allow-dup-ip resolve-prefer ipv4 check

resolvers consul
   nameserver consul 127.0.0.1:8600
   accepted_payload_size 8192
   hold valid 5s
EOF

        destination   = "local/haproxy.cfg"
        change_mode   = "signal"
        change_signal = "SIGUSR1"
      }

      resources {
        cpu    = 500
        memory = 128
      }

      service {
        name = "haproxy-ui"
        port = "haproxy_ui"

        check {
          type     = "http"
          path     = "/"
          interval = "10s"
          timeout  = "2s"
        }
      }
    }

    task "haproxy-exporter" {
      driver = "docker"

      lifecycle {
        hook    = "prestart"
        sidecar = true
      }

      config {
        image = "prom/haproxy-exporter:v0.10.0"
        ports = ["haproxy_exporter"]

        network_mode = "host"
        auth_soft_fail = true

        args = [
          "--web.listen-address",
          ":${NOMAD_PORT_haproxy_exporter}",
          "--haproxy.scrape-uri",
          "http://${NOMAD_ADDR_haproxy_ui}/?stats;csv",
        ]
      }

      resources {
        cpu    = 100
        memory = 32
      }

      service {
        name = "haproxy-exporter"
        port = "haproxy_exporter"

        check {
          type     = "http"
          path     = "/metrics"
          interval = "10s"
          timeout  = "2s"
        }
      }
    }
  }
}

http://<노마드 클라이언트 아이피>:2390/ 에 접속하면,
hello_world 가 실행되고 있는 것을 볼 수 있습니다.

답글 남기기