赞
踩
如何不通过内网穿透形式就能远程访问任何一台服务器,如:docker网络、k8s网关等、设备互联等,并且可以随意使用这些网关下服务器的端口服务,如:Mysql、Redis、Flink等
tip: 图中100.x.y.z为
macbook pro
、代理服务器(proxy)
、公司电脑(main)
组网后的ip,图中192.168.0.x
,为本例局域网A192.168.178.x
内部设备局域网ip
假设家庭内网有一台代理服务器(proxy)
安装了 Tailscale 客户端,我们希望其他 Tailscale 客户端macbook pro
、公司电脑(main)
可以直接通过家中的局域网 IP段(例如 192.168.178.0/24)访问家庭内网的任何一台设备,
我们来尝试实现组网后macbook pro 10.8.99.45
访问我的路由器192.168.178.1
8mbps
左右,并且有公网Ip
Headscale
服务端安装在一台具有公网IP地址的Linux服务器,例如公有云的云主机
mkdir -p /data/headscale/config
cd /data/headscale
在 headscale 目录中创建一个空的 SQlite 数据库:
touch ./config/db.sqlite
从headscale 存储库下载示例配置的副本(强烈推荐)。
wget -O ./config/config.yaml https://raw.githubusercontent.com/juanfont/headscale/main/config-example.yaml
修改配置文件,其中your-host-name 需要设置为服务器公网IP地址:
server_url: http://${your-host-name}:8080
metrics_listen_addr: 0.0.0.0:9090
private_key_path: /etc/headscale/private.key
db_path: /etc/headscale/db.sqlite
在主机 headscale 目录中工作时启动 headscale 服务器:
docker run -d --name headscale --restart always -v /data/headscale/config:/etc/headscale/ -p 8080:8080 -p 9090:9090 headscale/headscale:0.21 headscale serve
验证正在运行的容器:
root@centos-vm:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d1d9cd29c469 headscale/headscale:0.21 "headscale serve" 8 weeks ago Up 5 hours 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp headscale
验证headscale
是否可用:
root@centos-vm:~# curl http://127.0.0.1:9090/metrics
# HELP gin_request_duration_seconds The HTTP request latencies in seconds.
# TYPE gin_request_duration_seconds summary
gin_request_duration_seconds_sum 0
gin_request_duration_seconds_count 0
# HELP gin_request_size_bytes The HTTP request sizes in bytes.
# TYPE gin_request_size_bytes summary
gin_request_size_bytes_sum 0
gin_request_size_bytes_count 0
# HELP gin_response_size_bytes The HTTP response sizes in bytes.
# TYPE gin_response_size_bytes summary
.....
创建一个命名空间:
docker exec headscale \
headscale namespaces create myfirstnamespace
创建一个用户:
docker exec headscale \
headscale users create zy
生成认证key并查看生成的 authkey
,这样方便直接免密登录
root@centos-vm:~/# docker exec headscale headscale --namespace myfirstnamespace preauthkeys -u zy create --reusable --expiration 24h
fc42723daaxxxxxxxxxxxxxx00d69ddbad3d5afd0bd5
安装 tailscale
curl -fsSL https://tailscale.com/install.sh | sh
修改配置让其支持 IPv4 与 IPv6 路由转发,在如下目录下
/etc/sysctl.conf
中增加
net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=1
开启tailscale并设置
--advertise-routes
让其打通局域网内部的路由访问,这个网段的所有设备都可以被访问
tailscale up --accept-dns=false --advertise-routes=192.168.178.0/24 --login-server=http://<ip>:8080
或者直接使用之前授权的 authkey
直接登录
tailscale up --accept-dns=false --advertise-routes=192.168.178.0/24 --login-server=http://<ip>:8080 --authkey fc42723daaxxxxxxxxxxxxxx00d69ddbad3d5afd0bd5
headscale服务端设置
在 headscale 端查看路由,可以看到相关路由是关闭的。
root@centos-vm:~/# docker exec -it headscale headscale nodes list
ID | Hostname | Name | MachineKey | NodeKey | User | IP addresses | Ephemeral | Last seen | Expiration | Online | Expired
4 | porxy | porxy | [fTWM2] | [PHckt] | zy | 100.64.0.1, fd7a:115c:a1e0::1 | false | 2023-04-07 02:09:05 | 0001-01-01 00:00:00 | online | no
root@centos-vm:~/# docker exec -it headscale headscale routes list
ID | Machine | Prefix | Advertised | Enabled | Primary
1 | porxy | 192.168.178.0/24 | true | false | false
开启路由:
docker exec -it headscale headscale routes enable -r 1
ID | Machine | Prefix | Advertised | Enabled | Primary
1 | porxy | 192.168.178.0/24 | true | true | true
官方文档:Subnet routers and traffic relay nodes
现在你在任何一个 Tailscale 客户端所在的节点都可以 ping 通家庭内网的机器了
我们试试用macbook pro 10.8.99.45
访问我的路由器192.168.178.1
http://:8080/windows
http://:8080/apple
tailscale up --accept-dns=false --accept-routes --login-server=http://<headscale-ip>:8080 --authkey fc42723daaxxxxxxxxxxxxxx00d69ddbad3d5afd0bd5
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。