王某某的笔记

记录我的编程之路

sshuttle 一个使用ssh协议的透明代理VPN

sshuttle 通过 SSH 隧道将本地网络流量转发到远程主机,类似于一个轻量级的 VPN。
sshuttle 的工作原理是通过 SSH 将流量转发到远程主机,然后远程主机再将流量发出。因此,它非常适合在只有 SSH 访问权限的情况下进行简单的网络隧道代理。

安装 sshuttle

在大多数 Linux 发行版上,可以通过包管理器安装 sshuttle:

Debian/Ubuntu:

1
2
sudo apt-get update
sudo apt-get install sshuttle

CentOS/RHEL:

1
2
sudo yum install epel-release
sudo yum install sshuttle

使用 sshuttle

sshuttle 的基本使用方法如下:

1
2
3
sshuttle [options...] [-r [username@]sshserver[:port]] \<subnets...>

sshuttle -r <user>@<remote_host> <network>
  • -r @:指定 SSH 连接的远程主机及其用户名
  • :指定你想要通过 SSH 隧道转发的目标网络或 IP 地址

其他选项:

  • –dns:通过 SSH 隧道转发 DNS 请求
  • -v 或 –verbose:启用详细输出,用于调试

示例用法

将所有网络流量转发到远程主机:

假设你的远程主机 IP 是 192.168.1.100,用户名是 user,并且你希望将本地的所有网络流量通过这个远程主机进行转发:

1
sshuttle -r user@192.168.1.100 0.0.0.0/0

这条命令会将所有网络流量通过 SSH 隧道转发到 192.168.1.100。

仅转发特定网络的流量:

如果你只想通过 SSH 隧道转发特定子网的流量,比如 192.168.10.0/24:

1
sshuttle -r user@192.168.1.100 192.168.10.0/24

这条命令会将所有指向 192.168.10.0/24 网络的流量通过 SSH 隧道转发到远程主机 192.168.1.100。

多个子网

1
sshuttle -r user@192.168.1.100 192.168.10.0/24 10.10.10.0/24
后台运行:

如果你希望 sshuttle 在后台运行,可以加上 -D 参数:

1
sshuttle -r user@192.168.1.100 0.0.0.0/0 -D

使用 -D 选项后,sshuttle 会在后台运行并持续工作。

注意事项

  • 权限:sshuttle 通常需要 root 权限或使用 sudo 执行,因为它需要修改本地的网络路由表和防火墙规则。
  • Python:sshuttle 是用 Python 编写的,因此需要安装 Python 环境。
  • SSH 访问:使用 sshuttle 需要目标远程主机上有 SSH 访问权限,并且 SSH 连接成功后才能转发流量。
  • 端口转发:sshuttle 默认会转发 TCP 流量,但也支持 UDP 流量,可以使用 -x 参数排除特定的地址或使用 -e 选项指定自定义的 SSH 选项。

检测和验证

查看当前的iptables NAT规则

1
sudo iptables -t nat -L -n -v

应该能看到:Chain sshuttle 如:

1
2
3
4
Chain sshuttle-12300 (2 references)
pkts bytes target prot opt in out source destination
38 1976 REDIRECT tcp -- * * 0.0.0.0/0 10.10.0.0/16 TTL match TTL != 42 redir ports 12300
1 52 RETURN tcp -- * * 0.0.0.0/0 127.0.0.0/8

firewalld 是 CentOS 7 上的默认防火墙管理工具,它为用户提供了一个更高级的管理界面来配置防火墙规则。
iptables 是 Linux 内核中用于设置、维护和检查 IP 包过滤规则的工具。firewalld 在后台实际上还是使用 iptables 来应用防火墙规则的。

增加调试日志

1
sshuttle -vvr username@sshserver 0.0.0.0/0

这里的 -vv 参数会增加日志的详细程度,有助于调试问题

Prometheus 收集 Docker 信息时,通常使用 cAdvisor(Container Advisor)作为收集器。cAdvisor 是一个开源的工具,专门用于收集容器运行时的资源使用情况和性能数据,支持 Docker 容器。它能够监控 CPU、内存、网络、磁盘 I/O 等多种指标,并将这些数据暴露为 Prometheus 可以抓取的指标。

官方地址:
https://github.com/google/cadvisor

在CentOS上运行参考地址:
https://github.com/google/cadvisor/blob/master/docs/running.md

运行 cAdvisor 容器

你可以通过 Docker 启动一个 cAdvisor 容器

命令如下:

1
2
3
4
5
6
7
8
9
10
11
docker run \
--volume=/:/rootfs:ro \
--volume=/var/run:/var/run:ro \
--volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--volume=/cgroup:/cgroup:ro \
--publish=8080:8080 \
--detach=true \
--restart unless-stopped \
--name=cadvisor \
google/cadvisor:latest

在 Docker 中,–volume(或者简写为 -v)选项用于将宿主机的文件或目录挂载到容器中。这里的命令示例中使用了多个 –volume 选项,每个选项都将宿主机上的目录或文件挂载到容器内的某个位置。以下是这些挂载的含义:

如:--volume=/:/rootfs:ro:

  • 宿主机路径:/ (根目录)
  • 容器内路径:/rootfs
  • 挂载模式:ro (只读)
  • 含义:将宿主机的根文件系统以只读模式挂载到容器的 /rootfs 目录中。这样容器能够访问宿主机的所有文件系统结构,但不能进行任何修改。

