Cloudflare Argo Tunnel 安装教程
前言
- 本文均为一劳永逸的方法,故和网上教程可能大相径庭。
- 请自备Cloudflare账号且有域名。
- 如果有任何疑问,请查看官方文档。
安装运行
依据系统类型选择,安装完成后使用
cloudflared命令来调用。(官方文档)RHEL和Centos系统选用该代码
1
2
3
4
5
6
7
8
9
10sudo rpm -e gpg-pubkey-8e5f9a5d-* #删除旧密钥(如果有的话)
sudo rpm --import https://pkg.cloudflare.com/pubkey.gpg #安装新密钥
sudo rpm -ivh http://pkg.cloudflare.com/cloudflare-release-latest.el<VERSION>.rpm #安装存储库,VERSION为系统版本号自行修改。(6,7)
yum install cloudflared #安装cloudflared
#例子 在Centos7上安装
sudo rpm -e gpg-pubkey-8e5f9a5d-*
sudo rpm --import https://pkg.cloudflare.com/pubkey.gpg
sudo rpm -ivh http://pkg.cloudflare.com/cloudflare-release-latest.el7.rpm
yum install cloudflaredUbuntu
1
2
3
4
5
6
7
8
9
10
11
12sudo apt-key del 8e5f9a5d #删除旧密钥(如果有的话)
sudo curl https://pkg.cloudflare.com/cloudflare-main.gpg -o /usr/share/keyrings/cloudflare-main.gpg #安装新密钥
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/ <RELEASE> main' | sudo tee /etc/apt/sources.list.d/cloudflare-main.list #安装存储库,RELEASE为系统版本号自行修改。[focal(20.04),xenial(16.04),wily(15.10),vivid(15.04),utopic(14.10),trusty(14.04),precise(12.04)]
sudo apt-get update #更新缓存
apt-get install cloudflared #安装cloudflared
#例子 在Ubuntu20.04上安装
sudo apt-key del 8e5f9a5d
sudo curl https://pkg.cloudflare.com/cloudflare-main.gpg -o /usr/share/keyrings/cloudflare-main.gpg
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/ focal main' | sudo tee /etc/apt/sources.list.d/cloudflare-main.list
sudo apt-get update
apt-get install cloudflaredDebian
1
2
3
4
5
6
7
8
9
10
11
12sudo apt-key del 8e5f9a5d #删除旧密钥(如果有的话)
sudo curl https://pkg.cloudflare.com/cloudflare-main.gpg -o /usr/share/keyrings/cloudflare-main.gpg #安装新密钥
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/ <RELEASE> main' | sudo tee /etc/apt/sources.list.d/cloudflare-main.list #安装存储库,RELEASE为系统版本号自行修改。[buster(10),stretch(9),jessie(8),wheezy(7),squeeze(6)]
sudo apt-get update #更新缓存
apt-get install cloudflared #安装cloudflared
#例子 在Debian10上安装
sudo apt-key del 8e5f9a5d
sudo curl https://pkg.cloudflare.com/cloudflare-main.gpg -o /usr/share/keyrings/cloudflare-main.gpg
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/ buster main' | sudo tee /etc/apt/sources.list.d/cloudflare-main.list
sudo apt-get update
apt-get install cloudflared登录Cloudflare,输出为一串网址或直接打开浏览器,网址复制黏贴到浏览器中,登录自己的账户。
1
cloudflared tunnel login
创建隧道,名称随意。创建完成后会给出一个
<TUNNEL_ID>,写好后面要用。1
2
3
4cloudflared tunnel create <NAME>
#例子
cloudflared tunnel create Example绑定
<NAME>隧道到<DOMAIN>域名,如果想要绑定到子域名那么<DOMAIN>请填写完整的子域名。1
2
3
4cloudflared tunnel route dns <NAME> <DOMAIN>
#例子
cloudflared tunnel route dns Example a.domain.com此时已经可以正常使用,不过我们使用配置文件启动,所以还需要一些步骤。
打开
/root/.cloudflared/(可能有所不同,请自行查找.cloudflared文件夹),并新建一个config.yml填入以下内容。其中
<TUNNEL_ID>为你的隧道ID,在创建时会给出。credentials-file部分请根据你的实际路径填写。ingress这里,每一条-就是一个规则,其中hostname请填写刚刚创建时填写的<DOMAIN>,service就是填写你想要绑定的东西,http则代表http协议,文章末尾的表格给出了所有支持的服务。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16tunnel: <TUNNEL_ID>
credentials-file: /主目录/.cloudflared/<TUNNEL_ID>.json
ingress:
- hostname: <DOMAIN>
service: <SERVICE>://localhost:80
- service: http_status:404
#例子
tunnel: 81f41a92-7556-41dg-831h-21x5575fuh6f
credentials-file: /root/.cloudflared/81f41a92-7556-41dg-831h-21x5575fuh6f.json
ingress:
- hostname: a.domain.com
service: http://localhost:80
- service: http_status:404在完成编辑后,直接运行即可,此时的服务还是运行在前台的,可以使用nohup或screen来后台运行,但不推荐。
1
cloudflared tunnel run
将Cloudflared添加到service
这是我最推荐的用法,也是本教程的最后一步,完成以上步骤后才能添加到service。
1
2
3
4
5
6
7
8
9
10#运行以下命令创建服务
cloudflared service install
#运行以下命令启动服务
systemctl start cloudflared
#运行以下命令设置开机自启
systemctl enable cloudflared
#运行以下命令查看服务状态
systemctl status cloudflared
#运行以下命令关闭服务
systenctl stop cloudflared
删除隧道
运行以下指令删除隧道(执行后会立即将本地隧道文件删除,DNS记录将于24-48小时后删除,域名绑定请手动删除)
1
2
3
4
5cloudflared tunnel delete <NAME>
#添加 -f 强制删除运行中的tunnel
cloudflared tunnel delete -f <NAME>
#例子 删除我们刚刚创建的隧道
cloudflared tunnel delete Example
Tips
因为地区不同,部分tunnel存在ERR Connection terminated的问题,使用service会造成大量日志文件导致硬盘爆炸,如果发现有类似的问题,使用以下代码运行。(错误的原因大致为tunnel为了容错默认会连接到3个服务器,如果当前地区数量不够,那么tunnel就会重复的连接到同一台上而造成错误)
1
nohup cloudflared tunnel --ha-connections 1 run & #ha-connections的作用就是限制备份服务器的连接数量
在使用一段时间后可能在这里会发现大量的API KEY,具体原因未知,可进行删除,暂未发现影响。
所有支持的协议
| Service | Description | Example service value |
|---|---|---|
| HTTP/S | Incoming HTTP requests are proxied directly to your local service. | https://localhost:8000 |
| HTTP/S over Unix socket | Just like HTTP/S, but using a Unix socket instead. | unix:/home/production/echo.sock |
| TCP | TCP connections are proxied to your local service. | tcp://localhost:2222 |
| SSH | SSH connections are proxied to your local service. | ssh://localhost:22 |
| RDP | RDP connections are proxied to your local service. | rdp://localhost:3389 |
| kubectl bastion mode | cloudflared will act like a jumphost, allowing access to any local address. |
bastion |
| Hello World | Test server for validating your Cloudflare Tunnel setup. | hello_world |
| HTTP status | Responds to all requests with the given HTTP status. | http_status:404 |
参考
https://developers.cloudflare.com/cloudflare-one/connections

