---
name: php186-skill-829
description: "实现Linkerd服务网格模式，用于轻量级、安全优先的服务网格部署。在设置Linkerd、配置流量策略或实现最小开销的零信任网络时使用。"
---

# linkerd-patterns

## Linkerd 模式

 

Linkerd服务网格的生产模式 - 用于Kubernetes的轻量级、安全优先的服务网格。

 

## 何时不使用此技能

 

- 任务与Linkerd模式无关
- 需要此范围之外的其他领域或工具

 

## 说明

 

- 明确目标、约束条件和所需输入。
- 应用相关的最佳实践并验证结果。
- 提供可操作的步骤和验证方法。
- 如果需要详细示例，请打开 ` resources/implementation-playbook.md `。

 

## 何时使用此技能

 

- 设置轻量级服务网格
- 实现自动mTLS
- 配置金丝雀部署的流量拆分
- 设置按路由指标的服务配置文件
- 实现重试和超时
- 多集群服务网格

 

## 核心概念

 

### 1. Linkerd 架构

 

```
┌─────────────────────────────────────────────┐
│                控制平面                      │
│  ┌─────────┐ ┌──────────┐ ┌──────────────┐ │
│  │ destiny │ │ identity │ │ proxy-inject │ │
│  └─────────┘ └──────────┘ └──────────────┘ │
└─────────────────────────────────────────────┘
                      │
┌─────────────────────────────────────────────┐
│                数据平面                      │
│  ┌─────┐    ┌─────┐    ┌─────┐             │
│  │proxy│────│proxy│────│proxy│             │
│  └─────┘    └─────┘    └─────┘             │
│     │           │           │               │
│  ┌──┴──┐    ┌──┴──┐    ┌──┴──┐            │
│  │ app │    │ app │    │ app │            │
│  └─────┘    └─────┘    └─────┘            │
└─────────────────────────────────────────────┘
```

 

### 2. 关键资源

 

| 资源 | 用途 |
| --- | --- |
| **ServiceProfile** | 按路由指标、重试、超时 |
| **TrafficSplit** | 金丝雀部署、A/B测试 |
| **Server** | 定义服务端策略 |
| **ServerAuthorization** | 访问控制策略 |

 

## 模板

 

### 模板1：网格安装

 

```
# 安装CLI
curl --proto '=https' --tlsv1.2 -sSfL https://run.linkerd.io/install | sh

# 验证集群
linkerd check --pre

# 安装CRDs
linkerd install --crds | kubectl apply -f -

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

# 验证安装
linkerd check

# 安装viz扩展（可选）
linkerd viz install | kubectl apply -f -
```

 

### 模板2：注入命名空间

 

```
# 命名空间的自动注入
apiVersion: v1
kind: Namespace
metadata:
  name: my-app
  annotations:
    linkerd.io/inject: enabled
---
# 或注入特定部署
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  annotations:
    linkerd.io/inject: enabled
spec:
  template:
    metadata:
      annotations:
        linkerd.io/inject: enabled
```

 

### 模板3：带重试的服务配置文件

 

```
apiVersion: linkerd.io/v1alpha2
kind: ServiceProfile
metadata:
  name: my-service.my-namespace.svc.cluster.local
  namespace: my-namespace
spec:
  routes:
    - name: GET /api/users
      condition:
        method: GET
        pathRegex: /api/users
      responseClasses:
        - condition:
            status:
              min: 500
              max: 599
          isFailure: true
      isRetryable: true
    - name: POST /api/users
      condition:
        method: POST
        pathRegex: /api/users
      # POST 默认不可重试
      isRetryable: false
    - name: GET /api/users/{id}
      condition:
        method: GET
        pathRegex: /api/users/[^/]+
      timeout: 5s
      isRetryable: true
  retryBudget:
    retryRatio: 0.2
    minRetriesPerSecond: 10
    ttl: 10s
```

 

### 模板4：流量拆分（金丝雀）

 

```
apiVersion: split.smi-spec.io/v1alpha1
kind: TrafficSplit
metadata:
  name: my-service-canary
  namespace: my-namespace
spec:
  service: my-service
  backends:
    - service: my-service-stable
      weight: 900m  # 90%
    - service: my-service-canary
      weight: 100m  # 10%
```

 

### 模板5：服务器授权策略

 

```
# 定义服务器
apiVersion: policy.linkerd.io/v1beta1
kind: Server
metadata:
  name: my-service-http
  namespace: my-namespace
spec:
  podSelector:
    matchLabels:
      app: my-service
  port: http
  proxyProtocol: HTTP/1
---
# 允许来自特定客户端的流量
apiVersion: policy.linkerd.io/v1beta1
kind: ServerAuthorization
metadata:
  name: allow-frontend
  namespace: my-namespace
spec:
  server:
    name: my-service-http
  client:
    meshTLS:
      serviceAccounts:
        - name: frontend
          namespace: my-namespace
---
# 允许未经身份验证的流量（例如来自ingress）
apiVersion: policy.linkerd.io/v1beta1
kind: ServerAuthorization
metadata:
  name: allow-ingress
  namespace: my-namespace
spec:
  server:
    name: my-service-http
  client:
    unauthenticated: true
    networks:
      - cidr: 10.0.0.0/8
```

 

### 模板6：用于高级路由的HTTPRoute

 

```
apiVersion: policy.linkerd.io/v1beta2
kind: HTTPRoute
metadata:
  name: my-route
  namespace: my-namespace
spec:
  parentRefs:
    - name: my-service
      kind: Service
      group: core
      port: 8080
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /api/v2
        - headers:
            - name: x-api-version
              value: v2
      backendRefs:
        - name: my-service-v2
          port: 8080
    - matches:
        - path:
            type: PathPrefix
            value: /api
      backendRefs:
        - name: my-service-v1
          port: 8080
```

 

### 模板7：多集群设置

 

```
# 在每个集群上，使用集群凭证安装
linkerd multicluster install | kubectl apply -f -

# 链接集群
linkerd multicluster link --cluster-name west 
  --api-server-address https://west.example.com:6443 
  | kubectl apply -f -

# 将服务导出到其他集群
kubectl label svc/my-service mirror.linkerd.io/exported=true

# 验证跨集群连接
linkerd multicluster check
linkerd multicluster gateways
```

 

## 监控命令

 

```
# 实时流量视图
linkerd viz top deploy/my-app

# 按路由指标
linkerd viz routes deploy/my-app

# 检查代理状态
linkerd viz stat deploy -n my-namespace

# 查看服务依赖
linkerd viz edges deploy -n my-namespace

# 仪表板
linkerd viz dashboard
```

 

## 调试

 

```
# 检查注入状态
linkerd check --proxy -n my-namespace

# 查看代理日志
kubectl logs deploy/my-app -c linkerd-proxy

# 调试身份/TLS
linkerd identity -n my-namespace

# 流量抓取（实时）
linkerd viz tap deploy/my-app --to deploy/my-backend
```

 

## 最佳实践

 

### 应该做的

 

- **在所有地方启用mTLS** - Linkerd自动处理
- **使用ServiceProfiles** - 获取按路由指标和重试功能
- **设置重试预算** - 防止重试风暴
- **监控黄金指标** - 成功率、延迟、吞吐量

 

### 不应该做的

 

- **不要跳过检查** - 变更后始终运行 ` linkerd check `
- **不要过度配置** - Linkerd默认设置是合理的
- **不要忽略ServiceProfiles** - 它们解锁高级功能
- **不要忘记超时** - 为每个路由设置适当的值

 

## 资源

 

- Linkerd文档
- 服务配置文件
- 授权策略