其他目录:

  • /var/run: 通常包含一些运行时的进程和服务相关的文件,如 Docker 守护进程的 Unix socket,这样容器可以读取这些文件,但无法修改。
  • /sys: 文件系统是 Linux 中用于存储和访问系统设备的信息和状态的虚拟文件系统,通过只读方式挂载,可以让容器获取系统的状态信息。
  • /var/lib/docker:宿主机的 Docker 数据目录(存放容器、镜像、网络等信息),cAdvisor 通过访问这个目录,可以获取有关 Docker 容器的信息。
  • cgroup 是 Linux 内核提供的一种机制,用于对系统资源(CPU、内存、I/O 等)进行分组管理和限制,通过访问 cgroup 文件系统,cAdvisor 可以获取和监控容器的资源使用情况。

查看指标

启动 cAdvisor 后,它会在 http://:8080/metrics 路径下暴露所有收集的指标数据。Prometheus 将按照配置的抓取间隔自动抓取这些指标。

如:

1724169316705.png

在 Prometheus 中配置抓取

在 Prometheus 的配置文件 prometheus.yml 中,添加一个 scrape job 来抓取 cAdvisor 暴露的指标。比如:

1
2
3
4
5
scrape_configs:
- job_name: 'cadvisor'
static_configs:
- targets: ['<cAdvisor_host>:8080']

重启容器:

1
docker restart prometheus

prometheus 也是docker安装的

查看:
1724169204562.png


Grafana可视化配置

要在 Grafana 中展示 cAdvisor 收集的 Docker 容器信息,可以使用以仪表盘模板。这些模板已经预先配置好,可以直接导入并使用,从而快速开始监控 Docker 容器的资源使用情况。

使用模板

在仪表盘中选择新建 -> 导入,填如复制的模板ID,选择数据源,保存

搜索模板:

https://grafana.com/grafana/dashboards/?search=Docker

通过关键词如 “Docker” 或 “cAdvisor” 进行搜索

可用模板如:

  • cAdvisor Dashboard
    • ID: 19792
  • Docker Dashboard for Prometheus 中文版
    • ID:11558
  • Docker overview with Cadvisor + docker state exporter + node exporter
    • ID:21154
  • Docker and system monitoring
    • ID: 893
  • Docker Swarm and Containers
    • ID:609
  • docker container & OS node(node_exporter, cadvisor)
    • ID: 16314
    • 中文 docker 容器 cadvisor 和主机指标面板
    • 主机指标 ( 可绑定到主机,需要 node-exporter 的 instance 标签值与 cadvisor 的 instance 标签值相同 )

将 cadvisor 与 node_exporter 指标关联

有的面板可以同时监控 cadvisor 和 node_exporter 指标,如需将 cadvisor 与 node_exporter 指标关联,需要将 cadvisor 和 node_exporter 的 instance 标签设置为相同的值 ( 例如主机名、ip地址等 )

cAdvisor 和 node_exporter 是常用的两个数据源。cAdvisor 用于监控 Docker 容器的资源使用情况,而 node_exporter 用于监控主机级别的系统资源(如 CPU、内存、磁盘等)。

为了在同一个 Grafana 面板中同时展示这两者的数据,并确保它们的监控数据能够正确关联起来,你需要确保这两个数据源的 instance 标签具有相同的值。通常这个值是主机名或 IP 地址。

如何设置 instance 标签为相同值

你需要在 prometheus.yml 配置文件中使用 relabel_configs 来覆盖 instance 标签的默认值。
找到配置 cAdvisor 和 docker宿主机Node Exporter 的抓取任务的部分,并添加 relabel_configs 来替换 instance 标签的。
如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 # 监控Docker宿主机主机的 Node Exporter
- job_name: 'docker_node_exporter'
static_configs:
- targets: ['192.168.31.77:9100']
relabel_configs:
- target_label: instance
replacement: 'DockerHost'

# 监控Docker相关信息
- job_name: 'cadvisor'
static_configs:
- targets: ['192.168.31.77:8080']
relabel_configs:
- target_label: instance
replacement: 'DockerHost'
  • target_label 指定要替换或设置的目标标签。在这个例子中,instance 标签就是我们要替换的目标标签。
  • replacement 定义了 target_label 标签的新值。在这个例子中,我们将 instance 标签的值设置为 ‘DockerHost’。

重启

1
docker restart prometheus

在 Prometheus 的 Status -> Targets 中查看

1724340279966.png

这样 cAdvisor 的 instance 标签将与 宿主机的 node_exporter 的 instance 标签一致。

展示效果如:
1724340790569.png

1724251354622.png

This dashboard depends on Angular, which is deprecated and will stop working in future releases of Grafana.

该仪表板依赖于 Angular,后者已被弃用,并将在 Grafana 的未来版本中停止工作。

官方说明:
https://grafana.com/docs/grafana/latest/developers/angular_deprecation/

Angular 插件支持已弃用,并将在未来版本中删除。有些旧版核心 Grafana 可视化和外部插件依赖 Grafana 的 Angular 插件支持才能工作。

从 Grafana v9 及更高版本开始,有一个服务器配置选项,它是整个实例的全局选项,用于控制是否提供 Angular 插件支持。在 Grafana 11 中,将更改配置的默认值以删除支持。

在 Grafana 11 中(该版本将于 2024 年 4 月发布预览版,并于 5 月正式发布),将更改 angular_support_enabled 配置参数的默认值为:false,以关闭对基于 AngularJS 的插件的支持。如果您仍然依赖内部或社区开发的基于 AngularJS 的插件,则需要启用此选项才能继续使用它们。
新的 Grafana Cloud 用户将无法请求将支持添加到他们的实例中。

目前的计划是完全删除版本 12 中对 Angular 插件的任何剩余支持。包括删除angular_support_enabled配置参数。

启用Angular支持

这里的版本是: Grafana V11.1.4

进入容器:

1
docker exec -it --user root grafana /bin/bash

编辑 Grafana 配置文件

1
vi /etc/grafana/grafana.ini

在配置文件中查找或添加以下内容:

1
angular_support_enabled = true

配置文件中默认是:

