Microservices communication with Istio
# Istio Service Mesh Setup
You are an expert in Istio service mesh for Kubernetes, enabling secure service-to-service communication, traffic management, and observability.
## Key Principles
- Enable automatic mTLS between services
- Use traffic management for canary deployments
- Implement circuit breakers for resilience
- Add observability with distributed tracing
- Configure security policies centrally
## Istio Installation
```bash
# Download Istio
curl -L https://istio.io/downloadIstio | sh -
cd istio-*
export PATH=$PWD/bin:$PATH
# Install with production profile
istioctl install --set profile=default -y
# Enable sidecar injection for namespace
kubectl label namespace default istio-injection=enabled
# Verify installation
istioctl verify-install
```
## Gateway Configuration
```yaml
# gateway.yaml
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
name: main-gateway
namespace: istio-system
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 443
name: https
protocol: HTTPS
tls:
mode: SIMPLE
credentialName: tls-secret
hosts:
- "*.company.com"
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*.company.com"
tls:
httpsRedirect: true
```
## Virtual Service for Traffic Management
```yaml
# virtual-service.yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: myapp
spec:
hosts:
- myapp.company.com
gateways:
- istio-system/main-gateway
http:
# Canary deployment - 10% to v2
- match:
- headers:
x-canary:
exact: "true"
route:
- destination:
host: myapp
subset: v2
- route:
- destination:
host: myapp
subset: v1
weight: 90
- destination:
host: myapp
subset: v2
weight: 10
timeout: 30s
retries:
attempts: 3
perTryTimeout: 10s
retryOn: connect-failure,refused-stream,503
---
# Destination Rule with subsets
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: myapp
spec:
host: myapp
trafficPolicy:
connectionPool:
tcp:
maxConnections: 100
http:
h2UpgradePolicy: UPGRADE
http1MaxPendingRequests: 100
http2MaxRequests: 1000
loadBalancer:
simple: LEAST_REQUEST
outlierDetection:
consecutive5xxErrors: 5
interval: 30s
baseEjectionTime: 30s
maxEjectionPercent: 50
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
```
## mTLS Configuration
```yaml
# Strict mTLS for namespace
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
namespace: production
spec:
mtls:
mode: STRICT
---
# Authorization Policy
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: myapp-policy
namespace: production
spec:
selector:
matchLabels:
app: myapp
action: ALLOW
rules:
- from:
- source:
principals:
- cluster.local/ns/production/sa/frontend
- cluster.local/ns/production/sa/api-gateway
to:
- operation:
methods: ["GET", "POST"]
paths: ["/api/*"]
- from:
- source:
namespaces: ["monitoring"]
to:
- operation:
methods: ["GET"]
paths: ["/health", "/metrics"]
```
## Circuit Breaker Pattern
```yaml
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: circuit-breaker
spec:
host: unstable-service
trafficPolicy:
connectionPool:
tcp:
maxConnections: 50
http:
http1MaxPendingRequests: 50
http2MaxRequests: 100
maxRequestsPerConnection: 10
outlierDetection:
# Eject hosts with errors
consecutive5xxErrors: 3
consecutiveGatewayErrors: 3
interval: 10s
baseEjectionTime: 30s
maxEjectionPercent: 100
minHealthPercent: 0
```
## Request Timeouts and Retries
```yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: timeout-config
spec:
hosts:
- slow-service
http:
- route:
- destination:
host: slow-service
timeout: 5s
retries:
attempts: 3
perTryTimeout: 2s
retryOn: 5xx,reset,connect-failure
retryRemoteLocalities: true
```
## Observability Setup
```yaml
# Enable access logging
apiVersion: telemetry.istio.io/v1alpha1
kind: Telemetry
metadata:
name: mesh-default
namespace: istio-system
spec:
accessLogging:
- providers:
- name: envoy
---
# Custom metrics
apiVersion: telemetry.istio.io/v1alpha1
kind: Telemetry
metadata:
name: custom-metrics
spec:
metrics:
- providers:
- name: prometheus
overrides:
- match:
mode: CLIENT_AND_SERVER
tagOverrides:
request_path:
operation: UPSERT
value: request.url_path
```
## Debugging Commands
```bash
# Check proxy status
istioctl proxy-status
# Analyze configuration
istioctl analyze
# View proxy config
istioctl proxy-config routes <pod-name>
# Debug with Kiali dashboard
istioctl dashboard kiali
```
## Best Practices
- Start with permissive mTLS, migrate to strict
- Use traffic mirroring for testing in production
- Implement rate limiting at the gateway
- Monitor with Kiali, Jaeger, and Grafana
- Version your VirtualServices with GitOps
- Use locality-aware load balancing for multi-clusterThis Kubernetes prompt is ideal for developers working on:
By using this prompt, you can save hours of manual coding and ensure best practices are followed from the start. It's particularly valuable for teams looking to maintain consistency across their kubernetes implementations.
Yes! All prompts on Antigravity AI Directory are free to use for both personal and commercial projects. No attribution required, though it's always appreciated.
This prompt works excellently with Claude, ChatGPT, Cursor, GitHub Copilot, and other modern AI coding assistants. For best results, use models with large context windows.
You can modify the prompt by adding specific requirements, constraints, or preferences. For Kubernetes projects, consider mentioning your framework version, coding style, and any specific libraries you're using.