# 通过ELB Gateway暴露服务
ELB Gateway是CCE提供的Gateway API实现方案，直接以华为云ELB实例作为数据面，无需在集群内部署代理进程，将Gateway配置实时转化为ELB实例的监听器、转发策略和后端服务器组配置，管理集群南北向流量。本文介绍如何使用ELB Gateway来暴露服务至集群外部。
#### 前提条件
- 已创建Kubernetes集群，且集群版本满足以下要求：
  - v1.30集群：v1.30.14-r120及以上版本
  
  - v1.31集群：v1.31.14-r80及以上版本
  
  - v1.32集群：v1.32.13-r50及以上版本
  
  - v1.33集群：v1.33.13-r0及以上版本
  
  - v1.34集群：v1.34.10-r0及以上版本
  
  - v1.35集群：v1.35.7-r0及以上版本
  
  - v1.36集群：v1.36.3-r0及以上版本
  
  - 其他更高版本的集群
   
- 集群中已创建后端应用及对应的Service。本文中后端应用为Nginx，Service名称为backend，Service端口为3000。您可以将以下YAML内容保存为backend.yaml，然后执行**kubectl apply -f backend.yaml** 命令。
  ```
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: backend
    namespace: default
  spec:
    replicas: 2
    selector:
      matchLabels:
        app: backend
        version: v1
    template:
      metadata:
        labels:
          app: backend
          version: v1
      spec:
        containers:
          - name: container-1
            image: nginx:latest
        imagePullSecrets:
          - name: default-secret
  ---
  apiVersion: v1
  kind: Service
  metadata:
    name: backend
    labels:
      app: backend
      version: v1
    namespace: default
  spec:
    selector:
      app: backend
      version: v1
    ports:
      - name: cce-service-0
        targetPort: 80
        port: 3000
        protocol: TCP
    type: ClusterIP
  ```
  
 
#### 注意事项
- ELB Gateway不支持使用已有监听器。
- 集群删除时，由CCE创建的监听器及监听器下的所有转发策略和后端服务器组均会删除。
 
#### 核心概念
- **GatewayClass**：类似IngressClass定义了网关的控制器类型。
- **Gateway、ListenerSet** ：配置具体的网关实例，比如监听的端口（如80或443）、端口的协议等。详见[Gateway与ListenerSet参数配置](https://support.huaweicloud.com/usermanual-cce/cce_10_1179.html)。
- **HTTPRoute** ：定义具体的流量路由规则（比如路径、Header、流量权重、查询参数等），并绑定到后端Service。详见[HTTPRoute参数配置](https://support.huaweicloud.com/usermanual-cce/cce_10_1181.html)。
CCE新增了LoadBalancerAutoCreateConfiguration、ListenerConfiguration、PoolConfiguration三种自定义资源，通过创建和更新这些资源，可实现ELB参数配置、监听器参数配置和后端服务器组配置。
- **LoadBalancerAutoCreateConfiguration（简称lbconfig）** ：ELB参数配置。创建lbconfig后，将其关联到自定义GatewayClass或Gateway，CCE会按照配置的参数自动创建ELB实例。详见[自动创建负载均衡配置（lbconfig）](https://support.huaweicloud.com/usermanual-cce/cce_10_1182.html)。
- **ListenerConfiguration（简称lsconfig）** ：监听器参数配置。创建Gateway、ListenerSet后，CCE会自动创建关联的lsconfig，您可通过修改lsconfig来调整监听器配置。详见[监听器参数配置（lsconfig）](https://support.huaweicloud.com/usermanual-cce/cce_10_1183.html)。
- **PoolConfiguration（简称poolconfig）** ：后端服务器组配置，关联Service。创建HTTPRoute后，若关联的Service尚无poolconfig，CCE会自动创建。您可通过poolconfig设置健康检查等后端服务器组配置。多个HTTPRoute关联同一个Service时，共享同一个poolconfig。详见[后端服务器组配置（poolConfig）](https://support.huaweicloud.com/usermanual-cce/cce_10_1184.html)。
![](https://support.huaweicloud.com/usermanual-cce/zh-cn_image_0000002764338310.png "点击放大")
 #### 默认GatewayClass
CCE提供了以下默认GatewayClass，您可在创建Gateway时指定以下GatewayClass。
| GatewayClass名称          | 关联的lbconfig名称             | 说明                                                    |
|:---|:---|:---|
| cce-shared-internal   | cce-shared-internal      | Gateway关联该GatewayClass后，会自动创建共享型私网ELB。              |
| cce-shared-external    | cce-shared-external     | Gateway关联该GatewayClass后，会自动创建共享型公网ELB。               |
| cce-dedicated-internal   | cce-dedicated-internal | Gateway关联该GatewayClass后，会自动创建独享型私网ELB。               |
| cce-dedicated-external | cce-dedicated-external     | Gateway关联该GatewayClass后，会自动创建独享型公网ELB。             |
| cce-existing-elb         | 无                       | 无关联LoadBalancerAutoCreateConfiguration，需要配置已有ELB_ID。 |
   
#### 步骤一：创建Gateway
创建Gateway，关联CCE管理的GatewayClass。示例如下：
```
创建Gateway，关联CCE管理的GatewayClass。示例如下：
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway                      # 资源类型为Gateway
metadata:
  name: cce-gateway              # Gateway名称
  namespace: default
spec:
  gatewayClassName: cce-shared-internal  # GatewayClass名称
  listeners:
    - name: http
      protocol: HTTP               # 协议
      port: 80                     # 监听端口
```
在此示例中，该Gateway关联cce-shared-internal GatewayClass，会自动创建共享型私网ELB，并为此ELB创建协议为HTTP、端口为80的监听器。
#### 步骤二：使用ListenerSet
1. Gateway开启ListenerSet挂载许可。 
   ```
   apiVersion: gateway.networking.k8s.io/v1
   kind: Gateway
   metadata:
     name: parent-gateway
     namespace: default
   spec:
     gatewayClassName: cce-shared-internal
     # 必填：开启ListenerSet挂载许可
     allowedListeners:
       namespaces:
         from: Same       # 可选：Same（仅同命名空间）、All（跨所有命名空间）、Selector（按Label筛选）
     listeners:
       - name: default-http
         port: 80
         protocol: HTTP
   ```
   
   
2. 创建ListenerSet关联Gateway。 
   ```
   apiVersion: gateway.networking.k8s.io/v1
   kind: ListenerSet
   metadata:
     name: app-team-listeners
     namespace: default
   spec:
     # 绑定到目标Gateway
     parentRef:
       group: gateway.networking.k8s.io
       kind: Gateway
       name: parent-gateway
     # 定义该集合下的专属监听规则
     listeners:
       - name: https-app
         port: 443
         protocol: HTTPS
   ```
   创建完成后，CCE会自动为ListenerSet中的监听器创建对应的lsconfig，您可通过修改lsconfig来配置证书等监听器参数。
   
   
3. 修改lsconfig为HTTPS监听器添加证书。 
   1. 使用以下命令精确查询ListenerSet监听器所对应的lsconfig名称：
      ```
      kubectl get lsconfig -n default -l gateway.cce.io/listenerset-name=app-team-listeners,gateway.cce.io/listener-section-name=https-app
      ```
      输出结果示例：
      ```
      NAME                                 TARGET               ACCEPTED   AGE
      app-team-listeners-https-app-kprrt   app-team-listeners   False      11s
      ```
      
   
   2. 编辑ListenerConfiguration，添加证书。
      ```
      apiVersion: gateway.cce.io/v1alpha1
      kind: ListenerConfiguration
      metadata:
        name: app-team-listeners-https-app-kprrt
        namespace: default
      spec:
        targetRef:
          group: gateway.networking.k8s.io
          kind: ListenerSet
          name: app-team-listeners
          sectionName: https-app
        defaultServerCertificate:       #服务器证书
          secretRef:
            name: tls-default-cert      #同命名空间的Secret名称
        sniCertificates:
          - domain: "*.example.com"       #域名
            secretRef:
              name: tls-wildcard-example     #同命名空间的Secret名称
      ```
      
   
   3. 查看ELB侧监听器证书添加成功。
   
   
   
   
 
#### 步骤三：创建HTTPRoute
创建HTTPRoute，关联Gateway，并定义路由规则将流量转发到后端Service。示例如下：
```
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute                # 资源类型为HTTPRoute
metadata:
  name: backend                # HTTPRoute名称
  namespace: default
spec:
  parentRefs:
    - group: gateway.networking.k8s.io
      kind: Gateway
      name: cce-gateway
  hostnames:
    - "www.example.com"    #域名
  rules:
    - backendRefs:
        - group: ""
          kind: Service
          name: backend        # 已创建的Service名称
          port: 3000           # Service的端口
          weight: 1
      matches:
        - path:
            type: PathPrefix   # 前缀匹配
            value: /
```
在此示例中，来自Gateway的HTTP流量，访问域名为www.example.com将被路由到Service的3000端口。
#### 步骤四：测试访问
1. 查询Gateway的IP地址。 
   ```
   kubectl get gateway cce-gateway -o jsonpath='{.status.addresses[?(@.type=="IPAddress")].value}'
   ```
   
   
2. 测试访问，其中xx.xx.xx.xx为ELB地址。 
   ```
   curl -I -H "Host: www.example.com" http://xx.xx.xx.xx
   ```
   预期回显如下：
   ```
   HTTP/1.1 200 OK
   server: nginx/1.23.2
   date: Thu, 28 May 2026 01:47:02 GMT
   content-type: text/html
   content-length: 615
   last-modified: Wed, 19 Oct 2022 07:56:21 GMT
   etag: "634fada5-267"
   accept-ranges: bytes
   ```
   
   
 
#### 更多场景
#### HTTPS监听器添加证书
1. 使用以下命令精确查询指定Gateway或ListenerSet监听器所对应的lsconfig名称：
   - 查询Gateway关联的lsconfig名称：
     ```
     kubectl get lsconfig -n <namespace> -l gateway.cce.io/gateway-name=<name>,gateway.cce.io/listener-section-name=<sectionName>
     ```
     
   
   - 查询ListenerSet关联的lsconfig名称：
     ```
     kubectl get lsconfig -n <namespace> -l gateway.cce.io/listenerset-name=<name>,gateway.cce.io/listener-section-name=<sectionName>
     ```
     
   
   
   **参数说明：**
   - \<namespace\>：Gateway或ListenerSet所在的命名空间。
   
   - \<name\>：目标资源名称。不超过 63 个字符则直接使用原名称；超过则保留前 54 个字符（去除末尾 -_. 等分隔符），并通过连字符 - 拼接原名称的前 8 位 SHA-256 哈希值（例如：nameprefix-dc876bc7）。
   
   - \<sectionName\>：目标监听器的名称（即 spec.listeners\[\].name）。
   
   
   **使用示例：** 查询命名空间为default、ListenerSet名称为app-team-listeners、监听器名称为https-app的lsconfig：
   ```
   kubectl get lsconfig -n default -l gateway.cce.io/listenerset-name=app-team-listeners,gateway.cce.io/listener-section-name=https-app
   ```
   输出结果示例：
   ```
   NAME                                 TARGET               ACCEPTED   AGE
   app-team-listeners-https-app-kprrt   app-team-listeners   False      11s
   ```
   
2. 编辑ListenerConfiguration，添加证书。
   ```
   apiVersion: gateway.cce.io/v1alpha1
   kind: ListenerConfiguration
   metadata:
     name: app-team-listeners-https-app-kprrt
     namespace: default
   spec:
     targetRef:
       group: gateway.networking.k8s.io
       kind: ListenerSet
       name: app-team-listeners
       sectionName: https-app
     defaultServerCertificate:       #服务器证书
       secretRef:
         name: tls-default-cert      #同命名空间的Secret名称
     sniCertificates:
       - domain: "*.example.com"       #域名
         secretRef:
           name: tls-wildcard-example     #同命名空间的Secret名称
   ```
   
3. 查看ELB侧监听器证书添加成功。
 
#### 后端服务器添加健康检查
1. 查询Service关联的poolconfig。
   ```
   kubectl get poolconfig -n <namespace> -l gateway.cce.io/service-name=<serviceName>
   ```
   其中\<namespace\>是Service的命名空间，\<serviceName\>是Service的名称。
   示例：
   ```
   kubectl get poolconfig -n default -l gateway.cce.io/service-name=backend
   ```
   预期输出如下：
   ```
   NAME            TARGET    PORT   ACCEPTED   AGE
   backend-95qcg   backend   3000   True       2m10s
   ```
   
2. 编辑poolconfig添加健康检查。
   ```
   apiVersion: gateway.cce.io/v1alpha1
   kind: PoolConfiguration
   metadata:
     name: backend-95qcg
     namespace: default
   spec:
     targetRef:
       group: ""
       kind: Service
       name: backend
     defaultConfig:
       healthCheck:     #健康检查
         enable: true
         protocol: HTTP
         urlPath: "/healthz"
         delay: 5
         timeout: 10
         maxRetries: 3
         expectedCodes: "200-399"
         monitorPort: 8080
   ```
   
3. 查看ELB侧后端服务器组健康检查添加成功。
 
