Nomad快速入门

Database and Ruby, Python, History


同事介绍了一个轻量型的容器集群解决方案,我快速在自己电脑上搭了一下,记录一下。

因为不能够用Docker desktop for Mac, 用的是Orbstack,因为issue#291,所以我只能够创建3个Linux Machine来实现。

orb -m debian就可以ssh进去debian这个虚拟机。

首先安装依赖包

apt update && apt install lsb-release wget curl gpg coreutils ca-certificates

加个宿主机的代理

拉镜像快点

alias proxy='export https_proxy=http://host.orb.internal:9800 http_proxy=http://host.orb.internal:9800 all_proxy=socks5://host.orb.internal:9800'
alias unproxy='unset all_proxy http_proxy https_proxy'

更新一下命令行

source ~/.bashrc
proxy

安装docker

for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

安装Nomad

wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install nomad

定义Nomad service

sudo touch /etc/systemd/system/nomad.service
[Unit]
Description=Nomad
Documentation=https://www.nomadproject.io/docs/
Wants=network-online.target
After=network-online.target

# When using Nomad with Consul it is not necessary to start Consul first. These
# lines start Consul before Nomad as an optimization to avoid Nomad logging
# that Consul is unavailable at startup.
#Wants=consul.service
#After=consul.service

[Service]

# Nomad server should be run as the nomad user. Nomad clients
# should be run as root
# User=nomad
# Group=nomad

ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/bin/nomad agent -config /etc/nomad.d
KillMode=process
KillSignal=SIGINT
LimitNOFILE=65536
LimitNPROC=infinity
Restart=on-failure
RestartSec=2

## Configure unit start rate limiting. Units which are started more than
## *burst* times within an *interval* time span are not permitted to start any
## more. Use `StartLimitIntervalSec` or `StartLimitInterval` (depending on
## systemd version) to configure the checking interval and `StartLimitBurst`
## to configure how many starts per interval are allowed. The values in the
## commented lines are defaults.

# StartLimitBurst = 5

## StartLimitIntervalSec is used for systemd versions >= 230
# StartLimitIntervalSec = 10s

## StartLimitInterval is used for systemd versions < 230
# StartLimitInterval = 10s

TasksMax=infinity
OOMScoreAdjust=-1000

[Install]
WantedBy=multi-user.target

启动Nomad

sudo systemctl enable nomad
sudo systemctl start nomad

命令行查看集群情况

nomad server members

Name           Address         Port  Status  Leader  Raft Version  Build  Datacenter  Region
debian.global  198.19.249.222  4648  alive   true    3             1.9.5  dc1         global
web.global     198.19.249.226  4648  alive   false   3             1.9.5  dc1         global
worker.global  198.19.249.141  4648  alive   false   3             1.9.5  dc1         global

也可以通过浏览器http://localhost:4646/ui/servers查看集群的情况。

Reference

  1. https://developer.hashicorp.com/nomad/tutorials/enterprise/production-deployment-guide-vm-with-consul