Traffine I/O

日本語

2022-06-19

KubernetesのIngress

KubernetesのIngressとは

Kubernetes Ingressは、Kubernetesクラスタ内のServiceへの外部アクセスを管理するAPIオブジェクトです。Ingressは、ポッドがクラスタの外部からアクセスできるようにするためのLayer 7ロードバランサーの役割を果たします。URL内のホストとパスに基づいて外部トラフィックをルーティングします。

通常、Ingressを処理するためのIngressコントローラーがあり、多くの場合、ロードバランサーとともに使用されます。Ingressコントローラーは、IngressリソースのためにKubernetes APIを監視し、ロードバランサーを適切に更新します。

Ingressを通じてPodを公開する

Ingressリソースを使用して、単純なhelloworldポッドを外部トラフィックに公開する方法を紹介します。ローカルのKubernetesクラスタを作成するために、Minikubeを使用します。

Minikubeの起動

以下のコマンドを使用してMinikubeを起動します。

bash
$ minikube start

Ingressアドオンの追加

Minikubeはアドオンをサポートしています。アドオンとは、デフォルトで有効になっている機能や統合のセットです。Ingressの機能については、Ingressアドオンを有効にする必要があります。

bash
$ minikube addons enable ingress

Ingressコントローラーが実行されているかどうかは、ingress-nginxネームスペースのポッドの中から探すことで確認できます。

bash
$ kubectl get pods -n ingress-nginx

NAME                                        READY   STATUS      RESTARTS   AGE
helloworld                                  1/1     Running     0          12m
ingress-nginx-admission-create-7jxrq        0/1     Completed   0          30m
ingress-nginx-admission-patch-w7m8w         0/1     Completed   1          30m
ingress-nginx-controller-6cc5ccb977-qqbl6   1/1     Running     0          30m

Pod、NodePort、Ingressの作成

ingress-nginxネームスペースにhelloworldポッドとNodePortを作成します。

bash
$ kubectl run --image=gcr.io/google-samples/hello-app:1.0 --port=8080 --restart=Never helloworld --namespace ingress-nginx
$ kubectl expose pod helloworld --type NodePort --port 8080 --name helloworld-nodeport --namespace ingress-nginx

次に、/の全てのパスでhelloworld-nodeport Serviceを公開するIngressリソースを作成します。

ingress.yamlファイルを以下のように作成します。

ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: helloworld
  namespace: ingress-nginx
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: helloworld-nodeport
                port:
                  number: 8080

ingress.yamlファイルを適用します。

bash
$ kubectl apply -f ingress.yaml

Ingressリソースの一覧表示

以下のコマンドを使用して、全てのIngressリソースを確認します。

bash
$ kubectl get ingress

NAME         CLASS   HOSTS   ADDRESS        PORTS   AGE
helloworld   nginx   *       192.168.49.2   80      45s

IngressコントローラーのIPの取得

次のコマンドを実行して、Ingressコントローラーに割り当てられたIPを取得できます。

bash
$ kubectl get ingress | awk '{ print $4 }' | tail -1

192.168.49.2

このIPを使用してServiceにアクセスします。

Ingressを介したhelloworld-nodeportへのアクセス

Ingressを介してhelloworld-nodeport Serviceにアクセスします。

bash
$ curl $(kubectl get ingress -n ingress-nginx | awk '{ print $4 }' | tail -1)

このコマンドはIngressコントローラーのIPアドレスを取得し、それに対してcurlリクエストを送信します。全てが正しく設定されていれば、helloworld-nodeport Serviceの出力が表示されるはずです。

新しいIngressリソースの作成

また、Ingressを使用してServiceの別のバージョンであるhelloworld_v2を作成し、別のパスを介して公開することもできます。

まず、helloworld_v2のための新しいPodを作成し、NodePortとして公開し、それに対してIngressリソースを作成します。

ingress_path.yamlというファイルを以下のように作成します。

ingress_path.yaml
kind: Ingress
metadata:
  name: helloworld-v2
  namespace: ingress-nginx
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - http:
        paths:
          - path: /helloworld-v2
            pathType: Prefix
            backend:
              service:
                name: helloworld-v2-nodeport
                port:
                  number: 8080

次のコマンドを実行して、Pod、Service、Ingressリソースを作成します。

bash
# Create the helloworld-v2 pod
$ kubectl run --image=gcr.io/google-samples/hello-app:2.0 --port=8080 --restart=Never helloworld-v2 --namespace ingress-nginx

# Expose the pod as a service
$ kubectl expose pod helloworld-v2 --type=NodePort --port=8080 --name=helloworld-v2-nodeport --namespace ingress-nginx

# Create the Ingress resource
$ kubectl apply -f ingress_path.yaml

クラスタ内からhelloworld-v2 Podへのアクセスのテスト

クラスタ内からhelloworld-v2 Podへのアクセスをテストするために、一時的なcurlがインストールされたPodを作成します。

bash
$ kubectl run --namespace ingress-nginx --restart=Never --image=curlimages/curl:7.68.0 -it --rm curl sh

次に、curlを使用してhelloworld-v2 Podにリクエストを送信します。

bash
/ $ curl 10.244.0.8:8080 # 10.244.0.8 is obtained from `kubectl get pod,svc -o wide -n ingress-nginx`

Hello, world!
Version: 2.0.0
Hostname: helloworld-v2

クラスタ内からhelloworld-v2-nodeportへのアクセスのテスト

同じ一時的なPodから、helloworld-v2-nodeport Serviceへのアクセスをテストします。

bash
/ $ curl helloworld-v2-nodeport:8080

Hello, world!
Version: 2.0.0
Hostname: helloworld-v2

Ingressパス/helloworld_v2を介したhelloworld-v2-nodeport Serviceへのアクセス

最後に、Ingressパス/helloworld_v2を介して外部からhelloworld-v2-nodeport Serviceにアクセスします。

bash
$ curl $(kubectl get ingress helloworld-v2 -n ingress-nginx | awk '{ print $4 }' | tail -1)/helloworld-v2

Hello, world!
Version: 2.0.0
Hostname: helloworld-v2

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!