Skip to content

Instantly share code, notes, and snippets.

@vdparikh
Created January 28, 2025 23:32
Show Gist options
  • Save vdparikh/543cd64ccec604841d1cc1cb3348759a to your computer and use it in GitHub Desktop.
Save vdparikh/543cd64ccec604841d1cc1cb3348759a to your computer and use it in GitHub Desktop.
istio

Istio is a powerful service mesh that provides a way to control, secure, and observe microservices in a Kubernetes environment. It works by injecting a sidecar proxy (Envoy) alongside each service, which handles communication between services. Istio provides features like traffic management, security (mTLS, RBAC), and observability (metrics, logs, tracing).


1. Key Concepts of Istio

Before diving into code, let’s understand the key components of Istio:

  1. Sidecar Proxy (Envoy):

    • Injected into each pod in your Kubernetes cluster.
    • Handles all inbound and outbound traffic for the service.
  2. Control Plane:

    • Manages the configuration and behavior of the sidecar proxies.
    • Includes components like Pilot, Citadel, and Galley.
  3. Traffic Management:

    • Rules for routing, load balancing, retries, timeouts, and circuit breaking.
  4. Security:

    • Mutual TLS (mTLS) for secure communication between services.
    • Role-Based Access Control (RBAC) for fine-grained access control.
  5. Observability:

    • Metrics, logs, and traces for monitoring and debugging.

2. Setting Up Istio

To use Istio, you need a Kubernetes cluster and Istio installed. Here’s how to set it up:

Step 1: Install Istio

  1. Download Istio:

    curl -L https://istio.io/downloadIstio | sh -
    cd istio-*
  2. Add istioctl to your PATH:

    export PATH=$PWD/bin:$PATH
  3. Install Istio on your Kubernetes cluster:

    istioctl install --set profile=demo -y
  4. Verify the installation:

    kubectl get pods -n istio-system

Step 2: Deploy a Go Microservice

Let’s create a simple Go microservice and deploy it to Kubernetes with Istio.

Go Microservice Code

Create a simple HTTP server in Go (main.go):

package main

import (
    "fmt"
    "log"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello from Go service!\n")
}

func main() {
    http.HandleFunc("/", handler)
    log.Println("Starting server on :8080...")
    if err := http.ListenAndServe(":8080", nil); err != nil {
        log.Fatalf("Failed to start server: %v", err)
    }
}

Dockerfile

Create a Dockerfile to containerize the Go service:

FROM golang:1.19-alpine AS builder
WORKDIR /app
COPY . .
RUN go build -o service .

FROM alpine:latest
WORKDIR /app
COPY --from=builder /app/service .
CMD ["./service"]

Build and push the Docker image:

docker build -t your-dockerhub-username/go-service:latest .
docker push your-dockerhub-username/go-service:latest

Kubernetes Deployment

Create a Kubernetes deployment (deployment.yaml):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-service
spec:
  replicas: 2
  selector:
    matchLabels:
      app: go-service
  template:
    metadata:
      labels:
        app: go-service
    spec:
      containers:
      - name: go-service
        image: your-dockerhub-username/go-service:latest
        ports:
        - containerPort: 8080

Deploy to Kubernetes:

kubectl apply -f deployment.yaml

Step 3: Enable Istio Sidecar Injection

To inject the Istio sidecar into your Go service pods, label the namespace with istio-injection=enabled:

kubectl label namespace default istio-injection=enabled

Redeploy the service:

kubectl delete -f deployment.yaml
kubectl apply -f deployment.yaml

Verify that the sidecar is injected:

kubectl get pods

You should see two containers in each pod: one for your Go service and one for the Istio sidecar.


Step 4: Traffic Management with Istio

Let’s configure Istio to manage traffic for your Go service.

VirtualService

Create a VirtualService to define routing rules (virtualservice.yaml):

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: go-service
spec:
  hosts:
  - "go-service.default.svc.cluster.local"
  http:
  - route:
    - destination:
        host: go-service.default.svc.cluster.local
        subset: v1

DestinationRule

Create a DestinationRule to define subsets (destinationrule.yaml):

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: go-service
spec:
  host: go-service.default.svc.cluster.local
  subsets:
  - name: v1
    labels:
      version: v1

Apply the configurations:

kubectl apply -f virtualservice.yaml
kubectl apply -f destinationrule.yaml

Step 5: Secure Communication with mTLS

Istio can automatically enable mutual TLS (mTLS) between services.

PeerAuthentication

Enable mTLS for the namespace (peerauthentication.yaml):

apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: default
spec:
  mtls:
    mode: STRICT

Apply the configuration:

kubectl apply -f peerauthentication.yaml

Step 6: Observability

Istio provides built-in observability tools like Prometheus, Grafana, and Jaeger.

Access Metrics

  1. Install Prometheus and Grafana:

    kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.16/samples/addons/prometheus.yaml
    kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.16/samples/addons/grafana.yaml
  2. Access Grafana:

    istioctl dashboard grafana

Access Traces

  1. Install Jaeger:

    kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.16/samples/addons/jaeger.yaml
  2. Access Jaeger:

    istioctl dashboard jaeger

Step 7: Test the Setup

  1. Expose the Go service:

    kubectl expose deployment go-service --port=8080
  2. Send a request to the service:

    kubectl exec -it <pod-name> -- curl http://go-service:8080
  3. Check the Istio sidecar logs:

    kubectl logs <pod-name> -c istio-proxy

3. Key Takeaways

  • Istio injects a sidecar proxy (Envoy) into each pod to manage traffic, security, and observability.
  • You can use Istio to enforce mTLS, manage traffic routing, and monitor your services.
  • Go microservices work seamlessly with Istio when deployed in Kubernetes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment