kubernetes 容器 【K8S 云原生】K8S的对外服务—ingress - 自动更新的重载模块相对较慢

📅 2026-11-30 👤 拼多多助力营销技巧 📂 赚钱项目 👁️ 355 次阅读

目录

一、K8S的Service

1、Service的作用

2、Service类型:

二、ingress

1、ingress的组成:

2、ingress资源的定义项:

三、nginx-ingress-controller暴露服务端的方式

1、Deployment+LoadBalancer模式:

1、工作流程图:

2、Daemonset+hostnetwork+nodeSelector模式:

1、工作流程图

2、实验:

3、deployment+NodePort

1、实验:

4、通过虚拟主机的方式实现http代理

5、ingress实现https代理访问:

5.1、nginx的登录账户认证:

5.2、nginx的重写:

6、总结:

四、traefik-ingress暴露服务端的方式:

1、概念

2、工作流程图

3、traefik-ingress暴露服务端的方式

3.1、Daemonset模式:

3.2、deployment模式:

4、traefik-ingress和nginx-ingress的区别:

5、实验:

1、deployment模式:

2、Daemonset模式:

五、总结:

一、K8S的Service

1、Service的作用

Service的作用体现在两个方面:

1、集群内部:不断跟踪pod的变化,更新endpoints中的pod对象,基于pod的IP地址不断变化的一种服务发现机制

2、集群外部:类似负载均衡器,可以把流量IP+端口,不涉及转发url(http、https),把请求转发到pod中

2、Service类型:

NodePort:容器端口—Service端口—NodePort映射。设定了NodePort之后每个节点都会有一个端口被打开30000-32767

LoadBalancer:云平台上的Service服务。由云平台提供负载均衡的IP地址

ExternalName:域名映射

ingress:基于域名进行映射,转发的是url请求(http、https)转发到Service,再由Service转发到每一个pod

ingress只需要一个或者是少量的公网IP或者LB,可以把多个http请求暴露到外网

他是七层反向代理

可以将ingress理解为,Service的Service。是一组基于域名和url路径把请求转发到一个或者多个请求转发到Service的规则

ingress先七层转发转发都Service,Service再四层转发转发到pod

二、ingress

1、ingress的组成:

ingress是一个api对象,通过yaml文件来进行配置。ingress作用就是定义请求如何转发到Service的规则

ingress通过http和https暴露集群内部的Service,给Service提供一个外部的url、负载均衡、ssl/tls(https)的能力,实现一个基于域名的负载均衡

ingress-controller:由这个组件来具体的实现反向代理和负载均衡的程序,以及对ingress定义的规则进行解析,根据ingress的配置的规则进行请求的转发。

但是这个ingress-controller不是K8S自带的组件功能,ingress-controller是一个统称(一系列工具)

比如nginx ingress-controller、traefik都是ingress-controller,开源的软件

ingress-controller是pod的方式运行在节点上

2、ingress资源的定义项:

定义外部流量的路由规则定义的服务暴露的方式、主机名、访问的路径和其他的一些选项负载均衡(ingress-controller实现)

三、nginx-ingress-controller暴露服务端的方式

1、Deployment+LoadBalancer模式:

ingress部署在公有云。ingress配置文件中会有type。type:LoadBalancer。公有云平台会为这个LoadBalancer的Service自动创建一个负载均衡器。绑定公网地址

通过域名指向公网地址,实现集群对外暴露

1、工作流程图:

2、Daemonset+hostnetwork+nodeSelector模式:

Daemonset:在每个节点都会创建一个pod

hostnetwork:共享节点主机的网络命名空间。容器内直接使用节点主机的IP+端口。pod上的容器可以直接访问主机上的网络资源

nodeSelector:根据标签选择部署的节点。选择nginx-ingress-controller部署的节点

缺点:直接利用节点主机的网络和端口资源,一个node只能部署一个ingress-controller的pod

适用场景:大并发场景的生产环境。性能是最好的

1、工作流程图

Service和endpoints来发现节点

最终转发是由ingress-controller来转发请求(http)和负载均衡

用DaemonSet结合nodeselector来部署ingress-controller到特定的node上,然后使用HostNetwork直接把该pod与宿主机node的网络打通,直接使用宿主机的80/433端口就能访问服务。这时,ingress-controller所在的node机器就很类似传统架构的边缘节点,比如机房入口的nginx服务器。该方式整个请求链路最简单,性能相对NodePort模式更好。

缺点是由于直接利用宿主机节点的网络和端口,一个node只能部署一个ingress-controller pod。 比较适合大并发的生产环境使用。

2、实验:

#mandatory.yaml文件中包含了很多资源的创建,包括namespace、ConfigMap、role,ServiceAccount等等所有部署ingress-controller需要的资源。

官方下载地址:

wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.25.0/deploy/static/mandatory.yaml

上面可能无法下载,可用国内的 gitee

wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.25.0/deploy/static/mandatory.yaml

wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml

DaemonSet+HostNetwork具体的部署过程

下载安装ingress-controller pod及相关资源

每个节点上,上传ingress-contro控制器

解压安装

tar -xf ingree.contro-0.30.0.tar.gz

docker load -i ingree.contro-0.30.0.tar

#节点打标签,用于选择节点部署ingress-controller

kubectl label nodes node02 ingress=true

192-215行修改配置文件

启动 nginx-ingress-controller,并检查该pod的运行状况

kubectl apply -f mandatory.yaml

#nginx-ingress-controller 已经运行 node02 节点

kubectl get pod -n ingress-nginx -o wide

kubectl get cm,daemonset -n ingress-nginx -o wide

#到 node02 节点查看

netstat -lntp | grep nginx

由于配置了 hostnetwork,nginx 已经在 node 主机本地监听 80/443/8181 端口。其中 8181 是 nginx-controller 默认配置的一个 default backend(Ingress 资源没有匹配的 rule 对象时,流量就会被导向这个 default backend)。

这样,只要访问 node 主机有公网 IP,就可以直接映射域名来对外网暴露服务了。如果要 nginx 高可用的话,可以在多个 node

上部署,并在前面再搭建一套 LVS+keepalived 做负载均衡。

创建 ingress 规则

创建一个业务pod和svc资源和ingress规则:

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

name: nfs-pvc

spec:

accessModes:

- ReadWriteMany

storageClassName: nfs-client-storageclass

resources:

requests:

storage: 2Gi

---

apiVersion: apps/v1

kind: Deployment

metadata:

name: nginx-app

labels:

app: nginx1

spec:

replicas: 3

selector:

matchLabels:

app: nginx1

template:

metadata:

labels:

app: nginx1

spec:

containers:

- name: nginx

image: nginx:1.22

volumeMounts:

- name: nfs-pvc

mountPath: /usr/share/nginx/html

volumes:

- name: nfs-pvc

persistentVolumeClaim:

claimName: nfs-pvc

---

apiVersion: v1

kind: Service

metadata:

name: nginx-app-svc

spec:

ports:

- protocol: TCP

port: 80

targetPort: 80

selector:

app: nginx1

---

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

name: nginx-app-ingress

spec:

rules:

- host: www.test1.com

http:

paths:

- path: /

pathType: Prefix

backend:

service:

name: nginx-app-svc

port:

number: 80

#选择一台客户机,直接添加node02的节点的IP与pod的域名进行映射

vim /etc/hosts

20.0.0.63 www.test1.com

访问curl www.test1.com

3、deployment+NodePort

同样用deployment模式部署ingress-controller,并创建对应的service,但是type为NodePort。这样,ingress就会暴露在集群节点ip的特定端口上。由于nodeport暴露的端口是随机端口,一般会在前面再搭建一套负载均衡器来转发请求。该方式一般用于宿主机是相对固定的环境ip地址不变的场景

NodePort方式暴露ingress虽然简单方便,但是NodePort多了一层NAT,在请求量级很大时可能对性能会有一定影响

1、实验:

如果做了上一个操作,就将所有的yaml文件通过delete进行清除。继续下面的操作

下载相关的ingress和service-nodeport模板

mkdir /opt/ingress/test

cd /opt/ingress/test

#官方下载地址:

wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml

wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml

#国内 gitee 资源地址:

wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml

wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml

#在所有 node 节点上传镜像包 ingress-controller-0.30.0.tar 到 /opt/ingress-nodeport 目录,并加载镜像

docker load -i ingress-controller-0.30.0.tar

直接启动官方模板即可使用

#若是新的mandatory.yaml文件,直接启动即可,若是用上个实验的,要还原到最初始

kubectl apply -f mandatory.yaml

还原脚本如下:

在所有节点上部署ingress-controller

#启用Service-nodeport

kubectl apply -f service-nodeport.yaml

#//如果K8S Pod 调度失败,在 kubectl describe pod资源时显示:

Warning FailedScheduling 18s (x2 over 18s) default-scheduler 0/2 nodes are available: 2 node(s) didn't match node selector

解决方案:

1. 给需要调度的node加上对应标签

# 相对上面这个Yaml文件的例子

kubectl label nodes node_name kubernetes.io/os=linux

