使用ansible安装Nomad使用教程


原文链接: 使用ansible安装Nomad使用教程

Nomad 简介

Nomad是一个管理物理机器集群,并在集群上运行应用程序的工具。

Nomad的特点:

  1. 支持docker,Nomad的job可以使用docker驱动将应用部署到集群中。
  2. 支持多数据中心,可以跨数据中心调度。
  3. 分布式高可用,支持多种驱动程序(Docker、VMS、Java)运行job,支持多种系统(Linux、Windows、BSD、OSX)。
    Nomad安装在linux中仅需单一的二进制文件,不需要其他服务协调,Nomad将资源管理器和调度程序的功能集成到一个系统中。

    利用Ansible 进行安装

  4. 下载ansible的nomad role
    ansible-galaxy install brianshumate.nomad
    执行完后存放路径: /root/.ansible/roles/brianshumate.nomad

  5. 检查 nomad role 是否有问题
    ANSIBLE_ROLES_PATH=..
    ansible-playbook -i tests/inventory tests/test.yml --syntax-check

  6. inventory
    vi hosts
    ```ini
    [nomad_instances]
    l19 nomad_node_role=both
    l18 nomad_node_role=both
    l17 nomad_node_role=server
    l16 nomad_node_role=client
    l15 nomad_node_role=client
    l14 nomad_node_role=client
    [all:vars]
    nomad_bind_address="0.0.0.0"

File: vagrant_hosts

nomad cluster node hosts configuration for Vagrant

#

NB: Replace the hosts below with your preferred node hostnames and continue

the 'nodeN' pattern for additional nodes past 'nomad3'

Do not modify the labels (text appearing between []), however

[consul_instances]
l19 consul_iface=eth0 consul_node_role=bootstrap
l18 consul_iface=eth0 consul_node_role=server
l17 consul_iface=eth0 consul_node_role=server
l16 consul_iface=eth0 consul_node_role=client

[consul_instances:vars]
consul_data_path="/docker/consul/"
consul_debug=yes
[nomad_instances]
l19 nomad_node_role=both
l18 nomad_node_role=both
l17 nomad_node_role=server
l16 nomad_node_role=client
l15 nomad_node_role=client
l14 nomad_node_role=client
[nomad_instances:vars]
nomad_options={'driver.raw_exec.enable': '1'}

[all:vars]
nomad_bind_address="0.0.0.0"
nomad_docker_enable=yes
nomad_consul_address="10.114.0.17:8500"
nomad_options={"driver.raw_exec.enable":"1","driver.java.enable":"1"}


2. site.yml
```yml
---
# File: site.yml - Example nomad site playbook
- name: Installing Nomad
  hosts: nomad_instances
  become: yes
  become_user: root
  roles:
    - { role: brianshumate.nomad }

  tasks:
    - name: Start nomad
      service: name=nomad state=started enabled=yes

constraint

https://www.nomadproject.io/docs/job-specification/constraint.html

清理 nomad 数据

nomad stop -purge

deleteNomad() {
  job="$1"
  echo "deleting $job..."
  stringarray=($job)
  jobID=${stringarray[0]}
  echo $jobID
  nomad stop -purge $jobID
}

nomad status > ./stat.txt

input="./stat.txt"
while IFS= read -r var
do
  deleteNomad "$var";
done < "$input"


rm -r ./stat.txt

java application

# task for Java Application
# 
# Download this file: 
# curl -sSL https://gist.githubusercontent.com/ryanpadilha/5854213edbad8a9698c72c8a6dc66e24/raw/ -o nomad-operation-for-java-app.hcl
#
# Operation for job execution:
#
# validate job      - $ nomad validate nomad-operation-for-java-app.hcl
# plan job          - $ nomad plan nomad-operation-for-java-app.hcl
# execution plan    - $ nomad run -check-index 55 nomad-operation-for-java-app.hcl
# execution status  - $ nomad status vulcano-health-check-monitor
# verify error exec - $ nomad alloc-status -verbose 5f33b395-d370-7ebb-851c-287811143041 
# 

job "vulcano-health-check-monitor" {
  datacenters = ["dc1"] # "us-west-1", "us-east-1"
  type = "service"
  
  group "java" {
    count = 1
    
    task "java-application" {
      driver = "java"
      user = "nomad"

      config {
        jar_path = "${NOMAD_META_AWS_PATH}/lib/vulcano-health-check-monitor-${NOMAD_META_APP_VERSION}.jar"
        jvm_options = ["-Xms64m", "-Xmx256m",
          "-Dspring.config.location=${NOMAD_META_AWS_CONFIG}/application-prd-health-monitor.properties"]
      }

      # TODO verify register this on Consul by service element
      # service { }
    
      # artifact used to ship the jar to run
      # download from http, https, git, hg, s3 - automatic unarchived
      artifact {
        source = "https://s3-sa-east-1.amazonaws.com/nomad-artifact/vulcano-health-check-monitor-0.0.1-dev.tar.gz"
        destination = "/var/wplex/apps"
        options {
          aws_access_key_id = "AKIAI2ZEQ7JXLSS3UW7Q"
          aws_access_key_secret = "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"
          # aws_access_token = "<token>"
        }
      }
    
      meta {
        AWS_PATH = "/var/wplex/apps/vulcano-health-check-monitor"
        AWS_CONFIG = "/var/wplex/apps/config"
        APP_VERSION = "0.0.1"
      }
    }
  }

  # constraints defined after task is placed on a node
  constraint {
    attribute = "${attr.kernel.name}"
    value = "linux"
  }
  
  constraint {
    attribute = "${attr.cpu.arch}"
    value     = "amd64"
  }

  #constraint {
  #  attribute = "${attr.platform.aws.instance-type}"
  #  value     = "m4.xlarge"
  #}
  
  constraint {
      attribute = "${attr.driver.java.version}"
      operator  = ">="
      value     = "1.8.0"
  }
}
`