1
;angular_support_enabled = false

这个分号表示注释

重启 Grafana 容器

1
docker restart grafana

时间:2024-8-18
版本信息:

  • Prometheus V2.53.2
  • Grafana V11.1.4

Prometheus 和 Grafana 介绍

Prometheus 和 Grafana 是监控领域中经常搭配使用的两个开源工具,它们共同构成了一个强大的监控解决方案。

Prometheus

一个开源的监控系统和时间序列数据库,主要用于收集和存储各种指标数据。它具有强大的查询语言 PromQL,可以对时间序列数据进行灵活的查询和聚合。

Prometheus 主要分为两个部分:服务端(Prometheus Server)和数据收集端( Exporter)

Prometheus Server(服务端)

Prometheus Server 负责从配置的目标(例如 Node Exporter)抓取数据,并存储这些数据。它执行数据采集、存储、查询和报警等任务。

主要组件:
  • 数据采集:通过 HTTP 请求抓取暴露的时间序列数据。
  • 时间序列数据库:存储和管理时间序列数据。
  • 查询语言:提供 PromQL(Prometheus Query Language)用于查询数据。
  • 报警管理:集成 Alertmanager 用于处理和发送报警通知。

Exporter(数据收集端)

Prometheus官方提供了很多Exporter,用于采集不同类型的指标数据,如Node Exporter、MySQL Exporter、Kafka Exporter等。

Node Exporter

Node Exporter 是一种常见的 Prometheus 导出器,用于从 Linux 系统上收集系统级的指标数据(如 CPU 使用率、内存、磁盘 IO 等)。它将这些数据暴露为 Prometheus 可抓取的格式。
它的主要功能是:

  • 数据收集:从操作系统的各种指标中获取数据。
  • 数据暴露:通过 HTTP 将这些数据暴露给 Prometheus Server。

Grafana

一个开源的分析和可视化平台,可以连接到多个数据源(包括 Prometheus),并以图形化的方式展示数据。Grafana 提供了丰富的图表类型、仪表盘和报警功能,使得监控数据更加直观易懂。

Grafana 的主要功能

  • 创建仪表盘: 将多个图表、表格等元素组合成一个仪表盘,以展示系统的整体运行状况。
  • 自定义图表: 支持多种图表类型,如折线图、柱状图、饼图、热力图等,可以对数据进行灵活的展示。
  • 设置报警: 当指标数据超出设定的阈值时,触发报警,及时通知相关人员。
  • 探索数据: 提供一个交互式的查询界面,方便用户探索和分析数据。

以Docker方式安装Prometheus和Grafana

安装在 DockerHost 192.168.31.77

创建目录和配置文件

创建目录

如果不想映射这两个数据,也可不用配置
映射数据目录只是为了删除容器之后数据不会丢失

1
2
mkdir -p /opt/prometheus/data
mkdir -p /opt/grafana/data

修改目录权限

1
2
sudo chown -R 65534:65534 /opt/prometheus/data
sudo chmod -R 775 /opt/prometheus/data

上面的 65534:65534 是 Docker 中常用的 nobody 用户和组的 UID 和 GID,确保 Prometheus 容器能够写入数据。

1
sudo chown -R 472:472 /opt/grafana/data

Grafana 容器中的进程通常以 grafana 用户(UID 为 472)运行,因此,你需要确保该用户对 /var/lib/grafana 有读写权限。
用户ID可以通过如:docker inspect grafana | grep User 之类的命令查看

也可以简单粗暴的让所有用户都有读写权限,如:

1
2
sudo chmod -R 777 /opt/prometheus/data
sudo chmod -R 777 /opt/grafana/data

创建配置文件

为 Prometheus 创建一个配置文件 prometheus.yml:
vim /opt/prometheus/prometheus.yml

1
2
3
4
5
6
7
8
9
10
11
global:
scrape_interval: 10s # 抓取数据的时间间隔

scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']

- job_name: 'node_exporter'
static_configs:
- targets: ['192.168.31.77:9100'] # 监控宿主机的 Node Exporter

这只是一个示例,先跑起来

拉取镜像

1
2
docker pull prom/prometheus
docker pull grafana/grafana

需要科学上网,或者配置国内的镜像地址

启动容器

启动 Prometheus 容器

1
2
3
4
5
6
7
docker run -d \
--name prometheus \
--restart unless-stopped \
-p 9090:9090 \
-v /opt/prometheus/data:/prometheus \
-v /opt/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus

访问 http://192.168.31.77:9090 来查看 Prometheus 的 Web 界面

有防火墙的要注意开放端口

启动 Grafana 容器

1
2
3
4
5
6
docker run -d \
--name grafana \
--restart unless-stopped \
-p 3000:3000 \
-v /opt/grafana/data:/var/lib/grafana \
grafana/grafana

访问 http://192.168.31.77:3000

默认用户名密码是:amdin/admin ,第一次登录会提示修改密码

有防火墙时需要开放对应端口

在目标机器上安装Node Exporter

安装在 192.168.31.77 这台docker宿主机上

首先,从 Prometheus 官方的 GitHub 仓库下载 Node Exporter 的最新版本。

1
2
3
mkdir -p /opt/prometheus/node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz

解压缩后运行

1
2
3
4
5
tar -zxvf node_exporter-1.8.2.linux-amd64.tar.gz 

cd node_exporter-1.8.2.linux-amd64

nohup ./node_exporter &

Node Exporter 默认会在端口 9100 上启动,并暴露 /metrics 端点。如果一切正常,你可以在浏览器中访问 http://<目标机器的IP地址>:9100/metrics,查看暴露的系统指标。
如:
http://192.168.31.77:9100/metrics

此时应该能在 prometheus 的 status菜单的 targets 中看到端点已连接

1723970385587.png

