使用ASM全局限流实现源IP分别限流

简介: 本文介绍了如何在ASM中实现基于源IP的全局限流,防止恶意请求。内容包括前提条件、准备工作、部署步骤及验证方法,帮助用户通过配置限流策略保障业务入口的稳定性与安全性。

对于业务入口来说,为了实现公平访问,防止某些客户端恶意发送大量请求,会有需要根据不同的请求源IP分别限制每个IP的请求速率的需求。本文主要阐述如何基于ASM的全局限流能力实现源IP分别限流场景。

前提条件

  • 已添加Kubernetes托管版集群到ASM实例,且ASM实例为1.25及以上。具体操作,请参见添加集群到ASM实例
  • 已为Kubernetes集群中的default命名空间开启自动注入。具体操作,请参见启用自动注入
  • 已创建名为ingressgateway的入口网关,并开启80端口。具体操作,请参见创建入口网关

准备工作

  1. 部署全局限流服务依赖的redis服务(可使用Tair替代)
kubectl apply -f- <<EOF
kind: Namespace
apiVersion: v1
metadata:
  name: redis-system
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
  namespace: redis-system
  labels:
    app: redis
spec:
  serviceName: "redis"
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
        - image: registry-cn-hangzhou.ack.aliyuncs.com/dev/redis:6.0.6-for-ack-gateway
          name: redis
          ports:
            - containerPort: 6379
          resources:
            limits:
              cpu: 1500m
              memory: 512Mi
            requests:
              cpu: 200m
              memory: 256Mi
---
apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: redis-system
  labels:
    app: redis
spec:
  ports:
    - name: redis
      port: 6379
      protocol: TCP
      targetPort: 6379
  selector:
    app: redis
EOF
  1. 部署全局限流服务
kubectl apply -f- <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: ratelimit-config
data:
  config.yaml: |
    {}
---
apiVersion: v1
kind: Service
metadata:
  name: ratelimit
  labels:
    app: ratelimit
spec:
  ports:
  - name: http-port
    port: 8080
    targetPort: 8080
    protocol: TCP
  - name: grpc-port
    port: 8081
    targetPort: 8081
    protocol: TCP
  - name: http-debug
    port: 6070
    targetPort: 6070
    protocol: TCP
  selector:
    app: ratelimit
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ratelimit
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ratelimit
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: ratelimit
        sidecar.istio.io/inject: "false"
    spec:
      containers:
        # Latest image from https://hub.docker.com/r/envoyproxy/ratelimit/tags
      - image: registry-cn-hangzhou-vpc.ack.aliyuncs.com/acs/envoy-ratelimit:v1.4.0-g2210ce0-aliyun
        imagePullPolicy: Always
        name: ratelimit
        command: ["/bin/ratelimit"]
        env:
        - name: LOG_LEVEL
          value: debug
        - name: REDIS_SOCKET_TYPE
          value: tcp
        - name: REDIS_URL
          value: redis.redis-system.svc.cluster.local:6379
        - name: USE_STATSD
          value: "false"
        - name: RUNTIME_ROOT
          value: /data
        - name: RUNTIME_SUBDIRECTORY
          value: ratelimit
        - name: RUNTIME_WATCH_ROOT
          value: "false"
        - name: RUNTIME_IGNOREDOTFILES
          value: "true"
        ports:
        - containerPort: 8080
        - containerPort: 8081
        - containerPort: 6070
        volumeMounts:
        - name: config-volume
          # $RUNTIME_ROOT/$RUNTIME_SUBDIRECTORY/$RUNTIME_APPDIRECTORY/config.yaml
          mountPath: /data/ratelimit/config
      volumes:
      - name: config-volume
        configMap:
          name: ratelimit-config
EOF
  1. 部署示例服务和路由规则
kubectl apply -f- <<EOF
apiVersion: v1
kind: ServiceAccount
metadata:
  name: httpbin
---
apiVersion: v1
kind: Service
metadata:
  name: httpbin
  labels:
    app: httpbin
    service: httpbin
spec:
  ports:
  - name: http
    port: 8000
    targetPort: 80
  selector:
    app: httpbin
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpbin
spec:
  replicas: 1
  selector:
    matchLabels:
      app: httpbin
      version: v1
  template:
    metadata:
      labels:
        app: httpbin
        version: v1
      annotations:
        sidecar.istio.io/inject: "true"
    spec:
      serviceAccountName: httpbin
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/acs/httpbin:latest
        imagePullPolicy: IfNotPresent
        name: httpbin
        ports:
        - containerPort: 80
---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: httpbin-gateway
spec:
  selector:
    istio: ingressgateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: httpbin
spec:
  gateways:
    - httpbin-gateway
  hosts:
    - bf2.example.com
  http:
    - name: httpbin-route
      route:
        - destination:
            host: httpbin
            port:
              number: 8000

实践步骤

  1. 部署asmglobalratelimiter资源
kubectl apply -f- <<EOF
apiVersion: istio.alibabacloud.com/v1
kind: ASMGlobalRateLimiter
metadata:
  name: global-test2
  namespace: istio-system
spec:
  configs:
    - limit:
        quota: 100000
        unit: SECOND
      limit_overrides:
        - limit:
            quota: 1
            unit: MINUTE
          request_match:
            remote_address:
              distinct: true
      match:
        vhost:
          name: bf2.example.com
          port: 80
          route:
            name_match: httpbin-route
      name: httpbin
  isGateway: true
  rateLimitService:
    host: ratelimit.default.svc.cluster.local
    port: 8081
    timeout:
      seconds: 5
  workloadSelector:
    labels:
      app: istio-ingressgateway
EOF
  1. 执行以下命令,获取全局限流服务配置
kubectl get asmglobalratelimiter global-test -n istio-system -o yaml

预期输出

apiVersion: istio.alibabacloud.com/v1
kind: ASMGlobalRateLimiter
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: >
      {"apiVersion":"istio.alibabacloud.com/v1","kind":"ASMGlobalRateLimiter","metadata":{"annotations":{},"name":"global-test2","namespace":"istio-system"},"spec":{"configs":[{"limit":{"quota":100000,"unit":"SECOND"},"limit_overrides":[{"limit":{"quota":1,"unit":"MINUTE"},"request_match":{"remote_address":{"distinct":true}}}],"match":{"vhost":{"name":"bf2.example.com","port":80,"route":{"name_match":"httpbin-route"}}},"name":"httpbin"}],"isGateway":true,"rateLimitService":{"host":"ratelimit.default.svc.cluster.local","port":8081,"timeout":{"seconds":5}},"workloadSelector":{"labels":{"app":"istio-ingressgateway"}}}}
  creationTimestamp: '2025-07-24T02:02:20Z'
  generation: 3
  name: global-test2
  namespace: istio-system
  resourceVersion: '35117'
  uid: 81f60d14-ae73-411f-b6b4-57f071e36539
spec:
  configs:
    - limit:
        quota: 100000
        unit: SECOND
      limit_overrides:
        - limit:
            quota: 1
            unit: MINUTE
          request_match:
            remote_address:
              distinct: true
      match:
        vhost:
          name: bf2.example.com
          port: 80
          route:
            name_match: httpbin-route
      name: httpbin
  isGateway: true
  rateLimitService:
    host: ratelimit.default.svc.cluster.local
    port: 8081
    timeout:
      seconds: 5
  workloadSelector:
    labels:
      app: istio-ingressgateway
status:
  config.yaml: |
    descriptors:
    - descriptors:
      - key: remote_address
        rate_limit:
          requests_per_unit: 1
          unit: MINUTE
      key: generic_key
      rate_limit:
        requests_per_unit: 100000
        unit: SECOND
      value: RateLimit[global-test2.istio-system]-Id[3387964427]
    domain: ratelimit.default.svc.cluster.local
  message: ok
  status: successful
  1. 将asmglobalratelimiter的status中的全局限流服务配置粘贴至限流服务configmap中
kubectl apply -f- <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: ratelimit-config
data:
  config.yaml: |
    descriptors:
    - descriptors:
      - key: remote_address
        rate_limit:
          requests_per_unit: 1
          unit: MINUTE
      key: generic_key
      rate_limit:
        requests_per_unit: 100000
        unit: SECOND
      value: RateLimit[global-test2.istio-system]-Id[3387964427]
    domain: ratelimit.default.svc.cluster.local
EOF
  1. 执行以下命令用相同客户端连续访问ASM网关两次
export GATEWAY_URL=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
curl http://$GATEWAY_URL:80/get -H "host:bf2.example.com"
curl http://$GATEWAY_URL:80/get -H "host:bf2.example.com"

第二次访问时预期输出如下

< HTTP/1.1 429 Too Many Requests
< x-envoy-ratelimited: true
< x-ratelimit-limit: 1, 1;w=60, 100000;w=1
< x-ratelimit-remaining: 0
< x-ratelimit-reset: 33
< date: Thu, 24 Jul 2025 02:48:27 GMT
< server: istio-envoy
< content-length: 0
<

说明限流已经生效。使用不同客户端时,可发现请求仍然可以正常访问

