第五章:服务网格¶
Connect 概述¶
什么是 Connect?¶
Consul Connect 是 Consul 的服务网格功能,提供服务间安全通信、流量管理和可观测性。
┌─────────────────────────────────────────────────────────────┐
│ Connect 服务网格架构 │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ 应用服务 │ │
│ │ ┌───────────┐ ┌───────────┐ │ │
│ │ │ Service A │ │ Service B │ │ │
│ │ └─────┬─────┘ └─────┬─────┘ │ │
│ │ │ │ │ │
│ │ ▼ ▼ │ │
│ │ ┌───────────┐ ┌───────────┐ │ │
│ │ │ Proxy A │◄────── mTLS ──────►│ Proxy B │ │ │
│ │ │ (Sidecar) │ │ (Sidecar) │ │ │
│ │ └───────────┘ └───────────┘ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ Consul Server │ │
│ │ ┌───────────────────────────────────────────────┐ │ │
│ │ │ • CA 证书管理 │ │ │
│ │ │ • 服务发现 │ │ │
│ │ │ • 意图管理 │ │ │
│ │ └───────────────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
核心概念¶
1. Sidecar Proxy
- 与应用一起部署
- 处理入站和出站流量
- 自动 mTLS 加密
2. CA (Certificate Authority)
- 自动签发证书
- 自动轮换证书
- 验证服务身份
3. Intentions
- 服务间访问控制
- 白名单/黑名单
- 细粒度权限
启用 Connect¶
配置启用¶
启动 Connect¶
服务配置¶
注册 Connect 服务¶
# web-service.hcl
service {
name = "web"
port = 8080
connect {
sidecar_service {
proxy {
upstreams = [
{
destination_name = "api"
local_bind_port = 5000
}
]
}
}
}
}
启动服务¶
# 启动 Web 服务
consul connect proxy -sidecar-for web -log-level=info
# 启动 API 服务
consul connect proxy -sidecar-for api -log-level=info
意图管理¶
创建意图¶
# 允许 web 访问 api
consul intention create web api
# 拒绝访问
consul intention create -deny web api
# 查看意图
consul intention list
# 删除意图
consul intention delete web api
HTTP API¶
# 创建意图
curl -X PUT http://localhost:8500/v1/connect/intentions -d '{
"SourceName": "web",
"DestinationName": "api",
"Action": "allow"
}'
# 查看意图
curl http://localhost:8500/v1/connect/intentions
意图配置¶
# intention.hcl
Kind = "service-intentions"
Name = "api"
Sources = [
{
Name = "web"
Action = "allow"
},
{
Name = "admin"
Action = "deny"
}
]
代理配置¶
本地代理¶
代理配置文件¶
# proxy.hcl
proxy {
service {
name = "web"
kind = "connect-proxy"
proxy {
config {
bind_address = "0.0.0.0"
bind_port = 21000
}
upstreams = [
{
destination_name = "api"
local_bind_address = "127.0.0.1"
local_bind_port = 5000
}
]
}
}
}
Kubernetes 集成¶
安装 Consul Helm Chart¶
# 安装
helm install consul hashicorp/consul \
--set connectInject.enabled=true \
--set connectInject.transparentProxy.defaultEnabled=true
服务注解¶
apiVersion: v1
kind: Service
metadata:
name: api
annotations:
"consul.hashicorp.com/connect-service": "api"
spec:
ports:
- port: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
spec:
template:
metadata:
annotations:
"consul.hashicorp.com/connect-inject": "true"
"consul.hashicorp.com/connect-service": "api"
"consul.hashicorp.com/connect-service-port": "8080"
spec:
containers:
- name: api
image: api:latest
ports:
- containerPort: 8080
上游服务¶
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
template:
metadata:
annotations:
"consul.hashicorp.com/connect-inject": "true"
"consul.hashicorp.com/connect-service": "web"
"consul.hashicorp.com/connect-service-upstreams": "api:5000"
spec:
containers:
- name: web
image: web:latest
env:
- name: API_URL
value: "http://127.0.0.1:5000"
小结¶
服务网格要点:
- Connect 概念:Sidecar Proxy、CA、Intentions
- 启用 Connect:配置启用、启动服务
- 服务配置:注册服务、启动代理
- 意图管理:创建意图、访问控制
- Kubernetes 集成:Helm 安装、注解配置
下一章我们将学习健康检查。