Skip to content

Commit 1c4adaf

Browse files
authored
Merge pull request #150 from cloudnativelabs/nodeport_bind_all_ip
Kube-proxy like behaviour to listen on all ip's for NodePort service.
2 parents 6ea240f + 62900c7 commit 1c4adaf

File tree

3 files changed

+50
-33
lines changed

3 files changed

+50
-33
lines changed

Documentation/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Usage of ./kube-router:
117117
--run-firewall Enables Network Policy -- sets up iptables to provide ingress firewall for pods. (default true)
118118
--run-router Enables Pod Networking -- Advertises and learns the routes to Pods via iBGP. (default true)
119119
--run-service-proxy Enables Service Proxy -- sets up IPVS for Kubernetes Services. (default true)
120+
--nodeport-bindon-all-ip For service of NodePort type create IPVS service that listens on all IP's of the node. (default false)
120121
```
121122

122123
### requirements

app/controllers/network_services_controller.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,17 @@ var (
6767

6868
// NetworkServicesController struct stores information needed by the controller
6969
type NetworkServicesController struct {
70-
nodeIP net.IP
71-
nodeHostName string
72-
syncPeriod time.Duration
73-
mu sync.Mutex
74-
serviceMap serviceInfoMap
75-
endpointsMap endpointsInfoMap
76-
podCidr string
77-
masqueradeAll bool
78-
globalHairpin bool
79-
client *kubernetes.Clientset
70+
nodeIP net.IP
71+
nodeHostName string
72+
syncPeriod time.Duration
73+
mu sync.Mutex
74+
serviceMap serviceInfoMap
75+
endpointsMap endpointsInfoMap
76+
podCidr string
77+
masqueradeAll bool
78+
globalHairpin bool
79+
client *kubernetes.Clientset
80+
nodeportBindOnAllIp bool
8081
}
8182

8283
// internal representation of kubernetes service
@@ -262,12 +263,20 @@ func (nsc *NetworkServicesController) syncIpvsServices(serviceInfoMap serviceInf
262263
var ipvsNodeportSvc *ipvs.Service
263264
var nodeServiceId string
264265
if svc.nodePort != 0 {
265-
ipvsNodeportSvc, err = ipvsAddService(nsc.nodeIP, protocol, uint16(svc.nodePort), svc.sessionAffinity)
266+
var vip net.IP
267+
if vip = nsc.nodeIP; nsc.nodeportBindOnAllIp {
268+
vip = net.ParseIP("127.0.0.1")
269+
}
270+
ipvsNodeportSvc, err = ipvsAddService(vip, protocol, uint16(svc.nodePort), svc.sessionAffinity)
266271
if err != nil {
267272
glog.Errorf("Failed to create ipvs service for node port")
268273
continue
269274
}
270-
nodeServiceId = generateIpPortId(nsc.nodeIP.String(), svc.protocol, strconv.Itoa(svc.nodePort))
275+
if nsc.nodeportBindOnAllIp {
276+
nodeServiceId = generateIpPortId("127.0.0.1", svc.protocol, strconv.Itoa(svc.nodePort))
277+
} else {
278+
nodeServiceId = generateIpPortId(nsc.nodeIP.String(), svc.protocol, strconv.Itoa(svc.nodePort))
279+
}
271280
activeServiceEndpointMap[nodeServiceId] = make([]string, 0)
272281
}
273282

@@ -845,6 +854,10 @@ func NewNetworkServicesController(clientset *kubernetes.Clientset, config *optio
845854
nsc.masqueradeAll = true
846855
}
847856

857+
if config.NodePortBindOnAllIp {
858+
nsc.nodeportBindOnAllIp = true
859+
}
860+
848861
if config.RunRouter {
849862
cidr, err := utils.GetPodCidrFromNodeSpec(nsc.client, config.HostnameOverride)
850863
if err != nil {

app/options/options.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,28 @@ import (
77
)
88

99
type KubeRouterConfig struct {
10-
HelpRequested bool
11-
Kubeconfig string
12-
Master string
13-
ConfigSyncPeriod time.Duration
14-
CleanupConfig bool
15-
IPTablesSyncPeriod time.Duration
16-
IpvsSyncPeriod time.Duration
17-
RoutesSyncPeriod time.Duration
18-
RunServiceProxy bool
19-
RunFirewall bool
20-
RunRouter bool
21-
MasqueradeAll bool
22-
ClusterCIDR string
23-
EnablePodEgress bool
24-
HostnameOverride string
25-
AdvertiseClusterIp bool
26-
PeerRouter string
27-
ClusterAsn string
28-
PeerAsn string
29-
FullMeshMode bool
30-
GlobalHairpinMode bool
10+
HelpRequested bool
11+
Kubeconfig string
12+
Master string
13+
ConfigSyncPeriod time.Duration
14+
CleanupConfig bool
15+
IPTablesSyncPeriod time.Duration
16+
IpvsSyncPeriod time.Duration
17+
RoutesSyncPeriod time.Duration
18+
RunServiceProxy bool
19+
RunFirewall bool
20+
RunRouter bool
21+
MasqueradeAll bool
22+
ClusterCIDR string
23+
EnablePodEgress bool
24+
HostnameOverride string
25+
AdvertiseClusterIp bool
26+
PeerRouter string
27+
ClusterAsn string
28+
PeerAsn string
29+
FullMeshMode bool
30+
GlobalHairpinMode bool
31+
NodePortBindOnAllIp bool
3132
}
3233

3334
func NewKubeRouterConfig() *KubeRouterConfig {
@@ -81,4 +82,6 @@ func (s *KubeRouterConfig) AddFlags(fs *pflag.FlagSet) {
8182
"Overrides the NodeName of the node. Set this if kube-router is unable to determine your NodeName automatically.")
8283
fs.BoolVar(&s.GlobalHairpinMode, "hairpin-mode", false,
8384
"Add iptable rules for every Service Endpoint to support hairpin traffic.")
85+
fs.BoolVar(&s.NodePortBindOnAllIp, "nodeport-bindon-all-ip", false,
86+
"For service of NodePort type create IPVS service that listens on all IP's of the node.")
8487
}

0 commit comments

Comments
 (0)