-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kube-apiserver: healthcheck via sidecar container
kube-apiserver doesn't expose the healthcheck via a dedicated endpoint, instead relying on anonyomous-access being enabled. That has previously forced us to enable the unauthenticated endpoint on 127.0.0.1:8080. Instead we now run a small sidecar container, which proxies /healthz and /readyz requests (only) adding appropriate authentication using a client certificate. This will also enable better load balancer checks in future, as these have previously been hampered by the custom CA certificate.
- Loading branch information
Showing
28 changed files
with
1,000 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library") | ||
|
||
go_library( | ||
name = "go_default_library", | ||
srcs = ["main.go"], | ||
importpath = "k8s.io/kops/cmd/kube-apiserver-healthcheck", | ||
visibility = ["//visibility:private"], | ||
deps = ["//vendor/k8s.io/klog:go_default_library"], | ||
) | ||
|
||
go_binary( | ||
name = "kube-apiserver-healthcheck", | ||
embed = [":go_default_library"], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
load( | ||
"@io_bazel_rules_docker//container:container.bzl", | ||
"container_image", | ||
"container_push", | ||
"container_bundle", | ||
) | ||
|
||
container_image( | ||
name = "image", | ||
base = "@distroless_base//image", | ||
cmd = ["/usr/bin/kube-apiserver-healthcheck"], | ||
user = "10012", | ||
directory = "/usr/bin/", | ||
files = [ | ||
"//cmd/kube-apiserver-healthcheck", | ||
], | ||
stamp = True, | ||
) | ||
|
||
container_push( | ||
name = "push-image", | ||
format = "Docker", | ||
image = ":image", | ||
registry = "{STABLE_DOCKER_REGISTRY}", | ||
repository = "{STABLE_DOCKER_IMAGE_PREFIX}kube-apiserver-healthcheck", | ||
tag = "{STABLE_KUBE_APISERVER_HEALTHCHECK_TAG}", | ||
) | ||
|
||
container_bundle( | ||
name = "image-bundle", | ||
images = { | ||
"{STABLE_DOCKER_IMAGE_PREFIX}kube-apiserver-healthcheck:{STABLE_KUBE_APISERVER_HEALTHCHECK_TAG}": "image", | ||
}, | ||
) | ||
|
||
load("//tools:gzip.bzl", "gzip") | ||
|
||
gzip( | ||
name = "image-bundle.tar.gz", | ||
src = "image-bundle.tar", | ||
) | ||
|
||
load("//tools:hashes.bzl", "hashes") | ||
|
||
hashes( | ||
name = "image-bundle.tar.gz.hashes", | ||
src = "image-bundle.tar.gz", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
## kube-apiserver-healthcheck | ||
|
||
This is a small sidecar container that allows for health-checking the | ||
kube-apiserver without enabling anonymous authentication and without | ||
enabling the unauthenticated port. | ||
|
||
It listens on port 8080 (http), and proxies a few known-safe requests | ||
to the real apiserver listening on 443. It uses a client certificate | ||
to authenticate itself to the apiserver. | ||
|
||
This lets us turn off the unauthenticated kube-apiserver endpoint, but | ||
it also lets us have better load-balancer health-checks. | ||
|
||
Because it runs as a sidecar next to kube-apiserver, it is in the same | ||
network namespace, and thus it can reach apiserver on | ||
https://127.0.0.1 . The kube-apiserver-healthcheck process listens on | ||
8080, but the health checks for the apiserver container are configured | ||
for :8080 and actually go via the sidecar. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
|
||
"k8s.io/klog" | ||
) | ||
|
||
// healthCheckServer is the http server | ||
type healthCheckServer struct { | ||
transport *http.Transport | ||
} | ||
|
||
// handler processes a single http request | ||
func (s *healthCheckServer) handler(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == "GET" { | ||
switch r.URL.Path { | ||
case "/.kube-apiserver-healthcheck/healthz": | ||
// This is a check for our own health | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte("ok")) | ||
return | ||
|
||
case "/healthz", "/readyz": | ||
// This is a health-check we will proxy | ||
s.proxyRequest(w, r) | ||
return | ||
} | ||
} | ||
|
||
klog.Infof("unknown request: %s %s", r.Method, r.URL.Path) | ||
http.Error(w, "not found", http.StatusNotFound) | ||
} | ||
|
||
// httpClient builds an isolated http.Client | ||
func (s *healthCheckServer) httpClient() *http.Client { | ||
return &http.Client{Transport: s.transport} | ||
} | ||
|
||
// proxyRequest forwards a request, once it has been verified by handler | ||
func (s *healthCheckServer) proxyRequest(w http.ResponseWriter, r *http.Request) { | ||
httpClient := s.httpClient() | ||
|
||
u := &url.URL{ | ||
Scheme: "https", | ||
Host: "127.0.0.1", | ||
Path: r.URL.Path, | ||
RawQuery: r.URL.RawQuery, | ||
} | ||
|
||
req := &http.Request{ | ||
Method: r.Method, | ||
URL: u, | ||
} | ||
req.URL.RawQuery = r.URL.RawQuery | ||
|
||
resp, err := httpClient.Do(req) | ||
if err != nil { | ||
klog.Infof("error from %s: %v", u, err) | ||
http.Error(w, "internal error", http.StatusBadGateway) | ||
return | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
w.WriteHeader(resp.StatusCode) | ||
if _, err := io.Copy(w, resp.Body); err != nil { | ||
klog.Warningf("error writing response body: %v", err) | ||
return | ||
} | ||
|
||
switch resp.StatusCode { | ||
case 200: | ||
klog.V(2).Infof("proxied %s %s: %s", r.Method, r.URL, resp.Status) | ||
default: | ||
klog.Infof("proxied %s %s: %s", r.Method, r.URL, resp.Status) | ||
} | ||
} | ||
|
||
func run() error { | ||
listen := ":8080" | ||
|
||
clientCert := "" | ||
clientKey := "" | ||
caCert := "" | ||
|
||
flag.StringVar(&clientCert, "client-cert", clientCert, "path to client certificate") | ||
flag.StringVar(&clientKey, "client-key", clientKey, "path to client key") | ||
flag.StringVar(&caCert, "ca-cert", caCert, "path to ca certificate") | ||
|
||
klog.InitFlags(nil) | ||
|
||
flag.Parse() | ||
|
||
tlsConfig := &tls.Config{} | ||
|
||
if caCert != "" { | ||
b, err := ioutil.ReadFile(caCert) | ||
if err != nil { | ||
return fmt.Errorf("error reading certificate %q: %v", caCert, err) | ||
} | ||
rootCAs := x509.NewCertPool() | ||
rootCAs.AppendCertsFromPEM(b) | ||
tlsConfig.RootCAs = rootCAs | ||
} | ||
|
||
if clientKey != "" { | ||
keypair, err := tls.LoadX509KeyPair(clientCert, clientKey) | ||
if err != nil { | ||
return fmt.Errorf("error reading client keypair: %v", err) | ||
} | ||
|
||
tlsConfig.Certificates = []tls.Certificate{keypair} | ||
} | ||
|
||
transport := &http.Transport{ | ||
TLSClientConfig: tlsConfig, | ||
} | ||
|
||
s := &healthCheckServer{ | ||
transport: transport, | ||
} | ||
|
||
http.HandleFunc("/", s.handler) | ||
|
||
klog.Infof("listening on %s", listen) | ||
|
||
if err := http.ListenAndServe(listen, nil); err != nil { | ||
return fmt.Errorf("error listening on %q: %v", listen, err) | ||
} | ||
|
||
return fmt.Errorf("unexpected return from ListenAndServe") | ||
} | ||
|
||
func main() { | ||
if err := run(); err != nil { | ||
fmt.Fprintf(os.Stderr, "%v\n", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.