Skip to content

Commit 8023021

Browse files
authored
Merge pull request #65 from cloudnativelabs/prometheus
WIP : Expose active/inactive connection to service backend as prometheus metrics
2 parents 152ce38 + 60482bc commit 8023021

File tree

292 files changed

+49656
-74
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

292 files changed

+49656
-74
lines changed

app/controllers/network_services_controller.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io/ioutil"
77
"net"
8+
"net/http"
89
"reflect"
910
"strconv"
1011
"strings"
@@ -18,6 +19,8 @@ import (
1819
"github.com/coreos/go-iptables/iptables"
1920
"github.com/golang/glog"
2021
"github.com/mqliang/libipvs"
22+
"github.com/prometheus/client_golang/prometheus"
23+
"github.com/prometheus/client_golang/prometheus/promhttp"
2124
"github.com/vishvananda/netlink"
2225
"k8s.io/client-go/kubernetes"
2326
)
@@ -27,10 +30,21 @@ const (
2730
IFACE_NOT_FOUND = "Link not found"
2831
IFACE_HAS_ADDR = "file exists"
2932
IPVS_SERVER_EXISTS = "file exists"
33+
namespace = "kube_router"
3034
)
3135

3236
var (
3337
h libipvs.IPVSHandle
38+
serviceBackendActiveConn = prometheus.NewGaugeVec(prometheus.GaugeOpts{
39+
Namespace: namespace,
40+
Name: "service_backend_active_connections",
41+
Help: "Active conntection to backend of service",
42+
}, []string{"namespace", "service_name", "backend"})
43+
serviceBackendInactiveConn = prometheus.NewGaugeVec(prometheus.GaugeOpts{
44+
Namespace: namespace,
45+
Name: "service_backend_inactive_connections",
46+
Help: "Active conntection to backend of service",
47+
}, []string{"namespace", "service_name", "backend"})
3448
)
3549

3650
// Network services controller enables local node as network service proxy through IPVS/LVS.
@@ -55,6 +69,8 @@ type NetworkServicesController struct {
5569

5670
// internal representation of kubernetes service
5771
type serviceInfo struct {
72+
name string
73+
namespace string
5874
clusterIP net.IP
5975
port int
6076
protocol string
@@ -90,6 +106,12 @@ func (nsc *NetworkServicesController) Run(stopCh <-chan struct{}, wg *sync.WaitG
90106
return errors.New("Failed to do add masqurade rule in POSTROUTING chain of nat table due to: %s" + err.Error())
91107
}
92108

109+
// register metrics
110+
prometheus.MustRegister(serviceBackendActiveConn)
111+
prometheus.MustRegister(serviceBackendInactiveConn)
112+
http.Handle("/metrics", promhttp.Handler())
113+
go http.ListenAndServe(":8080", nil)
114+
93115
// enable ipvs connection tracking
94116
err = ensureIpvsConntrack()
95117
if err != nil {
@@ -132,6 +154,7 @@ func (nsc *NetworkServicesController) sync() {
132154
glog.Errorf("Error syncing hairpin iptable rules: %s", err.Error())
133155
}
134156
nsc.syncIpvsServices(nsc.serviceMap, nsc.endpointsMap)
157+
nsc.publishMetrics(nsc.serviceMap)
135158
}
136159

137160
// handle change in endpoints update from the API server
@@ -308,6 +331,41 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
308331
return nil
309332
}
310333

334+
func (nsc *NetworkServicesController) publishMetrics(serviceInfoMap serviceInfoMap) error {
335+
ipvsSvcs, err := h.ListServices()
336+
if err != nil {
337+
return errors.New("Failed to list IPVS services: " + err.Error())
338+
}
339+
340+
for _, svc := range serviceInfoMap {
341+
for _, ipvsSvc := range ipvsSvcs {
342+
if strings.Compare(svc.clusterIP.String(), ipvsSvc.Address.String()) == 0 &&
343+
svc.protocol == ipvsSvc.Protocol.String() && uint16(svc.port) == ipvsSvc.Port {
344+
dsts, err := h.ListDestinations(ipvsSvc)
345+
if err != nil {
346+
glog.Errorf("Failed to get list of servers from ipvs service")
347+
}
348+
for _, dst := range dsts {
349+
serviceBackendActiveConn.WithLabelValues(svc.namespace, svc.name, dst.Address.String()).Set(float64(dst.ActiveConns))
350+
serviceBackendInactiveConn.WithLabelValues(svc.namespace, svc.name, dst.Address.String()).Set(float64(dst.InactConns))
351+
}
352+
}
353+
if strings.Compare(nsc.nodeIP.String(), ipvsSvc.Address.String()) == 0 &&
354+
svc.protocol == ipvsSvc.Protocol.String() && uint16(svc.port) == ipvsSvc.Port {
355+
dsts, err := h.ListDestinations(ipvsSvc)
356+
if err != nil {
357+
glog.Errorf("Failed to get list of servers from ipvs service")
358+
}
359+
for _, dst := range dsts {
360+
serviceBackendActiveConn.WithLabelValues(svc.namespace, svc.name, dst.Address.String()).Set(float64(dst.ActiveConns))
361+
serviceBackendInactiveConn.WithLabelValues(svc.namespace, svc.name, dst.Address.String()).Set(float64(dst.InactConns))
362+
}
363+
}
364+
}
365+
}
366+
return nil
367+
}
368+
311369
func buildServicesInfo() serviceInfoMap {
312370
serviceMap := make(serviceInfoMap)
313371
for _, svc := range watchers.ServiceWatcher.List() {
@@ -328,6 +386,8 @@ func buildServicesInfo() serviceInfoMap {
328386
port: int(port.Port),
329387
protocol: strings.ToLower(string(port.Protocol)),
330388
nodePort: int(port.NodePort),
389+
name: svc.ObjectMeta.Name,
390+
namespace: svc.ObjectMeta.Namespace,
331391
}
332392

333393
svcInfo.sessionAffinity = (svc.Spec.SessionAffinity == "ClientIP")

glide.lock

Lines changed: 30 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

glide.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ import:
5858
version: master
5959
- package: github.com/aws/aws-sdk-go/
6060
version: ^v1.8.36
61+
- package: github.com/prometheus/client_golang
62+
version: ~0.8.0

vendor/github.com/aws/aws-sdk-go/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/aws/aws-sdk-go/aws/version.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/aws/aws-sdk-go/models/apis/discovery/2015-11-01/api-2.json

Lines changed: 26 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/aws/aws-sdk-go/models/apis/discovery/2015-11-01/docs-2.json

Lines changed: 30 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)