NGINX Gateway Fabricのセットアップ

NGINXをDataplaneとしてKubernetes Gatewayを利用する方法を紹介します。 NGINX Gateway Fabricの情報は GitHub: nginx-gateway-fabric を参照してください

Note

本資料作成時点では、NGINX OSSを利用した動作確認となります

Gateway APIについて

Gateway APIについて簡単にご説明します

Gateway APIはKubernetesにおいて、既存のIngressに比べより柔軟な設定方法の実現を目的としています。 GatewayClass, Gateway, HTTPRoute, TCPRoute, Service などのリソースを用いて、様々なベンダーや担当者などが自由に設定できるインタフェースを提供します。

https://gateway-api.sigs.k8s.io/images/api-model.png

各コンセプトについて紹介します

  • Role-oriented : Gatewayは複数のAPIリソースで構成されます。各Roleに応じたKubernetesネットワークの利用や設定を提供します

  • Portable(移植可能性) : 以前の実装から引き継がれる性質。Ingressは汎用的な仕様を備え、Gateway APIは移植可能な多くの機能が実装されています

  • Expressive(多様な設定) : Gatweay APIはヘッダーベースの処理、トラフィックのウェイト等、IngressではCustom Annotationが必要であったような機能がサポートされます

  • Extensible(拡張性) : Gateway APIは様々な層のAPIに接続可能で、より適切な箇所でより細かな要件の実現が可能となります

更に以下のような特徴を持ちます

  • GatewayCalss : GatewayClass はロードバランシングを形式化したものです。Kubernetesのリソースを利用し、簡単に且つそれぞれ個別にユーザが設定の管理をすることができます

  • Shared Gateways and cross-Namespace support(複数ネームスペース間でのGatewayの共有) : ロードバランサーや仮想IP(VIP)として通信を待ち受ける単一のゲートウェイに、それぞれ個別の Route リソースを割り当てることができます。これにより複数のチームが互いに密な連携をすることなくインフラストラクチャを安全に共有できます

  • Typed Routes and typed backends(予め定義されたRouteとBackend) : Gateway APIは予め役割が定義されたRouteとBackendがあります。これはHTTPやgRPCなどのプロトコルや、ServiceやStorage Bucketなどバックエンドを柔軟にサポートできます

これらの特徴から、複数の組織で多彩な設定と、各設定の柔軟な可搬性を同時に実現します

https://gateway-api.sigs.k8s.io/images/gateway-roles.png

環境のセットアップ

ファイルの取得

cd ~/
git clone https://github.com/nginxinc/nginx-gateway-fabric.git
cd nginx-gateway-fabric

Gateway APIリソースをデプロイします

## cd nginx-gateway-fabric
kubectl kustomize "https://github.com/nginx/nginx-gateway-fabric/config/crd/gateway-api/standard?ref=v1.6.1" | kubectl apply -f -
kubectl apply -f https://raw.githubusercontent.com/nginx/nginx-gateway-fabric/v1.6.1/deploy/crds.yaml

必要となるリソースをデプロイします

## cd nginx-gateway-fabric
kubectl apply -f ../nginx-kubernetes-gateway-conf/deploy.yaml

Kubernetes Gateway 用のNGINXが起動していることを確認します

## cd nginx-gateway-fabric
kubectl get pods -n nginx-gateway
実行結果サンプル
1NAME                             READY   STATUS    RESTARTS   AGE
2nginx-gateway-67cb7f7d65-gq8jp   2/2     Running   0          7m5s

外部へNodePortで公開します。 内容を追記し、NodePort Serviceをデプロイします

## cd nginx-gateway-fabric
cat << EOF > ../nginx-kubernetes-gateway-conf/nodeport-config.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-gateway
  namespace: nginx-gateway
  labels:
    app.kubernetes.io/name: nginx-gateway
    app.kubernetes.io/instance: nginx-gateway
    app.kubernetes.io/version: "1.6.1"
spec:
  type: NodePort
  selector:
    app.kubernetes.io/name: nginx-gateway
    app.kubernetes.io/instance: nginx-gateway
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: 80
      nodePort: 31437
    - name: https
      port: 443
      protocol: TCP
      targetPort: 443
      nodePort: 31438
EOF
kubectl apply -f ../nginx-kubernetes-gateway-conf/nodeport-config.yaml

以下コマンドでポート確認します

kubectl get svc -n nginx-gateway
実行結果サンプル
1NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
2nginx-gateway   NodePort   10.110.101.67   <none>        80:31437/TCP,443:31438/TCP   21m

このコマンドを実行した結果、Kubernetes の Worker Nodeでそれぞれのサービスに対しポートが割り当てられています。 6. NGINX Ingress Controller を外部へ NodePort で公開する の内容を参考に、NGINXのConfigを作成し、設定を反映します。

cd ~/
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf-
cat << EOF > nginx.conf
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
   worker_connections  1024;
}


# TCP/UDP load balancing
#
stream {
   upstream tcp80_backend {
      server node1:31437;    # HTTP(TCP/80)に割り当てられたポート番号
   }
   upstream tcp443_backend {
      server node1:31438;     # HTTPS(TCP/443)に割り当てられたポート番号
   }

   server {
      listen 80;
      proxy_pass tcp80_backend;
   }
   server {
      listen 443;
      proxy_pass tcp443_backend;
   }
}
EOF
sudo cp nginx.conf /etc/nginx/nginx.conf
sudo nginx -s reload