AI-Gateway系列_kagateway-08HTTP-HTTP访问测试

创建测试应用

apiVersion: v1
kind: Namespace
metadata:
  name: httpbin
---
##################################################################################################
# httpbin service
##################################################################################################
apiVersion: v1
kind: ServiceAccount
metadata:
  name: httpbin
  namespace: httpbin
---
apiVersion: v1
kind: Service
metadata:
  name: httpbin
  namespace: httpbin
  labels:
    app: httpbin
    service: httpbin
spec:
  ports:
    - name: http
      port: 8000
      targetPort: 8080
    - name: tcp
      port: 9000
  selector:
    app: httpbin
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpbin
  namespace: httpbin
spec:
  replicas: 1
  selector:
    matchLabels:
      app: httpbin
      version: v1
  template:
    metadata:
      labels:
        app: httpbin
        version: v1
    spec:
      serviceAccountName: httpbin
      containers:
        - image: 10.118.17.28:30002/kgateway/go-httpbin:v2.6.0
          imagePullPolicy: IfNotPresent
          name: httpbin
          command: [ go-httpbin ]
          args:
            - "-port"
            - "8080"
            - "-max-duration"
            - "600s" # override default 10s
          ports:
            - containerPort: 8080
        - name: curl
          image: 10.118.17.28:30002/kgateway/curl:7.83.1
          resources:
            requests:
              cpu: "100m"
            limits:
              cpu: "200m"
          imagePullPolicy: IfNotPresent
          command:
            - "tail"
            - "-f"
            - "/dev/null"
# 创建测试应用,把「后端业务服务」httpbin 部署到集群里。
kubectl apply -f httpbin.yaml

# 检查测试应用是否正常
kubectl -n httpbin get pods

创建网关实例

# 创建 Gateway(用agentgateway这个GatewayClass)
kubectl apply -f- <<EOF
kind: Gateway
apiVersion: gateway.networking.k8s.io/v1
metadata:
  name: http
  namespace: kgateway-system
spec:
  gatewayClassName: agentgateway
  listeners:
  - protocol: HTTP
    port: 8080
    name: http
    allowedRoutes:
      namespaces:
        from: All
EOF

# 查看这个Gateway配置对象本身的“健康状态”。“从控制面的视角确认:http这个Gateway已经被agentgateway正常接管并下发。”
kubectl get gateway http -n kgateway-system

# 从数据面的角度找出“为这个Gateway专门起的代理 Pod”。
kubectl get po -n kgateway-system -l gateway.networking.k8s.io/gateway-name=http

创建路由

# 创建http路由
kubectl apply -f- <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: httpbin
  namespace: httpbin
spec:
  parentRefs:
    - name: http
      namespace: kgateway-system
  hostnames:
    - "www.example.com"
  rules:
    - backendRefs:
        - name: httpbin
          port: 8000
EOF


# 检查测试
kubectl port-forward deployment/http -n kgateway-system 8080:8080

测试

# 配置端口转发,在本地通过curl完成测试验证
kubectl -n kgateway-system port-forward http-5599855456-knf7v 8080:8080

curl -i localhost:8080/headers -H "host: www.example.com"

HTTP/1.1 200 OK
access-control-allow-credentials: true
access-control-allow-origin: *
content-type: application/json; encoding=utf-8
date: Sat, 15 Nov 2025 04:01:39 GMT
content-length: 149

{
  "headers": {
    "Accept": [
      "*/*"
    ],
    "Host": [
      "www.example.com"
    ],
    "User-Agent": [
      "curl/7.68.0"
    ]
  }
}

流量路径

curl http://localhost:8080/headers -H "Host: www.example.com"
│

[本机 8080]
(port-forward)
│

[Gateway Pod 内 8080 端口(listener)]
│  (根据 HTTPRoute:host=www.example.com → backendRefs: httpbin:8000)

[Service httpbin:8000 (ClusterIP 虚拟端口)]
│  (根据 Service: port 8000 → targetPort 8080)

[httpbin Pod 容器内部 8080 端口]
│

go-httpbin 进程处理请求,返回响应

Leave a Reply