Skip to content

Commit 42e9699

Browse files
authored
Merge pull request #96 from Sh4d1/health_proxy
add health server for proxy
2 parents 402679d + be6b06d commit 42e9699

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

cmd/server/main.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ type ProxyRunOptions struct {
8383
agentPort uint
8484
// Port we listen for admin connections on.
8585
adminPort uint
86+
// Port we listen for health connections on.
87+
healthPort uint
8688

8789
// ID of this proxy server.
8890
serverID string
@@ -111,6 +113,7 @@ func (o *ProxyRunOptions) Flags() *pflag.FlagSet {
111113
flags.UintVar(&o.serverPort, "server-port", o.serverPort, "Port we listen for server connections on. Set to 0 for UDS.")
112114
flags.UintVar(&o.agentPort, "agent-port", o.agentPort, "Port we listen for agent connections on.")
113115
flags.UintVar(&o.adminPort, "admin-port", o.adminPort, "Port we listen for admin connections on.")
116+
flags.UintVar(&o.healthPort, "health-port", o.healthPort, "Port we listen for health connections on.")
114117
flags.StringVar(&o.serverID, "server-id", o.serverID, "The unique ID of this server.")
115118
flags.UintVar(&o.serverCount, "server-count", o.serverCount, "The number of proxy server instances, should be 1 unless it is an HA server.")
116119
flags.StringVar(&o.agentNamespace, "agent-namespace", o.agentNamespace, "Expected agent's namespace during agent authentication (used with agent-service-account, authentication-audience, kubeconfig).")
@@ -132,6 +135,7 @@ func (o *ProxyRunOptions) Print() {
132135
klog.Warningf("Server port set to %d.\n", o.serverPort)
133136
klog.Warningf("Agent port set to %d.\n", o.agentPort)
134137
klog.Warningf("Admin port set to %d.\n", o.adminPort)
138+
klog.Warningf("Health port set to %d.\n", o.healthPort)
135139
klog.Warningf("ServerID set to %s.\n", o.serverID)
136140
klog.Warningf("ServerCount set to %d.\n", o.serverCount)
137141
klog.Warningf("AgentNamespace set to %q.\n", o.agentNamespace)
@@ -209,6 +213,10 @@ func (o *ProxyRunOptions) Validate() error {
209213
if o.adminPort > 49151 {
210214
return fmt.Errorf("please do not try to use ephemeral port %d for the admin port", o.adminPort)
211215
}
216+
if o.healthPort > 49151 {
217+
return fmt.Errorf("please do not try to use ephemeral port %d for the health port", o.healthPort)
218+
}
219+
212220
if o.serverPort < 1024 {
213221
if o.udsName == "" {
214222
return fmt.Errorf("please do not try to use reserved port %d for the server port", o.serverPort)
@@ -220,6 +228,9 @@ func (o *ProxyRunOptions) Validate() error {
220228
if o.adminPort < 1024 {
221229
return fmt.Errorf("please do not try to use reserved port %d for the admin port", o.adminPort)
222230
}
231+
if o.healthPort < 1024 {
232+
return fmt.Errorf("please do not try to use reserved port %d for the health port", o.healthPort)
233+
}
223234

224235
// validate agent authentication params
225236
// all 4 parametes must be empty or must have value (except kubeconfigPath that might be empty)
@@ -255,7 +266,8 @@ func newProxyRunOptions() *ProxyRunOptions {
255266
udsName: "",
256267
serverPort: 8090,
257268
agentPort: 8091,
258-
adminPort: 8092,
269+
healthPort: 8092,
270+
adminPort: 8093,
259271
serverID: uuid.New().String(),
260272
serverCount: 1,
261273
agentNamespace: "",
@@ -331,6 +343,12 @@ func (p *Proxy) run(o *ProxyRunOptions) error {
331343
return fmt.Errorf("failed to run the admin server: %v", err)
332344
}
333345

346+
klog.Info("Starting health server for healthchecks.")
347+
err = p.runHealthServer(o, server)
348+
if err != nil {
349+
return fmt.Errorf("failed to run the health server: %v", err)
350+
}
351+
334352
stopCh := SetupSignalHandler()
335353
<-stopCh
336354
klog.Info("Shutting down server.")
@@ -496,28 +514,48 @@ func (p *Proxy) runAgentServer(o *ProxyRunOptions, server *server.ProxyServer) e
496514
}
497515

498516
func (p *Proxy) runAdminServer(o *ProxyRunOptions, server *server.ProxyServer) error {
517+
metricsHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
518+
prometheus.Handler().ServeHTTP(w, r)
519+
})
520+
521+
muxHandler := http.NewServeMux()
522+
muxHandler.HandleFunc("/metrics", metricsHandler)
523+
adminServer := &http.Server{
524+
Addr: fmt.Sprintf("127.0.0.1:%d", o.adminPort),
525+
Handler: muxHandler,
526+
MaxHeaderBytes: 1 << 20,
527+
}
528+
529+
go func() {
530+
err := adminServer.ListenAndServe()
531+
if err != nil {
532+
klog.Warningf("admin server received %v.\n", err)
533+
}
534+
klog.Warningf("Admin server stopped listening\n")
535+
}()
536+
537+
return nil
538+
}
539+
540+
func (p *Proxy) runHealthServer(o *ProxyRunOptions, server *server.ProxyServer) error {
499541
livenessHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
500542
fmt.Fprintf(w, "ok")
501543
})
502544
readinessHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
503545
fmt.Fprintf(w, "ok")
504546
})
505-
metricsHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
506-
prometheus.Handler().ServeHTTP(w, r)
507-
})
508547

509548
muxHandler := http.NewServeMux()
510549
muxHandler.HandleFunc("/healthz", livenessHandler)
511550
muxHandler.HandleFunc("/ready", readinessHandler)
512-
muxHandler.HandleFunc("/metrics", metricsHandler)
513-
adminServer := &http.Server{
514-
Addr: fmt.Sprintf("127.0.0.1:%d", o.adminPort),
551+
healthServer := &http.Server{
552+
Addr: fmt.Sprintf(":%d", o.healthPort),
515553
Handler: muxHandler,
516554
MaxHeaderBytes: 1 << 20,
517555
}
518556

519557
go func() {
520-
err := adminServer.ListenAndServe()
558+
err := healthServer.ListenAndServe()
521559
if err != nil {
522560
klog.Warningf("health server received %v.\n", err)
523561
}

examples/kubernetes/konnectivity-server.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ spec:
2424
"--cluster-key=/etc/srv/kubernetes/pki/apiserver.key",
2525
"--server-port=0",
2626
"--agent-port=8091",
27-
"--admin-port=8092",
27+
"--health-port=8092",
28+
"--admin-port=8093",
2829
"--mode=http-connect",
2930
"--agent-namespace=kube-system",
3031
"--agent-service-account=konnectivity-agent",
@@ -46,9 +47,12 @@ spec:
4647
- name: agentport
4748
containerPort: 8091
4849
hostPort: 8091
49-
- name: adminport
50+
- name: healthport
5051
containerPort: 8092
5152
hostPort: 8092
53+
- name: adminport
54+
containerPort: 8093
55+
hostPort: 8093
5256
volumeMounts:
5357
- name: varlogkonnectivityserver
5458
mountPath: /var/log/konnectivity-server.log

0 commit comments

Comments
 (0)