mcpField
L
linkerd-patterns

linkerd-patterns

github.com/wshobson云与 DevOps41.9k10.3k 次安装2 周前更新
代码仓库

关于 linkerd-patterns

为 Kubernetes 上的 Linkerd 服务网格提供生产配置模式与操作指引,面向负责部署和运维服务网格的团队。涵盖安装、流量管理、安全策略、多集群配置及监控排障。

主要功能

  1. 01Linkerd 安装与集群检查
  2. 02自动注入与 mTLS 配置
  3. 03金丝雀流量拆分
  4. 04路由指标、重试与超时
  5. 05服务端访问策略
  6. 06多集群配置与排障

适用场景

  1. 01为 Kubernetes 部署 Linkerd 服务网格
  2. 02配置金丝雀发布和 A/B 测试
  3. 03设置路由级重试、超时与指标
  4. 04检查代理状态及身份和 TLS

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/wshobson/agents --skill linkerd-patterns

安装后在 Claude Code 中输入/linkerd-patterns确认已加载。

详细介绍

Linkerd 模式

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

何时使用本技能

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

核心概念

1. Linkerd 架构
┌─────────────────────────────────────────────┐
│                Control Plane                 │
│  ┌─────────┐ ┌──────────┐ ┌──────────────┐ │
│  │ destiny │ │ identity │ │ proxy-inject │ │
│  └─────────┘ └──────────┘ └──────────────┘ │
└─────────────────────────────────────────────┘
                      │
┌─────────────────────────────────────────────┐
│                 Data Plane                   │
│  ┌─────┐    ┌─────┐    ┌─────┐             │
│  │proxy│────│proxy│────│proxy│             │
│  └─────┘    └─────┘    └─────┘             │
│     │           │           │               │
│  ┌──┴──┐    ┌──┴──┐    ┌──┴──┐            │
│  │ app │    │ app │    │ app │            │
│  └─────┘    └─────┘    └─────┘            │
└─────────────────────────────────────────────┘
2. 关键资源
资源用途
ServiceProfile按路由指标、重试、超时
TrafficSplit金丝雀部署、A/B 测试
Server定义服务端策略
ServerAuthorization访问控制策略

模板

模板 1:网格安装

使用与目标集群兼容的命令行版本。示例使用 Homebrew; 其他系统请遵循官方安装指南。

# Install the CLI with Homebrew
brew install linkerd
linkerd version

# Validate cluster
linkerd check --pre

# Install CRDs
linkerd install --crds | kubectl apply -f -

# Install control plane
linkerd install | kubectl apply -f -

# Verify installation
linkerd check

# Install viz extension (optional)
linkerd viz install | kubectl apply -f -
模板 2:注入命名空间
# Automatic injection for namespace
apiVersion: v1
kind: Namespace
metadata:
  name: my-app
  annotations:
    linkerd.io/inject: enabled
---
# Or inject specific deployment
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 not retryable by default
      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:服务端授权策略
# Define the server
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
---
# Allow traffic from specific clients
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
---
# Allow unauthenticated traffic (e.g., from 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:多集群设置
# On each cluster, install with cluster credentials
linkerd multicluster install | kubectl apply -f -

# Link clusters
linkerd multicluster link --cluster-name west \
  --api-server-address https://west.example.com:6443 \
  | kubectl apply -f -

# Export a service to other clusters
kubectl label svc/my-service mirror.linkerd.io/exported=true

# Verify cross-cluster connectivity
linkerd multicluster check
linkerd multicluster gateways

监控命令

# Live traffic view
linkerd viz top deploy/my-app

# Per-route metrics
linkerd viz routes deploy/my-app

# Check proxy status
linkerd viz stat deploy -n my-namespace

# View service dependencies
linkerd viz edges deploy -n my-namespace

# Dashboard
linkerd viz dashboard

调试

# Check injection status
linkerd check --proxy -n my-namespace

# View proxy logs
kubectl logs deploy/my-app -c linkerd-proxy

# Debug identity/TLS
linkerd identity -n my-namespace

# Tap traffic (live)
linkerd viz tap deploy/my-app --to deploy/my-backend

最佳实践

应当
  • 在所有位置启用 mTLS —— 在 Linkerd 中是自动的
  • 使用 ServiceProfile —— 获得按路由指标和重试
  • 设置重试预算 —— 防止重试风暴
  • 监控黄金指标 —— 成功率、延迟、吞吐量
不应
  • 不要跳过检查 —— 更改后始终运行 linkerd check
  • 不要过度配置 —— Linkerd 的默认值是合理的
  • 不要忽略 ServiceProfile —— 它们能解锁高级功能
  • 不要忘记超时 —— 为每条路由设置合适的值

评论

支持 Markdown