2. 删除Yaml文件中的nodeSelector,如果对节点没有要求的话,直接删除节点选择器即可

进行Ingress Http代理访问的操作演示

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

name: nfs-pvc

spec:

accessModes:

- ReadWriteMany

storageClassName: nfs-client-storageclass

resources:

requests:

storage: 2Gi

---

apiVersion: apps/v1

kind: Deployment

metadata:

name: nginx-app

labels:

app: nginx1

spec:

replicas: 3

selector:

matchLabels:

app: nginx1

template:

metadata:

labels:

app: nginx1

spec:

containers:

- name: nginx

image: nginx:1.22

volumeMounts:

- name: nfs-pvc

mountPath: /usr/share/nginx/html

volumes:

- name: nfs-pvc

persistentVolumeClaim:

claimName: nfs-pvc

---

apiVersion: v1

kind: Service

metadata:

name: nginx-app-svc

spec:

ports:

- protocol: TCP

port: 80

targetPort: 80

selector:

app: nginx1

---

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

name: nginx-app-ingress

spec:

rules:

- host: www.test1.com

http:

paths:

- path: /

pathType: Prefix

backend:

service:

name: nginx-app-svc

port:

number: 80

访问IP+NodePort端口

工作流程:

每个节点都是从controller----接收到解析后的请求IP+端口----ingress-----Service----pod

总结:

nginx-ingress-controller

host—>ingresss的配置找到pod—>controller—>请求发到pod

nodeport--->controller--->ingress--->service--->pod

NodePort暴露端口的方式最简单的方法,NodePort多了一层nat(地址转换)

并发量大的对性能会有一定影响,内部都会用nodePort

4、通过虚拟主机的方式实现http代理

通过ingress的方式实现:一个ingress可以访问不同的主机

我们知道nginx和apache能够设置三种虚拟主机,分别为:基于IP,基于域名,基于端口。同样ingress-nginx这里也可以设置出这三种虚拟主机。下面将以最常用的基于域名的虚拟主机为例子,进行操作演示

先创建两个pod和Service

虚拟主机1:

apiVersion: apps/v1

kind: Deployment

metadata:

name: deployment1

labels:

test: nginx1

spec:

replicas: 1

selector:

matchLabels:

test: nginx1

template:

metadata:

labels:

test: nginx1

spec:

containers:

- name: nginx1

image: nginx:1.22

---

apiVersion: v1

kind: Service

metadata:

name: svc-1

spec:

ports:

- port: 80

targetPort: 80

protocol: TCP

selector:

test: nginx1

虚拟主机2将上面标签全部替换

再创建ingress资源

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

name: ingress1

spec:

rules:

- host: www1.test.com

http:

paths:

- path: /

pathType: Prefix

backend:

service:

name: svc-1

port:

number: 80

---

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

name: ingress2

spec:

rules:

- host: www2.test.com

http:

paths:

- path: /

pathType: Prefix

backend:

service:

name: svc-2

port:

number: 80

映射域名:

vim /etc/hosts

#这里任意填写一个node节点的IP即可

20.0.0.62 www.test.com www.abc.com

然后就可以基于域名+NodePort端口进行访问了

5、ingress实现https代理访问:

需要证书和秘钥

创建证书 秘钥

secrets保存密钥信息,部署pod时把secrets挂载到pod

openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=nginxsvc/O=nginxsvc"

req:生成证书文件的

x509:生成x.509自签名的证书

-sha256:表示使用sha-256散列算法

-nodes:表示生成到的秘钥不加密

-day 365:证书有效期365天

-newkey rsa:2048:RSA的密钥对,长度2048为位

-keyout tls.key -out tls.crt:密钥文件key,证书文件crt

-subj "/CN=nginxsvc/O=nginxsvc":主题,CN: common name O:

kubectl create secret tls tls-secret --key tls.key --cert tls.crt

kubectl describe secrets tls-secret

创建deployment、service、ingress

vim traefik-nginx.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

name: nginx-https

labels:

app: https

spec:

replicas: 3

selector:

matchLabels:

app: https

template:

metadata:

labels:

app: https

spec:

containers:

- name: nginx

image: nginx:1.22

---

apiVersion: v1

kind: Service

metadata:

name: nginx-svc

spec:

ports:

- port: 80

targetPort: 80

selector:

app: https

---

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

name: nginx-ingress-https

spec:

tls:

- hosts:

- www.123ccc.com

secretName: tls-secret

rules:

- host: www.123ccc.com

http:

paths:

- pathType: Prefix

path: "/"

backend:

service:

name: nginx-svc

port:

number: 80

