1. 首页 > IT综合教程 > 正文

it教程FG387-服务网格

内容大纲

1. 服务网格概述

服务网格是一种专门用于管理服务间通信的基础设施层。它通过在每个服务实例旁边部署一个轻量级的代理(称为边车代理),来处理服务间的通信、安全、监控和流量管理等功能。服务网格的目标是使服务间通信更加可靠、安全和可观察。

服务网格的核心特点包括:

  • 服务间通信管理:处理服务发现、负载均衡、熔断、重试等
  • 安全通信:提供服务间的TLS加密、身份验证和授权
  • 可观察性:提供分布式追踪、指标和日志
  • 流量管理:支持流量分割、A/B测试、金丝雀发布等
  • 无侵入性:服务代码无需修改,所有功能由边车代理提供

更多学习教程www.fgedu.net.cn

2. 服务网格架构

2.1 服务网格组件

  • 数据平面:由边车代理组成,处理服务间的通信
  • 控制平面:管理和配置边车代理,提供策略管理和服务发现

2.2 边车代理模式

边车代理模式是服务网格的核心设计模式,它通过在每个服务实例旁边部署一个代理容器,来处理所有的服务间通信。这种模式的优点是:

  • 服务代码无需修改,所有通信逻辑由代理处理
  • 代理可以独立升级和配置
  • 集中管理服务间通信的安全和监控

2.3 服务网格工作流程

  1. 服务A需要调用服务B
  2. 服务A的边车代理拦截请求
  3. 边车代理从控制平面获取服务B的地址和策略
  4. 边车代理处理服务发现、负载均衡、TLS加密等
  5. 请求被发送到服务B的边车代理
  6. 服务B的边车代理验证请求并转发给服务B
  7. 服务B处理请求并返回响应
  8. 响应通过边车代理返回给服务A

3. Istio服务网格

3.1 Istio架构

# Istio组件
– Envoy:边车代理,处理服务间通信
– Pilot:控制平面,管理服务发现和流量管理
– Galley:配置验证和管理
– Citadel:证书管理和身份验证
– istiod:整合了Pilot、Galley、Citadel等组件的单一二进制文件

# 安装Istio
$ curl -L https://istio.io/downloadIstio | sh –
$ cd istio-1.12.0
$ export PATH=$PWD/bin:$PATH

# 安装Istio到Kubernetes集群
$ istioctl install –set profile=demo -y

# 验证安装
$ kubectl get pods -n istio-system

3.2 Istio服务部署

# 启用自动注入边车代理
$ kubectl label namespace default istio-injection=enabled

# 部署示例应用
$ kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml

# 查看部署状态
$ kubectl get pods

# 检查服务
$ kubectl get services

# 为应用创建Gateway和VirtualService
$ kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml

# 查看Gateway和VirtualService
$ kubectl get gateway
$ kubectl get virtualservice

