11--kubernetes的Ingress应用

2024-09-07 19:36
文章标签 应用 kubernetes ingress

本文主要是介绍11--kubernetes的Ingress应用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言:本章主要记录ingress暴露服务方式,会详细解释其原理及两种网络模式应用实操。

1、Ingress概念详解

Kubernetes 暴露服务的方式目前只有三种:LoadBlancer Service、NodePort Service、Ingress,Service属于4层负载均衡,而本章的ingress属于7层负载均衡。

要理解 Ingress,需要区分两个概念:Ingress(类似老板角色) 和 Ingress-Controller(类似职工角色)。

Ingress 对象: 这是 Kubernetes 中的一个 API 对象/资源对象,用 YAML文件 配置。它定义了请求如何转发到 Service 的规则,可以理解为配置模板。

Ingress-Controller: 这是具体实现反向代理和负载均衡的程序。它解析 Ingress 定义的规则,并根据配置转发请求。

简而言之,Ingress-Controller 负责实际的请求转发,并通过多种方式暴露在集群入口。外部请求首先到达 Ingress-Controller,而 Ingress 对象则告知 Ingress-Controller 如何转发请求,如哪些域名和路径转发到哪些服务。

Service 在第四层传输层(TCP/IP 层)以 IP:PORT 形式存在,对于不同的 URL 地址,可能需要不同的后端服务或虚拟服务器。这种应用层的转发机制通过 Kubernetes 的 Service 机制无法实现,此时可以使用 Ingress 策略和具体的 Ingress-Controller。Ingress 提供七层负载均衡能力(当然也无法脱离4层存在),如外部可访问的 URL、负载均衡、SSL、基于名称的虚拟主机等。在集群流量接入层,Ingress 的高可用性至关重要

Kubernetes 并没有自带 Ingress Controller,实际上ingress-controller只是一个统称,具体实现有多种,需要自己单独安装,目前,由k8s维护的ingress-controller只有google云的GCE与ingress-nginx两个,常用的是 Ingress-nginx Controller(基于nginx七层反向代理来实现)。

 

Ingress 一般由三个组件组成:

  1. Nginx 反向代理负载均衡器(干活用的工具):负责实际的请求转发和负载均衡。
  2. Ingress Controller(员工):作为控制器,与 Kubernetes API 交互,实时获取后端 Service 和 Pod 的变化(如新增或删除)。它结合 Ingress 定义的规则生成配置,动态更新 Nginx 负载均衡器,并使配置生效,实现服务自动发现。
  3. Ingress(老板):定义请求转发规则。通过 YAML 文件配置,指定某个域名的请求如何转发到集群中的指定 Service。可以为一个或多个 Service 定义一个或多个 Ingress 规则。

2、Ingress部署实战

2.1、部署 Ingress-Controller(Nginx)

下载Ingress控制器:

网盘链接: https://pan.baidu.com/s/1Y3eB-4OXeoOQp1AVoaXdYw?pwd=tfdm 提取码: tfdm

github地址:https://github.com/kubernetes/ingress-nginx/archive/nginx-0.30.0.tar.gz

