Kubernetes Service Mesh with Istio
/ 3 min read
Understanding Service Mesh and Istio
A service mesh provides infrastructure layer that handles service-to-service communication in microservices architectures. Istio is one of the most popular service mesh implementations for Kubernetes.
Installing Istio
1. Basic Installation
# Download Istiocurl -L https://istio.io/downloadIstio | sh -
# Add istioctl to PATHexport PATH=$PWD/istio-1.20.0/bin:$PATH
# Install Istio with demo profileistioctl install --set profile=demo -y2. Custom Installation
apiVersion: install.istio.io/v1alpha1kind: IstioOperatormetadata: namespace: istio-system name: istio-control-planespec: profile: demo components: egressGateways: - name: istio-egressgateway enabled: true ingressGateways: - name: istio-ingressgateway enabled: true pilot: enabled: true values: global: proxy: resources: requests: cpu: 100m memory: 128Mi limits: cpu: 200m memory: 256MiCore Concepts
1. Sidecar Injection
apiVersion: v1kind: Namespacemetadata: name: bookinfo labels: istio-injection: enabled---apiVersion: apps/v1kind: Deploymentmetadata: name: reviews namespace: bookinfospec: replicas: 3 selector: matchLabels: app: reviews template: metadata: labels: app: reviews spec: containers: - name: reviews image: docker.io/istio/examples-bookinfo-reviews-v1:1.16.2 ports: - containerPort: 90802. Virtual Services
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: reviews-routespec: hosts: - reviews http: - match: - headers: end-user: exact: jason route: - destination: host: reviews subset: v2 - route: - destination: host: reviews subset: v13. Destination Rules
apiVersion: networking.istio.io/v1alpha3kind: DestinationRulemetadata: name: reviews-destinationspec: host: reviews trafficPolicy: loadBalancer: simple: RANDOM subsets: - name: v1 labels: version: v1 - name: v2 labels: version: v2 trafficPolicy: loadBalancer: simple: ROUND_ROBINTraffic Management
1. Gateway Configuration
apiVersion: networking.istio.io/v1alpha3kind: Gatewaymetadata: name: bookinfo-gatewayspec: selector: istio: ingressgateway servers: - port: number: 80 name: http protocol: HTTP hosts: - "bookinfo.example.com"2. Traffic Splitting
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: reviews-splitspec: hosts: - reviews http: - route: - destination: host: reviews subset: v1 weight: 80 - destination: host: reviews subset: v2 weight: 20Security Features
1. Authentication Policy
apiVersion: security.istio.io/v1beta1kind: PeerAuthenticationmetadata: name: default namespace: istio-systemspec: mtls: mode: STRICT2. Authorization Policy
apiVersion: security.istio.io/v1beta1kind: AuthorizationPolicymetadata: name: reviews-viewer namespace: bookinfospec: selector: matchLabels: app: reviews rules: - from: - source: principals: ["cluster.local/ns/default/sa/bookinfo-productpage"] to: - operation: methods: ["GET"]Observability
1. Distributed Tracing
apiVersion: telemetry.istio.io/v1alpha1kind: Telemetrymetadata: name: mesh-default namespace: istio-systemspec: tracing: - randomSamplingPercentage: 100.0 customTags: my-tag: literal: value: "123"2. Metrics Collection
apiVersion: telemetry.istio.io/v1alpha1kind: Telemetrymetadata: name: mesh-metrics namespace: istio-systemspec: metrics: - providers: - name: prometheus overrides: - match: metric: REQUEST_COUNT mode: CLIENT_AND_SERVER tagOverrides: response_code: operation: UPSERT value: "response.code"Advanced Features
1. Circuit Breaking
apiVersion: networking.istio.io/v1alpha3kind: DestinationRulemetadata: name: reviews-circuit-breakerspec: host: reviews trafficPolicy: outlierDetection: consecutive5xxErrors: 1 interval: 1s baseEjectionTime: 3m maxEjectionPercent: 1002. Fault Injection
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: reviews-faultspec: hosts: - reviews http: - fault: delay: percentage: value: 10.0 fixedDelay: 5s route: - destination: host: reviews subset: v13. Rate Limiting
apiVersion: networking.istio.io/v1alpha3kind: EnvoyFiltermetadata: name: filter-ratelimit namespace: istio-systemspec: configPatches: - applyTo: HTTP_FILTER match: context: SIDECAR_INBOUND listener: filterChain: filter: name: "envoy.filters.network.http_connection_manager" patch: operation: INSERT_BEFORE value: name: envoy.filters.http.ratelimit typed_config: "@type": type.googleapis.com/envoy.extensions.filters.http.ratelimit.v3.RateLimit domain: productpage-ratelimit rate_limit_service: grpc_service: envoy_grpc: cluster_name: rate_limit_cluster transport_api_version: V3Best Practices
1. Resource Management
apiVersion: install.istio.io/v1alpha1kind: IstioOperatorspec: components: pilot: k8s: resources: requests: cpu: 500m memory: 2048Mi limits: cpu: 1000m memory: 4096Mi2. Monitoring Setup
apiVersion: monitoring.coreos.com/v1kind: ServiceMonitormetadata: name: istio-component-monitor namespace: istio-systemspec: selector: matchLabels: istio: pilot endpoints: - port: http-monitoringTroubleshooting
Common Issues
- Sidecar Injection Issues
kubectl get namespace -L istio-injectionkubectl describe pod <pod-name> -n <namespace>- Traffic Routing Issues
istioctl analyzeistioctl proxy-status- Security Policy Issues
istioctl authn tls-check <pod-name>.<namespace>Performance Tuning
1. Proxy Configuration
apiVersion: install.istio.io/v1alpha1kind: IstioOperatorspec: meshConfig: defaultConfig: concurrency: 2 proxyMetadata: ISTIO_META_HTTP10: "1"2. Resource Optimization
apiVersion: install.istio.io/v1alpha1kind: IstioOperatorspec: values: pilot: cpu: targetAverageUtilization: 80 global: proxy: resources: requests: cpu: 100m memory: 128Mi limits: cpu: 200m memory: 256MiConclusion
Istio provides a powerful service mesh solution for Kubernetes, offering advanced traffic management, security, and observability features. Understanding and properly implementing these features is crucial for building robust microservices architectures.
Series Navigation
- Previous: Kubernetes Multi-tenancy
- Next: Kubernetes Backup and Recovery