相关文章
|
5月前
|
人工智能 缓存 Kubernetes
ACK GIE配置建议
Gateway with Inference Extension是基于Kubernetes社区Gateway API及其扩展规范实现的增强型组件,支持四层/七层路由服务,并面向生成式AI推理场景提供负载均衡优化、服务管理简化等能力,适用于AI推理服务的高可用部署与性能优化。在不同的场景使用ACK Gateway with Inference Extension时,可能需要根据业务需求和高可用需要对网关和推理扩展进行不同的配置调整。本文主要介绍在实际业务场景中针对ACK GIE的配置建议,以获得更好的使用效果。
372 23
|
10月前
|
运维 Kubernetes 网络协议
基于虚拟服务配置的渐进式迁移实践:Istio集群至ASM集群的平滑切换
本文介绍了从Istio+k8s环境迁移到阿里云ASM+ACK环境的渐进式方法,通过配置虚拟服务和入口服务实现新老集群间的服务调用与流量转发,确保业务连续性与平滑迁移
894 132
|
4月前
|
API
使用Gateway with Inference Extension路由外部MaaS服务
本文介绍如何通过Gateway with Inference Extension对接百炼服务,实现请求路由时自动添加API Key并重写路径,包含操作步骤及验证方法。
|
6月前
|
Kubernetes 安全 API
【赵渝强老师】Kubernetes的安全框架
Kubernetes集群安全框架由认证、鉴权和准入控制三个核心阶段组成。认证阶段验证客户端身份,通过API Server配置的认证模块完成;鉴权阶段检查请求者操作权限,确保合法操作;准入控制阶段拦截并修改请求参数,通过插件实现特殊任务处理。每个阶段均可扩展自定义插件,增强安全性。文内附图与视频详解各阶段流程与实例。
165 1
|
5月前
|
运维 Cloud Native 应用服务中间件
阿里云微服务引擎 MSE 及 API 网关 2025 年 7 月产品动态
阿里云微服务引擎 MSE 面向业界主流开源微服务项目, 提供注册配置中心和分布式协调(原生支持 Nacos/ZooKeeper/Eureka )、云原生网关(原生支持Higress/Nginx/Envoy,遵循Ingress标准)、微服务治理(原生支持 Spring Cloud/Dubbo/Sentinel,遵循 OpenSergo 服务治理规范)能力。API 网关 (API Gateway),提供 APl 托管服务,覆盖设计、开发、测试、发布、售卖、运维监测、安全管控、下线等 API 生命周期阶段。帮助您快速构建以 API 为核心的系统架构.满足新技术引入、系统集成、业务中台等诸多场景需要。
|
关系型数据库 数据挖掘 分布式数据库
数据库+MCP,0编码自主完成数据洞察
本文介绍了一种全新的数据分析方案,结合PolarDB MySQL版与阿里云百炼,搭配MCP工具实现智能数据库分析应用。该方案解决传统数据分析工具高门槛、低效率的问题,通过零SQL操作和一站式部署,助力企业快速挖掘数据价值。方案具备高性能查询、快响应直连加速、高安全保障及易迁移上云等优势,并详细说明了部署资源、应用配置及验证步骤,帮助用户轻松完成实践体验。
1569 15
|
5月前
|
运维 Cloud Native 应用服务中间件
阿里云微服务引擎 MSE 及 API 网关 2025 年 6 月产品动态
阿里云微服务引擎 MSE 面向业界主流开源微服务项目, 提供注册配置中心和分布式协调(原生支持 Nacos/ZooKeeper/Eureka )、云原生网关(原生支持Higress/Nginx/Envoy,遵循Ingress标准)、微服务治理(原生支持 Spring Cloud/Dubbo/Sentinel,遵循 OpenSergo 服务治理规范)能力。API 网关 (API Gateway),提供 APl 托管服务,覆盖设计、开发、测试、发布、售卖、运维监测、安全管控、下线等 API 生命周期阶段。帮助您快速构建以 API 为核心的系统架构.满足新技术引入、系统集成、业务中台等诸多场景需要。
|
人工智能 自然语言处理 前端开发
3个月,上百家企业交流,和大家聊聊AI应用的落地实践(开篇)
企业希望自己的业务被 AI 赋能的诉求是强烈的,但大多数企业是不知道从哪里下手的
|
5月前
|
存储 Rust 安全
Rocket框架JWT鉴权实战:保护Rust Web API的安全方案​
本篇文章是基于rust语言和rocket依赖实现网页JWT认证和鉴权,完成简单的JWT token的验证和鉴权处理,使用cargo做依赖的导入和测试。
246 1