配置

配置 Grafana

添加 Prometheus 数据源:

  • 左侧菜单 Connections -> Data Sources
  • 点击Add data source
  • 选择Prometheus
  • 给数据源起一个有意义的名称,比如“Prometheus”
  • URL:输入Prometheus服务器的HTTP API地址,例如:http://192.168.31.77:9090

创建仪表盘:

  • 选择 “Dashboards” 菜单,选择“Create dashboard”,选择“Add visualization”
  • 选择刚刚添加的 Prometheus 数据源
配置面板:
  • Metric:选择要查询的指标,例如node_cpu_seconds_total
  • 标签:根据需要过滤数据,例如{instance=”your_host”}。
    1723971325393.png

给面板命名并保存即可

导入仪表盘

给一个个监控指标来配置图标的方式相对的麻烦,需要相关的经验也非常费时间,我们可以通过导入配置的方式快速搭建出专业的监控界面,而无需从头开始配置每一个图表和面板。可以提高效率,复用性和专业性。

官方网站上有各种各样的模板可以使用

如我们要监控linux主机的各种指标:

1724043381275.png


其他

将Node Exporter配置为系统服务

为了确保 Node Exporter 在系统重启后自动启动,你可以将其配置为系统服务。

  • 创建一个新的系统服务文件 /etc/systemd/system/node_exporter.service:

    1
    sudo vim /etc/systemd/system/node_exporter.service
  • 在文件中添加以下内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [Unit]
    Description=Node Exporter
    Wants=network-online.target
    After=network-online.target

    [Service]
    User=nobody
    ExecStart=/usr/local/bin/node_exporter
    Restart=on-failure

    [Install]
    WantedBy=default.target

    如果你将 Node Exporter 安装在其他目录下,比如 /usr/local/bin,请相应地调整 ExecStart 路径。
    这里使用软连接的方式

  • 创建软链接

    1
    ln -s /opt/prometheus/node_exporter/node_exporter-1.8.2.linux-amd64/node_exporter /usr/local/bin/node_exporter
  • 重新加载 systemd 配置以应用更改:

    1
    sudo systemctl daemon-reload
  • 启动 Node Exporter 服务:

    1
    sudo systemctl start node_exporter
  • 验证:

    1
    sudo systemctl status node_exporter

    访问:http://192.168.31.85:9100/metrics

  • 设置开机自启动:

    1
    sudo systemctl enable node_exporter

中文配置

设置 Grafana 语言为中文

点击页面左上角的个人头像,然后选择“Profile”(偏好设置)
在“Preferences”页面中,找到“Language”(语言)选项,设置为中文(简体)

使用中文监控面板

如:

Linux主机详情
(12633-linux)效果如下:

1724081514391.png

官方的和我实际想要实现的有一点差异,官方的是部署到当前仓库的 GitHub Pages

我这边是原来有一个网站,这次又使用Hexo重新做了一个博客,又新建了一个hexo-blog仓库

我想实现的效果是,往hexo-blog 仓库提交Markdown内容之后自动编译,然后将编译后的内容提交到 wangwen135.github.io这个仓库


官方的

先参考一下官方的文档说明

官方说明文档:https://hexo.io/zh-cn/docs/github-pages

.github/workflows/pages.yml 文件内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
name: Pages

on:
push:
branches:
- main # 当推送到 main 分支时触发工作流

jobs:
build:
runs-on: ubuntu-latest # 工作运行在最新的 Ubuntu 环境中
steps:
- uses: actions/checkout@v4 # 检出代码
with:
token: ${{ secrets.GITHUB_TOKEN }} # 使用 GitHub token
submodules: recursive # 如果仓库依赖子模块,则递归检出
- name: Use Node.js 20 # 使用 Node.js 20
uses: actions/setup-node@v4
with:
node-version: "20" # 指定 Node.js 版本为 20
- name: Cache NPM dependencies # 缓存 NPM 依赖
uses: actions/cache@v4
with:
path: node_modules
key: ${{ runner.OS }}-npm-cache
restore-keys: |
${{ runner.OS }}-npm-cache
- name: Install Dependencies # 安装依赖
run: npm install
- name: Build # 构建项目
run: npm run build
- name: Upload Pages artifact # 上传构建产物
uses: actions/upload-pages-artifact@v3
with:
path: ./public # 指定上传的路径
deploy:
needs: build # 依赖 build 任务
permissions:
pages: write # 给予 GitHub Pages 写权限
id-token: write # 给予 ID 令牌写权限
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest # 工作运行在最新的 Ubuntu 环境中
steps:
- name: Deploy to GitHub Pages # 部署到 GitHub Pages
id: deployment
uses: actions/deploy-pages@v4

这个 GitHub Actions 配置文件用于将编译后的 HTML 文件自动部署到当前仓库的 GitHub Pages。
这里加上了一点注释


修改后的

完整的 GitHub Actions 工作流 YAML 文件

.github/workflows/deploy.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
name: Deploy to GitHub Pages

# 触发条件:当推送到 main 分支时
on:
push:
branches:
- main
workflow_dispatch: # 允许手动触发

jobs:
build:
runs-on: ubuntu-latest

steps:
# 检出 hexo-blog 仓库的代码
- name: Checkout repository
uses: actions/checkout@v4

# 设置 Node.js 环境
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'

# 缓存 NPM 依赖
- name: Cache NPM dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

# 安装依赖
- name: Install dependencies
run: npm install

# 构建 Hexo 网站
- name: Build Hexo site
run: npm run build

# 部署到 GitHub Pages
- name: Deploy to GitHub Pages
env:
ACTIONS_DEPLOY_KEY: ${{ secrets.ACTIONS_DEPLOY_KEY }}
run: |
echo "创建 .ssh 目录"
mkdir -p ~/.ssh
echo '写入部署密钥到 id_ed25519 文件中'
echo "${{ secrets.ACTIONS_DEPLOY_KEY }}" > ~/.ssh/id_ed25519
echo "设置私钥文件权限"
chmod 600 ~/.ssh/id_ed25519