[root@k8s-master1 ~]# wget https://github.com/kubernetes/ingress-nginx/archive/nginx-0.30.0.tar.gz
[root@k8s-master1 ~]# tar -zxvf ingress-nginx-nginx-0.30.0.tar.gz -C /mnt/
[root@k8s-master1 ~]# cd /mnt/ingress-nginx-nginx-0.30.0/deploy/static/
[root@k8s-master1 static]# ls
configmap.yaml  mandatory.yaml  namespace.yaml  provider  rbac.yaml  with-rbac.yaml
[root@k8s-master1 static]# mkdir /root/ingress
[root@k8s-master1 static]# cp mandatory.yaml /root/ingress
[root@k8s-master1 ~]# cd /root/ingress
[root@k8s-master1 ~]# cd /root/ingress
[root@k8s-master1 ingress]# vim mandatory.yaml 
修改详情如下(修改后的文件网盘连接内有)
1、修改pod控制器为daemonset
apiVersion: apps/v1
kind: DaemonSet    #修改
metadata:name: nginx-ingress-controllernamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
spec:#replicas: 1    #注释掉selector:matchLabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx2、宿主机网络共享,注释掉node标签选择spec:# wait up to five minutes for the drain of connectionsterminationGracePeriodSeconds: 300hostNetwork: true #添加宿主机网络共享serviceAccountName: nginx-ingress-serviceaccount#nodeSelector:#kubernetes.io/os: linux    #这两行注释掉这里不注释的话需要使用
kubectl label nodes k8s-node1 kubernetes.io/os: linux=true为node节点添加标签3、修改镜像为阿里镜像(直接拉取也可以,但网络不稳定,成功拉取后记得上传到本地仓库)
#image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0
image: registry.cn-chengdu.aliyuncs.com/k8s_module_images/kubernetes-ingress-controller-nginx-ingress-controller:0.30.0
[root@k8s-master1 ingress]# kubectl apply -f mandatory.yaml
此处一般会有警告提示修改api版本,忽略的话不影响后续使用
[root@k8s-master1 ingress]# kubectl get pod -n ingress-nginx
NAME                             READY   STATUS    RESTARTS   AGE
nginx-ingress-controller-f6brt   1/1     Running   0          2m30s
nginx-ingress-controller-jbst2   1/1     Running   0          2m30s

此时Ingress-Controller部署完毕

2.2、部署web应用

这里选用http和nginx两个作为两种不同的web服务器,这里使用hostnetwork网络方式,所以部署的svc是默认的clusterip

[root@k8s-master1 ingress]# vim my-apache.yaml 
[root@k8s-master1 ingress]# cat my-apache.yaml 
---
apiVersion: apps/v1
kind: Deployment
metadata:name: my-apache
spec:selector:matchLabels:run: my-apachereplicas: 2template:metadata:labels:run: my-apachespec:containers:- name: my-apacheimage: registry.cn-chengdu.aliyuncs.com/liumuquan_app/httpd:2.4-alpine3.17ports:- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:name: my-apachelabels:run: my-apache
spec:#type: NodePortports:- port: 80targetPort: 80#nodePort: 30001selector:run: my-apache
[root@k8s-master1 ingress]# vim my-nginx.yaml
[root@k8s-master1 ingress]# cat my-nginx.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:name: my-nginx
spec:selector:matchLabels:run: my-nginxreplicas: 2template:metadata:labels:run: my-nginxspec:containers:- name: my-nginximage: registry.cn-chengdu.aliyuncs.com/liumuquan_app/nginx:1.12.0-alpineports:- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:name: my-nginxlabels:run: my-nginx
spec:#type: NodePortports:- port: 80targetPort: 80#nodePort: 30002selector:run: my-nginx

创建两个web服务

[root@k8s-master1 ingress]# kubectl apply -f my-apache.yaml 
deployment.apps/my-apache created
service/my-apache created
[root@k8s-master1 ingress]# kubectl apply -f my-nginx.yaml 
deployment.apps/my-nginx created
service/my-nginx created
[root@k8s-master1 ingress]# kubectl get pod -o wide
NAME                         READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
my-apache-764b8f49c5-g6hl6   1/1     Running   0          23s   10.244.1.60   k8s-node1   <none>           <none>
my-apache-764b8f49c5-t5ndt   1/1     Running   0          23s   10.244.2.45   k8s-node2   <none>           <none>
my-nginx-5c49645686-rm6h8    1/1     Running   0          19s   10.244.1.61   k8s-node1   <none>           <none>
my-nginx-5c49645686-z7g82    1/1     Running   0          19s   10.244.2.46   k8s-node2   <none>           <none>
[root@k8s-master1 ingress]# kubectl get endpoints
NAME                                             ENDPOINTS                       AGE
cluster.local-nfs-redis-nfs-client-provisioner   <none>                          2d23h
kubernetes                                       192.168.188.101:6443            23d
my-apache                                        10.244.1.60:80,10.244.2.45:80   34s
my-nginx                                         10.244.1.61:80,10.244.2.46:80   30s
[root@k8s-master1 ingress]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   23d
my-apache    ClusterIP   10.96.232.117   <none>        80/TCP    39s
my-nginx     ClusterIP   10.99.230.106   <none>        80/TCP    35s

