IT 铁人赛 k8s 入门30天 day26 k8s Task Set up Ingress on Minikube with the NGINX Ingress Controller

前言

参考文件: https://kubernetes.io/docs/tasks/access-application-cluster/ingress-minikube/
今天要来实作 Set up Ingress on Minikube with the NGINX Ingress Controller 这个任务

Ingress 是一个用来定义路由规则的 k8s 元件

Ingress controller 则是实作这个路由的实体

这个任务将会使用 nginx 当作 Ingress Controller

透过设定路由规则, 让 client 端透过 HTTP URI 存取到对应的应用

布署目标

1 启动 minikube 的 Ingress Controller 功能

2 布署一个 hello world 的应用

3 建立 Ingress 路由, 并且测试路由

4 建立第二个应用

5 更改路由导入第二个应用

启动 minikube 的 Ingress Controller 功能

使用以下指令开启 minikube 的 ingres controller 功能

minikube addons enable ingress

使用以下指令查看 enable 之後的结果

kubectl get pods -n ingress-nginx

布署一个 hello world 的应用

使用以下指令布署一个 hello world app

kubectl create deployment web --image=gcr.io/google-samples/hello-app:1.0

使用 NodePort 的方式让使用者可以透过 clusterIP 存取到 hello world 应用

执行以下指令

kubectl expose deployment web --type=NodePort --port=8080

使用以下指令检查 NodePort 设定

kubectl get service web

使用以下指令让 minikube 给予 service 一个对外 IP

minikube service web --url

透过上面的 url 从浏览器存取 hello world 应用

建立 Ingress 路由, 并且测试路由

建立 example-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

上面设定内容如下

建立一个 Ingress

名称设定为 example-ingress

设定路由规则如下

domain name 是 hello-world.info

对应到的 service 名称是 web

对应到的 service port 是 8080

使用的 matchPath 为 Prefix

建立指令如下

kubectl apply -f example-ingress.yaml

检查 ingress 设定使用以下指令

kubectl get ingress

设定 domain resolve 到 /etc/hosts 新增一行如下

192.168.49.2 hello-world.info

要注意的是, 这边的 IP 是根据 kubectl get ingress 查到的那个 IP, 每个实作各有不同

验证刚刚的设定透过以下指令

curl hello-world.info

建立第二个应用

使用以下指令布署第2个 hello world app

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

使用 NodePort 的方式让使用者可以透过 clusterIP 存取到第2个 hello world 应用

执行以下指令

kubectl expose deployment web2 --type=NodePort --port=8080

更改路由导入第二个应用

新增以下项目到 example-ingress.yaml

- path: /v2
  pathType: Prefix
  backend:
    service:
      name: web2
      port:
        number: 8080

更新之後的 example-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

设定说明如下:

新增路由 /v2

对应到 service 名称 web2

对应到 service Port 8080

使用以下指令更新 Ingress 路由

kubectl apply -f example-ingress.yaml

测试设定的 Ingress 路由

测试 hello-world.info

curl hello-world.info

测试 hello-world.info/v2

curl hello-world.info/v2

清除 Ingress 布署

kubectl delete -f example-ingress.yaml

後记

部署每个资源之前都必须理解布署目标的架构

否则就会迷失在该怎麽处理设定

做个图像化的架构图是个不错的方式


<<:  Microkernel

>>:  误打误撞跳到网路组

Valid ServiceNow CIS-PPM Dumps - Best Exam Preparation Material

Start Exam Preparation with Real ServiceNow CIS-PP...

Day27 - 使用 LIFF 表单收集用户讯息

LINE Developers:https://developers.line.biz/zh-ha...

关於封装

什麽是封装 In object-oriented programming (OOP), encaps...

Day27 - 登出及连线中断

今天来做登出的功能以及连线中断的处理。 Navigation Action 不论是登出还是中断连线,...

SuiteScript - Entry Point

Schedule Script execute Map/Reduce Script getInput...