容器相关
非root用户Docker与K8S
Containerd安装
Containerd常用命令
Docker
镜像创建
国内镜像仓库
容器创建(Dockerfile)
容器系统
docker配置
docker数据管理
docker网络管理
docker容器自启动
docker镜像加速
docker问题
搭建Portainer可视化界面
Docker Swarm
Swarm搭建Docker集群
Docker Compose
Docker Compose命令
Docker Compose模板
Docker Machine
Kubernetes常用命令
k8s部署(kubeadmin)
k8s高可用部署
MiniKube
k8s1.24部署(containerd)
k8s1.24部署(docker)
部署 Dashboard
Kuboard K8S管理台
k8s权限管理
k8s网络插件
私有仓密码镜像拉取
k8s集群管理
POD--基本单位
Pod模板
Pod生命周期
Pod健康检查
初始化容器(initContainer)
Deployment--Pod的管理
Deployment模板
Deployment升级与回滚
DaemonSet控制器
StatefulSet控制器(有状态)
JOB与CRONJOB
Service--发布服务
ingress-traefix
ingress-nginx
MetalLB
存储与配置
持久存储卷
配置存储卷
资源管理
标签、选择器与注解
资源预留
调度管理
自动扩容
Proxy API与API Server
Helm--K8S的包管理器
helm常用命令
自定义Chart
私有chart仓库
helm dashboard
K8S证书过期
K8S问题解决
Harbor安装
Harbor操作
Harbor问题
Harbor升级
Docker Registry安装
Docker Registry鉴权
Registry用Nginx代理SSL及鉴权
Docker Registry问题
Istio 服务网络
常用示例
Gateway【服务网关】
kiali 可视化页面
开启HTTPS
linkerd 服务网络
本文档使用MrDoc发布
返回首页
-
+
ingress-nginx
2021年8月12日 10:19
admin
##版本关系 ![](/media//202311/2023-11-14121619279968.png) --- ##ingress参数: https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#proxy-redirect --- ##添加域名解析: vim /etc/hosts 192.168.0.3 soulchild.cn test.soulchild.cn --- ##部署ingress-nginx-controller wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/baremetal/deploy.yaml kubectl apply -f deploy.yaml ####查看nodeport: ![](/media//202108/2021-08-12141032515920.png) --- ##创建两个测试资源 ####nginx-deploy.yaml vim nginx-deploy.yaml #内容: apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-deploy labels: app: nginx spec: replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.2 ports: - name: nginx-http containerPort: 80 protocol: TCP lifecycle: postStart: exec: command: - "/bin/bash" - "-c" - "echo ingress-test1 > /usr/share/nginx/html/index.html" --- apiVersion: v1 kind: Service metadata: name: nginx labels: app: nginx spec: selector: app: nginx ports: - name: web port: 80 targetPort: nginx-http --- ####nginx-deploy2.yaml vim nginx-deploy2.yaml #内容: apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-deploy2 labels: app: nginx2 spec: replicas: 3 template: metadata: labels: app: nginx2 spec: containers: - name: nginx2 image: nginx:1.14.2 ports: - name: nginx-http containerPort: 80 protocol: TCP lifecycle: postStart: exec: command: - "/bin/bash" - "-c" - "echo ingress-test2 > /usr/share/nginx/html/index.html" --- apiVersion: v1 kind: Service metadata: name: nginx2 labels: app: nginx2 spec: selector: app: nginx2 ports: - name: web port: 80 targetPort: nginx-http --- kubectl apply -f nginx-deploy.yaml kubectl apply -f nginx-deploy2.yaml --- ##新版ignress模板 apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: my-ingress annotations: kubernetes.io/ingress.class: nginx spec: tls: - hosts: - example.com secretName: tls-secret rules: - host: example.com http: paths: - path: / pathType: Prefix backend: service: name: my-service port: number: 80 --- ##新版ingress-负载 apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: eureka annotations: nginx.ingress.kubernetes.io/ssl-redirect: "false" kubernetes.io/ingress.class: nginx spec: ingressClassName: nginx-sij rules: - http: paths: - path: /registry pathType: Prefix backend: service: name: eureka port: number: 80 status: loadBalancer: ingress: - ip: 139.9.35.17 - ip: 172.16.20.5 --- ##创建ingress资源 ##第一种:单服务 apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: test-ingress spec: backend: serviceName: nginx servicePort: 80 ###访问:soulchild.cn:30960 ![](/media//202108/2021-08-12141347881893.png) --- ##第二种:基于uri apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: simple-fanout-example annotations: nginx.ingress.kubernetes.io/rewrite-target: / spec: rules: - host: soulchild.cn http: paths: - path: /foo backend: serviceName: nginx servicePort: nginx-http - path: /bar backend: serviceName: nginx2 servicePort: nginx-http ![](/media//202108/2021-08-12141550968195.png) --- ##第三种:基于域名 apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: name-virtual-host-ingress spec: rules: - host: soulchild.cn http: paths: - backend: serviceName: nginx servicePort: nginx-http - host: test.soulchild.cn http: paths: - backend: serviceName: nginx2 servicePort: nginx-http ####和上面的一样,当没有主机匹配时,访问df apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: name-virtual-host-ingress spec: rules: - host: soulchild.cn http: paths: - backend: serviceName: nginx servicePort: nginx-http - host: test.soulchild.cn http: paths: - backend: serviceName: nginx2 servicePort: nginx-http #以下内容为上面域名没有匹配到时,默认匹配下面内容 - http: paths: - backend: serviceName: df servicePort: df-http --- #重写 |注解名|描述| |--|--| |nginx.ingress.kubernetes.io/app-root|访问主域名的时候会自动跳转到app-root注解指定的路径 |nginx.ingress.kubernetes.io/rewrite-target|将匹配到的url重定向到rewrite-target注解指定的路径 |nginx.ingress.kubernetes.io/server-snippet|使用注解可以在server{...}配置块中添加自定义的配置 |nginx.ingress.kubernetes.io/configuration-snippet|使用该注解可以在Nginx的location{...}位置添加额外配置 ##例子: apiVersion: extensions/v1beta1 kind: Ingress metadata: name: voicenotify-ingress namespace: voicenotify annotations: nginx.ingress.kubernetes.io/ssl-redirect: "false" nginx.ingress.kubernetes.io/proxy-body-size: 500m nginx.ingress.kubernetes.io/rewrite-target: / #改写访问uri spec: rules: - http: paths: - path: /voicenotify/server/voice-notify-core backend: serviceName: voice-notify-core servicePort: 9702 --- ####服务访问:http://xxxx/voicenotify/server/voice-notify-core ####则会访问到服务的:http://voice-notify-core:9702/ --- ####如果不填 nginx.ingress.kubernetes.io/rewrite-target: / ####上述则会访问:http://voice-notify-core:9702/voicenotify/server/voice-notify-core --- #限制访问方法 nginx.ingress.kubernetes.io/cors-allow-methods: "PUT, GET, POST, OPTIONS" --- #校验Referer nginx.ingress.kubernetes.io/configuration-snippet: | if ($http_referer !~* (172.16.93.210|subdomain.example.com)) { return 403; } --- #重定向https nginx.ingress.kubernetes.io/proxy-redirect-from: http:// nginx.ingress.kubernetes.io/proxy-redirect-to: $scheme:// ![](/media//202408/2024-08-29174603448298.png) --- #参考资料 https://blog.csdn.net/jialiu111111/article/details/129709488 https://soulchild.cn/1583.html https://blog.csdn.net/Jerry00713/article/details/127525258 https://blog.csdn.net/hongxiaolu/article/details/113711612
分享到: