NIUCLOUD是一款SaaS管理后台框架多应用插件+云编译。上千名开发者、服务商正在积极拥抱开发者生态。欢迎开发者们免费入驻。一起助力发展! 广告
[TOC] # Ingress-nginx 使用示例 ### 使用 HelloWolrd 镜像测试 ``` apiVersion: apps/v1 kind: Deployment metadata: name: hello-world-1 labels: app: hello-world-1 spec: replicas: 1 selector: matchLabels: app: hello-world-1 template: metadata: labels: app: hello-world-1 spec: containers: - name: hello-world-1 image: nginx ports: - containerPort: 80 --- apiVersion: apps/v1 kind: Deployment metadata: name: hello-world-2 labels: app: hello-world-2 spec: replicas: 1 selector: matchLabels: app: hello-world-2 template: metadata: labels: app: hello-world-2 spec: containers: - name: hello-world-2 image: nginx ports: - containerPort: 80 ``` ### 编写 SVC 的 YAML ``` apiVersion: v1 kind: Service metadata: name: hello-world-1-svc labels: spec: ports: - port: 80 targetPort: 80 protocol: TCP name: http selector: app: hello-world-1 --- apiVersion: v1 kind: Service metadata: name: hello-world-2-svc labels: spec: ports: - port: 80 targetPort: 80 protocol: TCP name: http selector: app: hello-world-1 ``` ### 分别向两个 Pod 写入不同的页面 ![](https://img.kancloud.cn/db/f5/dbf5a948dd15e23275cd670b7fff2737_673x276.png) ### 编写 Ingress > 每个版本的编写方式可能会有变动 > 所以实际编写的时候要查看版本当时的写法 ``` apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: test-ingress spec: ingressClassName: nginx # 这个 ingressClassName 通过 kubectl get ingressclasses 来查看需要指定的名称 rules: - host: "hello-world-1.com" http: paths: - path: / pathType: Prefix backend: service: name: hello-world-1-svc port: number: 80 - host: "hello-world-2.com" http: paths: - path: / pathType: Prefix backend: service: name: hello-world-2-svc port: number: 80 ``` ### 验证访问 ##### 访问地址写入 hosts 中 ![](https://img.kancloud.cn/60/2b/602b53af1a4b99b4bdbf61d0a5a16424_349x44.png) ##### 验证 ![](https://img.kancloud.cn/13/eb/13eb2055658ba0c15b97a38b4a7cb910_380x93.png) ![](https://img.kancloud.cn/71/a2/71a221af711a5cc82aa1bfdf2236824a_445x114.png)