Files
ConfTemplate/src/schemas/haproxy.js
T
cnbugs d80d33e6f6 feat: 新增 15 个服务组件,总计 59 个
系统服务: NTP, DHCP, BIND, NFS, SAMBA, RSYNC, Sersync, vsftpd, Subversion
数据库中间件: Mycat
CI/CD: GitLab, Jenkins
高可用: Keepalived, HAProxy, LVS

新增 4 个分类: 系统服务、数据库中间件、CI/CD、高可用
2026-07-19 16:11:09 +08:00

319 lines
8.3 KiB
JavaScript

export const haproxySchema = {
id: 'haproxy',
name: 'HAProxy',
icon: 'Connection',
category: '高可用',
description: '高性能 TCP/HTTP 负载均衡器和代理服务器',
format: 'cfg',
fileName: 'haproxy.cfg',
groups: [
{
title: 'Global 全局配置',
fields: [
{
key: 'globalMaxconn',
label: '最大连接数',
type: 'number',
min: 100,
max: 1000000,
default: 1000,
},
{
key: 'globalLog',
label: '日志输出',
type: 'text',
default: '127.0.0.1 local0',
},
{
key: 'globalChroot',
label: '启用 chroot',
type: 'switch',
default: false,
tip: '安全加固,限制进程访问范围',
},
{
key: 'chrootDir',
label: 'chroot 目录',
type: 'text',
default: '/var/lib/haproxy',
dependsOn: { key: 'globalChroot', value: true },
},
{
key: 'globalUser',
label: '运行用户',
type: 'text',
default: 'haproxy',
},
{
key: 'globalGroup',
label: '运行用户组',
type: 'text',
default: 'haproxy',
},
{
key: 'globalStatsEnable',
label: '启用 Stats Socket',
type: 'switch',
default: true,
},
{
key: 'statsSocket',
label: 'Stats Socket 路径',
type: 'text',
default: '/var/run/haproxy.sock',
dependsOn: { key: 'globalStatsEnable', value: true },
},
],
},
{
title: 'Defaults 默认配置',
fields: [
{
key: 'defaultsMode',
label: '代理模式',
type: 'select',
options: [
{ label: 'HTTP 模式', value: 'http' },
{ label: 'TCP 模式', value: 'tcp' },
],
default: 'http',
},
{
key: 'defaultsLog',
label: '日志模式',
type: 'text',
default: 'global',
},
{
key: 'defaultsOptionHttplog',
label: '启用 HTTP 日志',
type: 'switch',
default: true,
},
{
key: 'defaultsOptionDontlognull',
label: '忽略空连接日志',
type: 'switch',
default: true,
},
{
key: 'defaultsTimeoutConnect',
label: '连接超时 (ms)',
type: 'number',
min: 100,
max: 60000,
default: 5000,
},
{
key: 'defaultsTimeoutClient',
label: '客户端超时 (ms)',
type: 'number',
min: 1000,
max: 300000,
default: 50000,
},
{
key: 'defaultsTimeoutServer',
label: '服务端超时 (ms)',
type: 'number',
min: 1000,
max: 300000,
default: 50000,
},
{
key: 'defaultsRetries',
label: '重试次数',
type: 'number',
min: 0,
max: 10,
default: 3,
},
],
},
{
title: 'Frontend 前端配置',
fields: [
{
key: 'frontendName',
label: '前端名称',
type: 'text',
default: 'myapp_front',
},
{
key: 'frontendBind',
label: '绑定地址',
type: 'text',
default: '*:80',
tip: '格式: 地址:端口',
},
{
key: 'frontendBackend',
label: '关联后端',
type: 'text',
default: 'myapp_back',
},
{
key: 'enableSsl',
label: '启用 SSL',
type: 'switch',
default: false,
},
{
key: 'sslBind',
label: 'SSL 绑定配置',
type: 'text',
default: '*:443 ssl crt /etc/haproxy/cert.pem',
dependsOn: { key: 'enableSsl', value: true },
},
],
},
{
title: 'Backend 后端配置',
fields: [
{
key: 'backendName',
label: '后端名称',
type: 'text',
default: 'myapp_back',
},
{
key: 'balanceType',
label: '负载均衡算法',
type: 'select',
options: [
{ label: 'roundrobin - 轮询 (推荐)', value: 'roundrobin' },
{ label: 'leastconn - 最少连接', value: 'leastconn' },
{ label: 'source - 源地址哈希', value: 'source' },
{ label: 'uri - URI 哈希', value: 'uri' },
],
default: 'roundrobin',
},
{
key: 'backendServer1',
label: '后端服务器 1',
type: 'text',
default: 'web1 192.168.1.10:8080 check',
tip: '格式: 名称 IP:端口 check',
},
{
key: 'backendServer2',
label: '后端服务器 2',
type: 'text',
default: 'web2 192.168.1.11:8080 check',
},
],
},
{
title: 'Stats 统计页面',
fields: [
{
key: 'enableStats',
label: '启用统计页面',
type: 'switch',
default: true,
},
{
key: 'statsUri',
label: '统计页面 URI',
type: 'text',
default: '/haproxy?stats',
dependsOn: { key: 'enableStats', value: true },
},
{
key: 'statsAuthEnable',
label: '统计页面认证',
type: 'switch',
default: true,
dependsOn: { key: 'enableStats', value: true },
},
{
key: 'statsUser',
label: '统计页面用户名',
type: 'text',
default: 'admin',
dependsOn: { key: 'statsAuthEnable', value: true },
},
{
key: 'statsPassword',
label: '统计页面密码',
type: 'text',
default: 'admin',
dependsOn: { key: 'statsAuthEnable', value: true },
},
],
},
],
}
export function generateHaproxyCfg(config) {
const lines = []
lines.push(`# HAProxy 配置文件 - 由 ConfTemplate 生成`)
lines.push(`# 生成时间: ${new Date().toLocaleString('zh-CN')}`)
lines.push(``)
// Global
lines.push(`global`)
lines.push(` log ${config.globalLog} local0`)
lines.push(` maxconn ${config.globalMaxconn}`)
lines.push(` user ${config.globalUser}`)
lines.push(` group ${config.globalGroup}`)
lines.push(` daemon`)
if (config.globalChroot) {
lines.push(` chroot ${config.chrootDir}`)
}
if (config.globalStatsEnable) {
lines.push(` stats socket ${config.statsSocket} mode 660 level admin`)
}
lines.push(``)
// Defaults
lines.push(`defaults`)
lines.push(` mode ${config.defaultsMode}`)
lines.push(` log ${config.defaultsLog}`)
if (config.defaultsOptionHttplog) {
lines.push(` option httplog`)
}
if (config.defaultsOptionDontlognull) {
lines.push(` option dontlognull`)
}
lines.push(` timeout connect ${config.defaultsTimeoutConnect}`)
lines.push(` timeout client ${config.defaultsTimeoutClient}`)
lines.push(` timeout server ${config.defaultsTimeoutServer}`)
lines.push(` retries ${config.defaultsRetries}`)
lines.push(``)
// Frontend
lines.push(`frontend ${config.frontendName}`)
lines.push(` bind ${config.frontendBind}`)
if (config.enableSsl) {
lines.push(` bind ${config.sslBind}`)
}
lines.push(` default_backend ${config.frontendBackend}`)
lines.push(``)
// Backend
lines.push(`backend ${config.backendName}`)
lines.push(` balance ${config.balanceType}`)
if (config.backendServer1) {
lines.push(` server ${config.backendServer1}`)
}
if (config.backendServer2) {
lines.push(` server ${config.backendServer2}`)
}
lines.push(``)
// Stats
if (config.enableStats) {
lines.push(`listen stats`)
lines.push(` bind *:8404`)
lines.push(` stats enable`)
lines.push(` stats uri ${config.statsUri}`)
if (config.statsAuthEnable) {
lines.push(` stats auth ${config.statsUser}:${config.statsPassword}`)
}
lines.push(` stats refresh 10s`)
}
return lines.join('\n')
}