# 获取应用访问URL
$ export INGRESS_HOST=$(kubectl get svc istio-ingressgateway -n istio-system -o jsonpath='{.status.loadBalancer.ingress[0].ip}’)
$ export INGRESS_PORT=$(kubectl get svc istio-ingressgateway -n istio-system -o jsonpath='{.spec.ports[?(@.name==”http2″)].port}’)
$ export GATEWAY_URL=$INGRESS_HOST:$INGRESS_PORT
$ echo $GATEWAY_URL

# 测试应用访问
$ curl http://$GATEWAY_URL/productpage

3.3 Istio流量管理

# 创建VirtualService实现A/B测试
$ cat virtualservice.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
namespace: default
spec:
hosts:
– reviews
http:
– route:
– destination:
host: reviews
subset: v1
weight: 90
– destination:
host: reviews
subset: v2
weight: 10

# 创建DestinationRule
$ cat destinationrule.yaml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: reviews
namespace: default
spec:
host: reviews
subsets:
– name: v1
labels:
version: v1
– name: v2
labels:
version: v2
– name: v3
labels:
version: v3

# 应用配置
$ kubectl apply -f virtualservice.yaml
$ kubectl apply -f destinationrule.yaml

# 测试A/B测试
$ for i in $(seq 1 10); do curl -s http://$GATEWAY_URL/productpage | grep -o “Reviews v[0-9]” | head -1; done

# 创建金丝雀发布配置
$ cat canary.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: reviews
namespace: default
spec:
hosts:
– reviews
http:
– match:
– headers:
end-user:
exact: jason
route:
– destination:
host: reviews
subset: v3
– route:
– destination:
host: reviews
subset: v1

# 应用金丝雀配置
$ kubectl apply -f canary.yaml

# 测试金丝雀发布
$ curl -s -H “end-user: jason” http://$GATEWAY_URL/productpage | grep -o “Reviews v[0-9]”
$ curl -s http://$GATEWAY_URL/productpage | grep -o “Reviews v[0-9]”

风哥风哥提示:Istio是目前最流行的服务网格解决方案,提供了丰富的流量管理、安全和可观察性功能,适合复杂的微服务架构。

4. Linkerd服务网格

4.1 Linkerd架构

# Linkerd组件
– linkerd-proxy:边车代理,基于Rust实现的轻量级代理
– linkerd-control-plane:控制平面,管理代理配置和服务发现
– linkerd-cni:CNI插件,用于自动注入边车代理

# 安装Linkerd CLI
$ curl -sL https://run.linkerd.io/install | sh
$ export PATH=$HOME/.linkerd2/bin:$PATH

# 验证CLI安装
$ linkerd version

# 检查Kubernetes集群兼容性
$ linkerd check –pre

# 安装Linkerd控制平面
$ linkerd install | kubectl apply -f –

# 验证安装
$ linkerd check

# 查看控制平面组件
$ kubectl get pods -n linkerd

4.2 Linkerd服务部署

# 部署示例应用
$ kubectl create ns emojivoto
$ kubectl apply -f https://run.linkerd.io/emojivoto.yml

# 为应用注入边车代理
$ kubectl get deploy -n emojivoto | grep -v linkerd | awk ‘{print $1}’ | xargs -I {} linkerd inject kubectl get deploy {} -n emojivoto -o yaml | kubectl apply -f –

# 查看部署状态
$ kubectl get pods -n emojivoto

# 查看应用状态
$ linkerd viz dashboard

# 测试应用访问
$ kubectl port-forward svc/web-svc -n emojivoto 8080:80
# 打开浏览器访问 http://fgedudb:8080

4.3 Linkerd流量管理

# 创建服务配置
$ cat service-profile.yaml
apiVersion: linkerd.io/v1alpha2
kind: ServiceProfile
metadata:
name: web-svc.emojivoto.svc.cluster.local
namespace: emojivoto
spec:
routes:
– condition:
method: GET
pathRegex: /
name: root
– condition:
method: GET
pathRegex: /api/list
name: api-list
– condition:
method: POST
pathRegex: /api/vote
name: api-vote

# 应用服务配置
$ kubectl apply -f service-profile.yaml

# 查看服务配置
$ linkerd profile emojivoto/web-svc

# 查看服务拓扑
$ linkerd viz stat deployment -n emojivoto

# 查看服务流量
$ linkerd viz tap deployment/web -n emojivoto

学习交流加群风哥微信: itpux-com

5. Consul Connect

5.1 Consul Connect架构

# Consul Connect组件
– Consul Server:提供服务发现和配置管理
– Consul Client:部署在每个节点上,与Server通信
– Envoy Proxy:作为边车代理,处理服务间通信

# 安装Consul
$ curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add –
$ sudo apt-add-repository “deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main”
$ sudo apt-get update && sudo apt-get install consul

# 启动Consul Server
$ consul agent -server -bootstrap-expect=1 -data-dir=/tmp/consul -node=server1 -bind=192.168.1.100 -ui

# 启动Consul Client
$ consul agent -data-dir=/tmp/consul -node=client1 -bind=192.168.1.101 -join=192.168.1.100

# 查看Consul状态
$ consul members
$ consul catalog services

5.2 Consul Connect服务部署

# 注册服务
$ cat service-definition.json
{
“service”: {
“name”: “web”,
“port”: 8080,
“connect”: {
“sidecar_service”: {
“proxy”: {
“upstreams”: [
{
“destination_name”: “api”,
“local_bind_port”: 9090
}
]
}
}
}
}
}

$ consul services register service-definition.json

# 启动边车代理
$ consul connect proxy -sidecar-for web

# 注册API服务
$ cat api-service.json
{
“service”: {
“name”: “api”,
“port”: 9090,
“connect”: {
“sidecar_service”: {}
}
}
}

$ consul services register api-service.json

# 启动API服务边车代理
$ consul connect proxy -sidecar-for api

# 测试服务通信
$ curl fgedudb:9090

5.3 Consul Connect安全

# 启用Consul Connect CA
$ consul connect ca set-config -config-file ca-config.json

# 创建服务意图
$ cat service-intentions.json
[
{
“Source”: “web”,
“Destination”: “api”,
“Action”: “allow”
}
]

$ consul config write -kind service-intentions -name web-api service-intentions.json

# 查看服务意图
$ consul config read -kind service-intentions -name web-api

# 测试服务通信
$ curl fgedudb:9090

# 创建拒绝意图
$ cat deny-intention.json
[
{
“Source”: “web”,
“Destination”: “api”,
“Action”: “deny”
}
]

$ consul config write -kind service-intentions -name web-api deny-intention.json

# 测试服务通信(应该失败)
$ curl fgedudb:9090

学习交流加群风哥QQ113257174

6. 服务网格部署

6.1 Kubernetes部署

# Istio部署到Kubernetes
$ istioctl install –set profile=default -y

# 验证Istio安装
$ kubectl get pods -n istio-system

# Linkerd部署到Kubernetes
$ linkerd install | kubectl apply -f –

# 验证Linkerd安装
$ kubectl get pods -n linkerd

# Consul Connect部署到Kubernetes
$ helm repo add hashicorp https://helm.releases.hashicorp.com
$ helm install consul hashicorp/consul –set connectInject.enabled=true

# 验证Consul安装
$ kubectl get pods

6.2 自动注入边车代理

# Istio自动注入
$ kubectl label namespace default istio-injection=enabled

# 部署应用
$ kubectl apply -f app.yaml

# 验证边车代理注入
$ kubectl get pods

# Linkerd自动注入
$ kubectl get deploy -n default | xargs -I {} linkerd inject kubectl get deploy {} -o yaml | kubectl apply -f –

# 验证边车代理注入
$ kubectl get pods

# Consul Connect自动注入
$ kubectl annotate deployment app consul.hashicorp.com/connect-inject=true

# 验证边车代理注入
$ kubectl get pods

6.3 服务网格配置

# Istio配置示例
$ cat istio-config.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: app-gateway
namespace: default
spec:
selector:
istio: ingressgateway
servers:
– port:
number: 80
name: http
protocol: HTTP
hosts:
– “*”


apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: app-virtualservice
namespace: default
spec:
hosts:
– “*”
gateways:
– app-gateway
http:
– route:
– destination:
host: app
port:
number: 8080

# 应用Istio配置
$ kubectl apply -f istio-config.yaml

# Linkerd配置示例
$ cat linkerd-config.yaml
apiVersion: linkerd.io/v1alpha2
kind: ServiceProfile
metadata:
name: app.default.svc.cluster.local
namespace: default
spec:
routes:
– condition:
method: GET
pathRegex: /
name: root
– condition:
method: GET
pathRegex: /api
name: api

# 应用Linkerd配置
$ kubectl apply -f linkerd-config.yaml

更多学习教程公众号风哥教程itpux_com

7. 流量管理

7.1 流量分割

# Istio流量分割
$ cat traffic-splitting.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: app
namespace: default
spec:
hosts:
– app
http:
– route:
– destination:
host: app
subset: v1
weight: 80
– destination:
host: app
subset: v2
weight: 20


apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: app
namespace: default
spec:
host: app
subsets:
– name: v1
labels:
version: v1
– name: v2
labels:
version: v2

# 应用配置
$ kubectl apply -f traffic-splitting.yaml

# 测试流量分割
$ for i in $(seq 1 10); do curl -s http://app.fgedu.net.cn | grep -o “Version v[0-9]”; done

7.2 故障注入

# Istio故障注入
$ cat fault-injection.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: app
namespace: default
spec:
hosts:
– app
http:
– fault:
delay:
percentage:
value: 50
fixedDelay: 2s
abort:
percentage:
value: 10
httpStatus: 503
route:
– destination:
host: app
subset: v1

# 应用配置
$ kubectl apply -f fault-injection.yaml

# 测试故障注入
$ for i in $(seq 1 10); do curl -s -w “%{http_code}” http://app.fgedu.net.cn -o /dev/null; echo; done

7.3 熔断和重试

# Istio熔断配置
$ cat circuit-breaking.yaml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: app
namespace: default
spec:
host: app
subsets:
– name: v1
labels:
version: v1
trafficPolicy:
connectionPool:
tcp:
maxConnections: 100
http:
http1MaxPendingRequests: 100
maxRequestsPerConnection: 10
outlierDetection:
consecutiveErrors: 5
interval: 10s
baseEjectionTime: 30s
maxEjectionPercent: 50

# 应用配置
$ kubectl apply -f circuit-breaking.yaml

# Istio重试配置
$ cat retry.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: app
namespace: default
spec:
hosts:
– app
http:
– route:
– destination:
host: app
subset: v1
retries:
attempts: 3
perTryTimeout: 2s
retryOn: 5xx

# 应用配置
$ kubectl apply -f retry.yaml

author:www.itpux.com

8. 服务网格安全

8.1 mTLS加密

# Istio mTLS配置
$ cat mtls.yaml
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
name: default
namespace: default
spec:
mtls:
mode: STRICT

# 应用配置
$ kubectl apply -f mtls.yaml

# 验证mTLS
$ istioctl authn tls-check app.default.svc.cluster.local

# Linkerd mTLS
$ linkerd check –proxy

# 查看mTLS状态
$ linkerd viz stat deployment -n default

8.2 授权策略

# Istio授权策略
$ cat authorization-policy.yaml
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: app-authz
namespace: default
spec:
selector:
matchLabels:
app: app
rules:
– from:
– source:
principals: [“cluster.local/ns/default/sa/web”]
to:
– operation:
methods: [“GET”, “POST”]
paths: [“/api/*”]

# 应用配置
$ kubectl apply -f authorization-policy.yaml

# 测试授权策略
$ kubectl run -it –rm –restart=Never test –image=curlimages/curl — curl -s http://app:8080/api

8.3 身份验证

# Istio请求身份验证
$ cat request-authentication.yaml
apiVersion: security.istio.io/v1beta1
kind: RequestAuthentication
metadata:
name: app-auth
namespace: default
spec:
selector:
matchLabels:
app: app
jwtRules:
– issuer: “https://auth.fgedu.net.cn”
jwksUri: “https://auth.fgedu.net.cn/.well-known/jwks.json”

# 应用配置
$ kubectl apply -f request-authentication.yaml

# 测试身份验证
$ kubectl run -it –rm –restart=Never test –image=curlimages/curl — curl -s -H “Authorization: Bearer ” http://app:8080/api

9. 服务网格监控

9.1 指标监控

# Istio监控
$ kubectl apply -f samples/addons/prometheus.yaml
$ kubectl apply -f samples/addons/grafana.yaml

# 访问Grafana
$ kubectl port-forward svc/grafana -n istio-system 3000:3000
# 打开浏览器访问 http://fgedudb:3000

# Linkerd监控
$ linkerd viz install | kubectl apply -f –

# 访问Linkerd dashboard
$ linkerd viz dashboard

# Consul Connect监控
$ consul monitor -log-level=info

# 查看服务健康状态
$ consul health check status

9.2 分布式追踪

# Istio分布式追踪
$ kubectl apply -f samples/addons/jaeger.yaml

# 访问Jaeger
$ kubectl port-forward svc/jaeger -n istio-system 16686:16686
# 打开浏览器访问 http://fgedudb:16686

# Linkerd分布式追踪
$ linkerd jaeger install | kubectl apply -f –

# 访问Jaeger
$ kubectl port-forward svc/jaeger-query -n linkerd-jaeger 16686:16686

# 测试分布式追踪
$ curl http://app.fgedu.net.cn
# 在Jaeger中查看追踪信息

9.3 日志管理

# Istio日志配置
$ cat log-config.yaml
apiVersion: telemetry.istio.io/v1alpha1
kind: Telemetry
metadata:
name: app-logging
namespace: default
spec:
accessLogging:
– providers:
– name: stdout

# 应用配置
$ kubectl apply -f log-config.yaml

# 查看服务日志
$ kubectl logs -f deployment/app -c istio-proxy

# Linkerd日志
$ linkerd viz tap deployment/app

# 查看服务日志
$ kubectl logs -f deployment/app -c linkerd-proxy

10. 最佳实践

10.1 服务网格选择

  • 小型集群:Linkerd,轻量级,易于部署
  • 中型集群:Istio,功能丰富,适合复杂场景
  • 已有Consul的环境:Consul Connect,集成度高

10.2 服务网格部署最佳实践

# 分阶段部署服务网格
1. 先在测试环境部署
2. 选择非关键服务进行试点
3. 逐步扩展到所有服务
4. 监控性能和稳定性

# 资源配置
$ cat resource-config.yaml
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
metadata:
name: istio-resources
spec:
components:
pilot:
k8s:
resources:
requests:
cpu: 500m
memory: 512Mi
limits:
cpu: 1
memory: 1Gi
proxy:
k8s:
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi

# 应用资源配置
$ istioctl install -f resource-config.yaml -y

10.3 服务网格使用最佳实践

  • 合理规划服务网格范围:不是所有服务都需要服务网格
  • 优化边车代理配置:根据服务需求调整资源限制
  • 实施渐进式流量管理:先测试,后推广
  • 建立监控和告警:及时发现和解决问题
  • 定期更新服务网格版本:获取新特性和安全补丁
  • 培训团队:确保团队掌握服务网格技能
  • 文档化配置:记录服务网格配置和策略
  • 性能测试:确保服务网格不会影响应用性能
  • 安全审计:定期检查服务网格安全配置
  • 故障演练:测试服务网格的容错能力

生产环境建议

  • 选择适合自己规模的服务网格解决方案
  • 实施分阶段部署策略
  • 合理配置资源限制
  • 建立完善的监控和告警系统
  • 定期进行安全审计
  • 培训团队掌握服务网格技能
  • 文档化配置和最佳实践
  • 测试服务网格性能影响
  • 制定故障处理预案
  • 持续优化服务网格配置

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息