web服务创建完成

2.3、配置ingress规则

1)宿主机网络模式 hostNetwork: true

[root@k8s-master1 ingress]# vim ingress-test.yaml
[root@k8s-master1 ingress]# cat ingress-test.yaml
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: test-ingressnamespace: defaultannotations:nginx.ingress.kubernetes.io/rewrite-target: /
spec:rules:  #定义转发规则- host: www.apache111.com  #指定域名方式http:paths:- path: /  #指定访问的路径pathType: Prefix  #定义路径的类型backend:   #定义转发后端的服务service:  #定义转发的servicename: my-apacheport:number: 80 #由于Ingress控制器开启了hostNetwork: true。且clusterip的配置这里必须设置80- host: www.nginx222.comhttp:paths:- path: /pathType: Prefixbackend:service:name: my-nginxport:number: 80
[root@k8s-master1 ingress]# kubectl apply -f ingress-test.yaml 
ingress.networking.k8s.io/test-ingress created
[root@k8s-master1 ingress]# kubectl get ingress
Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
NAME           CLASS    HOSTS                                ADDRESS   PORTS   AGE
test-ingress   <none>   www.apache111.com,www.nginx222.com             80      8s

配置物理机解析

windows电脑:

C:\Windows\System32\drivers\etc\hosts内添加

192.168.188.102 k8s-node1 www.apache111.com www.nginx222.com
192.168.188.103 k8s-node2 www.apache111.com www.nginx222.com

 使用物理机浏览器访问

 

2)修改网络模式为nodeport 

[root@k8s-master1 ingress]# kubectl delete -f ingress-test.yaml 
[root@k8s-master1 ingress]# kubectl delete -f my-apache.yaml 
[root@k8s-master1 ingress]# kubectl delete -f my-nginx.yaml 
[root@k8s-master1 ingress]# kubectl delete -f mandatory.yaml

ingress-controller配置如下

[root@k8s-master1 ingress]# vim ingress-controller.yaml
[root@k8s-master1 ingress]# cat ingress-controller.yaml
---
apiVersion: v1
kind: Namespace
metadata:name: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx---kind: ConfigMap
apiVersion: v1
metadata:name: nginx-configurationnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx---
kind: ConfigMap
apiVersion: v1
metadata:name: tcp-servicesnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx---
kind: ConfigMap
apiVersion: v1
metadata:name: udp-servicesnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx---
apiVersion: v1
kind: ServiceAccount
metadata:name: nginx-ingress-serviceaccountnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:name: nginx-ingress-clusterrolelabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
rules:- apiGroups:- ""resources:- configmaps- endpoints- nodes- pods- secretsverbs:- list- watch- apiGroups:- ""resources:- nodesverbs:- get- apiGroups:- ""resources:- servicesverbs:- get- list- watch- apiGroups:- ""resources:- eventsverbs:- create- patch- apiGroups:- "extensions"- "networking.k8s.io"resources:- ingressesverbs:- get- list- watch- apiGroups:- "extensions"- "networking.k8s.io"resources:- ingresses/statusverbs:- update---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:name: nginx-ingress-rolenamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
rules:- apiGroups:- ""resources:- configmaps- pods- secrets- namespacesverbs:- get- apiGroups:- ""resources:- configmapsresourceNames:# Defaults to "<election-id>-<ingress-class>"# Here: "<ingress-controller-leader>-<nginx>"# This has to be adapted if you change either parameter# when launching the nginx-ingress-controller.- "ingress-controller-leader-nginx"verbs:- get- update- apiGroups:- ""resources:- configmapsverbs:- create- apiGroups:- ""resources:- endpointsverbs:- get---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:name: nginx-ingress-role-nisa-bindingnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: nginx-ingress-role
subjects:- kind: ServiceAccountname: nginx-ingress-serviceaccountnamespace: ingress-nginx---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:name: nginx-ingress-clusterrole-nisa-bindinglabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: nginx-ingress-clusterrole
subjects:- kind: ServiceAccountname: nginx-ingress-serviceaccountnamespace: ingress-nginx---apiVersion: apps/v1
#kind: DaemonSet
kind: Deployment
metadata:name: nginx-ingress-controllernamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
spec:replicas: 2selector:matchLabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginxtemplate:metadata:labels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginxannotations:prometheus.io/port: "10254"prometheus.io/scrape: "true"spec:# wait up to five minutes for the drain of connectionsterminationGracePeriodSeconds: 300
#      hostNetwork: true   # 关闭此网络模式serviceAccountName: nginx-ingress-serviceaccount
#      nodeSelector:
#        custom/ingress-controller-ready: "true"
#        kubernetes.io/os: linuxcontainers:- name: nginx-ingress-controllerimage: registry.cn-chengdu.aliyuncs.com/k8s_module_images/kubernetes-ingress-controller-nginx-ingress-controller:0.30.0 args:- /nginx-ingress-controller- --configmap=$(POD_NAMESPACE)/nginx-configuration- --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services- --udp-services-configmap=$(POD_NAMESPACE)/udp-services- --publish-service=$(POD_NAMESPACE)/ingress-nginx- --annotations-prefix=nginx.ingress.kubernetes.iosecurityContext:allowPrivilegeEscalation: truecapabilities:drop:- ALLadd:- NET_BIND_SERVICE# www-data -> 101runAsUser: 101env:- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: POD_NAMESPACEvalueFrom:fieldRef:fieldPath: metadata.namespaceports:- name: httpcontainerPort: 80protocol: TCP- name: httpscontainerPort: 443protocol: TCPlivenessProbe:failureThreshold: 3httpGet:path: /healthzport: 10254scheme: HTTPinitialDelaySeconds: 10periodSeconds: 10successThreshold: 1timeoutSeconds: 10readinessProbe:failureThreshold: 3httpGet:path: /healthzport: 10254scheme: HTTPperiodSeconds: 10successThreshold: 1timeoutSeconds: 10lifecycle:preStop:exec:command:- /wait-shutdown---apiVersion: v1
kind: LimitRange
metadata:name: ingress-nginxnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
spec:limits:- min:memory: 90Micpu: 100mtype: Container

