Skip to content

Instantly share code, notes, and snippets.

@wsong
Created September 8, 2017 21:38
Show Gist options
  • Save wsong/36e55b3bca0495370daeffc3f54f8ac6 to your computer and use it in GitHub Desktop.
Save wsong/36e55b3bca0495370daeffc3f54f8ac6 to your computer and use it in GitHub Desktop.
Health checker for Docker Swarm nodes
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/ca"
"github.com/docker/swarmkit/manager"
"google.golang.org/grpc"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("must pass in node address with port (e.g. 123.123.123.123:2377) as the first argument")
return
}
nodeCertPaths := ca.CertPaths{
Cert: "/var/lib/docker/swarm/certificates/swarm-node.crt",
Key: "/var/lib/docker/swarm/certificates/swarm-node.key",
}
certPaths := ca.CertPaths{
Cert: "/var/lib/docker/swarm/certificates/swarm-root-ca.crt",
Key: "/var/lib/docker/swarm/certificates/swarm-root-ca.key",
}
rootCA, err := ca.GetLocalRootCA(certPaths)
if err != nil {
fmt.Printf("could not load CA: %s\n", err)
return
}
var unlockKey []byte
krw := ca.NewKeyReadWriter(nodeCertPaths, unlockKey, &manager.RaftDEKData{})
securityConfig, err := ca.LoadSecurityConfig(context.TODO(), rootCA, krw, false)
if err != nil {
fmt.Printf("could not load security config: %s\n", err)
return
}
grpcOptions := []grpc.DialOption{
grpc.WithBackoffMaxDelay(8 * time.Second),
grpc.WithTransportCredentials(securityConfig.ClientTLSCreds),
grpc.WithTimeout(2 * time.Second),
}
cc, err := grpc.Dial(os.Args[1], grpcOptions...)
if err != nil {
fmt.Printf("failed to dial to grpc: %s\n", err)
return
}
resp, err := api.NewHealthClient(cc).Check(context.TODO(), &api.HealthCheckRequest{Service: "Raft"})
if err != nil {
fmt.Printf("failed to check health: %s\n", err)
return
}
fmt.Printf("got a health check response: %+v\n", resp)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment