前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于Golang + Ansible构建Nginx管理系统

基于Golang + Ansible构建Nginx管理系统

作者头像
用户1107783
发布2024-08-19 15:10:40
1890
发布2024-08-19 15:10:40
举报
文章被收录于专栏:云原生运维社区

背景

基于Golang + Ansible构建作业系统的文章发布已有一段时间,Ansible 在运维自动化领域的应用场景非常广泛。这次,我们将通过 Golang 和 Ansible 构建一个支持多集群的 Nginx 管理系统,无需登录服务器实现对Nginx进行管理。利用周末时间开发了这个小系统,供大家参考与学习。

技术栈

  • 前端:Vue+Typescript+ArcoDesign
  • 后端:Golang+Gin+Gorm+Go Template
  • 依赖:Ansible

主要功能

Go Templete使用指南

Go Template 是 Go 语言中用于生成文本或 HTML 内容的模板引擎。它提供了一种灵活的方式来处理动态内容生成,可以在 Go 语言中嵌入模板代码,从而在运行时将数据渲染成字符串输出。Go Template 被广泛用于生成 HTML、JSON、XML 或其他文本格式的内容,尤其是在 Web 开发中常见。它的主要特点包括:

  1. 简单的语法:Go Template 的语法非常简单,易于学习和使用。它支持条件语句(如 if)、循环(如 range)、以及自定义函数。
  2. 类型安全:Go Template 是类型安全的,这意味着模板中的变量必须符合预期的数据类型,否则会产生编译错误。
  3. 强大的函数支持:Go Template 提供了一系列内置函数(如 len、print、index 等),同时允许开发者注册自定义函数,从而增强模板的功能。
  4. 分离数据和视图:Go Template 强调将数据逻辑与视图分离,这有助于保持代码的可读性和可维护性。
  5. 高效性:Go Template 是 Go 标准库的一部分,因此其性能非常高,适合大规模的文本生成需求。

一个简单的 Go Template 示例如下:

代码语言:javascript
复制
package main

import (
    "os"
    "text/template"
)

func main() {
    tmpl, err := template.New("example").Parse("Hello, {{.Name}}!")
    if err != nil {
        panic(err)
    }

    data := struct {
        Name string
    }{
        Name: "World",
    }

    err = tmpl.Execute(os.Stdout, data)
    if err != nil {
        panic(err)
    }
}

这个程序会输出 Hello, World!。模板中的 {{.Name}} 占位符被数据结构中的 Name 字段替换成了 "World"。

在Nginx管理系统中通过Go Template生成Nginx相应的配置文件,代码如下:

代码语言:javascript
复制
package main

import (
 "os"
 "text/template"
)

// 定义Location结构体,表示每个location块
type Location struct {
 Path      string
 ProxyPass string
}

// 定义ServerConfig结构体,表示Nginx服务器配置
type ServerConfig struct {
 ServerName string
 Locations  []Location
}

const nginxTemplate = `server {
    listen 80;
    server_name {{ .ServerName }};

    {{ range .Locations }}
    location {{ .Path }} {
        proxy_pass http://{{ .ProxyPass }};
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    {{ end }}
}`

func main() {
 // 准备数据
 config := ServerConfig{
  ServerName: "example.com",
  Locations: []Location{
   {
    Path:      "/api/",
    ProxyPass: "api.example.com",
   },
   {
    Path:      "/app/",
    ProxyPass: "app.example.com",
   },
  },
 }

 // 解析模板
 tmpl, err := template.New("nginxConfig").Parse(nginxTemplate)
 if err != nil {
  panic(err)
 }

 // 生成配置并输出到标准输出
 err = tmpl.Execute(os.Stdout, config)
 if err != nil {
  panic(err)
 }
}

生成的 Nginx 配置文件示例 :

代码语言:javascript
复制
server {
    listen 80;
    server_name example.com;

    
    location /api/ {
        proxy_pass http://api.example.com;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    
    location /app/ {
        proxy_pass http://app.example.com;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    
}

功能展示

主机管理(Nginx节点主机)

创建主机:

主机列表:

集群管理

创建集群:

集群列表:

服务管理(Nginx Upstream)

创建服务:

服务列表:

服务详情:

当创建或编辑后则会生成Upstream配置存储于数据库。

路由配置(Nginx Config)

创建路由:

编辑路由:

路由列表:

路由详情:

当创建或编辑后则会生成Nginx配置存储于数据库。

同步配置:

重载配置:

接下来登录服务器确认一下是否生效:

代码语言:javascript
复制
$ cat /application/nginx/conf/upstream/kubesre-web.conf 

 upstream kubesre-web {
  server 192.168.1.1:8080;
  server 192.168.1.2:8080;
  server 192.168.1.3:8080;
 }

$ cat /application/nginx/conf/conf.d/kubesre-router.conf 
server {
    listen 80;
    server_name www.kubesre.com;

    
    location / {
        proxy_pass http://kubesre-web;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
    
}

证书管理

创建证书:

证书列表:

证书详情:

敬请期待后续文章!

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-08-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 云原生运维圈 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 技术栈
  • 主要功能
  • Go Templete使用指南
  • 功能展示
    • 主机管理(Nginx节点主机)
      • 集群管理
        • 服务管理(Nginx Upstream)
          • 路由配置(Nginx Config)
            • 证书管理
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档