NGINX Ingress Controller(NIC) 環境のセットアップ

以下の手順に従ってNGINX Ingress Controllerのイメージを作成します

1. 環境セットアップ

ファイルを取得します

cd ~/
git clone https://github.com/nginxinc/kubernetes-ingress/ --branch v3.1.1
cd ~/kubernetes-ingress

ライセンスファイルをコピーしてください ファイルが配置されていない場合、トライアルを申請し証明書と鍵を取得してください

cp ~/nginx-repo* .
ls nginx-repo.*

2. コンテナイメージの作成

NGINX Plus + NGINX App Protectのコンテナイメージを作成します
make debian-image-nap-dos-plus PREFIX=registry.example.com/root/nic/nginxplus-ingress-nap-dos TARGET=container TAG=3.1.1
# Image の Build は数分(約5分)必要となります
docker images | grep nginxplus-ingress-nap-dos
実行結果サンプル
1
registry.example.com/root/nic/nginxplus-ingress-nap-dos   3.1.1     0003d5b72586   About a minute ago   781MB

Container Image のPushのためにレジストリへログイン

# registry.example.com にログイン
docker login registry.example.com
Username: root       << 左の文字列を入力
Password: password   << 左の文字列を入力
実行結果サンプル
1
2
3
4
5
WARNING! Your password will be stored unencrypted in /home/ubuntu/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

Container Image のPush

docker push registry.example.com/root/nic/nginxplus-ingress-nap-dos:3.1.1

3. NGINX Ingress Controller環境のセットアップ

先程の手順で取得したGitHubのフォルダへ移動し、必要となるリソースをデプロイします。

cd ~/kubernetes-ingress/deployments
# Create RBAC
kubectl apply -f common/ns-and-sa.yaml
kubectl apply -f rbac/rbac.yaml
kubectl apply -f rbac/ap-rbac.yaml
kubectl apply -f rbac/apdos-rbac.yaml
# Create Common resources
kubectl apply -f ../examples/shared-examples/default-server-secret/default-server-secret.yaml
kubectl apply -f common/nginx-config.yaml
kubectl apply -f common/ingress-class.yaml
# Create Custom Resource
kubectl apply -f common/crds/k8s.nginx.org_virtualservers.yaml
kubectl apply -f common/crds/k8s.nginx.org_virtualserverroutes.yaml
kubectl apply -f common/crds/k8s.nginx.org_transportservers.yaml
kubectl apply -f common/crds/k8s.nginx.org_policies.yaml
kubectl apply -f common/crds/k8s.nginx.org_globalconfigurations.yaml
kubectl apply -f common/crds/appprotect.f5.com_aplogconfs.yaml
kubectl apply -f common/crds/appprotect.f5.com_appolicies.yaml
kubectl apply -f common/crds/appprotect.f5.com_apusersigs.yaml
kubectl apply -f common/crds/appprotectdos.f5.com_apdoslogconfs.yaml
kubectl apply -f common/crds/appprotectdos.f5.com_apdospolicy.yaml
kubectl apply -f common/crds/appprotectdos.f5.com_dosprotectedresources.yaml

4. NGINX App Protect Dosで利用するArbitratorを実行

Deploymentの内容を確認

deployment/appprotect-dos-arb.yaml の内容を確認します。

deployment/appprotect-dos-arb.yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
kind: Deployment
metadata:
  name: appprotect-dos-arb
  namespace: nginx-ingress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: appprotect-dos-arb
  template:
    metadata:
      labels:
        app: appprotect-dos-arb
    spec:
      containers:
      - name: appprotect-dos-arb
        image: docker-registry.nginx.com/nap-dos/app_protect_dos_arb:1.1.0
        imagePullPolicy: IfNotPresent
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
          - containerPort: 3000
        securityContext:
          allowPrivilegeEscalation: false
          runAsUser: 1001
          capabilities:
            drop:
              - ALL

service/appprotect-dos-arb-svc.yaml の内容を確認します。

service/appprotect-dos-arb-svc.yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
apiVersion: v1
kind: Service
metadata:
  name: svc-appprotect-dos-arb
  namespace: nginx-ingress
spec:
  selector:
    app: appprotect-dos-arb
  ports:
    - name: arb
      port: 3000
      protocol: TCP
      targetPort: 3000

デプロイします。

kubectl apply -f deployment/appprotect-dos-arb.yaml
kubectl apply -f service/appprotect-dos-arb-svc.yaml

デプロイ結果を確認します。

