Bladeren bron

add healthz

Andy Xie 7 jaren geleden
bovenliggende
commit
406c7e72cd
2 gewijzigde bestanden met toevoegingen van 33 en 0 verwijderingen
  1. 8 0
      Documentation/configuration.md
  2. 25 0
      main.go

+ 8 - 0
Documentation/configuration.md

@@ -58,6 +58,8 @@ The following configuration illustrates the use of most options with `udp` backe
 --subnet-lease-renew-margin=60: subnet lease renewal margin, in minutes.
 --ip-masq=false: setup IP masquerade for traffic destined for outside the flannel network. Flannel assumes that the default policy is ACCEPT in the NAT POSTROUTING chain.
 -v=0: log level for V logs. Set to 1 to see messages related to data path.
+--healthz-ip="0.0.0.0": The IP address for healthz server to listen (default "0.0.0.0")
+--healthz-port=0: The port for healthz server to listen(0 to disable)
 --version: print version and exit
 ```
 
@@ -68,3 +70,9 @@ MTU is calculated and set automatically by flannel. It then reports that value i
 The command line options outlined above can also be specified via environment variables.
 For example `--etcd-endpoints=http://10.0.0.2:2379` is equivalent to `FLANNELD_ETCD_ENDPOINTS=http://10.0.0.2:2379` environment variable.
 Any command line option can be turned into an environment variable by prefixing it with `FLANNELD_`, stripping leading dashes, converting to uppercase and replacing all other dashes to underscores.
+
+## Health Check
+
+Flannel provides a health check http endpoint `healthz`. Currently this endpoint will blindly
+return http status ok(i.e. 200) when flannel is running. This feature is by default disabled.
+Set `healthz-port` to a non-zero value will enable a healthz server for flannel.

+ 25 - 0
main.go

@@ -19,11 +19,13 @@ import (
 	"flag"
 	"fmt"
 	"net"
+	"net/http"
 	"os"
 	"os/signal"
 	"path/filepath"
 	"strings"
 	"syscall"
+	"strconv"
 
 	"github.com/coreos/pkg/flagutil"
 	log "github.com/golang/glog"
@@ -67,6 +69,8 @@ type CmdLineOpts struct {
 	subnetDir              string
 	publicIP               string
 	subnetLeaseRenewMargin int
+	healthzIP              string
+	healthzPort            int
 }
 
 var (
@@ -91,6 +95,8 @@ func init() {
 	flag.BoolVar(&opts.kubeSubnetMgr, "kube-subnet-mgr", false, "Contact the Kubernetes API for subnet assignement instead of etcd.")
 	flag.BoolVar(&opts.help, "help", false, "print this message")
 	flag.BoolVar(&opts.version, "version", false, "print version and exit")
+	flag.StringVar(&opts.healthzIP, "healthz-ip", "0.0.0.0", "The IP address for healthz server to listen")
+	flag.IntVar(&opts.healthzPort, "healthz-port", 0, "The port for healthz server to listen(0 to disable)")
 }
 
 func newSubnetManager() (subnet.Manager, error) {
@@ -154,6 +160,10 @@ func main() {
 	ctx, cancel := context.WithCancel(context.Background())
 	go shutdown(sigs, cancel)
 
+	if opts.healthzPort > 0 {
+		go mustRunHealthz()
+	}
+
 	// Fetch the network config (i.e. what backend to use etc..).
 	config, err := getConfig(ctx, sm)
 	if err == errCanceled {
@@ -378,3 +388,18 @@ func WriteSubnetFile(path string, nw ip.IP4Net, ipMasq bool, bn backend.Network)
 	return os.Rename(tempFile, path)
 	//TODO - is this safe? What if it's not on the same FS?
 }
+
+func mustRunHealthz() {
+	address := net.JoinHostPort(opts.healthzIP, strconv.Itoa(opts.healthzPort))
+	log.Infof("Start healthz server on %s", address)
+
+	http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
+		w.WriteHeader(http.StatusOK)
+		w.Write([]byte("flanneld is running"))
+	})
+
+	if err := http.ListenAndServe(address, nil); err != nil {
+		log.Errorf("Start healthz server error. %v", err)
+		panic(err)
+	}
+}