KubernetesNotes10:Ingress域名访问

KubernetesNotes10:Ingress域名访问

Created
Nov 22, 2021 08:47 AM
Last Edited
Last updated December 3, 2021
Tags

1. 配置文件

1.1 测试环境

应用如下yaml,准备好测试环境
创建文件ingress_domain.yaml
apiVersion: apps/v1 kind: Deployment metadata: labels: app: hello-server name: hello-server spec: replicas: 2 selector: matchLabels: app: hello-server template: metadata: labels: app: hello-server spec: containers: - name: hello-server image: tomcat ports: - containerPort: 8080 --- apiVersion: apps/v1 kind: Deployment metadata: labels: app: nginx-demo name: nginx-demo spec: replicas: 2 selector: matchLabels: app: nginx-demo template: metadata: labels: app: nginx-demo spec: containers: - image: nginx name: nginx --- apiVersion: v1 kind: Service metadata: labels: app: nginx-demo name: nginx-demo spec: selector: app: nginx-demo ports: - port: 8000 protocol: TCP targetPort: 80 --- apiVersion: v1 kind: Service metadata: labels: app: hello-server name: hello-server spec: selector: app: hello-server ports: - port: 8000 protocol: TCP targetPort: 8080

2. 运行

2.1 生成Pod

# 运行yaml文件 kubectl apply -f ingress_domain.yaml

2.1 查看Service服务

root@docker01:/home# kubectl get svc
notion image
# 1. 查看tomcat镜像生成的hello-server root@docker01:/home# curl 10.96.196.37:8000 <!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/10.0.13</h3></body></html>root@docker01:/home# # 查看nginx生成的nginx-demo root@docker01:/home# curl 10.96.116.66:8000 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> root@docker01:/home#
 

3. 按域名访问

3.1 Ingress规则配置文件

创建ingress-rule.yaml
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: ingress-host-bar spec: ingressClassName: nginx rules: - host: "hello.henggao.com" http: paths: - pathType: Prefix path: "/" backend: service: name: hello-server port: number: 8000 - host: "demo.henggao.com" http: paths: - pathType: Prefix path: "/" backend: service: name: nginx-demo port: number: 8000

3.2 运行规则

# 1. 创建ingress规则 root@docker01:/home# kubectl apply -f ingress-rule.yaml ingress.networking.k8s.io/ingress-host-bar created # 2. 查看ingress规则 root@docker01:/home# kubectl get ingress NAME CLASS HOSTS ADDRESS PORTS AGE ingress-host-bar nginx hello.henggao.com,demo.henggao.com 192.168.92.140 80 30s root@docker01:/home#
 

3.3 使用域名访问

这里模拟域名,需要修改windows域名和端口映射,参考下面内容4
  1. 查看到端口号
root@docker01:/home# kubectl get svc -A
notion image
  1. 浏览器访问
http://demo.henggao.com:30763/ http://hello.henggao.com:30763/
notion image
notion image

3.5 修改规则

 
kubectl edit ing ingress-host-bar
修改路径
notion image
notion image
notion image
访问:http://hello.henggao.com:30763/nginx ,才可以成功访问到
notion image
 
Ingress本身就是一个Nginx服务,Ingress网关层
未来部署服务比如后台请求api路径,需要路径重写,参照官方文档:
 

3.6 路径重写

notion image
  • Ingress规则配置文件ingress-rule.yaml 添加两个地方即可
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: ingress-host-bar annotations: nginx.ingress.kubernetes.io/rewrite-target: /$2 spec: ingressClassName: nginx rules: - host: "hello.henggao.com" http: paths: - pathType: Prefix path: "/" backend: service: name: hello-server port: number: 8000 - host: "demo.henggao.com" http: paths: - pathType: Prefix path: "/nginx(/|$)(.*)" backend: service: name: nginx-demo port: number: 8000
修改文件,保存,运行配置文件
# 1. 修改文件 root@docker01:/home# vim ingress-rule.yaml # 2. 运行配置文件 root@docker01:/home# kubectl apply -f ingress-rule.yaml ingress.networking.k8s.io/ingress-host-bar configured root@docker01:/home#
notion image
 

4. 修改window本地hosts文件修改域名指向

  • 我这里模拟域名,修改windows域名和端口映射
文件目录: C:\Windows\System32\drivers\etc 的hosts文件
# C:\Windows\System32\drivers\etc\hosts 192.168.92.138 hello.henggao.com 192.168.92.138 demo.henggao.com
notion image