Kubernetes Istio
Introduction
Istio is an open-source service mesh that adds a layer of infrastructure to your Kubernetes cluster without requiring any changes to your application code. It provides powerful features for managing, securing, and monitoring microservices, making it an essential tool in the Kubernetes ecosystem.
In this guide, we'll explore what Istio is, why it's useful, and how to get started with implementing it in your Kubernetes clusters.
What is a Service Mesh?
Before diving into Istio specifically, let's understand what a service mesh is:
A service mesh is a dedicated infrastructure layer for handling service-to-service communication. It's responsible for the reliable delivery of requests through the complex topology of services that comprise a modern, cloud-native application.
In a microservices architecture, the number of services and their interactions can grow exponentially, making them difficult to manage. A service mesh addresses these challenges by providing:
- Traffic Management: Routing, load balancing, and fault handling
- Security: Authentication, authorization, and encryption
- Observability: Metrics, logs, and tracing
Istio Architecture
Istio consists of two main components:
- Control Plane: Manages and configures the proxies to route traffic
- Data Plane: Consists of a set of intelligent proxies (Envoy) deployed as sidecars
Key Components
- istiod: The central component that combines control plane functionality
- Envoy Proxy: High-performance proxy that mediates all inbound and outbound traffic for services in the mesh
- Pilot: Service discovery and traffic management
- Citadel: Certificate authority and identity management
- Galley: Configuration validation and distribution
Getting Started with Istio
Prerequisites
Before installing Istio, ensure you have:
- A running Kubernetes cluster (v1.16 or higher)
kubectl
command-line tool installed- Administrative access to your cluster
Installation
Let's go through the installation process step by step:
- Download Istio:
curl -L https://istio.io/downloadIstio | sh -
cd istio-1.19.0 # version may differ
export PATH=$PWD/bin:$PATH
- Install Istio using the default profile:
istioctl install --set profile=default -y
- Enable automatic sidecar injection for a namespace:
kubectl label namespace default istio-injection=enabled
- Verify the installation:
kubectl get svc -n istio-system
kubectl get pods -n istio-system
Expected output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-egressgateway ClusterIP 10.111.219.223 <none> 80/TCP,443/TCP 93s
istio-ingressgateway LoadBalancer 10.109.31.226 <pending> 15021:30505/TCP,80:31211/TCP,443:30947/TCP 93s
istiod ClusterIP 10.102.57.228 <none> 15010/TCP,15012/TCP,443/TCP,15014/TCP 2m2s
Traffic Management with Istio
One of Istio's most powerful features is its ability to control the flow of traffic and API calls between services.
Virtual Services and Destination Rules
Istio uses two main resources for traffic management:
- VirtualService: Defines how requests are routed to a service
- DestinationRule: Defines policies that apply to traffic after routing
Let's create a simple example with two versions of a service:
- Deploy two versions of a sample application:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-v1
spec:
replicas: 1
selector:
matchLabels:
app: hello
version: v1
template:
metadata:
labels:
app: hello
version: v1
spec:
containers:
- name: hello
image: istio/examples-helloworld-v1
ports:
- containerPort: 5000
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-v2
spec:
replicas: 1
selector:
matchLabels:
app: hello
version: v2
template:
metadata:
labels:
app: hello
version: v2
spec:
containers:
- name: hello
image: istio/examples-helloworld-v2
ports:
- containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
name: hello
spec:
selector:
app: hello
ports:
- port: 80
targetPort: 5000
- Create a VirtualService to route traffic:
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: hello-routes
spec:
hosts:
- hello
http:
- match:
- uri:
prefix: /v1
route:
- destination:
host: hello
subset: v1
- match:
- uri:
prefix: /v2
route:
- destination:
host: hello
subset: v2
- route:
- destination:
host: hello
subset: v1
weight: 90
- destination:
host: hello
subset: v2
weight: 10
- Create a DestinationRule to define subsets:
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: hello-destination
spec:
host: hello
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
This configuration does the following:
- Routes all traffic with path
/v1/*
to the v1 subset - Routes all traffic with path
/v2/*
to the v2 subset - For all other traffic, splits it with 90% to v1 and 10% to v2 (canary deployment)
Security Features
Istio provides robust security features without requiring changes to your application code.
mTLS (Mutual TLS)
Enable mutual TLS for all services in a namespace:
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
namespace: default
spec:
mtls:
mode: STRICT
Authentication Policies
Define who can access your services:
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: hello-viewer
namespace: default
spec:
selector:
matchLabels:
app: hello
rules:
- from:
- source:
namespaces: ["default"]
- to:
- operation:
methods: ["GET"]
This policy only allows GET requests from the default namespace to the hello service.
Observability with Istio
Istio integrates with several telemetry tools to provide insights into your service mesh.