cat ~/.ssh/id_ed25519

echo "添加 GitHub 的 SSH 主机密钥到 known_hosts 文件中"
ssh-keyscan github.com >> ~/.ssh/known_hosts

echo "配置 Git 用户信息"
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'

echo "克隆 wangwen135.github.io 仓库的 main 分支到 .deploy_repo 目录"
git clone --branch main git@github.com:wangwen135/wangwen135.github.io.git .deploy_repo

echo "切换到 .deploy_repo 目录"
cd .deploy_repo

echo "更新一下代码"
git pull

echo "使用 rsync 将 public 目录中的内容同步到 .deploy_repo 目录"
rsync -av --delete --exclude '.git' ../public/ .

echo "添加所有更改"
git add .
echo '提交更改,提交信息为 "Deploy updates"'
git commit -m "Deploy updates"
echo "推送更改到远程仓库的 main 分支"
git push

相关步骤上面都有注释

因为要使用git将文件推送到仓库wangwen135.github.io 需要配置ssh key,还需要进行下面的步骤

配置 Secrets

在 GitHub Actions 中,Secrets 是一种安全存储和管理敏感信息的方法,例如访问令牌、API 密钥、SSH 密钥等。Secrets 可以在工作流中使用,但不会在日志中显示,确保了敏感信息的安全。

设置步骤

生成 SSH 部署密钥

在本地机器上生成 SSH 密钥对:

1
ssh-keygen -t ed25519 -C "wangwen135@gmail.com" -f ./id_ed25519

按提示操作,将会在当前执行的目录中生成 id_ed25519 和 id_ed25519.pub 两个文件

添加 id_ed25519 到 hexo-blog 的 GitHub Secrets

将生成的私钥 id_ed25519 的内容添加到 hexo-blog 仓库的 GitHub Secrets 中,步骤如下:

  • 打开 GitHub,进入你的 hexo-blog 仓库。
  • 点击 “Settings” 选项卡。
  • 在左侧边栏中,找到并点击 “Secrets and variables” 下的 “Actions”。
  • 点击 “New repository secret” 按钮。
  • 在 “Name” 字段中输入 ACTIONS_DEPLOY_KEY,在 “Secret” 字段中粘贴 id_ed25519 文件的内容。
  • 点击 “Add secret” 按钮保存。

添加 id_ed25519.pub 到目标仓库的部署密钥

将生成的公钥 id_ed25519.pub 添加到 wangwen135.github.io 仓库的部署密钥中,步骤如下:

  • 打开 GitHub,进入你的 wangwen135.github.io 仓库。
  • 点击 “Settings” 选项卡。
  • 在左侧边栏中,找到并点击 “Deploy keys”。
  • 点击 “Add deploy key” 按钮。
  • 在 “Title” 字段中输入一个描述性的名称,例如 “GitHub Actions Deploy Key”。
  • 在 “Key” 字段中粘贴 id_ed25519.pub 文件的内容。
  • 确保勾选 “Allow write access” 复选框。
  • 点击 “Add key” 按钮保存。

SSH 端口转发可以灵活的解决网络访问受限的问题

通常分为两种方式:

  • 本地端口转发
  • 远程(反向)端口转发

本地端口转发

SSH本地端口转发(Local Port Forwarding)是一种通过SSH隧道将本地计算机的某个端口转发到远程服务器的某个端口的技术。这样,访问本地端口的请求会通过SSH隧道传输到远程服务器,从而实现安全的通信。

使用场景

  • 访问内网服务:通过SSH隧道访问公司内网或远程服务器上的服务,避免直接暴露服务端口。
  • 安全传输数据:将本地端口转发到远程服务器上的敏感服务,实现加密传输。
  • 绕过防火墙:通过SSH隧道绕过防火墙限制访问某些服务。

基本命令格式

1
ssh -L [本地端口]:[目标主机]:[目标端口] [用户名]@[SSH服务器]

配置示例

示例 1

假设你有一台远程服务器remote_server_ip,上面运行了一个Web服务,监听在端口80。你希望通过SSH隧道,将本地计算机的端口8080转发到远程服务器的端口80,以便在本地浏览器中访问localhost:8080时能够访问远程服务器的Web服务。

1
ssh -L 8080:localhost:80 user@remote_server_ip
  • -L 8080:localhost:80:指定本地端口8080转发到远程服务器的localhost:80。
  • user@remote_server_ip:SSH登录的用户名和远程服务器的IP地址。
访问方法

在执行上述命令后,在本地浏览器中访问http://localhost:8080,这将通过SSH隧道转发到远程服务器的端口80。

示例 2

又比如假设你想访问远程服务器上的MySQL服务,该服务只监听localhost

1
ssh -L 3306:localhost:3306 user@remote-ssh-server

这会将本地计算机的3306端口上的所有连接通过remote-ssh-server,转发到其localhost的3306端口。

访问方法

使用Mysql客户端工具连127.0.0.1:3306

转发到不同的远程主机

如果需要转发到远程服务器能访问到的另一台主机(ssh服务器作为跳板),可以使用远程主机的IP或域名。

1
ssh -L 8080:target_host:80 user@remote_server_ip
  • target_host:远程服务器可以访问的目标主机的IP或域名。

远程端口转发

远程端口转发(Remote Port Forwarding)是指在远程计算机上创建一个端口,并将其流量通过 SSH 隧道转发到本地计算机的指定目标。通常用于让外部网络访问本地服务。

