Bpazy / blog

我的博客,欢迎关注和讨论
https://github.com/Bpazy/blog/issues
MIT License
39 stars 2 forks source link

使用 Netmaker 管理 WireGuard #213

Open Bpazy opened 2 years ago

Bpazy commented 2 years ago

https://github.com/gravitl/netmaker

Bpazy commented 2 years ago

安装 netmaker 服务端

采用 Docker 安装,这里我使用了 docker compose,首先创建配置文件:

cd ~
mkdir netmaker && cd netmaker
cat << EOF > docker-compose.yaml
version: "3.4"

services:
  netmaker:
    container_name: netmaker
    image: gravitl/netmaker:v0.9.2
    volumes:
      - ./dnsconfig:/root/config/dnsconfig
      - /usr/bin/wg:/usr/bin/wg
      - ./sqldata:/root/data
    cap_add: 
      - NET_ADMIN
    restart: always
    privileged: true
    environment:
      SERVER_HOST: "SERVER_PUBLIC_IP"
      SERVER_API_CONN_STRING: "api.NETMAKER_BASE_DOMAIN:443"
      SERVER_GRPC_CONN_STRING: "grpc.NETMAKER_BASE_DOMAIN:443"
      COREDNS_ADDR: "SERVER_PUBLIC_IP"
      GRPC_SSL: "on"
      DNS_MODE: "on"
      SERVER_HTTP_HOST: "api.NETMAKER_BASE_DOMAIN"
      SERVER_GRPC_HOST: "grpc.NETMAKER_BASE_DOMAIN"
      API_PORT: "8081"
      GRPC_PORT: "50051"
      CLIENT_MODE: "on"
      MASTER_KEY: "REPLACE_MASTER_KEY"
      SERVER_GRPC_WIREGUARD: "off"
      CORS_ALLOWED_ORIGIN: "*"
      DATABASE: "sqlite"
      DISPLAY_KEYS: "on"
      NODE_ID: "netmaker-server-1"
    network_mode: host
  netmaker-ui:
    container_name: netmaker-ui
    depends_on:
      - netmaker
    image: gravitl/netmaker-ui:v0.9.2
    links:
      - "netmaker:api"
    ports:
      - "8082:80"
    environment:
      BACKEND_URL: "https://api.NETMAKER_BASE_DOMAIN"
    restart: always
  coredns:
    depends_on:
      - netmaker 
    image: coredns/coredns
    command: -conf /root/dnsconfig/Corefile
    container_name: coredns
    restart: always
    ports:
      - "COREDNS_IP:53:53/udp"
      - "COREDNS_IP:53:53/tcp"
    volumes:
      - ./dnsconfig:/root/dnsconfig
EOF

配置文件参考了官方的: https://github.com/gravitl/netmaker/blob/master/compose/docker-compose.yml, 在此基础上将 volumns 映射到了本地。

然后自定义配置文件中的域名、IP、DNS 等配置:

sed -i 's/NETMAKER_BASE_DOMAIN/<your base domain>/g' docker-compose.yml
sed -i 's/SERVER_PUBLIC_IP/<your server ip>/g' docker-compose.yml
sed -i 's/COREDNS_IP/<default interface ip>/g' docker-compose.yml

然后启动: sudo docker-compose up -d 如果遇到了 53 端口无法绑定的问题,尝试替换 53 为 533 端口

Bpazy commented 2 years ago

配置 Nginx SSL 证书和 GRPC 转发

接上条,netmaker 服务端目前仍无法使用,需要配置 nginx 将页面请求、GRPC 请求转发到上一步启动的几个后端上。

nginx 详细配置如下,酌情修改:

server {
    listen 443 ssl;

    server_name dashboard.netmaker.example.com;

    ssl_certificate     /etc/nginx/ssl/netmaker.fullchain.cer;
    ssl_certificate_key /etc/nginx/ssl/netmaker.example.com.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    location / {
        proxy_pass http://127.0.0.1:8082/;
    }
}

server {
    listen 443 ssl;

    server_name api.netmaker.example.com;

    ssl_certificate     /etc/nginx/ssl/netmaker.fullchain.cer;
    ssl_certificate_key /etc/nginx/ssl/netmaker.example.com.key;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    location / {
        proxy_pass http://127.0.0.1:8081/;
        proxy_set_header                Host api.inetmaker.example.com;
        proxy_pass_request_headers      on;
    }
}

