Idrac-console-gui 项目架构

导航

项目架构

/project-root
├─ frontend/ # 前端(vite + vue3)
│ ├─ src/
│ │ ├─ assets/
│ │ │ └─ images/ # 你的 svg 放这里(例如: idrac.svg、dell.svg、huawei.svg、langchao.svg、log.svg)
│ │ ├─ components/
│ │ └─ App.vue
│ └─ package.json
└─ backend(go)/…

一、环境准备/检查

  1. 安装/检查 Go 与 Node

Go ≥ 1.22(go version)

Node ≥ 18(node -v / npm -v)

  1. 安装 Wails v2 CLI
1
2
go install github.com/wailsapp/wails/v2/cmd/wails@latest
wails doctor

wails doctor 会检查前端打包器、WebView2 等是否齐全。官方快速开始 & 模板说明参考这里(确认 Vue 模板可用):wails go packages

二、初始化「Go + Wails + Vue3 + TypeScript」项目

我们直接用官方 Vue-TS 模板,最省心。

1
2
3
# 在你想放项目的目录执行
wails init -n idrac-console-ui -t vue-ts
cd idrac-console-gui

说明

  • 这条命令会生成一个包含 Go 后端 + Vue3 前端的完整骨架。
  • 之所以选 vue-ts,是因为 TypeScript 对新手更安全(有类型提示)。
  • Wails 官方文档确认提供 Vue 模板与初始化流程。

三、安装前端依赖(含 Axios)

进入项目根目录(里面有 frontend/ 与 go.mod):

1
2
3
4
5
6
7
8
9
cd frontend
npm i # 安装模板自带依赖
npm i axios # 安装 axios
# 新手友好建议:可加路由+状态管理(可选)
npm i vue-router pinia
cd idrac-console-ui/frontend && npm install typescript@^5.0.0 --save-dev # 如果安装失败,可以尝试使用npm安装更新ts版本
# vite vue不支持直接引入svg,需要安装vite-svg-loader
npm install vite-svg-loader -D
cd ..

