github.com/projectcontour/contour@v1.28.2/site/content/docs/1.27/guides/gateway-api.md (about)

     1  ---
     2  title: Using Gateway API with Contour
     3  ---
     4  
     5  This tutorial walks through an example of using [Gateway API][1] with Contour.
     6  See the [Contour reference documentation][5] for more information on Contour's Gateway API support.
     7  
     8  ### Prerequisites
     9  The following prerequisites must be met before following this guide:
    10  
    11  - A working [Kubernetes][2] cluster. Refer to the [compatibility matrix][3] for cluster version requirements.
    12  - The [kubectl][4] command-line tool, installed and configured to access your cluster.
    13  
    14  ## Deploy Contour with Gateway API enabled
    15  
    16  First, deploy Contour with Gateway API enabled.
    17  This can be done using either [static or dynamic provisioning][6].
    18  
    19  ### Option #1: Statically provisioned
    20  
    21  Create Gateway API CRDs:
    22  ```shell
    23  $ kubectl apply -f {{< param github_raw_url>}}/{{< param branch >}}/examples/gateway/00-crds.yaml
    24  ```
    25  
    26  Create a GatewayClass:
    27  ```shell
    28  kubectl apply -f - <<EOF
    29  kind: GatewayClass
    30  apiVersion: gateway.networking.k8s.io/v1beta1
    31  metadata:
    32    name: contour
    33  spec:
    34    controllerName: projectcontour.io/gateway-controller
    35  EOF
    36  ```
    37  
    38  Create a Gateway in the `projectcontour` namespace:
    39  ```shell
    40  kubectl apply -f - <<EOF
    41  kind: Namespace
    42  apiVersion: v1
    43  metadata:
    44    name: projectcontour
    45  ---
    46  kind: Gateway
    47  apiVersion: gateway.networking.k8s.io/v1beta1
    48  metadata:
    49    name: contour
    50    namespace: projectcontour
    51  spec:
    52    gatewayClassName: contour
    53    listeners:
    54      - name: http
    55        protocol: HTTP
    56        port: 80
    57        allowedRoutes:
    58          namespaces:
    59            from: All
    60  EOF
    61  ```
    62  
    63  Deploy Contour:
    64  ```shell
    65  $ kubectl apply -f {{< param base_url >}}/quickstart/contour.yaml
    66  ```
    67  This command creates:
    68  
    69  - Namespace `projectcontour` to run Contour
    70  - Contour CRDs
    71  - Contour RBAC resources
    72  - Contour Deployment / Service
    73  - Envoy DaemonSet / Service
    74  - Contour ConfigMap
    75  
    76  Update the Contour configmap to enable Gateway API processing by specifying a gateway controller name, and restart Contour to pick up the config change:
    77  
    78  ```shell
    79  kubectl apply -f - <<EOF
    80  kind: ConfigMap
    81  apiVersion: v1
    82  metadata:
    83    name: contour
    84    namespace: projectcontour
    85  data:
    86    contour.yaml: |
    87      gateway:
    88        controllerName: projectcontour.io/gateway-controller
    89  EOF
    90  
    91  kubectl -n projectcontour rollout restart deployment/contour
    92  ```
    93  
    94  See the next section ([Testing the Gateway API](#testing-the-gateway-api)) for how to deploy an application and route traffic to it using Gateway API!
    95  
    96  ### Option #2: Dynamically provisioned
    97  
    98  Deploy the Gateway provisioner:
    99  ```shell
   100  $ kubectl apply -f {{< param base_url >}}/quickstart/contour-gateway-provisioner.yaml
   101  ```
   102  
   103  This command creates:
   104  
   105  - Namespace `projectcontour` to run the Gateway provisioner
   106  - Contour CRDs
   107  - Gateway API CRDs
   108  - Gateway provisioner RBAC resources
   109  - Gateway provisioner Deployment
   110  
   111  Create a GatewayClass:
   112  
   113  ```shell
   114  kubectl apply -f - <<EOF
   115  kind: GatewayClass
   116  apiVersion: gateway.networking.k8s.io/v1beta1
   117  metadata:
   118    name: contour
   119  spec:
   120    controllerName: projectcontour.io/gateway-controller
   121  EOF
   122  ```
   123  
   124  Create a Gateway:
   125  
   126  ```shell
   127  kubectl apply -f - <<EOF
   128  kind: Gateway
   129  apiVersion: gateway.networking.k8s.io/v1beta1
   130  metadata:
   131    name: contour
   132    namespace: projectcontour
   133  spec:
   134    gatewayClassName: contour
   135    listeners:
   136      - name: http
   137        protocol: HTTP
   138        port: 80
   139        allowedRoutes:
   140          namespaces:
   141            from: All
   142  EOF
   143  ```
   144  
   145  The above creates:
   146  - A `GatewayClass` named `contour` controlled by the Gateway provisioner (via the `projectcontour.io/gateway-controller` string)
   147  - A `Gateway` resource named `contour` in the `projectcontour` namespace, using the `contour` GatewayClass
   148  - Contour and Envoy resources in the `projectcontour` namespace to implement the `Gateway`, i.e. a Contour deployment, an Envoy daemonset, an Envoy service, etc.
   149  
   150  See the next section ([Testing the Gateway API](#test-routing)) for how to deploy an application and route traffic to it using Gateway API!
   151  
   152  ## Configure an HTTPRoute
   153  
   154  Deploy the test application:
   155  ```shell
   156  $ kubectl apply -f {{< param github_raw_url>}}/{{< param branch >}}/examples/example-workload/gatewayapi/kuard/kuard.yaml
   157  ```
   158  This command creates:
   159  
   160  - A Deployment named `kuard` in the default namespace to run kuard as the test application.
   161  - A Service named `kuard` in the default namespace to expose the kuard application on TCP port 80.
   162  - An HTTPRoute named `kuard` in the default namespace, attached to the `contour` Gateway, to route requests for `local.projectcontour.io` to the kuard service.
   163  
   164  Verify the kuard resources are available:
   165  ```shell
   166  $ kubectl get po,svc,httproute -l app=kuard
   167  NAME                         READY   STATUS    RESTARTS   AGE
   168  pod/kuard-798585497b-78x6x   1/1     Running   0          21s
   169  pod/kuard-798585497b-7gktg   1/1     Running   0          21s
   170  pod/kuard-798585497b-zw42m   1/1     Running   0          21s
   171  
   172  NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
   173  service/kuard   ClusterIP   172.30.168.168   <none>        80/TCP    21s
   174  
   175  NAME                                        HOSTNAMES
   176  httproute.gateway.networking.k8s.io/kuard   ["local.projectcontour.io"]
   177  ```
   178  
   179  ## Test Routing
   180  
   181  _Note, for simplicity and compatibility across all platforms we'll use `kubectl port-forward` to get traffic to Envoy, but in a production environment you would typically use the Envoy service's address._
   182  
   183  Port-forward from your local machine to the Envoy service:
   184  ```shell
   185  # If using static provisioning
   186  $ kubectl -n projectcontour port-forward service/envoy 8888:80
   187  
   188  # If using dynamic provisioning
   189  $ kubectl -n projectcontour port-forward service/envoy-contour 8888:80
   190  ```
   191  
   192  In another terminal, make a request to the application via the forwarded port (note, `local.projectcontour.io` is a public DNS record resolving to 127.0.0.1 to make use of the forwarded port):
   193  ```shell
   194  $ curl -i http://local.projectcontour.io:8888
   195  ```
   196  You should receive a 200 response code along with the HTML body of the main `kuard` page.
   197  
   198  You can also open http://local.projectcontour.io:8888/ in a browser.
   199  
   200  ### Further reading
   201  
   202  This guide only scratches the surface of the Gateway API's capabilities. See the [Gateway API website][1] for more information.
   203  
   204  
   205  [1]: https://gateway-api.sigs.k8s.io/
   206  [2]: https://kubernetes.io/
   207  [3]: https://projectcontour.io/resources/compatibility-matrix/
   208  [4]: https://kubernetes.io/docs/tasks/tools/install-kubectl/
   209  [5]: /docs/{{< param version >}}/config/gateway-api
   210  [6]: /docs/{{< param version >}}/config/gateway-api#enabling-gateway-api-in-contour