# Routing traffic to applications
Type of document: How-to guide
Product: NGINX Gateway Fabric
---
Learn how to route external traffic to your Kubernetes applications using NGINX Gateway Fabric.
## Overview
You can route traffic to your Kubernetes applications using the Gateway API and NGINX Gateway Fabric. Whether you're managing a web application or a REST backend API, you can use NGINX Gateway Fabric to expose your application outside the cluster.
## Before you begin
- [Install](/ngf/install/) NGINX Gateway Fabric.
## Example application
The application we are going to use in this guide is a simple **coffee** application comprised of one service and two pods:
```mermaid
graph TB
subgraph cluster [Kubernetes Cluster]
style cluster fill:#FFFFFF,stroke:#000000
svc[Service
coffee]
pod1[Pod
coffee]
pod2[Pod
coffee]
end
svc --> pod1 & pod2
class pod1,pod2,svc appNode
classDef appNode fill:#edbd8c,stroke:#D9822B
```
Using this architecture, the **coffee** application is not accessible outside the cluster. We want to expose this application on the hostname "cafe.example.com" so that clients outside the cluster can access it.
Install NGINX Gateway Fabric and create two Gateway API resources: a [gateway](https://gateway-api.sigs.k8s.io/reference/spec/#gateway) and an [HTTPRoute](https://gateway-api.sigs.k8s.io/reference/spec/#httproute).
Using these resources we will configure a simple routing rule to match all HTTP traffic with the hostname "cafe.example.com" and route it to the **coffee** service.
---
## Set up
Create the **coffee** application in Kubernetes by copying and pasting the following block into your terminal:
```yaml
kubectl apply -f - < 80/TCP 77s
```
---
## Application architecture with NGINX Gateway Fabric
To route traffic to the **coffee** application, we will create a Gateway and HTTPRoute. The following diagram shows the configuration we are creating in the next step:
```mermaid
graph LR
subgraph config [Namespace default]
subgraph padding [" "]
direction LR
style config fill:#FFFFFF,stroke:#000000
subgraph gw[Gateway cafe]
subgraph gwPadding [" "]
gwContents[HTTP/80]
end
end
subgraph hr[HTTPRoute coffee]
subgraph hrPadding [" "]
hrContents[cafe.example.com]
subgraph describeMatchAll [Match all
traffic]
subgraph describeMatchPadding [" "]
matchAll[Host: *
Path: *]
end
end
subgraph describeService [Group matching
pods within a Service]
subgraph describePadding [" "]
coffeeSvc[Service
coffee]
end
end
end
end
end
end
gwContents --> hrContents --> matchAll --> coffeeSvc
class padding,gwPadding,hrPadding,describeMatchAll,describeService,describePadding,describeMatchPadding noBorder
class gw gateway
class hr httpRoute
class matchAll,hrContents,coffeeSvc appDevNode
class gwContents clusterOppNode
classDef noBorder stroke:none,fill:none,text-align:center
classDef default fill:#FFFFFF,stroke:#000000
classDef gateway fill:#FFFFFF,stroke:#blue,stroke-dasharray: 3 3,text-align:center
classDef httpRoute fill:#FFFFFF,stroke:#D9822B,stroke-dasharray: 3 3,text-align:center
classDef appDevNode fill:#edbd8c,stroke:#D9822B
classDef clusterOppNode fill:lightblue,stroke:darkblue
```
We need a Gateway to create an entry point for HTTP traffic coming into the cluster. The **cafe** Gateway we are going to create will open an entry point to the cluster on port 80 for HTTP traffic.
To route HTTP traffic from the Gateway to the **coffee** service, we need to create an HTTPRoute named **coffee** and attach it to the Gateway. This HTTPRoute will have a single routing rule that routes all traffic to the hostname "cafe.example.com" from the Gateway to the **coffee** service.
Once NGINX Gateway Fabric processes the **cafe** Gateway and **coffee** HTTPRoute, it will configure a data plane (NGINX) to route all HTTP requests sent to "cafe.example.com" to the pods that the **coffee** service targets:
```mermaid
graph LR
style cluster fill:#FFFFFF,stroke:#000000
clients[Clients]
ngfSvc["Public IP Address for
cafe.example.com"]
subgraph cluster [Kubernetes Cluster]
subgraph appNs [Namespace
default]
subgraph nsPadding [" "]
nginxPod[Pod NGINX]
coffeePod1[Pod coffee]
coffeePod2[Pod coffee]
end
end
end
ngfSvc --> nginxPod
nginxPod --> coffeePod1 & coffeePod2
clients --> ngfSvc
class clusterPadding,nsPadding,clusterPadding2 noBorder
class gwNS,appNs namespace
class nginxPod nginxNode
class coffeePod1,coffeePod2 coffeeNode
class ngfSvc svc
class clients clientNode
classDef noBorder stroke:none,fill:none
classDef default fill:#FFFFFF,stroke:#000000
classDef namespace fill:#FFFFFF,stroke:#036ffc,stroke-dasharray: 5 5,text-align:center
classDef nginxNode fill:#b4e0ad,stroke:#2AA317
classDef svc fill:lightblue,stroke:darkblue
classDef coffeeNode fill:#edbd8c,stroke:#D9822B
classDef clientNode fill:#D3D3D3
```
The **coffee** service is omitted from the diagram above because the NGINX Pod routes directly to the pods that the **coffee** service targets.
**Note:** In the diagrams above, all resources that are the responsibility of the cluster operator are shown in blue. The orange resources are the responsibility of the application developers.
See the [roles and personas](https://gateway-api.sigs.k8s.io/concepts/roles-and-personas/#roles-and-personas_1) Gateway API document for more information on these roles.
---
## Create the Gateway API resources
To create the **cafe** gateway, copy and paste the following into your terminal:
```yaml
kubectl apply -f - <
```
**Note:**
In a production environment, you should have a DNS record for the external IP address that is exposed, and it should refer to the hostname that the gateway will forward for.
This Gateway is associated with NGINX Gateway Fabric through the **gatewayClassName** field. The default installation of NGINX Gateway Fabric creates a GatewayClass with the name **nginx**. NGINX Gateway Fabric will only configure Gateways with a **gatewayClassName** of **nginx** unless you change the name via the `--gatewayclass` [command-line flag](/ngf/reference/cli-help.md#controller).
We specify a [listener](https://gateway-api.sigs.k8s.io/reference/spec/#listener) on the Gateway to open an entry point on the cluster. In this case, since the coffee application accepts HTTP requests, we create an HTTP listener, named **http**, that listens on port 80.
By default, Gateways only allow routes (such as HTTPRoutes) to attach if they are in the same namespace as the Gateway. If you want to change this behavior, you can set
the [**allowedRoutes**](https://gateway-api.sigs.k8s.io/reference/spec/#allowedroutes) field.
Next you will create the HTTPRoute by copying and pasting the following into your terminal:
```yaml
kubectl apply -f - <
404 Not Found
404 Not Found
nginx/1.25.2