kubectl get deployment -n nginx-ingress
実行結果サンプル
1
2
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
appprotect-dos-arb   1/1     1            1           4m32s
kubectl get pod -n nginx-ingress
実行結果サンプル
1
2
NAME                                  READY   STATUS    RESTARTS   AGE
appprotect-dos-arb-5d89486bbc-pkbrg   1/1     Running   0          4m43s
kubectl get svc -n nginx-ingress
実行結果サンプル
1
2
NAME                     TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
svc-appprotect-dos-arb   ClusterIP   None         <none>        3000/TCP   6s

5. NGINX Ingress Controllerの実行

NGINX Ingress Controllerのpodを実行します。DeploymentとDaemonSetによる実行が可能ですが、のこの記事ではDeploymentで実行します。DaemonSetで実行したい場合にはマニュアルを参照して適切に読み替えて進めてください。

argsで指定するパラメータの詳細は Command-line Arguments を参照してください

## cd ~/kubernetes-ingress/deployments
vi deployment/nginx-plus-ingress.yaml

コメントを付与した行を適切な内容に修正してください

deployment/nginx-plus-ingress.yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
** 省略 **
spec:
   serviceAccountName: nginx-ingress
   containers:
   - image: registry.example.com/root/nic/nginxplus-ingress-nap-dos:3.1.1  # 対象のレジストリを指定してください
   imagePullPolicy: IfNotPresent
   name: nginx-plus-ingress
** 省略 **
   args:
      - -nginx-plus
      - -nginx-configmaps=$(POD_NAMESPACE)/nginx-config
      - -default-server-tls-secret=$(POD_NAMESPACE)/default-server-secret
      - -enable-app-protect                            # App Protect WAFを有効にします
      - -enable-app-protect-dos                        # App Protect DoSを利用する場合、有効にします
      - -enable-oidc                                   # (追加) OIDCに必要となるArgsを有効にします
      - -enable-snippets                               # (追加) OIDCで一部設定を追加するためsnippetsを有効にします
     #- -v=3 # Enables extensive logging. Useful for troubleshooting.
     #- -report-ingress-status
     #- -external-service=nginx-ingress
     #- -enable-prometheus-metrics
     #- -enable-service-insight
     #- -global-configuration=$(POD_NAMESPACE)/nginx-configuration

修正したマニフェストを指定しPodを作成します。

## cd ~/kubernetes-ingress/deployments
kubectl apply -f deployment/nginx-plus-ingress.yaml
実行結果サンプル
1
deployment.apps/nginx-ingress created
kubectl get pods --namespace=nginx-ingress | grep nginx-ingress
実行結果サンプル
1
nginx-ingress-7f67968b56-d8gf5       1/1     Running   0          3s
kubectl get deployment -n nginx-ingress | grep nginx-ingress
実行結果サンプル
1
nginx-ingress   1/1     1            1           2m52s

6. NGINX Ingress Controller を外部へ NodePort で公開する

本ラボの環境ではKubernetesへのアクセスを受けるため、NGINX Ingress Controllerを外部へNodePortで公開します。 以下コマンドで設定の内容を確認します。type NodePortでHTTP、HTTPSで待ち受ける設定であることを確認します。

service/nodeport.yaml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
apiVersion: v1
kind: Service
metadata:
  name: nginx-ingress
  namespace: nginx-ingress
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: http
  - port: 443
    targetPort: 443
    protocol: TCP
    name: https
  selector:
    app: nginx-ingress

NodePortをデプロイします。

## cd ~/kubernetes-ingress/deployments
kubectl apply -f service/nodeport.yaml
実行結果サンプル
1
service/nginx-ingress created
kubectl get svc -n nginx-ingress | grep nginx-ingress
実行結果サンプル
1
nginx-ingress   NodePort   10.108.250.160   <none>        80:32692/TCP,443:31957/TCP   5s

このコマンドを実行した結果、Kubernetes の Worker Nodeでそれぞれのサービスに対しポートが割り当てられています。 図の内容を確認してください。

../../_images/kube_nodeport.jpg
クライアントからアクセスするため、HTTP(TCP/80)、HTTPS(TCP/443)を待ち受け、それぞれNodePortで公開するポート番号へ転送するLBを用意します。
今回のラボ環境では同Linux Host上にNGINX Plusをインストールし以下nginx.confとしました。NGINX OSSでも同様の設定で問題ありません

Note

NGINX Plusをインストールする場合、こちらの手順「 NGINX Plusのインストール (15min)」を参考に、NGINX Plusをインストールしてください。

先程確認したNodePortで割り当てられたポート番号宛に通信を転送するように、NGINXを設定します。

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:32692;    # HTTP(TCP/80)に割り当てられたポート番号
   }
   upstream tcp443_backend {
      server node1:31957;     # 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

現在の状態は以下となり、サービスを外部に公開する準備が完了しました。

../../_images/kube_external_nginx.jpg