当前位置: 首页 > news >正文

k8s 使用ingress-nginx访问集群内部应用

k8s搭建和部署应用完成后,可以通过NodePort,Loadbalancer,Ingress方式将应用端口暴露到集群外部,提供外部访问。
缺点:
NodePort占用端口,大量暴露端口非常不安全,并且有端口数量限制【不推荐】;
Loadbalancer 非常好用,但是得加钱才行【视情况定】
Ingress 配置稍微复杂,并且需要内部DNS解析转发【推荐】

Ingress-nginx-controller 安装部署见k8s安装ingress-nginx DaemonSet + HostNetwork + nodeSelector
因为Ingress-nginx-controller是部署在k8s集群内部以pod方式运行,是可以访问到集群内部k8s的各个服务的,而只需要将Ingress-nginx-controller监听的80/443端口提供暴露给外部访问,并安装一定规则进行代理转发,即可实现外部通过Ingress-nginx-controller访问到内部应用中。

简易理解ingress 同 svc,pod关系,ingress --转发–> svc --转发–> pods
这里简易部署nginx、tomcat为例:
vi nginx-tomcat-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploymentnamespace: default
spec:replicas: 3selector:matchLabels:app: nginx-podtemplate:metadata:labels:app: nginx-podspec:containers:- name: nginx-containerimage: nginx:latestports:- name: nginx-portcontainerPort: 80protocol: TCP---apiVersion: apps/v1
kind: Deployment
metadata:name: tomcat-deploymentnamespace: default
spec:replicas: 2selector:matchLabels:app: tomcat-podtemplate:metadata:labels:app: tomcat-podspec:containers:- name: tomcat-containerimage: tomcat:8.5-jre10-slimports:- name: tomcat-portcontainerPort: 8080protocol: TCP

执行部署
kubectl apply -f nginx-tomcat-deployment.yaml

配置nginx和tomcat访问服务svc脚本,监听nginx-80/tomcat-8080端口nginx-tomcat-service.yaml
vi nginx-tomcat-service.yaml

apiVersion: v1
kind: Service
metadata:name: nginx-svcnamespace: default
spec:selector:app: nginx-podtype: ClusterIP     # 默认使用ClusterIP不使用NodePort方式
#  clusterIP: Noneports:- protocol: TCPport: 80targetPort: 80  # 部署的nginx端口---
apiVersion: v1
kind: Service
metadata:name: tomcat-svcnamespace: default
spec:selector:app: tomcat-podtype: ClusterIP       # 默认使用ClusterIP不使用NodePort方式
#  clusterIP: Noneports:- protocol: TCPport: 8080targetPort: 8080  # 部署的tomcat端口

执行部署
kubectl apply -f nginx-tomcat-service.yaml

编写ingress脚本,ingress目的就是访问将指定访问url或域名的配置提交给ingress-nginx-controller做反向代理转发到选择绑定的service中,实现pod应用暴露给外部访问。
nginx-tomcat-ingress.yaml由nginx代理转发到nginx/tomcat的svc端口
vi nginx-tomcat-ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: ingress-nginx-httpannotations:kubernetes.io/ingress.class: "nginx"  # 必须添加防止访问404
spec:rules:- host: ng.yunzaixin.top                # 配置转发地址http:paths:- path: /pathType: Prefix                   # 前缀匹配backend:service:name: nginx-svc                # 转发到那个svc中port:number: 80                   # 转发到svc中绑定的端口---apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: ingress-tomcat-httpannotations:kubernetes.io/ingress.class: "nginx"  # 必须添加防止访问404
spec:rules:- host: tomcat.yunzaixin.top            # 配置转发地址http:paths:- path: /pathType: Prefix                   # 前缀匹配backend:service:name: tomcat-svc               # 转发到那个svc中port:number: 8080                 # 转发到svc中绑定的端口

执行部署
kubectl apply -f nginx-tomcat-ingress.yaml

相当于访问tomcat.yunzaixin.top 或者ng.yunzaixin.top ,ingress-nginx-controller将会转发到nginx-svc或tomcat-svc,nginx-svc或tomcat-svc转发到对应得nginx或tomcat中
在这里插入图片描述

在运行ingress-nginx-controller节点主机上访问:
curl ng.yunzaixin.top
在这里插入图片描述

curl tomcat.yunzaixin.top
在这里插入图片描述

http://www.lryc.cn/news/204424.html

相关文章:

  • 企业数据泄露怎么办?
  • GoLong的学习之路(一)语法之变量与常量
  • Go-Python-Java-C-LeetCode高分解法-第十一周合集
  • 封装axios的两种方式
  • 【自然语言处理】NLTK库的概念和作用
  • Python爬虫如何解决提交参数js加密
  • 云数据库及RDS数据库介绍
  • c语言进阶部分详解(详细解析自定义类型——枚举,联合(共用体))
  • 使用 Requests 库和 PHP 的下载
  • ConcurrentHashMap底层具体实现知道吗?实现原理是什么
  • Go语言“Go语言:掌握未来编程的利器“
  • 达梦管理工具报错“结果集不可更新,请确认查询列是否出自同一张表,并且包含值唯一的列。”
  • TensorFlow2从磁盘读取图片数据集的示例(tf.keras.utils.image_dataset_from_directory)
  • Unity开发过程中的一些小知识点
  • 大语言模型(LLM)综述(一):大语言模型介绍
  • 在Ubuntu上安装和挂载NFS
  • Python 实现的简易数据库管理系统 (DBMS)
  • 1.初识MySQL
  • 【列存储学习总结】
  • 小记java正则表达式中matcher.find() 和 matcher.matches() 的区别
  • 当中国走进全球化的“深水区”,亚马逊云科技解码云时代的中国式跃升
  • 零基础Linux_21(多线程)页表详解+轻量级进程+pthread_create
  • nodejs+wasm+rust debug及性能分析
  • IP证书针对公网IP签发
  • SpringBoot-集成Minio
  • 【ML】cheatsheet
  • 【字符串】【将字符数组转为字符串】Leetcode 122 路径加密
  • 网络基础知识100问
  • 女孩子就是要打扮漂亮,让童年不留遗憾
  • 实现目录数据的上移(up)、下移(down)、置顶(top)、置底(bottom)的操作