访问测试

域名:https端口

curl -k https://www.123ccc.com:32561

​

是基于service-nodeport.yaml完成的,首先要有ingress-nginx

5.1、nginx的登录账户认证:

下载http,用htpasswd生成密钥(这里秘钥的名字只能值auth)

创建秘钥

生成新的ingress:

htpasswd -c auth koeda

New password: 123456

Re-type new password: 123456

kubectl create secret generic basic-auth --from-file=auth

vim ingress-auth.yaml

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

name: ingress-auth

annotations:

#开启认证模块的配置

nginx.ingress.kubernetes.io/auth-type: basic

#设置认证类型为basic。是k8s自带的认证加密模块

nginx.ingress.kubernetes.io/auth-secret: basic-auth

#把认证的加密模块导入到ingress当中

nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required-zyg'

#设置认证窗口的提示信息。

spec:

rules:

- host: www.zyg.com

http:

paths:

- path: /

pathType: Prefix

backend:

service:

name: nginx-svc

port:

number: 80

#使用http创建basic-auth

#开启认证加密的文件

#创建ingress开启认证模式

#将auth配置文件导入ingress当中

做映射www.koeda.com

在虚拟机访问测试

5.2、nginx的重写:

vim ingress-rewrite.yaml(要进入加入https端口)

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

name: nginx-rewrite

annotations:

nginx.ingress.kubernetes.io/rewrite-target: https://www.123ccc.com:32561

spec:

rules:

- host: www.ddd.com

http:

paths:

- path: /

pathType: Prefix

backend:

service:

name: nginx-svc

port:

number: 80

自定义域名需要映射

通过www.ddd.com+http端口访问

容器内页面跳转

到此nginx-ingress-controller全部介绍完毕

6、总结:

ingress两个重要的组件:

nginx-ingress-controller

traefik-ingress-controller

都是开源的ingress-controller

三种工作方式:

1、deployment+LoadBalancer,需要云平台提供一个负载均衡的公网地址,公有云上做

2、Daemonset+hostnetwork+nodeSelector,指定节点部署controller,缺点就是和宿主机共享网络,只能是一个controller的pod

hostnetwork这个配置回合宿主机共享网络资源

3、deployment+NodePort,最常见,最常用,也是最简单的方式。他是集中一个NodePort端口,所有的ingress的请求都会转发到NodePort,然后由Service把流量转发到pod

一个ingress的NodePort可以实现访问多个虚拟主机(域名),和nginx是一样的

四、traefik-ingress暴露服务端的方式:

1、概念

traefik是一个为了让部署微服务更加快捷而诞生的一个http反向代理,负载均衡

traefik设计时就能实时的和K8S的api交互,可以感知后端service以及pod的变化,还可以自动更新配置和重载

pod内nginx容器,80 8081

traefik的部署方式:

Daemonset和deployment

2、工作流程图

3、traefik-ingress暴露服务端的方式

3.1、Daemonset模式:

用的还是hostnetwork网络模式

特点:每个节点都会部署一个traefik。节点感知,可以自动发现,更新容器的配置,不需要手动重载

缺点:资源占用,大型集群中,Daemonset可能会运行多个traefik的实例,尤其是在节点上不需要大量容器运行的情况下。他是没有办法扩所容器的

适用于部署对外集群:因为对外的业务会经常变更,使用Daemonset模式可以更好的、自动的发现服务配置变更

3.2、deployment模式:

用的还是NodePort网络模式

优点:集中控制,可以使用少量的实例来运行处理整个集群的流量,更容易升级和维护。

缺点:

deployment负载均衡不会均匀地分布到每个节点。

需要手动更新,他无法感知容器的配置变化

适用于部署对内集群:对内的业务相对稳定,更新和变化也比较少,适合deployment的方式

一般部署的时候会设置标签:

traffic-type:internal 声明对内服务

traffic-type:external 声明对外服务

4、traefik-ingress和nginx-ingress的区别:

没有大区别

工作原理都一样,都是七层代理,都可以动态的更新配置,都可以自动发现服务

nginx-ingress:

自动更新的重载模块相对较慢

traefik-ingress

自动更新的重载模块更快、更方便

traefik的并发能力只有nginx-ingress的6成 60%

工作中见得最多的就是nginx-ingress-controller

5、实验:

1、deployment模式:

​wget https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-deployment.yaml

wget https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-rbac.yaml

wget https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/traefik-ds.yaml

wget https://gitee.com/mirrors/traefik/raw/v1.7/examples/k8s/ui.yaml

​

#先启动rbac权限配置

kubectl apply -f traefik-rbac.yaml

#再启动deployment或者Daemonset

kubectl apply -f traefik-deployment.yaml

#最后启动ui工具

kubectl apply -f ui.yaml

vim traefik-ingress-nginx.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

name: nginx-traefik

labels:

nginx: traefik

spec:

replicas: 3

selector:

matchLabels:

nginx: traefik

template:

metadata:

labels:

nginx: traefik

spec:

containers:

- name: nginx

image: nginx:1.22

---

apiVersion: v1

kind: Service

metadata:

name: nginx-traefik-svc1

spec:

ports:

- port: 80

targetPort: 80

protocol: TCP

selector:

nginx: traefik

---

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

name: nginx-traefik-test1

spec:

rules:

- host: www.yyw.com

http:

paths:

- path: /

pathType: Prefix

backend:

service:

name: nginx-traefik-svc1

port:

number: 80

ui查看发现

2、Daemonset模式:

Daemonset,以及配置更新的自动重载

先运行traefik-ds.yaml,使用traefik的Daemonset+hostnetwork模式

创建configmap,挂载

worker_processes 2;

events {

worker_connections 1024;

}

http {

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

server {

listen 80;

server_name localhost;

charset utf-8;

location / {

root /usr/share/nginx/html;

index index.html index.php;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}

kubectl create configmap nginx-con --from-file=/opt/nginx-configmap/nginx.conf

创建业务资源,service和ingress

apiVersion: apps/v1

kind: DaemonSet

metadata:

name: nginx-app

labels:

app: nginx1

spec:

selector:

matchLabels:

app: nginx1

template:

metadata:

labels:

app: nginx1

spec:

containers:

- name: nginx

image: nginx:1.22

volumeMounts:

- name: nginx-config

mountPath: /etc/nginx/

- name: nginx-mount

mountPath: /usr/share/nginx/html

volumes:

- name: nginx-config

configMap:

name: nginx-con

- name: nginx-mount

hostPath:

path: /opt/html

type: DirectoryOrCreate

---

apiVersion: v1

kind: Service

metadata:

name: nginx-app-svc

spec:

ports:

- protocol: TCP

port: 80

targetPort: 80

selector:

app: nginx1

---

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

name: nginx-app-ingress

spec:

rules:

- host: www.test1.com

http:

paths:

- path: /

pathType: Prefix

backend:

service:

name: nginx-app-svc

port:

number: 80

hostnetwork端口80,nginx监听端口也是80

自动发现,ingress

apiVersion: apps/v1

kind: DaemonSet

metadata:

name: nginx-app

labels:

app: nginx1

spec:

selector:

matchLabels:

app: nginx1

template:

metadata:

labels:

app: nginx1

spec:

containers:

- name: nginx

image: nginx:1.22

volumeMounts:

- name: nginx-config

mountPath: /etc/nginx/

- name: nginx-mount

mountPath: /usr/share/nginx/html

volumes:

- name: nginx-config

configMap:

name: nginx-con

- name: nginx-mount

hostPath:

path: /opt/html

type: DirectoryOrCreate

---

apiVersion: v1

kind: Service

metadata:

name: nginx-app-svc

spec:

ports:

- protocol: TCP

port: 8081 拼多多帮助力入口

targetPort: 8081

selector:

app: nginx1

---

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

name: nginx-app-ingress

spec:

rules:

- host: www.test1.com

http:

paths:

- path: /

pathType: Prefix

backend:

service:

name: nginx-app-svc

port:

number: 8081

重新运行,将hostnetwork的监听端口改成8081

kubectl edit cm nginx-con

更改configmap,将nginx的监听端口改成8081

五、总结:

开源的ingress控制软件

nginx-ingress-controller

traefik-ingress-controller

nginx-ingress-controller:

模式:

deployment+LoadBalancer:公有云提供负载均衡的公网地址

Daemonset+hostnetwork+nodeSelector:和节点服务器共享网络,一个节点只能部署一个controller的pod,使用宿主机的端口,性能最好,适合大并发

deployment+NodePort:最常见也是最常用,也是最简单的方法。但是性能不太好,多了一层nat地址转发,不太适合大并发

traefik-ingress-controller:(小集群)

模式:

Daemonset模式:对外,可以自动更新容器的配置。hostnetwork模式,用的是节点网络

deployment模式:对内,无法自动更新配置。NodePort网络模式

https:

生成证书和秘钥创建Secret保存秘钥创建ingress的时候把Secret导入

加密认证:

1、借助htpasswd工具,生成名字叫 auth 的秘钥(文件名只能是auth)

2、创建ingress的两行代码

精彩文章

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