Ingress によるサービス公開! nginx-ingress-controller を試してみる。

こんにちは k-jun です。今回は nginx-ingress-controller を試してみます。

https://github.com/kubernetes/ingress-nginx

そもそも ingress とはなんぞやという話ですが、k8s における 外部からのアクセスを内部の適切な Pod へ ルーティングする LB のようなものです。

https://kubernetes.io/docs/concepts/services-networking/ingress/

Ingress exposes HTTP and HTTPS routes from outside the cluster to services within the cluster. Traffic routing is controlled by rules defined on the Ingress resource.

ここらへんの リソースは自分が前触った際には クラウドサービスの LB をプラグイン連携して使用するみたいになっていた気がしますが、いつの間にか nginx を使用できるようになっていたみたいですね。

Setup

ここ を参考にセットアップを進めていきます。

アプリケーションをデプロイしていきます。使用するイメージはサンプルアプリケーションのもの。

$ kubectl create deployment web --image=gcr.io/google-samples/hello-app:1.0
deployment.apps/web created
$ kubectl expose deployment web --type=NodePort --port=8080
service/web exposed
$ kubectl get service web
NAME   TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
web    NodePort   10.97.252.61   <none>        8080:31074/TCP   35s

Run

ということで、本題の Ingress を作っていきます。

$ kubectl apply -f https://k8s.io/examples/service/networking/example-ingress.yaml

中身で叩いている yaml はこんな感じ。

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: hello-world.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web
                port:
                  number: 8080

ingress が起動している様子もコマンドから確認できます。

$ kubectl get ingress
NAME              CLASS    HOSTS              ADDRESS        PORTS   AGE
example-ingress   <none>   hello-world.info   35.190.25.54   80      8m58s

External IP でアクセスしてみるとこんな感じ。

$ curl -I http://35.190.25.54 -H 'Host: hello-world.info'
HTTP/1.1 200 OK
date: Sat, 04 Dec 2021 13:35:51 GMT
content-length: 60
content-type: text/plain; charset=utf-8
x-envoy-upstream-service-time: 0
server: istio-envoy
x-envoy-decorator-operation: web.default.svc.cluster.local:8080/*
Via: 1.1 google
$ curl -I http://35.190.25.54 -H 'Host: hello-world.dummy'
HTTP/1.1 404 Not Found
Date: Sat, 04 Dec 2021 13:37:48 GMT
Content-Length: 74
Content-Type: text/plain; charset=utf-8
Via: 1.1 google

HostName の制限もしっかりと生きている様子。外部 IP が簡単に付与できて感激です。数年前はここまで簡単にできなかった記憶。

$ kubectl create deployment web2 --image=gcr.io/google-samples/hello-app:2.0
deployment.apps/web2 created

path ごとの振り分けを試すべく、2つ目のサンプルアプリケーションを作成。

$ kubectl expose deployment web2 --port=8080 --type=NodePort
$ kubectl expose deployment web2 --port=8080 --type=NodePort
$ curl -s https://kubernetes.io/examples/service/networking/example-ingress.yaml > ./ingress.yaml
$ vim ./ingress.yaml
$ cat ./ingress.yaml
$ cat ./ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: hello-world.info
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web
                port:
                  number: 8080
          - path: /v2
            pathType: Prefix
            backend:
              service:
                name: web2
                port:
                  number: 8080
$ curl http://35.190.25.54/v2 -H 'Host: hello-world.info'
Hello, world!
Version: 2.0.0
Hostname: web2-5d47994f45-nj67t

いい感じに、version 2 の方にアクセスが飛んでいますね。 ということで、nginx-ingress-controller を使用することで、いい感じにサービスを外部に公開できることがわかりました。 それでは今回はこのへんで。