Lab 05: Service Mesh

Time: 60 minutes | Level: Architect | Docker: golang:1.22-alpine

Overview

Service mesh patterns in Go: mTLS with crypto/tls, service discovery via DNS-SD, load balancing strategies (round-robin/consistent hash), gRPC health v1, and Envoy xDS API concepts.


Step 1: mTLS — Mutual TLS Authentication

package mtls

import (
	"crypto/tls"
	"crypto/x509"
	"os"
)

// Server: requires client certificate
func ServerTLSConfig(certFile, keyFile, caFile string) (*tls.Config, error) {
	cert, err := tls.LoadX509KeyPair(certFile, keyFile)
	if err != nil {
		return nil, err
	}

	caCert, err := os.ReadFile(caFile)
	if err != nil {
		return nil, err
	}
	caPool := x509.NewCertPool()
	caPool.AppendCertsFromPEM(caCert)

	return &tls.Config{
		Certificates: []tls.Certificate{cert},
		ClientAuth:   tls.RequireAndVerifyClientCert,
		ClientCAs:    caPool,
		MinVersion:   tls.VersionTLS13,
		CipherSuites: []uint16{
			tls.TLS_AES_256_GCM_SHA384,
			tls.TLS_CHACHA20_POLY1305_SHA256,
		},
	}, nil
}

// Client: presents certificate to server
func ClientTLSConfig(certFile, keyFile, caFile string) (*tls.Config, error) {
	cert, err := tls.LoadX509KeyPair(certFile, keyFile)
	if err != nil {
		return nil, err
	}

	caCert, err := os.ReadFile(caFile)
	if err != nil {
		return nil, err
	}
	caPool := x509.NewCertPool()
	caPool.AppendCertsFromPEM(caCert)

	return &tls.Config{
		Certificates: []tls.Certificate{cert},
		RootCAs:      caPool,
		MinVersion:   tls.VersionTLS13,
	}, nil
}

Step 2: Certificate Generation (Self-Signed for Development)


Step 3: Service Discovery — DNS-SD


Step 4: Load Balancing Strategies


Step 5: gRPC Health Check Protocol


Step 6: HTTP Health Endpoint


Step 7: Envoy xDS — Concept


Step 8: Capstone — mTLS Certificate Generation

📸 Verified Output:


Summary

Pattern
Implementation
Notes

mTLS

crypto/tls.RequireAndVerifyClientCert

Both sides present cert

Short-lived certs

24h cert TTL

Rotation via SPIFFE/SPIRE

DNS-SD

net.LookupSRV

Kubernetes-native

Round-robin

atomic.Uint64 counter

Lock-free

Least conn

Min ActiveConns scan

For long connections

Consistent hash

FNV hash % backends

Session affinity

Health check

gRPC health v1

Standard protocol

Envoy xDS

Go control plane

Dynamic config push

Last updated