Ingress详解

Ingress介绍

Service对集群之外暴露服务的主要方式有两种:NotePortLoadBalancer,但是这两种方式,都有一定的缺点:

  • NodePort方式的缺点是会占用很多集群机器的端口,那么当集群服务变多的时候,这个缺点就愈发明显
  • LB方式的缺点是每个service需要一个LB,浪费、麻烦,并且需要kubernetes之外设备的支持

基于这种现状,kubernetes提供了Ingress资源对象,Ingress只需要一个NodePort或者一个LB就可以满足暴露多个Service的需求。工作机制大致如下图表示:

image-20220526161703832

实际上,Ingress相当于一个7层的负载均衡器,是kubernetes对反向代理的一个抽象,它的工作原理类似于Nginx,可以理解成在Ingress里建立诸多映射规则,Ingress Controller通过监听这些配置规则并转化成Nginx的反向代理配置 , 然后对外部提供服务。在这里有两个核心概念:

  • ingress:kubernetes中的一个对象,作用是定义请求如何转发到service的规则
  • ingress controller:具体实现反向代理及负载均衡的程序,对ingress定义的规则进行解析,根据配置的规则来实现请求转发,实现方式有很多,比如Nginx, Contour, Haproxy等等

Ingress(以Nginx为例)的工作原理如下:

  1. 用户编写Ingress规则,说明哪个域名对应kubernetes集群中的哪个Service
  2. Ingress控制器动态感知Ingress服务规则的变化,然后生成一段对应的Nginx反向代理配置
  3. Ingress控制器会将生成的Nginx配置写入到一个运行着的Nginx服务中,并动态更新
  4. 到此为止,其实真正在工作的就是一个Nginx了,内部配置了用户定义的请求转发规则

img

Ingress使用

搭建ingress环境

# 创建文件夹
mkdir ingress-controller
cd ingress-controller/

# 获取ingress-nginx,本使用的是0.30版本
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

# 修改mandatory.yaml文件中的仓库
# 修改quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0
# 为quay-mirror.qiniu.com/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0
# 创建ingress-nginx
kubectl apply -f ./

# 查看ingress-nginx
kubectl get pod -n ingress-nginx

# 查看service
kubectl get svc -n ingress-nginx

image-20220526152732994

image-20220526152934599

image-20220526153004272

准备service和pod

为了后面的实验比较方便,创建如下图所示的模型

img

创建tomcat-nginx.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1
        ports:
        - containerPort: 80

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
      app: tomcat-pod
  template:
    metadata:
      labels:
        app: tomcat-pod
    spec:
      containers:
      - name: tomcat
        image: tomcat:8.5-jre10-slim
        ports:
        - containerPort: 8080

---

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  namespace: dev
spec:
  selector:
    app: nginx-pod
  clusterIP: None
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80

---

apiVersion: v1
kind: Service
metadata:
  name: tomcat-service
  namespace: dev
spec:
  selector:
    app: tomcat-pod
  clusterIP: None
  type: ClusterIP
  ports:
  - port: 8080
    targetPort: 8080
# 创建
kubectl create -f tomcat-nginx.yaml

# 查看
kubectl get svc -n dev

image-20220526153947847

Http代理

创建ingress-http.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-http
  namespace: dev
spec:
  rules:
  - host: nginx.makalo.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-service
          servicePort: 80
  - host: tomcat.makalo.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-service
          servicePort: 8080
# 创建
kubectl create -f ingress-http.yaml

# 查看
kubectl get ing ingress-http -n dev

# 查看详情
kubectl describe ing ingress-http  -n dev

# 查看ingress-nginx端口
# 80为http服务,443为https的服务
kubectl get svc -n ingress-nginx

# 接下来,在本地电脑上配置host(C:\Windows\System32\drivers\etc)文件,解析上面的两个域名到192.168.176.100(master)上
# 然后,就可以分别访问tomcat.makalo.com:31355  和  nginx.makalo.com:31355 查看效果了

image-20220526154744835

image-20220526154817010

image-20220526155039236

host 配置

image-20220526155229409

查看端口

image-20220526155436128

访问

image-20220526155813771

image-20220526155830729

Https代理

创建证书

# 生成证书
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/C=CN/ST=BJ/L=BJ/O=nginx/CN=makalo.com"

# 创建密钥
kubectl create secret tls tls-secret --key tls.key --cert tls.crt

image-20220526160250365

创建ingress-https.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-https
  namespace: dev
spec:
  tls:
    - hosts:
      - nginx.makalo.com
      - tomcat.makalo.com
      secretName: tls-secret # 指定秘钥
  rules:
  - host: nginx.itheima.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-service
          servicePort: 80
  - host: tomcat.itheima.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-service
          servicePort: 8080
# 创建
kubectl create -f ingress-https.yaml

# 查看
kubectl get ing ingress-https -n dev

# 查看详情
kubectl describe ing ingress-https -n dev

# 查看ingress-nginx端口
# 80为http服务,443为https的服务
kubectl get svc -n ingress-nginx

# 下面可以通过浏览器访问 https://nginx.makalo.com:32025 和 https://tomcat.makalo.com:32025 来查看了

image-20220526160521926

查看端口

image-20220526155436128

访问

image-20220526161112614

image-20220526161126204

posted @ 2022-05-26 16:18  makalo  阅读(431)  评论(0编辑  收藏  举报