LoginSignup
3
5

More than 3 years have passed since last update.

Nginx Ingressで1本のLBにまとめる

Last updated at Posted at 2019-05-30

背景

Kubernetesを使ってて、ServiceやIngressでロードバランサー(LB)を作ると思いますが、LBは1つにつき、GKE、AWS共に3000円くらいかかります。(他のベンダーはどうか知らないので、知ってる方教えてください!)
本番環境で1本3000円ならいいのですが、開発環境や、モニタリング、分析ツール等もエンドポイントとして吐き出したいですよね!!
ただ、、3000円払う価値、、って感じで断念しちゃたりってことがあったかもしれません。

目標

LBを1本にまとめる!!

この記事の対象者

以下を理解していること!! 知らない方は、他の方が丁寧に説明してると思うので検索してみてください!

  • kubernetesのよく使う系のリソース

問題点

Ingressから他のnamespaceのサービスが見えない!!

作っていく全体図

詳しくはこちら!
https://github.com/ryicoh/cross-namespaced-ingress

  • デプロイするもの -> sample-web(dev, stg, prodのどれかを返すアプリ)
  • development, staging, productionのnamespaceにデプロイ
  • nginx-ingressで、/devにきたら、namespaceがdevelopmentのアプリにフォワーディング

さっそくNginx-Ingressをデプロイ

kubectl create ns nginx-ingress # 名前はご自分で!!
kubens nginx-ingress
helm install -n nginx-ingress stable/nginx-ingress

sample-webをデプロイ

git clone https://github.com/ryicoh/cross-namespaced-ingress
cd cross-namespaced-ingress

kubectl apply -f namespace/
kubectl apply -f development/ --record --prune -l=app=sample-web,stage=development
kubectl apply -f staging --record --prune -l=app=sample-web,stage=staging
kubectl apply -f production --record --prune -l=app=sample-web,stage=production

ExternalNameについて説明

大体の人は、ServiceリソースのClusterIP,NodePortしか使ってないと思います。私もそうでした。
しかし、なんと、すごい便利なExternalNameっていうTypeがありました。

apiVersion: v1
kind: Service
metadata:
  name: developmnet-sample-web-svc
  labels:
    group: nginx-ingress
  namespace: nginx-ingress
spec:
  type: ExternalName
  externalName: sample-web-svc.development.svc.cluster.local
  1. Serviseリソース全てに言えることなのですが、Podsの負荷分散の他に、 <service-name>.<namespace>.svc.cluster.local っていう感じでFQDNが登録されてて、クラスタ内からであれば、どこからでも、これで通信できます! (NetworkPolicyとかゆうやつで疎通性をなくすことも可能)
  2. IngressでServiceを指定すると思うのですが、それを sample-web-svc.development.svc.cluster.local に変換するのがExternalNameの役割です。

ってなわけで、

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: sample-web-ingress
  labels:
    group: nginx-ingress
  namespace: nginx-ingress
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: developmnet-sample-web-svc
          servicePort: 8080
        path: /dev(/|$)(.*)
      - backend:
          serviceName: staging-sample-web-svc
          servicePort: 8080
        path: /stg(/|$)(.*)
      - backend:
          serviceName: production-sample-web-svc
          servicePort: 8080
        path: /prod(/|$)(.*)

これで、/dev何かの時は、development namespaceのsample-webに通すことが可能
ちなみに、 nginx.ingress.kubernetes.io/rewrite-target: /$2 でリソースの/devの部分が消せます。
https://github.com/kubernetes/ingress-nginx/tree/master/docs/examples/rewrite
しかしこれよりも、MultiTLSがいいと思われ
https://github.com/kubernetes/ingress-nginx/tree/master/docs/examples/multi-tls

私のGithubをCloneした方はこちらでデプロイが試せる!

kubectl apply -f nginx-ingress/ --record --prune -l=group=nginx-ingress

以上です!
わからない点あればなんでも言ってください!

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5