依赖存放位置:

  • 前端依赖:frontend/node_modules
  • Go 依赖:模块缓存(Linux/macOS 通常在 ~/go/pkg/mod;Windows 通常在 %USERPROFILE%\go\pkg\mod),项目本地记录在 go.mod/go.sum。
  • Wails 生成的前端绑定代码(前端调用 Go 的“桥”):frontend/wailsjs/*(运行 wails dev 或 wails build 后会生成/更新)。

四、运行开发环境

1
wails dev
  • 这会同时跑起 Go 后端与前端开发服务器,并把 Vue 打进桌面窗口。

  • 首次运行会生成 frontend/wailsjs 目录,里面是 TypeScript 的“桥接”代码(如何从 Vue 调 Go 方法)。这点在 Wails 模板/项目生成文档里也有说明。

项目架构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
idrac-console-gui/
├─ go.mod / go.sum # Go 依赖
├─ main.go # 程序入口,注册窗口、绑定后端对象
├─ app/ # (建议) 放你的 Go 业务逻辑
│ └─ app.go # 暴露给前端调用的方法
├─ wails.json # Wails 项目配置
└─ frontend/ # Vue3 + TS 前端
├─ package.json
├─ node_modules/ # 前端依赖安装目录
├─ src/
│ ├─ main.ts # Vue 应用入口
│ ├─ App.vue # 根组件
│ ├─ api/axios.ts # (我们要新建)Axios 实例
│ ├─ pages/... # 你的页面
│ ├─ router/index.ts # (可选)Vue Router
│ └─ stores/... # (可选)Pinia
└─ wailsjs/ # 自动生成的 Go 调用桥
└─ go/main/App.ts

六、后端:用 Go 暴露方法给前端(Wails “桥”)

创建 app/app.go(如果目录不存在就建一个),示例:

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
// 文件: app/app.go
package app

import "context"

// App 是暴露给前端的对象(注意: 导出字段/方法需大写)
type App struct {
ctx context.Context
}

// NewApp 创建实例(Wails 会在 main.go 里用到)
func NewApp() *App {
return &App{}
}

// Startup 在应用启动时被调用,可保存上下文
func (a *App) Startup(ctx context.Context) {
a.ctx = ctx
}

// Greet 示例:前端可通过 wailsjs 调用这个方法
func (a *App) Greet(name string) string {
if name == "" {
name = "朋友"
}
return "你好," + name + "!这是来自 Go 后端的问候。"
}

main.go 里把这个对象挂到 Wails 应用上(模板已有类似代码,我们补齐关键点):

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
// 文件: main.go(示例关键片段)
package main

import (
"embed"
"log"

"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
"idrac-console-ui/app" // 引入你刚创建的包
)

//go:embed frontend/dist
var assets embed.FS

func main() {
myApp := app.NewApp()

err := wails.Run(&options.App{
Title: "idrac-console-ui",
Width: 1200,
Height: 800,
AssetServer: &options.AssetServer{
Assets: assets,
},
OnStartup: myApp.Startup,
Bind: []interface{}{ myApp }, // 绑定到前端
})
if err != nil {
log.Fatal(err)
}
}

七、前端:在 Vue 里调用 Go 方法 + 使用 Axios

1.新建一个 Axios 实例(frontend/src/api/axios.ts):

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
// 文件: frontend/src/api/axios.ts
// 作用:集中创建和配置 Axios 实例(统一 baseURL、超时、拦截器)
import axios from "axios";

const api = axios.create({
// 如果你要访问外部 REST 服务,配置它的地址
baseURL: "http://127.0.0.1:8080", // 示例:外部服务地址(不是 Wails 内置 Go)
timeout: 10000,
});

// 请求拦截器(附加认证头等)
api.interceptors.request.use((config) => {
// 示例:如果你有 token,可以这样加
// const token = localStorage.getItem("token")
// if (token) config.headers.Authorization = `Bearer ${token}`
return config;
});

// 响应拦截器(统一错误弹窗/日志)
api.interceptors.response.use(
(resp) => resp,
(err) => {
console.error("API Error:", err);
// 这里可以根据 err.response?.status 做统一处理
return Promise.reject(err);
}
);

export default api;

2.在 Vue 组件里:既可以调用 Go,也可以调用 Axios
假设在 frontend/src/pages/Home.vue:

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
<script setup lang="ts">
// 1) 调用 Go 后端(通过 wailsjs 自动生成的桥)
// 路径以你的模块名为准:通常在 frontend/wailsjs/go/main/App.ts
import { Greet } from "@/wailsjs/go/main/App";

// 2) 调用外部 REST(通过 Axios)
import api from "@/src/api/axios";
import { ref } from "vue";

const hello = ref("");
const serverList = ref<any[]>([]);

// 从 Go 获取一段问候
async function callGoGreet() {
hello.value = await Greet("zeromiss");
}

// 从外部 API 获取数据(只是示例)
async function fetchServers() {
const { data } = await api.get("/api/servers");
serverList.value = data;
}
</script>

<template>
<div class="p-4">
<h1>首页</h1>
<button @click="callGoGreet">调用 Go: Greet</button>
<p>{{ hello }}</p>

<button @click="fetchServers">请求外部API: /api/servers</button>
<pre>{{ serverList }}</pre>
</div>
</template>

<style scoped>
.p-4 { padding: 16px; }
</style>

重要理解:

  • 前端 ↔ Go(应用内):通过 Wails 生成的 wailsjs,像调用本地函数一样。
  • 前端 ↔ 外部 REST(例如你将来要调 iDRAC 相关网关服务):通过 Axios,走 HTTP 请求。

两者互不冲突,你可以把敏感逻辑放在 Go 里(更安全),Vue 负责界面和交互。

八、路由与页面(可选但强烈建议)

创建 frontend/src/router/index.ts:

1
2
3
4
5
6
7
8
9
10
11
12
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import Home from "@/src/pages/Home.vue";

const routes: RouteRecordRaw[] = [
{ path: "/", name: "home", component: Home },
// { path: "/idrac/:host", component: () => import("@/src/pages/IdracConsole.vue") }
];

export default createRouter({
history: createWebHashHistory(), // 桌面应用用 hash 更省心
routes,
});

在 frontend/src/main.ts 里挂上:

1
2
3
4
5
import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";

createApp(App).use(router).mount("#app");

python 虚拟环境配置

概述

示例

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
# 1. 创建项目目录
mkdir my_project
cd my_project

# 2. 创建虚拟环境
python3 -m venv venv

# 3. 激活环境
source venv/bin/activate

# 4. 升级pip
pip install --upgrade pip

# 5. 安装需要的包
pip install requests

# 6. 保存依赖列表
pip freeze > requirements.txt

# 7. 清除缓存
pip cache purge

# 8. 工作完成后退出
deactivate


添加永久镜像源

1
2
3
4
5
6
7
8
9
# 创建配置目录
mkdir -p ~/.pip

# 创建配置文件
cat > ~/.pip/pip.conf << EOF
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF

go map使用

概述

go 语言中的 map 是一种无序的键值对的集合。它就像 python 中的字典,使用 key-value 的形式存储数据。map 和其他基本类型的区别是,map 的声明需要使用 make()函数,而不是使用 var 声明,这点和其他语言很不一样。

Go 语言 Map 使用总结

什么是 Map?

Map 是 Go 语言中的内建数据类型,类似于其他语言中的哈希表、字典或关联数组。它存储键值对,提供快速的查找、插入和删除操作。

Map 的基本语法

1. 声明和初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 方式1: 使用make函数
m1 := make(map[string]int)

// 方式2: 字面量初始化
m2 := map[string]int{
"apple": 5,
"banana": 3,
}

// 方式3: 声明后初始化
var m3 map[string]int
m3 = make(map[string]int)

// 方式4: 空map
m4 := map[string]int{}

2. 基本操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
m := make(map[string]int)

// 添加/修改
m["key"] = 100

// 获取
value := m["key"]

// 检查是否存在
if value, exists := m["key"]; exists {
fmt.Println("存在:", value)
}

// 删除
delete(m, "key")

// 获取长度
length := len(m)

3. 遍历 Map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
m := map[string]int{"a": 1, "b": 2, "c": 3}

// 遍历键值对
for key, value := range m {
fmt.Printf("%s: %d\n", key, value)
}

// 只遍历键
for key := range m {
fmt.Println(key)
}

// 只遍历值
for _, value := range m {
fmt.Println(value)
}

重要特性和注意事项

1. Map 的零值是 nil

1
2
3
4
5
6
7
8
9
var m map[string]int
fmt.Println(m == nil) // true

// 不能向nil map写入,会panic
// m["key"] = 1 // panic!

// 必须先初始化
m = make(map[string]int)
m["key"] = 1 // 正常

2. Map 不是线程安全的

1
2
3
4
// 并发环境下需要使用sync.Map或加锁
var safeMap sync.Map
safeMap.Store("key", "value")
value, ok := safeMap.Load("key")

3. 遍历顺序是随机的

1
2
// Map的遍历顺序每次可能不同
// 如需有序遍历,应先对键排序

4. 键必须是可比较类型

1
2
3
4
5
6
7
// 可以作为键的类型:
// - 基本类型:bool, int, float, string
// - 数组(元素可比较)
// - 结构体(所有字段可比较)

// 不能作为键的类型:
// - slice, map, function

高级用法

1. 嵌套 Map

1
2
3
4
nestedMap := map[string]map[string]int{
"group1": {"item1": 10, "item2": 20},
"group2": {"item3": 30, "item4": 40},
}

2. Map 作为集合

1
2
3
4
5
6
7
8
9
set := map[string]bool{
"apple": true,
"banana": true,
}

// 检查元素是否在集合中
if set["apple"] {
fmt.Println("apple在集合中")
}

3. Map 的值为切片

1
2
3
4
groupItems := map[string][]string{
"fruits": {"apple", "banana"},
"vegetables": {"carrot", "potato"},
}

性能优化建议

  1. 预分配容量make(map[K]V, capacity)
  2. 避免频繁查找:一次查找,多次使用
  3. 选择合适的键类型:整数键比字符串键性能更好
  4. 大结构体使用指针:避免值复制

常见应用场景

  1. 缓存系统:键值对存储
  2. 计数器:统计频率
  3. 索引:快速查找
  4. 配置管理:存储配置项
  5. 去重:利用键的唯一性

最佳实践

  1. 始终检查 nil map
  2. 使用 comma ok 语法检查键是否存在
  3. 并发场景下使用适当的同步机制
  4. 需要有序遍历时对键进行排序
  5. 大数据量时考虑预分配容量

这就是 Go 语言中 Map 的完整使用指南!Map 是非常强大和常用的数据结构,掌握它对 Go 编程非常重要。

git ssh不可用时

概述

git ssh 不可用时用https方式

使用

  1. 换成 HTTPS 方式(最简单)

如果你不强依赖 SSH,可以直接改用 HTTPS:

1
git remote set-url origin https://gitee.com/你的用户名/你的仓库名.git
  1. 使用 SSH 443 端口

Gitee 提供了基于 443 端口的 SSH 连接方式,绕过 22 端口限制。

修改 ~/.ssh/config 文件(如果没有就新建):

1
2
3
4
Host gitee.com
HostName gitee.com
User git
Port 443

然后在运行

1
git push -u origin master

find 命令

概述

find 命令可以根据指定的条件,去文件系统中搜寻符合条件的文件

使用

1
2
3
4
5
6
find -type f # 查找普通文件
find -type d # 查找文件夹
find / -perm 644 # 查找权限为644的文件
find -atime +1 -size 50k # 查找最后修改时间大于1天的文件,同时是50k
find -maxdepth 3 -type d -name "nginx" # 查找名字为nginx的文件夹,最大搜索深度是三级
find -maxdepth 3 -type d -not -name "nginx" # 查找名字不包含nginx的文件夹,最大搜索深度是三级
1
2
3
4
5
6
find /* -name "nginx"

# 查找根下所有的nginx目录参数说明

-name
根据文件名查找文件,-name 后面可以使用通配符
1
2
3
4
find ~/ -type f -name "1.txt" 2>/dev/null -exec sh -c 'echo "hello" >> {}' \;
# 查找当前目录下的所有.txt文件,并且给这些文件后面添加内容
find ~/ -type f -name "1.txt" 2>/dev/null | xargs -I@ sh -c 'echo "hello" > @'
# 第二个方式,上面方式不支持对文件夹处理

python 打包exe

概述

打包exe 是为了方便用户使用,不需要安装 python 环境,直接运行 exe 文件即可。
python 打包exe 需要使用 pyinstaller 模块

安装 pyinstaller

1
pip install pyinstaller

打包

找到要打包的 python 文件,在文件目录下打开终端,执行以下命令

1
pyinstaller 

夜莺架构

Linux服务器监控系统

一个基于容器化的Linux服务器监控解决方案,使用VictoriaMetrics作为时序数据库,夜莺(Nightingale)作为监控平台。

🎯 项目目标

  • 🖥️ 多服务器监控: 支持大规模Linux服务器集群监控
  • 📊 自定义指标: 支持自定义业务指标采集和上传
  • 🔄 容器化部署: 通过Docker容器快速部署到各个服务器
  • 📈 可视化展示: 通过夜莺平台进行数据可视化和告警
  • 🚀 高性能存储: 使用VictoriaMetrics提供高性能时序数据存储

🏗️ 系统架构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│ Linux服务器1 │ │ Linux服务器2 │ │ Linux服务器N │
│ │ │ │ │ │
│ ┌─────────────┐ │ │ ┌─────────────┐ │ │ ┌─────────────┐ │
│ │监控代理容器 │ │ │ │监控代理容器 │ │ │ │监控代理容器 │ │
│ │- node-exp │ │ │ │- node-exp │ │ │ │- node-exp │ │
│ │- vmagent │ │ │ │- vmagent │ │ │ │- vmagent │ │
│ │- custom │ │ │ │- custom │ │ │ │- custom │ │
│ └─────────────┘ │ │ └─────────────┘ │ │ └─────────────┘ │
└─────────┬───────┘ └─────────┬───────┘ └─────────┬───────┘
│ │ │
└──────────────────────┼──────────────────────┘

┌─────────────▼─────────────┐
│ VictoriaMetrics │
│ 时序数据库 │
└─────────────┬─────────────┘

┌─────────────▼─────────────┐
│ 夜莺(Nightingale) │
│ 监控平台 & 可视化 │
└───────────────────────────┘

📦 项目结构

1
2
3
4
5
6
7
8
9
shell/
├── monitoring-agent/ # 监控代理配置
│ ├── docker-compose-agent.yaml # 代理容器编排文件
│ └── entrypoint.sh # 自定义指标采集脚本
├── prometheus.yml # Prometheus配置文件
├── docker-compose-vm.yaml # VictoriaMetrics服务器配置
├── deploy_monitoring.sh # 一键部署脚本
├── nightingale-config.md # 夜莺配置指南
└── README.md # 项目说明文档

🚀 快速开始

1. 部署VictoriaMetrics服务器

在您的中央服务器上部署VictoriaMetrics:

1
2
3
4
5
6
7
8
9
10
11
12
# 克隆项目
git clone <your-repo>
cd shell

# 修改配置
vim docker-compose-vm.yaml # 修改端口和存储配置

# 启动VictoriaMetrics服务器
docker compose -f docker-compose-vm.yaml up -d

# 验证部署
curl http://localhost:8428/api/v1/label/__name__/values

2. 部署监控代理到Linux服务器

在每台需要监控的Linux服务器上运行:

1
2
3
4
5
6
7
8
9
# 下载部署脚本
wget https://your-server/deploy_monitoring.sh
chmod +x deploy_monitoring.sh

# 修改VictoriaMetrics服务器地址
vim deploy_monitoring.sh # 修改VM_URL变量

# 一键部署监控代理
./deploy_monitoring.sh

3. 配置夜莺数据源

参考 nightingale-config.md 文档配置夜莺:

1
2
3
# 在夜莺中添加数据源
# URL: http://your-vm-server:8428
# 类型: Prometheus

📊 自定义指标上传

方法1: 使用脚本上传

1
2
3
4
5
6
7
8
# 上传CPU负载指标
/opt/monitoring/scripts/upload_custom_metrics.sh m_1010_cpu_load 1.25 'type="1min"'

# 上传内存使用率
/opt/monitoring/scripts/upload_custom_metrics.sh m_1010_memory_usage 75.5

# 上传磁盘使用率
/opt/monitoring/scripts/upload_custom_metrics.sh m_1010_disk_usage 45 'mount="/"'

方法2: 直接API调用

1
2
3
4
5
6
7
8
9
# 构造指标数据
HOSTNAME=$(hostname)
TIMESTAMP=$(date +%s)000
METRIC_VALUE="1.25"

# 上传到VictoriaMetrics
curl -X POST "http://vm-server:8428/api/v1/import/prometheus" \
-H "Content-Type: text/plain" \
-d "m_1010_cpu_load{hostname=\"$HOSTNAME\",type=\"1min\"} $METRIC_VALUE $TIMESTAMP"

方法3: 程序中集成

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import requests
import time
import socket

def upload_metric(metric_name, value, labels=None):
hostname = socket.gethostname()
timestamp = int(time.time() * 1000)

if labels:
label_str = f'hostname="{hostname}",' + labels
else:
label_str = f'hostname="{hostname}"'

metric_line = f'{metric_name}{{{label_str}}} {value} {timestamp}'

response = requests.post(
'http://vm-server:8428/api/v1/import/prometheus',
headers={'Content-Type': 'text/plain'},
data=metric_line
)
return response.status_code == 204

# 使用示例
upload_metric('m_1010_cpu_load', 1.25, 'type="1min"')

🔍 数据查询

PromQL查询示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 查询CPU负载
m_1010_cpu_load{type="1min"}

# 查询特定主机的内存使用率
m_1010_memory_usage{hostname="server1"}

# 查询磁盘使用率超过80%的主机
m_1010_disk_usage > 80

# 计算平均CPU负载
avg(m_1010_cpu_load{type="1min"})

# 查询过去1小时的CPU负载趋势
m_1010_cpu_load{type="1min"}[1h]

API查询示例

1
2
3
4
5
6
7
8
9
10
# 查询当前CPU负载
curl -G "http://vm-server:8428/api/v1/query" \
--data-urlencode "query=m_1010_cpu_load{type=\"1min\"}"

# 查询时间范围数据
curl -G "http://vm-server:8428/api/v1/query_range" \
--data-urlencode "query=m_1010_cpu_load{type=\"1min\"}" \
--data-urlencode "start=$(date -d '1 hour ago' +%s)" \
--data-urlencode "end=$(date +%s)" \
--data-urlencode "step=60"

🎛️ 夜莺可视化

1. 添加数据源

  • 进入夜莺管理界面
  • 系统管理 -> 数据源管理 -> 新增数据源
  • URL: http://your-vm-server:8428
  • 类型: Prometheus

2. 创建仪表盘

  • 仪表盘管理 -> 新建仪表盘
  • 添加Panel,使用上述PromQL查询语句

3. 配置告警规则

  • 告警管理 -> 新建规则
  • 查询语句: m_1010_cpu_load{type="1min"} > 2
  • 持续时间: 5m

🔧 高级配置

自定义指标规范

建议的指标命名规范:

  • m_<业务编号>_<指标名>
  • 示例: m_1010_cpu_load, m_2020_api_response_time

标签规范

推荐的标签:

  • hostname: 主机名
  • type: 指标类型
  • mount: 挂载点(磁盘相关)
  • interface: 网络接口(网络相关)

性能优化

  1. 数据保留策略: 修改VictoriaMetrics的retentionPeriod
  2. 采集频率: 调整prometheus.yml中的scrape_interval
  3. 内存限制: 设置VictoriaMetrics的内存使用限制

🐛 故障排查

常见问题

  1. 指标无法上传

    1
    2
    3
    4
    5
    # 检查VictoriaMetrics连接
    curl http://vm-server:8428/api/v1/query?query=up

    # 检查容器状态
    docker ps | grep victoria
  2. 夜莺无法连接数据源

    1
    2
    3
    4
    5
    # 检查网络连通性
    telnet vm-server 8428

    # 检查防火墙设置
    iptables -L | grep 8428
  3. 自定义指标丢失

    1
    2
    # 查看指标列表
    curl http://vm-server:8428/api/v1/label/__name__/values | grep m_1010

日志查看

1
2
3
4
5
6
7
8
# 查看VictoriaMetrics日志
docker logs victoriametrics

# 查看监控代理日志
docker logs custom-metrics-collector

# 查看vmagent日志
docker logs vmagent

📈 性能指标

  • 数据压缩率: VictoriaMetrics提供20:1的压缩比
  • 查询性能: 支持百万级时间序列的秒级查询
  • 存储效率: 比Prometheus节省50%以上存储空间
  • 内存使用: 比Prometheus减少70%内存使用

🤝 贡献指南

  1. Fork 项目
  2. 创建特性分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m 'Add some AmazingFeature')
  4. 推送分支 (git push origin feature/AmazingFeature)
  5. 提交Pull Request

📄 许可证

本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情

📞 支持与联系


🎉 更新日志

v1.0.0 (2024-01-15)

  • ✨ 初始版本发布
  • 🚀 支持VictoriaMetrics时序数据库
  • 📊 集成夜莺监控平台
  • 🐳 完整的容器化部署方案
  • 📝 详细的配置文档和使用指南

Nginx 一些配置介绍

Nginx 一些配置介绍

gzip 介绍

开启 gzip 后,Nginx 会在将响应返回给客户端前,对响应内容进行 压缩处理。这样可以减小数据传输体积,提升访问速度,尤其在移动网络下更明显。下面我详细说明其 工作机制 和相关参数:

1
2
3
4
5
6
7
8
9
10
11
gzip on;  # 启用 gzip 压缩

gzip_min_length 1024; # 最小压缩大小(小于此值的内容不会压缩)

gzip_comp_level 5; # 压缩等级(1-9,越高压缩率越好但 CPU 占用越高,推荐 4~6)

gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; # 指定哪些类型的响应启用 gzip

gzip_disable "MSIE [1-6]\."; # 禁用旧版 IE 浏览器的 gzip(它们支持不好)

gzip_vary on; # 告诉缓存服务器保存 gzip 和非 gzip 两种版本(设置响应头:Vary: Accept-Encoding)

Harbor 使用

1
2
3
4
5
6
7
8
9
10
11
# 登录 Harbor
docker login harbor.yourdomain.com

# 打标签
docker tag nginx:latest harbor.yourdomain.com/myproject/nginx:latest

# 上传
docker push harbor.yourdomain.com/myproject/nginx:latest

# 下载
docker pull harbor.yourdomain.com/myproject/nginx:latest

查看本地镜像的标签

1
docker images
1
2
3
4
REPOSITORY                    TAG       IMAGE ID       CREATED         SIZE
mynginx ssl 375ac92326a5 23 hours ago 192MB
nginx.baby/myproject/nginx ssl 375ac92326a5 23 hours ago 192MB
nginx latest 2cd1d97f893f 2 weeks ago 192MB
1
docker inspect nginx.baby/myproject/nginx:ssl

查看更详细的信息,包括 tag、镜像大小、构建层

确认 Docker 是否登录 harbor

1
cat ~/.docker/config.json
1
2
3
4
5
6
7
{
"auths": {
"harbor.yourdomain.com": {
"auth": "xxxxxxxxxxxx"
}
}
}

Docker 进阶用法

查询容器

1
2
3
docker ps -aqf "ancestor=mysql:8.0" # 找出镜像是 mysql:8.0 的容器
docker ps -aqf "status=exited" # 找出已退出的容器
docker ps -aqf "name=^mysql8" # 找出名字以 mysql8 开头的容器
  • exited:容器已退出

  • running:容器正在运行

  • created:容器已创建但未运行

  • paused:容器被暂停

  • restarting:容器正在重启

  • dead:容器进程已经终止但还未清理

查询容器重启策略

1
docker run -itd --name test_nginx --restart unless-stopped test_nginx_image_backup
1
docker inspect -f '{{.HostConfig.RestartPolicy.Name}}' <容器名\id>
1
docker update --restart=always <容器名\id>    # 修改已有容器的重启策略
策略 说明 系统重启后 异常退出 正常退出 手动停止后
no(默认) 不自动重启 N/A
always 总是自动重启 会重启
on-failure[:N] 非 0 退出才重启,最多 N 次
unless-stopped 总是自动重启,但如果你手动停过它,就不再启动 不会重启

一串命令

docker

更新容器策略

1
2
3
docker ps -a --format '{{.ID}} {{.Names}}'| grep -v -E 'mynginx_ssl|test_nginx_ssl' | awk '{print $1}' | xargs -I {} docker update --restart=unless-stopped {}

(--format {{.ID}} {{.Names}} 为显示 ID 和名称)

harbor的安装

安装依赖

1
2
3
4
5
6
apt update &&  apt install -y \
docker.io \
docker-compose \
curl \
openssl \
jq

下载harbor解压

1
2
3
4
wget https://github.com/goharbor/harbor/releases/download/v2.9.3/harbor-online-installer-v2.9.3.tgz

tar -zxvf harbor-online-installer-v2.9.3.tgz
cd harbor

配置harbor

1
cp harbor.yml.tmpl harbor.yml

建议修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
hostname: nginx.baby  # 改成你自己的域名或公网 IP(如47.xx.xx.xx)

https:
port: 443
certificate: /apps/htpass/ssl/nginx.baby.pem # 后续你需要放置的证书路径
private_key: /apps/htpass/ssl/nginx.baby.key

# 若你不想使用 HTTPS(仅测试环境),可以关闭:
# 注释掉 https: 段,并取消 http 的注释
# http:
# port: 80

harbor_admin_password: Harbor12345 # 管理员密码

data_volume: /data

安装和访问

1
./install.sh
1
https://your.domain.com
  • 用户名: admin
  • 密码: 设置里的:hrabor_admin_password

开机启动

1
2
3
crontab -e

@reboot cd /path/to/harbor && docker-compose up -d