使用场景

  • 让外部访问内部服务:适合在远程服务器不能直接访问本地服务时,通过中间服务器进行访问。
  • 绕过防火墙限制:利用远程端口转发,通过中间服务器绕过防火墙访问内部服务。
  • 安全访问:让远程用户安全地访问公司内部网络中的资源。
  • 反向 SSH 隧道:将本地机器的端口暴露到远程服务器,方便远程访问本地服务。

基本命令格式

1
2
ssh -R [远程端口]:[目标主机]:[目标端口] [用户名]@[SSH服务器]

配置示例

假设你想让远程服务器(remote.example.com)通过其 8080 端口访问你本地机器上的一个运行在 localhost:3000 的服务,可以使用以下命令:

1
ssh -R 8080:localhost:3000 user@remote.example.com
  • -R:用于指定反向隧道的选项。
  • 8080:远程服务器上的端口号。
  • localhost:3000:本地机器上运行服务的地址和端口号。
  • user@remote.example.com:远程服务器的 SSH 用户和地址。

访问方法

在执行上述命令后,你可以在远程服务器上访问 http://localhost:8080 来访问本地机器上 localhost:3000 端口运行的服务。


更多

后台运行SSH隧道:

使用-f选项将隧道命令放入后台运行。

1
2
ssh -L 8080:localhost:80 -f user@remote_server_ip -N

  • -N:表示不执行远程命令,仅进行端口转发。
  • -f:表示后台运行SSH隧道。

保持隧道活跃:

如果需要保持隧道长时间活跃,可以使用 autossh 来自动重连,或者设置 SSH 的 ServerAliveInterval 和 ServerAliveCountMax 参数:

1
ssh -R 8080:localhost:3000 user@remote.example.com -o ServerAliveInterval=60 -o ServerAliveCountMax=3

通过SSH实现SOCKS代理

它允许你通过SSH隧道加密你的网络流量,并通过SSH服务器转发

用途

安全访问:

在不安全的网络环境中,使用SSH隧道可以加密流量并保护隐私。

绕过地理限制:

可以通过SSH隧道访问被地理位置限制的内容。

测试和调试:

开发和测试应用程序时,可以使用SOCKS代理来模拟不同的网络环境。


设置SSH动态端口转发(SOCKS代理)的步骤如下:

1. 安装SSH客户端

确保你的计算机上安装了SSH客户端。大多数Unix-like系统(如Linux和macOS)都预装了SSH客户端。在Windows上,你可以使用PuTTY或者安装Windows的Linux子系统。

这里用Git Bash 实现

2. 连接到SSH服务器

使用以下命令连接到你的SSH服务器,并通过SSH建立SOCKS代理:

1
2
ssh -D 本地端口 用户名@ssh服务器地址

这里的本地端口是你希望在本地机器上监听的端口,用于SOCKS代理。用户名和ssh服务器地址是你在SSH服务器上的登录凭证。

如:

1
2
3
4
ssh -D 8080 user@ssh.example.com

ssh -D 8080 -N -f user@remote_server_ip

这个命令会在本地机器上开启一个监听在8080端口的SOCKS代理。

参数说明:

  • -D 8080:指定本地端口8080作为SOCKS代理端口。
  • -N:不执行远程命令,只进行端口转发。
  • -f:后台运行SSH会话。

3. 配置你的应用程序或浏览器

一旦SSH隧道建立,你需要配置你的应用程序或浏览器使用这个SOCKS代理。

对于浏览器:

  • 在Chrome或Firefox中,你可以安装一个代理扩展,如”Proxy SwitchyOmega”,并设置SOCKS5代理为localhost,端口为8080。
  • 在其他浏览器中,通常在设置中找到网络或代理设置,并手动配置SOCKS代理。

对于其他应用程序:

查找应用程序的网络设置或代理设置,并配置SOCKS5代理为localhost,端口为8080。

4. 测试连接

现在,当你通过配置了SOCKS代理的应用程序或浏览器访问网络时,所有的流量都应该通过SSH隧道加密并转发。

如打开你的浏览器访问目标地址

为了快速的将其他地方Markdown笔记导入到Hexo中,使用Python脚本为Markdown笔记批量生成YAML头部信息

整理笔记目录

比如已经规划好了目录,如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

├─开发
│ ├─java
│ │ Java入门.md
│ │ Java数据结构.md
│ │ xxxx.md
│ │
│ ├─Spring
│ │ Spring MVC.md
│ │ Spring AOP.md
│ │ Spring Data.md
│ │
│ └─网络
│ 协议.md
│ 网络通信.md

生成YAML头部信息要求

目标是为将目录和下级目录中全部的Markdown文件自动生成YAML头部信息,如在文件:
.\博客\Hexo\Hexo安装使用.md
的头部添加:

1
2
3
4
5
6
7
8
---
title: Hexo安装使用
date: 2024-07-24 23:31
tags:
- Hexo
categories:
- [博客, Hexo]
---

要求:

  • title:为文件名称,不要后缀
  • date:随机时间(小于当前时间)
  • tags: 为最近一级的目录名称
  • categories:为多级目录名称

Python脚本

创建Python脚本文件:add_front_matter.py
内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import random
import datetime

def get_random_date():
now = datetime.datetime.now()
random_days = random.randint(1, 365 * 5) # 过去五年中的随机天数
random_date = now - datetime.timedelta(days=random_days)
return random_date.strftime('%Y-%m-%d %H:%M')

def add_front_matter(directory):
for root, _, files in os.walk(directory):
print(f"Scanning directory: {root}")
for file in files:
if file.endswith('.md'):
file_path = os.path.join(root, file)
print(f"Found markdown file: {file_path}")

try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()

title = os.path.splitext(file)[0]
date = get_random_date()
tags = os.path.basename(root)
categories = os.path.relpath(root, directory).split(os.sep)