创建ingress-controller

[root@k8s-master1 ingress]# kubectl apply -f ingress-controller.yaml
[root@k8s-master1 ingress]# kubectl get pod -n ingress-nginx
NAME                                        READY   STATUS    RESTARTS   AGE
nginx-ingress-controller-7b5d6f74c7-sxm7l   1/1     Running   0          85s
nginx-ingress-controller-7b5d6f74c7-tqpgl   1/1     Running   0          85s

修改web-pod文件svc为nodeport方式

[root@k8s-master1 ingress]# vim my-apache.yaml 
[root@k8s-master1 ingress]# cat my-apache.yaml 
---
apiVersion: apps/v1
kind: Deployment
metadata:name: my-apache
spec:selector:matchLabels:run: my-apachereplicas: 2template:metadata:labels:run: my-apachespec:containers:- name: my-apacheimage: registry.cn-chengdu.aliyuncs.com/liumuquan_app/httpd:2.4-alpine3.17ports:- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:name: my-apachelabels:run: my-apache
spec:type: NodePortports:- port: 80targetPort: 80nodePort: 30001selector:run: my-apache
[root@k8s-master1 ingress]# vim my-nginx.yaml 
[root@k8s-master1 ingress]# cat my-nginx.yaml 
---
apiVersion: apps/v1
kind: Deployment
metadata:name: my-nginx
spec:selector:matchLabels:run: my-nginxreplicas: 2template:metadata:labels:run: my-nginxspec:containers:- name: my-nginximage: registry.cn-chengdu.aliyuncs.com/liumuquan_app/nginx:1.12.0-alpineports:- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:name: my-nginxlabels:run: my-nginx
spec:type: NodePortports:- port: 80targetPort: 80nodePort: 30002selector:run: my-nginx

创建webpod和svc

