泉州企业网站制作定制,可以做外链的网站,织梦做的网站有哪些,杭州品牌网站建设文章目录 环境准备什么是 Ingress认识 Ingress 资源Ingress 控制器(controller)Ingress 规则pathType 路径类型多重匹配Ingress 类TLS生成证书创建密钥 环境准备
下面的 yaml 文件内容#xff0c;是使用 sts 创建两个 web 服务#xff0c;并配置对应的 servcie。web 服务的首… 文章目录 环境准备什么是 Ingress认识 Ingress 资源Ingress 控制器(controller)Ingress 规则pathType 路径类型多重匹配Ingress 类TLS生成证书创建密钥 环境准备
下面的 yaml 文件内容是使用 sts 创建两个 web 服务并配置对应的 servcie。web 服务的首页内容使用 configmap 配置并挂载到各自的 POD 中。
apiVersion: v1
kind: Namespace
metadata:name: shark-test
---
apiVersion: v1
kind: ConfigMap
metadata:namespace: shark-testname: index.html
data:# 类属性键每一个键都映射到一个简单的值web1.index.html: |web1 站点web2.index.html: |web2 站点
---
apiVersion: apps/v1
kind: StatefulSet
metadata:namespace: shark-testname: web1
spec:selector:matchLabels:app: web1 # 必须匹配 .spec.template.metadata.labelsserviceName: web1replicas: 1minReadySeconds: 10 # 默认值是 0template:metadata:labels:app: web1 # 必须匹配 .spec.selector.matchLabelsspec:terminationGracePeriodSeconds: 10containers:- name: web1image: nginx:1.21.6-alpineports:- containerPort: 80name: web1volumeMounts:- name: index-htmlmountPath: /usr/share/nginx/htmlvolumes:- name: index-html # 给 volumeMounts[].name使用configMap:name: index.html # configmap 的名称items:- key: web1.index.html # configmap 对象中 data 中的一个 keypath: index.html # 挂载到 pod 后被创建的文件名称
---
apiVersion: apps/v1
kind: StatefulSet
metadata:namespace: shark-testname: web2
spec:selector:matchLabels:app: web2 # 必须匹配 .spec.template.metadata.labelsserviceName: web2replicas: 1minReadySeconds: 10 # 默认值是 0template:metadata:labels:app: web2 # 必须匹配 .spec.selector.matchLabelsspec:terminationGracePeriodSeconds: 10containers:- name: web2image: nginx:1.21.6-alpineports:- containerPort: 80name: web2volumeMounts:- name: index-htmlmountPath: /usr/share/nginx/htmlvolumes:- name: index-html # 给 volumeMounts[].name使用configMap:name: index.html # configmap 的名称items:- key: web2.index.html # configmap 对象中 data 中的一个 keypath: index.html # 挂载到 pod 后被创建的文件名称
---
apiVersion: v1
kind: Service
metadata:namespace: shark-testname: web1labels:app: web1
spec:type: ClusterIPports:- port: 8080name: web1-httptargetPort: 80clusterIP: Noneselector:app: web1
---
apiVersion: v1
kind: Service
metadata:namespace: shark-testname: web2labels:app: web2
spec:type: ClusterIPports:- port: 8080name: web2-httptargetPort: 80clusterIP: Noneselector:app: web2
什么是 Ingress
Ingress 是管理集群外部访问集群内部服务的流量的 API 对象是 kubernetes 中对 service 的反向代理。 流量的去向由 Ingress 资源所定义的规则来控制。 可提供负载均衡、SSL 和基于名称的虚拟主机功能。
主要的访问方式是 HTTP/HTTPS不支持四层协议。下一代替代 Ingress 的产品 Gateway API 可以实现 四层和七层协议流量管理。 Ingress 在 kubernentes v1.28 版本停止更新并推出了可以实现更多功能的 Gateway API。 #mermaid-svg-CmVh8Oy2rZiVi2tB {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-CmVh8Oy2rZiVi2tB .error-icon{fill:#552222;}#mermaid-svg-CmVh8Oy2rZiVi2tB .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-CmVh8Oy2rZiVi2tB .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-CmVh8Oy2rZiVi2tB .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-CmVh8Oy2rZiVi2tB .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-CmVh8Oy2rZiVi2tB .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-CmVh8Oy2rZiVi2tB .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-CmVh8Oy2rZiVi2tB .marker{fill:#333333;stroke:#333333;}#mermaid-svg-CmVh8Oy2rZiVi2tB .marker.cross{stroke:#333333;}#mermaid-svg-CmVh8Oy2rZiVi2tB svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-CmVh8Oy2rZiVi2tB .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-CmVh8Oy2rZiVi2tB .cluster-label text{fill:#333;}#mermaid-svg-CmVh8Oy2rZiVi2tB .cluster-label span{color:#333;}#mermaid-svg-CmVh8Oy2rZiVi2tB .label text,#mermaid-svg-CmVh8Oy2rZiVi2tB span{fill:#333;color:#333;}#mermaid-svg-CmVh8Oy2rZiVi2tB .node rect,#mermaid-svg-CmVh8Oy2rZiVi2tB .node circle,#mermaid-svg-CmVh8Oy2rZiVi2tB .node ellipse,#mermaid-svg-CmVh8Oy2rZiVi2tB .node polygon,#mermaid-svg-CmVh8Oy2rZiVi2tB .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-CmVh8Oy2rZiVi2tB .node .label{text-align:center;}#mermaid-svg-CmVh8Oy2rZiVi2tB .node.clickable{cursor:pointer;}#mermaid-svg-CmVh8Oy2rZiVi2tB .arrowheadPath{fill:#333333;}#mermaid-svg-CmVh8Oy2rZiVi2tB .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-CmVh8Oy2rZiVi2tB .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-CmVh8Oy2rZiVi2tB .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-CmVh8Oy2rZiVi2tB .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-CmVh8Oy2rZiVi2tB .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-CmVh8Oy2rZiVi2tB .cluster text{fill:#333;}#mermaid-svg-CmVh8Oy2rZiVi2tB .cluster span{color:#333;}#mermaid-svg-CmVh8Oy2rZiVi2tB div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-CmVh8Oy2rZiVi2tB :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}#mermaid-svg-CmVh8Oy2rZiVi2tB .plain*{fill:#ddd!important;stroke:#fff!important;stroke-width:4px!important;color:#000!important;}#mermaid-svg-CmVh8Oy2rZiVi2tB .plain span{fill:#ddd!important;stroke:#fff!important;stroke-width:4px!important;color:#000!important;}#mermaid-svg-CmVh8Oy2rZiVi2tB .k8s*{fill:#326ce5!important;stroke:#fff!important;stroke-width:4px!important;color:#fff!important;}#mermaid-svg-CmVh8Oy2rZiVi2tB .k8s span{fill:#326ce5!important;stroke:#fff!important;stroke-width:4px!important;color:#fff!important;}#mermaid-svg-CmVh8Oy2rZiVi2tB .cluster*{fill:#fff!important;stroke:#bbb!important;stroke-width:2px!important;color:#326ce5!important;}#mermaid-svg-CmVh8Oy2rZiVi2tB .cluster span{fill:#fff!important;stroke:#bbb!important;stroke-width:2px!important;color:#326ce5!important;} 集群 负载均衡器 Ingress 的路由规则 Ingress Pod Service Pod 客户端 认识 Ingress 资源
Ingress 资源的 YAML 文件可以理解为是对 Nginx 子配置文件的抽象因为它和 nginx 中关于虚拟机主机 server 配置块的功能一致 。
下面是 Nginx 子配置文件和Ingress 的 yaml 文件的对比图。
红色框就是请求中的 url黄色框就是 nginx 的 rewrite 用于实现地址url重写,这里实现的功能是去掉请求 url 中的前缀。绿色框就是 后端服务的连接信息nginx 中是 upstream 的名称kubernetes 中是 service 名称紫色框就是后端服务的监听端口。
资源名称 Ingress 对象的命名必须是合法的 DNS 子域名名称。
不能超过 253 个字符只能包含小写字母、数字以及 ‘-’ 和 ‘.’必须以字母数字开头必须以字母数字结尾 注解(annotations): Ingress 经常使用一些注解来配置一些选项以便扩展功能例如 rewrite-target 注解。 不同的 Ingress 控制器支持不同的注解。
规则(rule) 其中包含对所有入站请求进行匹配的规则列表。
Ingress 控制器(controller)
为了让 Ingress 对象有效的创建和工作需要有一个 Ingress Controller 。 Ingress Controller 的角色是用一个反向代理实现的。反向代理可以是 nginx、haproxy 等其中的一个因此 Ingress Controller 可以有很多种。
这里我用最常用的 Ingress-Nginx Controller 为例说明。
当一个 Ingress 对象被成功创建后Ingress Controller 会把 Ingress 对象中的内容转换成 nginx 的子配置文件并让 Ingress Controller 使用。
后续会详细介绍。
Ingress 规则
一个简单的 Ingress 资源示例。
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: minimal-ingressannotations:nginx.ingress.kubernetes.io/rewrite-target: /
spec:ingressClassName: nginx-examplerules:- http:paths:- path: /web1pathType: Prefixbackend:service:name: web1port:number: 8080
每个 HTTP 规则都包含以下信息
可选的 host。在此示例中未指定 host因此该规则适用于使用指定 IP 地址进行入站 HTTP 请求的情况。 如果提供了 host例如 foo.shark.com则 rules 适用于所指定的主机( foo.shark.com)。path 路径列表例如 /web1。每个路径都有一个由 service.name 和 service.port.name 或 service.port.number 确定的关联后端。入站请求的内容都必须与 host和 path 的值相匹配负载均衡器才会将流量引导到所引用的 Servicebackend后端是 Service 中所定义的 名称和端口的组合 或者是通过 CRD 方式来实现的自定义资源后端。
通常会在 Ingress 控制器中配置 defaultBackend默认后端 以便为无法与规约中任何路径匹配的所有请求提供服务,也就是通常会返回 404 页面。
pathType 路径类型
Ingress 中的每个路径都需要有对应的路径类型Path Type。未明确设置 pathType 的路径无法通过合法性检查。当前支持的路径类型有三种 ImplementationSpecific对于这种路径类型匹配方法取决于 IngressClass。 具体实现可以将其作为单独的 pathType 处理或者作与 Prefix 或 Exact 类型相同的处理。 Exact精确匹配 URL 路径且区分大小写。 Prefix基于以 / 分隔的 URL 路径前缀匹配。匹配区分大小写 并且对路径中各个元素逐个执行匹配操作。 路径元素指的是由 / 分隔符分隔的路径中的标签列表。 说明 如果 path 值的最后一个元素是请求路径中最后一个元素的子字符串则不会被视为匹配 例如/foo/bar 匹配 /foo/bar/baz, 但不匹配 /foo/barbaz。 多重匹配
有的时候一个请求会和一个 Ingress 中的多个 path 匹配这时 path 最长者优先匹配。 如果仍然有两条同等的匹配路径则精确路径类型优先于前缀路径类型。
Ingress 类
Ingress 可以由不同的控制器实现通常使用不同的配置。 每个 Ingress 应当指定一个类也就是一个对 IngressClass 资源的引用。 IngressClass 资源包含额外的配置其中包括应当实现该类的控制器名称。
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:labels:app.kubernetes.io/component: controllername: nginx-example
spec:controller: k8s.io/ingress-nginx设置默认的 Ingress 类,只需要在注解中添加: ingressclass.kubernetes.io/is-default-class: true
apiVersion: networking.k8s.io/v1
kind: IngressClass
metadata:labels:app.kubernetes.io/component: controllername: nginx-exampleannotations:ingressclass.kubernetes.io/is-default-class: true
spec:controller: k8s.io/ingress-nginxTLS
生成证书
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj “/CCN/STBJ/LBJ/Onginx/CNitheima.com”
创建密钥
可以通过设定包含 TLS 私钥和证书的Secret 来保护 Ingress。TLS Secret 的数据中必须包含键名为 tls.crt 的证书和键名为 tls.key 的私钥 才能用于 TLS 目的。例如
apiVersion: v1
kind: Secret
metadata:name: testsecret-tlsnamespace: default
data:tls.crt: base64 编码的证书tls.key: base64 编码的私钥
type: kubernetes.io/tls也可以使用下面的方法创建
kubectl create secret tls tls-secret --key tls.key --cert tls.crt注意不能针对默认规则使用 TLS因为这样做需要为所有可能的子域名签发证书。 因此tls 字段中的 hosts 的取值需要与 rules 字段中的 host 完全匹配。 apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: tls-example-ingress
spec:tls:- hosts:- https-example.foo.comsecretName: testsecret-tlsrules:- host: https-example.foo.comhttp:paths:- path: /pathType: Prefixbackend:service:name: service1port:number: 80