gtm

package module
v0.0.0-...-edeeda6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2018 License: Apache-2.0 Imports: 10 Imported by: 0

README

A DNS Load Balancer by golang

Run it

gtm --config config.json

Configuration

{
  "domains": [
    {
        "name" : "xip.io.",
        "records": [
          {
            "name": "abc",
            "ips": [
                {
                  "address": "10.193.190.181"
                },
                {
                  "address": "10.193.148.200"
                }
            ],
            "ttl" : 5
          },
          {
            "name": "*",
            "ips": [
                {
                  "address": "10.193.190.103"
                },
                {
                  "address": "10.193.148.251"
                }
            ],
            "ttl" : 10,
            "health_check" : {
              "type": "layer4",
              "port": 443,
              "frequency": "5s"
            }
          }
        ]
    },
    {
          "name" : "example.io.",
          "records" : [
            {
              "name": "*",
              "ips": [
                  {
                    "address": "10.0.5.4"
                  },
                  {
                    "address": "10.0.5.5"
                  }
              ],
              "ttl" : 5
            }
          ]
    }
  ],
  "port" : 5050,
  "relay_server" : "8.8.8.8:53"
}
Domain and records
  • It supports wild card records E.g.
dig [anything].xip.io @localhost -p 5050

will results either 10.193.190.103 or 10.193.148.251

  • It supports a specify record and will take precedence over
    E.g.
dig abc.xip.io @localhost -p 5050

will results either 10.193.190.181 or 10.193.148.200

Port

Port Number DNS server should listen on.

Relay Server

If the records/domains not defined in the configured domains, they will be resolved from the relay server

Plugin in your own Load Balancing logic

Currently, the load balancing strategy is extremely naive:

var simpleLoadBalancer gtm.LoadBalancing = func(ips []gtm.IP) string {
  //Simple Round Robin
  return ips[rand.Intn(len(ips))].Address
}

The load balancing logic is pluggable as long as developer implement another load balancer method func(ips[]gtm.IP) string

Layer4 Health Check

For each record, a layer 4 health check endpoint can be configured

"health_check" : {
  "type": "layer4",
  "port": 5000,
  "frequency": "5s"
}

Layer7 Health Check

For each record, you can configure a layer7 health check. Health check will do http/https check and match the http return code.

"records": [
  {
    "name": "api.test-gtm",
    "ips": [
        {
          "address": "10.193.190.103",
          "layer7_health_check_host": "simple-api.pks.nsx.shaozhenpcf.com"
        },
        {
          "address": "10.193.148.251",
          "layer7_health_check_host": "simple-api.pas.nsx-t.shaozhenpcf.com"
        }
    ],
    "ttl" : 5,
    "health_check" : {
      "type": "layer7",
      "https": true,
      "skip_ssl": true,
      "frequency": "5s",
      "path": "/whichcf",
      "http_status_code": 200
    }
  }
]

Documentation

Index

Constants

View Source
const WILD_CARD string = "*"

Variables

This section is empty.

Functions

func LBAnswer

func LBAnswer(records []*Record, getRecord SelectRecord, domain string) func(loadBalancer LoadBalancing) GetAnswer

LBAnswer Given ips and ttl configuration, return a Get Answer func

Types

type Config

type Config struct {
	Domains     []*Domain `json:"domains"`
	Port        int       `json:"port"`
	RelayServer string    `json:"relay_server"`
}

func ParseConfig

func ParseConfig(reader io.Reader) (*Config, error)

type DNSClient

type DNSClient interface {
	Exchange(m *dns.Msg, address string) (r *dns.Msg, rtt time.Duration, err error)
}

DNSClient An interface of dns client

type DefaultHealthCheck

type DefaultHealthCheck struct {
	EndPoints []IP

	Frequency   time.Duration
	CheckHealth HealthCheckMethod
	SleepFunc   sleep
	// contains filtered or unexported fields
}

func (*DefaultHealthCheck) Receive

func (hk *DefaultHealthCheck) Receive() []IP

func (*DefaultHealthCheck) Start

func (hk *DefaultHealthCheck) Start()

Start Start HealthCheck

type DoNothingHealthCheck

type DoNothingHealthCheck struct {
	// contains filtered or unexported fields
}

func (*DoNothingHealthCheck) Receive

func (hk *DoNothingHealthCheck) Receive() []IP

Receive empty

func (*DoNothingHealthCheck) Start

func (hk *DoNothingHealthCheck) Start()

Start empty start method

type Domain

type Domain struct {
	DomainName string    `json:"name"`
	Records    []*Record `json:"records"`
}

type GetAnswer

type GetAnswer func(dns.Question) []dns.RR

type HealthCheck

type HealthCheck interface {
	Start()
	Receive() []IP
}

type HealthCheckConfig

type HealthCheckConfig struct {
	Type           string `json:"type"`
	PORT           int    `json:"port"`
	HTTPS          bool   `json:"https"`
	SkipSSL        bool   `json:"skip_ssl"`
	PATH           string `json:"path"`
	HTTPStatusCode int    `json:"http_status_code"`
	Fequency       string `json:"frequency"`
}

HealthCheckConfig Config for health check

type HealthCheckMethod

type HealthCheckMethod func(IP) bool

func Layer4HealthCheck

func Layer4HealthCheck(port int) HealthCheckMethod

func Layer7HealthCheck

func Layer7HealthCheck(check HttpCheckClient, schema string, path string, statusCode int) HealthCheckMethod

type HttpCheckClient

type HttpCheckClient interface {
	Get(string) (*http.Response, error)
}

type IP

type IP struct {
	Address string `json:"address"`
	Host    string `json:"layer7_health_check_host"`
}

type LoadBalancing

type LoadBalancing func([]IP) string

LoadBalancing define how to load balance

type Record

type Record struct {
	Name              string            `json:"name"`
	IPs               []IP              `json:"ips"`
	TTL               int               `json:"ttl"`
	HealthCheckConfig HealthCheckConfig `json:"health_check"`
	HealthCheck       HealthCheck
}

func DefaultSelectRecord

func DefaultSelectRecord(q dns.Question, records []*Record, domain string) *Record

func (*Record) UnmarshalJSON

func (r *Record) UnmarshalJSON(data []byte) error

UnmarshalJSON Record parsing the data

type RelayDNSCLient

type RelayDNSCLient struct {
	Client DNSClient
}

RelayDNSClient A client to relay dns request to another DNS server

func (*RelayDNSCLient) RelayAnswer

func (c *RelayDNSCLient) RelayAnswer(server string) GetAnswer

type SelectRecord

type SelectRecord func(q dns.Question, records []*Record, domain string) *Record

type ServeDNS

type ServeDNS func(dns.ResponseWriter, *dns.Msg)

func DNSRequest

func DNSRequest(answerFunc GetAnswer) ServeDNS

DNSRequest Serve DNS Request

Directories

Path Synopsis
Code generated by counterfeiter.
Code generated by counterfeiter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL