生产环境keepalived.conf 文件使用golang模板渲染示例

//执行渲染代码样例
import "text/template"
type LvsData struct {
    Laddr_v4    []string
    Laddr_v6    []string
    Naddr_v4    string
    Naddr_v6    string
    Static_addr []string
    Channel     []common.Lvsmap
    //special_channel []map[string]interface{}
}
type Lvsmap struct {
    Ip_type string `json:"ip_type"`
    Nat_type string `json:"nat_type"`
    Remark string `json:"remark"`
    Proto string `json:"proto"`
    Port string `json:"port"`
    Rport string `json:"rport"`
    Vip []string `json:"vip"`
    Sch_type string `json:"sch_type"`
    Rs []map[string]string `json:"rs"`
    rstag map[string]int
}
func RenderKeepalivedFile(nodeinfo common.NodeInfo) {
    temp := "D:\\GOPATH\\src\\github.com\\tele_test\\template\\keepalived.txt"
    keepfile := "D:\\GOPATH\\src\\github.com\\tele_test\\template\\keepalived.temp"
    responsefile := "D:\\GOPATH\\src\\github.com\\tele_test\\template\\keepalived.conf"

    v := LvsData{}
    v.Channel = nodeinfo.Channel
    v.Laddr_v4 = common.GenAddrInfo().LaddrIpv4 // 获取lvs用于流量转发ipv4的ip
    v.Laddr_v6 = common.GenAddrInfo().LaddrIpv6
    v.Naddr_v4,_ = common.Config.String("Node","NodeIp4")
    v.Naddr_v6,_ = common.Config.String("Node","NodeIp6")
    v.Static_addr = common.GenAddrInfo().StaticIpaddress

    tmpl, err := template.ParseFiles(temp)
    if err != nil {
        panic(err)
    }
    err = tmpl.Execute(os.Stdout, v)
    if err != nil {
        panic(err)
    }
    file, err := os.OpenFile(keepfile, os.O_CREATE|os.O_WRONLY, 0755)
    if err != nil {
        panic(err)
    }
    tmpl.Execute(file, v)
    err = common.DeleteBlankFile(keepfile, responsefile)
    if err != nil {
        panic(err)
    }
}

模板文件编写示例:

{{$proto := "TCP"}} {{$ip_type :="ipv4"}} {{$remark :="backtosource"}} {{$Survi1 := "TCP_CHECK" }} {{$Survi2 := "HTTP_GET" }} {{$Survi3 := "SSL_GET" }}
local_address_group laddr_v4 {
    {{ range .Laddr_v4 }}
    {{ . }}
    {{ end }}
}

local_address_group laddr_v6 {
    {{ range .Laddr_v6 }}
    {{ . }}
    {{ end }}
}
local_address_group naddr_v4 {
    {{.Naddr_v4}}
}
local_address_group naddr_v6 {
    {{.Naddr_v6}}
}

static_ipaddress {
    {{ range .Static_addr }}
    {{ . }}
    {{ end }}
}

{{ range .Channel }}
  {{$port:=.Port}}
  virtual_server_group ipv4_vip_group_{{.Port}}_{{.Rport}} {
    {{ range .Vip }}
    {{.}} {{$port}}
    {{end}}
  }
  virtual_server group ipv4_vip_group_{{ .Port }}_{{.Rport}} {
    delay_loop 5
    lb_algo {{ .Sch_type }}
    lb_kind {{ .Nat_type }}
    protocol TCP
    {{if eq .Proto $proto }}
    syn_proxy
    virtualhost lvscheck.xitong.qihoo.net
    {{ end }}
    {{if eq .Ip_type $ip_type}}
     {{if eq .Remark $remark}}
    laddr_group_name naddr_v4
     {{else}}
    laddr_group_name laddr_v4
     {{end}}
    {{else}}
     {{if eq .Remark $remark}}
    laddr_group_name naddr_v6
     {{else}}
    laddr_group_name laddr_v6
     {{end}}
    {{end}}

    {{ range .Rs }}
     real_server {{ .ip }} {{ .port }} {
      weight {{ .weight }}
      {{ if eq .survi $Survi1}}
       TCP_CHECK {
        connect_port {{ .port }}
        connect_timeout 5
      }
      {{ end }}
      {{ if eq .survi $Survi2}}
        HTTP_GET {
         url {
             path /status.html
             digest {{ .md5 }}
         }
         connect_port {{ .port }}
         connect_timeout 3
         nb_get_retry 3
         delay_before_retry 3
        }
      {{ end }}
      {{ if eq .survi $Survi3}}
       SSL_GET {
        url {
            path /status.html
            digest {{ .md5 }}
        }
        connect_port {{ .port }}
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        }
      {{ end }}
     }
    {{ end }}
  }

{{ end }}


有疑问加站长微信联系(非本文作者)