[root@k8s-master1 ingress]# kubectl apply -f my-apache.yaml 
deployment.apps/my-apache created
service/my-apache created
[root@k8s-master1 ingress]# kubectl apply -f my-nginx.yaml 
deployment.apps/my-nginx created
service/my-nginx created
[root@k8s-master1 ingress]# kubectl get pod
NAME                         READY   STATUS    RESTARTS   AGE
my-apache-764b8f49c5-5bgdb   1/1     Running   0          19s
my-apache-764b8f49c5-ck4vl   1/1     Running   0          19s
my-nginx-5c49645686-7csv8    1/1     Running   0          15s
my-nginx-5c49645686-ptxhv    1/1     Running   0          15s
[root@k8s-master1 ingress]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        23d
my-apache    NodePort    10.105.128.125   <none>        80:30001/TCP   22s
my-nginx     NodePort    10.99.184.224    <none>        80:30002/TCP   18s

ingress无需修改

[root@k8s-master1 ingress]# kubectl apply -f ingress-test.yaml 
ingress.networking.k8s.io/test-ingress created

此时物理机访问需要添加nodeport暴露的端口号才能正常访问

 

 

这篇关于11--kubernetes的Ingress应用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1145965

相关文章

将Python应用部署到生产环境的小技巧分享

《将Python应用部署到生产环境的小技巧分享》文章主要讲述了在将Python应用程序部署到生产环境之前,需要进行的准备工作和最佳实践,包括心态调整、代码审查、测试覆盖率提升、配置文件优化、日志记录完... 目录部署前夜:从开发到生产的心理准备与检查清单环境搭建:打造稳固的应用运行平台自动化流水线:让部署像

Linux中Curl参数详解实践应用

《Linux中Curl参数详解实践应用》在现代网络开发和运维工作中,curl命令是一个不可或缺的工具,它是一个利用URL语法在命令行下工作的文件传输工具,支持多种协议,如HTTP、HTTPS、FTP等... 目录引言一、基础请求参数1. -X 或 --request2. -d 或 --data3. -H 或

在Ubuntu上部署SpringBoot应用的操作步骤

《在Ubuntu上部署SpringBoot应用的操作步骤》随着云计算和容器化技术的普及,Linux服务器已成为部署Web应用程序的主流平台之一,Java作为一种跨平台的编程语言,具有广泛的应用场景,本... 目录一、部署准备二、安装 Java 环境1. 安装 JDK2. 验证 Java 安装三、安装 mys

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

Node.js 中 http 模块的深度剖析与实战应用小结

《Node.js中http模块的深度剖析与实战应用小结》本文详细介绍了Node.js中的http模块,从创建HTTP服务器、处理请求与响应,到获取请求参数,每个环节都通过代码示例进行解析,旨在帮... 目录Node.js 中 http 模块的深度剖析与实战应用一、引言二、创建 HTTP 服务器:基石搭建(一

java中VO PO DTO POJO BO DO对象的应用场景及使用方式

《java中VOPODTOPOJOBODO对象的应用场景及使用方式》文章介绍了Java开发中常用的几种对象类型及其应用场景,包括VO、PO、DTO、POJO、BO和DO等,并通过示例说明了它... 目录Java中VO PO DTO POJO BO DO对象的应用VO (View Object) - 视图对象

Go信号处理如何优雅地关闭你的应用

《Go信号处理如何优雅地关闭你的应用》Go中的优雅关闭机制使得在应用程序接收到终止信号时,能够进行平滑的资源清理,通过使用context来管理goroutine的生命周期,结合signal... 目录1. 什么是信号处理?2. 如何优雅地关闭 Go 应用?3. 代码实现3.1 基本的信号捕获和优雅关闭3.2

正则表达式高级应用与性能优化记录

《正则表达式高级应用与性能优化记录》本文介绍了正则表达式的高级应用和性能优化技巧,包括文本拆分、合并、XML/HTML解析、数据分析、以及性能优化方法,通过这些技巧,可以更高效地利用正则表达式进行复杂... 目录第6章:正则表达式的高级应用6.1 模式匹配与文本处理6.1.1 文本拆分6.1.2 文本合并6

python中的与时间相关的模块应用场景分析

《python中的与时间相关的模块应用场景分析》本文介绍了Python中与时间相关的几个重要模块:`time`、`datetime`、`calendar`、`timeit`、`pytz`和`dateu... 目录1. time 模块2. datetime 模块3. calendar 模块4. timeit

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取