自宅の kubernetes に ingress-nginx を入れてみる
自宅の kubernetes 環境が何かと混雑してきたので、なんかそのあたいうまいことできそうな Ingress とやらに取り掛かってみます
もちろん Xi IoT 上への Ingress 導入も視野に入れて
こんなことができるんじゃないの?の図
(kubernetesノードは一旦忘れる)
Ingressコントローラにも何個か種類があるようですが、一番有名そうで Xi IoT にも対応している nginx版にチャレンジしてみます
Ingress nginx の導入方法ググってみると、kubernetesのサイトと、NGINXのサイトそれぞれがあるようです
ingress-nginx か nginx-ingress か、できることも若干違う模様
kubernetes版 で Ingres Controller をインストールしてみる
NGINX 自体は使う予定はないので、Kubernetesさん側のサイトを参照してみる
Installation Guide - NGINX Ingress Controller
どっちかでも良いようなぁキガスル
・Docker for Mac
・Bare-metal
とりあえず、Docker for Mac のソースをダウンロードして
ingress-nginx/deploy.yaml at master · kubernetes/ingress-nginx · GitHub
External IP を追記してやります
# Source: ingress-nginx/templates/controller-service.yaml apiVersion: v1 kind: Service metadata: labels: helm.sh/chart: ingress-nginx-2.11.0 app.kubernetes.io/name: ingress-nginx app.kubernetes.io/instance: ingress-nginx app.kubernetes.io/version: 0.34.0 app.kubernetes.io/managed-by: Helm app.kubernetes.io/component: controller name: ingress-nginx-controller namespace: ingress-nginx spec: type: LoadBalancer externalTrafficPolicy: Local ports: - name: http port: 80 protocol: TCP targetPort: http - name: https port: 443 protocol: TCP targetPort: https selector: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/instance: ingress-nginx app.kubernetes.io/component: controller # ↓ココニ追記↓ externalIPs: - 192.168.199.3 # ↑ココニ追記↑
あとはただ、デプロイしてみるだけ
> kubectl apply -f .\ingress-nginx_mac.yaml namespace/ingress-nginx created serviceaccount/ingress-nginx created configmap/ingress-nginx-controller created clusterrole.rbac.authorization.k8s.io/ingress-nginx created clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx created role.rbac.authorization.k8s.io/ingress-nginx created rolebinding.rbac.authorization.k8s.io/ingress-nginx created service/ingress-nginx-controller-admission created service/ingress-nginx-controller created deployment.apps/ingress-nginx-controller created validatingwebhookconfiguration.admissionregistration.k8s.io/ingress-nginx-admission configured clusterrole.rbac.authorization.k8s.io/ingress-nginx-admission unchanged clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx-admission unchanged rolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created serviceaccount/ingress-nginx-admission created
ingress-nginx という namespace の中に作られています
デプロイ後の様子 "ingress-nginx-admission" と "ingress-nginx-admission-patch" はイマイチ使い方よく分かっていない
> kubectl get all NAME READY STATUS RESTARTS AGE pod/ingress-nginx-admission-create-vl8tj 0/1 Completed 0 62s pod/ingress-nginx-admission-patch-t2f7l 0/1 Completed 1 62s pod/ingress-nginx-controller-68679c6884-xdm7x 1/1 Running 0 72s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/ingress-nginx-controller LoadBalancer 10.106.111.205 localhost,192.168.199.3 80:31722/TCP,443:32507/TCP 72s service/ingress-nginx-controller-admission ClusterIP 10.106.195.147 <none> 443/TCP 72s NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/ingress-nginx-controller 1/1 1 1 72s NAME DESIRED CURRENT READY AGE replicaset.apps/ingress-nginx-controller-68679c6884 1 1 1 72s NAME COMPLETIONS DURATION AGE job.batch/ingress-nginx-admission-create 1/1 1s 72s job.batch/ingress-nginx-admission-patch 1/1 2s 72s
Webサーバで簡易テストしてみる
簡単な Webサーバを2つ作ります
apiVersion: v1 kind: Service metadata: name: hello-world-svc1 namespace: ingress-nginx spec: ports: - port: 8081 protocol: TCP targetPort: 80 selector: app: hello-world1 --- apiVersion: apps/v1 kind: Deployment metadata: name: hello-world-deployment1 namespace: ingress-nginx spec: replicas: 1 selector: matchLabels: app: hello-world1 template: metadata: labels: app: hello-world1 spec: containers: - image: "strm/helloworld-http" imagePullPolicy: Always name: hello-world-container ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: hello-world-svc2 namespace: ingress-nginx spec: ports: - port: 8082 protocol: TCP targetPort: 80 selector: app: hello-world2 --- apiVersion: apps/v1 kind: Deployment metadata: name: hello-world-deployment2 namespace: ingress-nginx spec: replicas: 1 selector: matchLabels: app: hello-world2 template: metadata: labels: app: hello-world2 spec: containers: - image: "strm/helloworld-http" imagePullPolicy: Always name: hello-world-container ports: - containerPort: 80
「HTTP Hello World」って表示されるだけの軽いやつです
これに外からアクセスできるように ingress の設定をします
”http://192.168.199.3/hello1” と ”http://192.168.199.3/hello2” でアクセスする先を変えるんだ
apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: ingress-nginx namespace: ingress-nginx annotations: nginx.ingress.kubernetes.io/rewrite-target: / spec: rules: - http: paths: - path: /hello1 backend: serviceName: hello-world-svc1 servicePort: 8081 - path: /hello2 backend: serviceName: hello-world-svc2 servicePort: 8082
これで外部からアクセスできるようになったっぽいですね
http://192.168.199.3 → アクセスできず
http://192.168.199.3/hello1 → hello-world-svc1
http://192.168.199.3/hello2 → hello-world-svc2
Xi IoT での使いどころも考えてみよう