k8s gateway api - setting up gateway and routes to different service in a cluster
To setup a k8s gateway api in a gke cluster, you typically have to
- create gatewayapi
- setup necessary http route
- deploy your services A and service B.
Setting up gateway by running the following yaml
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: my-shared-gateway
spec:
gatewayClassName: gke-l7-regional-external-managed
listeners:
- protocol: HTTP # Or HTTPS for production
port: 80 # Or 443 for HTTPS
name: http
hostname: "*.example.com"
create your http route A.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: a-route
namespace: default
spec:
parentRefs:
- name: my-shared-gateway
hostnames:
- "a.example.com"
rules:
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: a-service
port: 80
create http route b
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: b-route
namespace: default
spec:
parentRefs:
- name: my-shared-gateway
hostnames:
- "b.example.com"
rules:
- matches:
- path:
type: PathPrefix
value: /
backendRefs:
- name: b-service
port: 80
Next, you can deploy your service A:
apiVersion: apps/v1
kind: Deployment
metadata:
name: a-deployment
labels:
app: a
spec:
replicas: 1
selector:
matchLabels:
app: a
template:
metadata:
labels:
app: a
spec:
containers:
- name: hello
image: hashicorp/http-echo
args:
- "-text=Hello from Service A"
ports:
- containerPort: 5678
---
apiVersion: v1
kind: Service
metadata:
name: a-service
spec:
selector:
app: a
ports:
- protocol: TCP
port: 80
targetPort: 5678
Next, you can deploy your service B
apiVersion: apps/v1
kind: Deployment
metadata:
name: b-deployment
labels:
app: b
spec:
replicas: 1
selector:
matchLabels:
app: b
template:
metadata:
labels:
app: b
spec:
containers:
- name: hello
image: hashicorp/http-echo
args:
- "-text=Hello from Service B"
ports:
- containerPort: 5678
---
apiVersion: v1
kind: Service
metadata:
name: b-service
spec:
selector:
app: b
ports:
- protocol: TCP
port: 80
targetPort: 5678
The github repo for this setup can be found here.
https://github.com/mitzenjeremywoo/k8s-gateway-api-differnet-host-routing
Comments