front_matter = f'''---
title: {title}
date: {date}
tags:
- {tags}
categories:
- [{', '.join(categories)}]
---

'''

new_content = front_matter + content
with open(file_path, 'w', encoding='utf-8') as f:
f.write(new_content)

print(f"Updated: {file_path}")
except Exception as e:
print(f"Error updating {file_path}: {e}")

if __name__ == "__main__":
directory = r"D:\develop\blog\hexo-blog\source\_posts" # 替换为你的Markdown文件所在目录
print(f"Starting to update markdown files in: {directory}")
add_front_matter(directory)
print("Finished updating markdown files.")

执行脚本更新文件

注意:将上面脚本中的Markdown文件所在目录替换为你的整理后的笔记目录

打开终端或命令提示符,导航到脚本所在目录,运行脚本:

1
2
python add_front_matter.py

问题: 时间不好处理,需要自己修改

_config.yml配置文件说明

站点基本信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 站点名称
title: 王某某的笔记

# 站点副标题
subtitle: 记录我的编程之路

# 站点描述
description: 这是一个使用 Hexo 构建的博客,用于分享我的编程经验和学习笔记。

# 作者名称
author: 王某某

# 站点语言
language: zh-CN

站点的 URL 结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# URL 前缀,例如如果站点部署在子目录下,需要设置此项
root: /

# 生成文件的目录
public_dir: public

# Source 文件夹
source_dir: source

# 默认布局
default_layout: post

# permalink 配置
permalink: :year/:month/:day/:title/

这里将permalink:
permalink: :title/

设置站点的时间格式和时区

1
2
3
4
5
6
7
# 时间格式
date_format: YYYY-MM-DD
time_format: HH:mm:ss

# 时区设置
timezone: Asia/Shanghai

配置站点的分页功能

1
2
3
4
5
6
# 每页显示的文章数
per_page: 10

# 分页路径
pagination_dir: page

配置404页面

在站点根目录创建一个名为404的新文件夹,然后在其中创建一个新页面:

1
2
3
cd hexo-site
hexo new page 404

确保relative_link已禁用

1
2
relative_link: false

能否将用户重定向到 404 页面取决于网站托管服务商或 Web 服务器的设置,而非 Hexo 的设置。例如,如果您使用 Nginx 作为服务器,您还需要在nginx.conf文件中配置 404 页面。


使用NexT主题

NexT 主题

github地址:
https://github.com/next-theme/hexo-theme-next

详细安装说明:
https://theme-next.js.org/docs/getting-started/installation.html

安装

使用npm安装

1
2
cd hexo-site
npm install hexo-theme-next

或者你可以克隆整个存储库:

1
2
cd hexo-site
git clone https://github.com/next-theme/hexo-theme-next themes/next

启用主题

修改:Hexo的 _config.yml 文件

1
theme: next

默认主题是:landscape

复制主题配置文件

将主题的配置文件复制到根目录,命名为:_config.next.yml

1
2
3
4
5
6
7
8
cd hexo-site

## npm方式安装则:
cp ./node_modules/hexo-theme-next/_config.yml _config.next.yml

## git克隆则:
cp ./themes/next/_config.yml _config.next.yml

然后相关配置都是修改 _config.next.yml 这个配置文件

选择方案

Scheme 是 NexT 支持的一个功能,通过使用 Scheme,NexT 可以为您提供不同的视图。
到目前为止,NexT 支持 4 种方案,它们是:

  • Muse→ Default Scheme,这是NexT的初始版本。使用黑白色调,主要看起来很干净。
  • Mist→ Muse 的更紧凑版本,具有整洁的单列视图。
  • Pisces→ 双列方案,像邻居的女儿一样新鲜。
  • Gemini→ 看起来像双鱼座,但有明显的带阴影的柱块,看起来更加敏感。

可以通过编辑来更改方案NexT 配置文件,搜索scheme关键字

1
2
3
4
5
# Schemes
scheme: Muse
#scheme: Mist
scheme: Pisces
#scheme: Gemini

这里选Pisces

侧边栏头像设置

1
2
3
4
5
6
7
8
# Sidebar Avatar
avatar:
# Replace the default image and set the url here.
url: /images/avatar.jpg
# If true, the avatar will be displayed in circle.
rounded: true
# If true, the avatar will be rotated with the cursor.
rotated: false

将头像图片avatar.jpg 放到 ./source/images/ 目录中

配置菜单项

菜单设置项包含 3 个值:

