# 使用 appProtocol 检测上游协议

`appProtocol` 是 Service 端口上的可选字段，用于说明后端期望接收的流量类型。Ingress Controller 可以使用该值自动判断上游协议。

该示例将同一 Service 的 HTTP 与 HTTPS 端口映射为对应的网关上游 scheme。示例假设后端 Pod 已经运行；下面的 Service manifest 不会创建这些 Pod。

## 支持的 appProtocol 值[​](#支持的-appprotocol-值 "支持的 appProtocol 值的直接链接")

Ingress Controller 支持在 Service 端口中使用以下 `appProtocol` 值：

| 值                  | 说明                                                                      |
| ------------------- | ------------------------------------------------------------------------- |
| `http`              | 将上游协议设置为 `http`。                                                 |
| `https`             | 将上游协议设置为 `https`。                                                |
| `kubernetes.io/ws`  | 将上游协议设置为 `http`，并在路由上将 `enable_websocket` 设置为 `true`。  |
| `kubernetes.io/wss` | 将上游协议设置为 `https`，并在路由上将 `enable_websocket` 设置为 `true`。 |

如果未指定 `appProtocol`，默认使用 `http` 协议。

引用 `ExternalName` Service 的 HTTPRoute 与 Ingress 使用相同的 `appProtocol` 检测逻辑。请定义被引用的 Service 端口及其 `appProtocol`；控制器会解析外部 DNS 名称，并应用所选上游协议。

## 前置条件[​](#前置条件 "前置条件的直接链接")

* 完成[设置 Ingress Controller 和网关](https://docs.apiseven.com/ingress-controller/set-up-ingress-controller-and-gateway.md)。
* 在 `aic` 命名空间中提供带有 `app: httpbin` 标签且已就绪的后端 Pod。Pod 必须分别在端口 `80` 和 `443` 提供 HTTP 与 HTTPS 服务，并与下面的 Service 匹配。本指南使用现有后端；创建 Service 不会部署这些 Pod。

## 定义 Kubernetes Service[​](#定义-kubernetes-service "定义 Kubernetes Service的直接链接")

假设你要暴露上游服务 `httpbin`。可以在 Service 中使用 `appProtocol` 指明每个端口期望的协议：

httpbin-service.yaml

```
apiVersion: v1

kind: Service

metadata:

  namespace: aic

  name: httpbin

spec:

  selector:

    app: httpbin

  ports:

    - name: http

      port: 80

      targetPort: 80

      appProtocol: http

    - name: https

      port: 443

      targetPort: 443

      appProtocol: https
```

后端开始运行后，请先应用 Service，再配置 HTTPRoute。请确认该 Service 存在就绪的 EndpointSlice；没有端点的 Service 无法创建可用的 HTTPRoute 上游。协议来自 Service 端口的 `appProtocol`，而不是端点。

```
kubectl apply -f httpbin-service.yaml

kubectl get endpointslice -n aic -l kubernetes.io/service-name=httpbin
```

## 创建路由[​](#创建路由 "创建路由的直接链接")

创建引用指定 Service 端口的路由。Ingress Controller 会读取被引用端口关联的 `appProtocol` 值，并据此自动配置上游协议。

在本示例中，路由会指向 Service 的 `443` 端口，该端口配置了 `appProtocol: https`。因此无需额外配置，上游协议会自动设置为 `https`。

* Gateway API
* Ingress
* APISIX CRD

httpbin-https.yaml

```
apiVersion: gateway.networking.k8s.io/v1

kind: HTTPRoute

metadata:

  namespace: aic

  name: httpbin-route

spec:

  parentRefs:

  - name: apisix

  rules:

  - matches:

    - path:

        type: Exact

        value: /anything

    backendRefs:

    - name: httpbin

      port: 443
```

httpbin-https.yaml

```
apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

  namespace: aic

  name: httpbin-route

spec:

  ingressClassName: apisix

  rules:

    - http:

        paths:

          - path: /anything

            pathType: Exact

            backend:

              service:

                name: httpbin

                port:

                  number: 443
```

httpbin-https.yaml

```
apiVersion: apisix.apache.org/v2

kind: ApisixRoute

metadata:

  namespace: aic

  name: httpbin-route

spec:

  ingressClassName: apisix

  http:

  - name: httpbin-route

    match:

      paths:

      - /anything

    backends:

    - serviceName: httpbin

      servicePort: 443
```

将配置应用到集群：

```
kubectl apply -f httpbin-https.yaml
```

## 验证上游协议[​](#验证上游协议 "验证上游协议的直接链接")

如果你使用 API7 企业版，可以在 API7 控制台中看到该服务的上游协议已设置为 `https`。

如果使用设置指南中的 APISIX 本地评估环境，请先导出其生成的 admin 角色 Key：

```
export ADMIN_API_KEY="$(kubectl get secret apisix-admin-credentials \

  --namespace aic \

  -o jsonpath='{.data.admin}' | base64 --decode)"
```

将 Admin API 的 Service 端口转发到本地：

```
kubectl port-forward service/apisix-admin -n aic 9180:9180 &
```

向 `/apisix/admin/configs` 发送请求，查看同步到 APISIX 独立模式部署中网关的全部配置：

```
curl "http://127.0.0.1:9180/apisix/admin/configs" -H "X-API-KEY: ${ADMIN_API_KEY}" | jq
```

你应看到该服务的上游协议已设置为 `https`：

```
{

  "upstreams": [

    {

      "scheme": "https",

      "labels": {

        "managed-by": "apisix-ingress-controller"

      },

      "name": "aic_httpbin-route_0",

      "id": "ee55f349"

    }

  ]

}
```
