コンテンツにスキップ

04. Gateway APIを使う

04. Gateway APIを使う

uml diagram

  • GatewayとHttpRouteを作成する
  • Gateway API CRDsをインストールする
  • Gateway Controller(例: nginx-gateway-fabric)をインストールする

Gateway

Gatewayはロードバランサーを定義する。

Gateway APIは規格であり、Gatewayを使うには規格に則った実装が必要となる。nginx-gateway-fabricはGateway APIの実装の一つである。

nextjs-example-manifests/nextjs-example-gateway.yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
  name: nextjs-example-gateway
spec:
  gatewayClassName: nginx
  listeners:
    - name: http
      port: 80
      protocol: HTTP
      allowedRoutes:
        namespaces:
          from: Same

HTTPRoute

HTTPRouteはリクエストをどのサービスに送るかのルーティングを定義する。

nextjs-example-manifests/nextjs-example-httproute.yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: nextjs-example-route
spec:
  parentRefs:
    - name: nextjs-example-gateway
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /
      backendRefs:
        - name: nextjs-example
          port: 3000

動作確認する

下記を参考にGateway APIをインストールする。minikube deleteで消える。

Install NGINX Gateway Fabric with Helm | NGINX Documentation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ minikube start --insecure-registry="192.168.11.42:8085"

# Gateway APIをインストールする (Gateway、HTTPRouteを使えるようになる)
$ kubectl kustomize "https://github.com/nginx/nginx-gateway-fabric/config/crd/gateway-api/standard?ref=v2.4.1" | kubectl apply -f -

# NGINX Gateway Fabricをインストールする
$ helm install ngf oci://ghcr.io/nginx/charts/nginx-gateway-fabric \
  --create-namespace -n nginx-gateway \
  --set service.type=NodePort

$ kubectl apply -f ./nextjs-example-manifests/

デプロイすると自動的にServiceが作成される。

1
2
3
4
5
$ kubectl get services
NAME                           TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes                     ClusterIP      10.96.0.1        <none>        443/TCP        12m
nextjs-example                 ClusterIP      10.96.31.32      <none>        3000/TCP       11m
nextjs-example-gateway-nginx   LoadBalancer   10.106.143.246   <pending>     80:30462/TCP   2m31s
1
$ kubectl port-forward service/nextjs-example-gateway-nginx 8080:80

http://localhost:8080でアクセスできるようになる。

Minikubeの場合ポートフォワーディングが必要だが、クラウドコンピューティングの場合Gatewayを作成するだけで自動的に外部IPアドレスが付与され、ポートフォワーディングは不要らしい。

K3sの場合、以下のコマンドを実行し、アクセス先ポートを確認する。(TYPEがLoadBalancerの行について、PORTSの右側に書かれているポート番号)

1
2
3
4
5
$ kubectl get service -A
NAMESPACE       NAME                                             TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)                        AGE
app             app                                              NodePort       10.43.83.156    <none>         3000:32674/TCP                 4h48m
app             app-nginx                                        LoadBalancer   10.43.205.14    <pending>      80:30351/TCP                   26m
app             db                                               ClusterIP      10.43.106.111   <none>         5432/TCP                       4h48m