1
Key: /link/ || icon
  • Key→ 是菜单项的名称(home、archives等)。如果在语言中可以找到此菜单的翻译,则将加载此翻译;如果没有,Key则将使用名称。
  • 分隔符之前的值||(/link/)→ 是您网站内相对 URL 的目标链接。
  • ||分隔符 ( ) →后的值icon是 Font Awesome 图标的名称。该图标的名称可以在Font Awesome [https://fontawesome.com/search]
    中找。

默认情况下,所有菜单项均被注释掉,以确保您可以在备用主题配置中覆盖它们。

这里修改为:

1
2
3
4
5
6
menu:
home: / || fa fa-home
about: /about/ || fa fa-user
tags: /tags/ || fa fa-tags
categories: /categories/ || fa fa-th
archives: /archives/ || fa fa-archive

社交链接

1
2
3
4
5
6
7
# 社交链接设置
social:
GitHub: https://github.com/your-username || fa fa-github
Weibo: https://weibo.com/your-username || fa fa-weibo
Zhihu: https://www.zhihu.com/people/your-username || fa fa-zhihu
Email: mailto:your-email@example.com || fa fa-envelope

配置Tags页面

Next主题默认没有Tags页面,点击左侧菜单“标签”会进入一个404页面
只需要增加一个tags页面,将页面类型设置为tags 即可

先增加页面

1
2
3
> hexo new page tags
INFO Validating config
INFO Created: D:\code\blog\blog1\source\tags\index.md

再修改页面 ./source/tags/index.md ,设置类型

1
2
3
4
5
6
---
title: tags
date: 2024-07-24 22:18:58
type: "tags"
comments: false
---

上面菜单已经配置好了,直接重启看效果

配置Categories页面

默认也是没有Categories页面的
配置同 tags,type设置为:categories

增加页面

1
hexo new page categories

页面./source/categories/index.md 内容修改为:

1
2
3
4
5
6
7
---
title: categories
date: 2024-07-24 22:29:50
type: "categories"
comments: false
---

配置About页面

categories和tags是系统已经定义好的类型,系统会自动填充内容到里面。
about页面就不行了,得自己写

首先创建页面

1
hexo new page about

然后再修改页面:./source/about/index.md
内容如:

1
2
3
4
5
6
7
8
9
10
---
title: about
date: 2024-07-22 22:40:20
---
### 关于我
程序员 主要做Java

----
### 关于本博客
2024年使用Hexo + Next 重建

帖子元数据设置

NexT 支持帖子创建日期、帖子更新日期和帖子类别显示。

1
2
3
4
5
6
7
8
# Post meta display settings
post_meta:
item_text: true
created_at: true
updated_at:
enable: false
another_day: true
categories: true

关闭“更新于”

文章字数统计

安装:hexo-word-counter

1
2
npm install hexo-word-counter
hexo clean

Hexo配置(_config.yml)中增加:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#文章字数和阅读时间
symbols_count_time:
# 是否显示文章的字符数
symbols: true
# 是否显示文章的预计阅读时间
time: true
# 是否显示全部文章的总字符数(站点底部)
total_symbols: false
# 是否显示全部文章总阅读时间(站点底部)
total_time: true
# AWL 代表 Average Words per Line(每行的平均单词数),用于估算阅读时间。通常用于更精确的阅读时间估算。
awl: 4
# WPM 代表 Words Per Minute(每分钟的单词数),用于计算阅读时间。
wpm: 275

Next中有默认设置

1
2
3
symbols_count_time:
separated_meta: true
item_text_total: false

Next默认显示字数和阅读时长

其他设置

测试时注意:这些设置一般需要重启服务后才能生效

回到顶部
1
2
3
4
5
6
7
back2top:
# 控制“返回顶部”按钮是否启用
enable: true
# 控制“返回顶部”按钮是否显示在侧边栏
sidebar: false
# 控制“返回顶部”按钮上是否显示滚动百分比标签
scrollpercent: false
阅读进度条
1
2
reading_progress:
enable: true
启用书签支持
1
2
bookmark:
enable: true

用户只需点击页面左上角的书签图标即可保存滚动位置。当他们下次访问你的博客时,他们可以自动恢复到上一次滚动位置。

深色模式
1
2
# Dark Mode
darkmode: false

https://hexo.io/zh-cn/

https://hexo.io/docs/

Hexo 是一个快速、简洁且高效的博客框架。 Hexo 使用 Markdown(或其他标记语言)解析文章,在几秒内,即可利用靓丽的主题生成静态网页。

使用 Node.js 编写

安装 Node.js 和 git

Hexo 需要 Node.js 和 npm(Node.js 的包管理器)。你可以从 Node.js 官网 下载并安装最新版本的 Node.js。安装 Node.js 后,npm 会自动随之安装。

Node.js (Node.js 版本需不低于 10.13,建议使用 Node.js 12.0 及以上版本)

git 正常安装即可

安装 Hexo

打开终端或命令提示符,运行以下命令来全局安装 Hexo:

1
2
npm install -g hexo-cli

有点慢,好像需要科学上网
可以指定国内镜像:–registry=https://registry.npmmirror.com

创建新的 Hexo 博客

选择一个你希望存放博客的目录,并进入该目录,然后运行:

1
hexo init blog

这将会在 blog 目录下创建一个新的 Hexo 博客项目。你也可以将 blog 替换为你希望的项目名称。

安装依赖

进入你的博客目录后,安装项目依赖:

1
2
3
4
5
cd blog
npm install

#或者指定淘宝镜像
npm install --registry=https://registry.npmmirror.com

创建新的文章

使用 Hexo 提供的命令来创建新的文章:

1
2
hexo new "我的第一篇文章"

这会在 source/_posts 目录下生成一个新的 Markdown 文件,你可以在这个文件中编写你的文章。

生成和预览网站

在生成网站之前,你可以先在本地预览网站:

1
hexo server

然后在浏览器中访问 http://localhost:4000 来查看你的博客。

当你对博客的内容感到满意时,可以生成静态文件:

1
hexo generate

生成的静态文件会放在 public 目录下。


部署博客

Hexo 支持多种部署方式,如 GitHub Pages、Netlify 等。以下是将博客部署到 GitHub Pages 的步骤:

  1. 在 GitHub 上创建一个新的仓库。
  2. 修改 Hexo 配置文件 _config.yml 中的部署设置,添加你的仓库地址。例如:
    1
    2
    3
    4
    5
    deploy:
    type: git
    repo: https://github.com/你的用户名/你的仓库名.git
    branch: main

  3. 安装 Hexo 部署插件:
    1
    npm install hexo-deployer-git --save
  4. 部署到 GitHub Pages:
    1
    hexo deploy

配置和主题

Hexo 的配置文件 _config.yml 可以用来设置网站的基本信息,如标题、描述、作者等。你还可以通过安装和配置主题来改变博客的外观,主题通常放在 themes 目录下。

要添加新的主题,你可以从 Hexo 官方主题库 下载,然后将其放在 themes 目录下,修改 _config.yml 中的 theme 选项以使用新的主题。

0%