使用nomad管理集群-2 tailscale组网

发布时间 2023-10-08 21:13:15作者: velor2012

背景

上一篇文章介绍了如何单机部署nomad,这一篇介绍如何使用tailscale组成大内网,并使用nomad管理该内网集群

TAILSCALE介绍

下面是引用知乎的原话

Tailscale 是一种基于 WireGuard 的虚拟组网工具,它能帮助我们把安装了 Tailscale 服务的机器,都放到同一个局域网内,即公司或者家里的 PC 机器连到同一网络,甚至云服务器都能放到同一个局域网。

如果用过zerotier,n2n或者openvpn,应该就能明白什么是虚拟局域网,tailscale的功能就是组一个局域网。

除此之外,tailscale有一个叫magic DNS的东西。一般来说我们需要通过ip去访问一个远程机器,但是tailscale可以直接通过一个hostname去访问,非常方便,具体看下面教程。

部署过程

  1. 登陆tailscale官网,生成一个token用于后续在vps中登陆账号

    1379887140

    token类似下面这种形式

    tskey-auth-kE9MUB4CNTRL-cxxxx
    

    最后在dns中打开magic dns

    120002058

  2. 修改hostname(可选)

    修改/etc/hostname中的名称即可

    然后重启vps

  3. 运行以下命令安装tailscale

    sudo apt-get update && sudo apt-get install wget gpg coreutils curl
    curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/focal.noarmor.gpg | sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null
    curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/focal.tailscale-keyring.list | sudo tee /etc/apt/sources.list.d/tailscale.list
    sudo apt-get update
    sudo apt-get install tailscal
    
  4. 登陆,用刚刚生成的token替换下面的tailscaleAuthkey 那部分

    tailscale login --auth-key "{{tailscaleAuthkey}}"
    
  5. 启动tailscale

    tailscale up
    
  6. 管理面板查看是否显示机器信息

    machine name默认是vps的hostname,也可以自行在管理面板修改,这个名字需要记住

  7. 在vps中ping当前vps的machine name。可以ping通就ok

  8. 在其他机器上(比如自己的本地电脑)也搭建tailscale

修改nomad的配置文件

修改/etc/nomad.d/nomad.hcl

# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0

# Full configuration options can be found at https://www.nomadproject.io/docs/configuration

data_dir  = "/opt/nomad/data"
bind_addr = "{{hostname}}"
name = "{{hostname}}"
datacenter="other"
region="1"
server {
  # license_path is required for Nomad Enterprise as of Nomad v1.1.1+
  #license_path = "/etc/nomad.d/license.hclic"
  enabled          = "false"
  #bootstrap_expect = 3
}

client {
  enabled = "true"
  servers = ["{{hostname}}"]
  network_interface="tailscale0"
}

其中name可以随便取,其余"{{hostname}}"填tailscale的machine name。如果client中需要配置多个server,则在后面接着填他们的名字即可。然后重启nomad

clients中的address都会变成tailscale分配的局域网地址,此时外网是访问不了nomad管理界面的,变成了纯内网集群。此时再打开防火墙,允许ssh以及tailscale网卡的流量就可以保证集群的安全了。