server {
    listen 443 ssl http2;

    server_name grpc.netmaker.example.com;

    ssl_certificate     /etc/nginx/ssl/netmaker.fullchain.cer;
    ssl_certificate_key /etc/nginx/ssl/netmaker.example.com.key;
    # ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    # ssl_ciphers         HIGH:!aNULL:!MD5;

    # Forces the header to be the one that is visible from the outside
    proxy_set_header                Host api.netmaker.example.com;

    # Pass all headers through to the backend
    proxy_pass_request_headers      on;

    location / {
        grpc_pass grpc://127.0.0.1:50051;
    }
}

配置中涉及到了一些 SSL 证书的申请,我使用的是 acme.sh, 你可以参考我的这一篇文章来设置: acme.sh 使用记录

nginx 配置完了,就可以正常访问了。

Bpazy commented 2 years ago

初始化 Netmaker

  1. 第一次登录配置账号密码;

  2. 新建默认 network,我起名为 demo image

  3. 第一步安装时指定了 CLIENT_MODE: "on",所以 network 新建完之后会自带一个 node 名为 netmaker image

  4. 创建 Create an Access Key,此时页面会展示一些利用该 Access Key 的脚本,你可以选择一个脚本在客户端运行 image

  5. 我使用的是上图展示的 Docker 方式,不过改造成了 docker-compose,这样便于我开机自启:

    version: '3'
    services:
    netclient:
    image: gravitl/netclient:v0.9.2
    restart: unless-stopped
    network_mode: host
    volumes:
      - /etc/netclient:/etc/netclient
    environment:
      - TOKEN=
    privileged: true
  6. Enjoy it.

Bpazy commented 2 years ago

Docker 方式运行 netclient 不支持 Engress

接上条,我在客户端上使用的是 Docker 运行,但是发现在该客户端的 Engress 无效。研究了一下,发现 Engress 的原理是在 Wireguard 启动时 ( Postup ) 添加以下 iptables 规则:

iptables -A FORWARD -i nm-default -j ACCEPT; iptables -t nat -A POSTROUTING -o ens18 -j MASQUERADE

在 Wireguard 关闭时 ( Postdown ) 移除上一条规则:

iptables -D FORWARD -i nm-default -j ACCEPT; iptables -t nat -D POSTROUTING -o ens18 -j MASQUERADE

但是我已经在启动容器的时候指定了 privileged 参数,为何还是无法生效呢?测试一下:

使用 netclient 程序手动连入 netmaker 网络

ziyuan@pve-ubuntu:proxychains wget https://github.com/gravitl/netmaker/releases/download/v0.9.2/netclient
ziyuan@pve-ubuntu:sudo ./netclient join -t YOUR_TOKEN

ziyuan@pve-ubuntu:sudo iptables -n -v -L FORWARD
Chain FORWARD (policy ACCEPT 24 packets, 2016 bytes)
 pkts bytes target     prot opt in     out     source               destination
   24  2016 ACCEPT     all  --  nm-default *       0.0.0.0/0            0.0.0.0/0

可以看到,iptables 规则已经存在了,再用 netmaker 网络中其他机器测试下:

ubuntu@shan-tencent:~$ ping 192.168.31.1
PING 192.168.31.1 (192.168.31.1) 56(84) bytes of data.
64 bytes from 192.168.31.1: icmp_seq=1 ttl=63 time=10.7 ms

通的。

使用 Docker 程序自动连入 netmaker 网络

ziyuan@pve-ubuntu:sudo docker run -d --network host  --privileged -e TOKEN=YOUR_TOKEN -v /etc/netclient:/etc/netclient --name netclient gravitl/netclient:v0.9.2

ziyuan@pve-ubuntu:~/netclient$ sudo iptables -n -v -L FORWARD
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

没有对应的 iptables 规则,再用 netmaker 网络中其他机器测试下:

ubuntu@shan-tencent:~$ ping 192.168.31.1
PING 192.168.31.1 (192.168.31.1) 56(84) bytes of data.
^C
--- 192.168.31.1 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2030ms

网络不通。

解决方案

先临时下载 netclient 手动接入顶一阵子,Docker 的方案还是要研究的,不然我的 QNAP 上怎么运行呢 :)