K8S之Ingress资源

文章目录

  • 一、Ingress介绍
  • 二、Ingress使用
      • 准备ingress环境
      • 准备service和pod
      • Http代理
      • Https代理

一、Ingress介绍

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

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

基于这种现状,kubernetes提供了Ingress资源对象,Ingress只需要一个NodePort或者一个LB就可以满足暴露多个Service的需求。

工作机制如下图表示:
K8S之Ingress资源_第1张图片
实际上,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了, 内部配置了用户定义的请求转发规则

K8S之Ingress资源_第2张图片

二、Ingress使用

准备ingress环境

# 创建文件夹
[root@k8s-master ~]# mkdir ingress-controller
[root@k8s-master ~]# cd ingress-controller
# 获取ingress-nginx,本次案例使用的是0.30版本
[root@k8s-master ingress-controller]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml
[root@k8s-master ingress-controller]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml
[root@k8s-master ingress-controller]# ls
mandatory.yaml  service-nodeport.yaml

# 创建ingress-nginx
[root@k8s-master ingress-controller]# kubectl apply -f .
namespace/ingress-nginx created
configmap/nginx-configuration created
configmap/tcp-services created
configmap/udp-services created
serviceaccount/nginx-ingress-serviceaccount created
clusterrole.rbac.authorization.k8s.io/nginx-ingress-clusterrole created
role.rbac.authorization.k8s.io/nginx-ingress-role created
rolebinding.rbac.authorization.k8s.io/nginx-ingress-role-nisa-binding created
clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-clusterrole-nisa-binding created
deployment.apps/nginx-ingress-controller created
limitrange/ingress-nginx created
service/ingress-nginx created

# 查看 ingress-nginx 
[root@k8s-master ingress-controller]# kubectl get pod -n ingress-nginx -o wide
NAME                                        READY   STATUS    RESTARTS   AGE   IP             NODE         NOMINATED NODE   READINESS GATES
nginx-ingress-controller-7f74f657bd-cz5vx   1/1     Running   0          82s   10.244.2.129   k8s-node02   <none>           <none>
# 如果一直拉取失败,那么修改 mandatory.yaml 文件中的仓库,将 image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0 改为 image: quay-mirror.qiniu.com/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0,然后重新apply

# 查看service
[root@k8s-master ingress-controller]# kubectl get svc -n ingress-nginx -o wide
NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE     SELECTOR
ingress-nginx   NodePort   10.104.194.71   <none>        80:32578/TCP,443:32553/TCP   3m14s   app.kubernetes.io/name=ingress-nginx,app.kubernetes.io/part-of=ingress-nginx

准备service和pod

K8S之Ingress资源_第3张图片

# 创建nginx-deployment,用于管理运行nginx的pod
# 创建tomcat-deployment,用于管理运行tomcat的pod
# 对nginx-deployment进行暴露
# 对tomcat-deployment进行暴露
[root@k8s-master ~]# vim nginx-tomcat.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: test
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: test
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: test
spec:
  selector:
    app: ngnx-pod
  clusterIP: None
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80
    
---

apiVersion: v1
kind: Service
metadata:
  name: tomcat-service
  namespace: test
spec:
  selector:
    app: tomcat-pod
  clusterIP: None
  type: ClusterIP
  ports:
  - port: 8080
    targetPort: 8080

# 创建
[root@k8s-master ~]# kubectl create -f nginx-tomcat.yaml 
deployment.apps/nginx-deployment created
deployment.apps/tomcat-deployment created
service/nginx-service created
service/tomcat-service created

# 查看
[root@k8s-master ~]# kubectl get svc,deploy,pod -n test -o wide
NAME                     TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE   SELECTOR
service/nginx-service    ClusterIP   None         <none>        80/TCP     6s    app=nginx-pod
service/tomcat-service   ClusterIP   None         <none>        8080/TCP   6s    app=tomcat-pod

NAME                                READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                  SELECTOR
deployment.apps/nginx-deployment    3/3     3            3           6s    nginx        nginx:1.17.1            app=nginx-pod
deployment.apps/tomcat-deployment   3/3     3            3           6s    tomcat       tomcat:8.5-jre10-slim   app=tomcat-pod

NAME                                     READY   STATUS    RESTARTS   AGE   IP             NODE         NOMINATED NODE   READINESS GATES
pod/nginx-deployment-6696798b78-kw8sk    1/1     Running   0          6s    10.244.1.164   k8s-node01   <none>           <none>
pod/nginx-deployment-6696798b78-r9ffh    1/1     Running   0          6s    10.244.2.138   k8s-node02   <none>           <none>
pod/nginx-deployment-6696798b78-wcj6p    1/1     Running   0          6s    10.244.1.163   k8s-node01   <none>           <none>
pod/tomcat-deployment-58467d5474-gnfr8   1/1     Running   0          6s    10.244.2.137   k8s-node02   <none>           <none>
pod/tomcat-deployment-58467d5474-hk84r   1/1     Running   0          6s    10.244.2.136   k8s-node02   <none>           <none>
pod/tomcat-deployment-58467d5474-x5cc2   1/1     Running   0          6s    10.244.1.162   k8s-node01   <none>           <none>

Http代理

# 编辑ingress资源清单文件
[root@k8s-master ~]# vim ingress-http.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-http
  namespace: test
spec:
  rules:
  - host: nginx.test.com  #域名
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-service
          servicePort: 80		# 当访问nginx.test.com/时,会将请求转发到名为nginx-service的Service的80端口上,然后Service在将请求转发到后端的pod中
  - host: tomcat.test.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-service
          servicePort: 8080

# 创建
[root@k8s-master ~]# kubectl create -f ingress-http.yaml 
ingress.extensions/ingress-http created

# 查看
[root@k8s-master ~]# kubectl get ing ingress-http -n test 
NAME           HOSTS                            ADDRESS   PORTS   AGE
ingress-http   nginx.test.com,tomcat.test.com             80      8s
[root@k8s-master ~]# kubectl describe ing ingress-http -n test
Name:             ingress-http
Namespace:        test
Address:          
Default backend:  default-http-backend:80 (<none>)
Rules:
  Host             Path  Backends
  ----             ----  --------
  nginx.test.com   
                   /   nginx-service:80 (10.244.1.163:80,10.244.1.164:80,10.244.2.138:80)
  tomcat.test.com  
                   /   tomcat-service:8080 (10.244.1.162:8080,10.244.2.136:8080,10.244.2.137:8080)
Annotations:
Events:
  Type    Reason  Age   From                      Message
  ----    ------  ----  ----                      -------
  Normal  CREATE  20s   nginx-ingress-controller  Ingress test/ingress-http

#接下来,在本地电脑上配置host文件,解析上面的两个域名到master主机上

C:\Windows\System32\drivers\etc\hosts
192.168.126.100 nginx.test.com
192.168.126.100 tomcat.test.com

查看服务对应的端口

[root@k8s-master ~]# kubectl get svc -n ingress-nginx -o wide
NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE     SELECTOR
ingress-nginx   NodePort   10.104.194.71   <none>        80:32578/TCP,443:32553/TCP   3h10m   app.kubernetes.io/name=ingress-nginx,app.kubernetes.io/part-of=ingress-nginx
# http对外提供的端口为32578

测试
K8S之Ingress资源_第4张图片
K8S之Ingress资源_第5张图片
使用Ingress完成基于HTTP的反向代理功能就已实现

Https代理

# 创建证书
[root@k8s-master ~]# openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/C=CN/ST=BJ/L=BJ/0=nginx/CN=test.com"
Generating a 2048 bit RSA private key
.......................................................................+++
....................................................................................+++
writing new private key to 'tls.key'
-----
Subject Attribute 0 has no known NID, skipped

# 创建密钥
[root@k8s-master ~]# kubectl create secret tls tls-secret --key tls.key --cert tls.crt
secret/tls-secret created

# 创建nginx-deployment,用于管理运行nginx的pod
# 创建tomcat-deployment,用于管理运行tomcat的pod
# 对nginx-deployment进行暴露
# 对tomcat-deployment进行暴露
[root@k8s-master ~]# vim nginx-tomcat.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: test
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: test
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: test
spec:
  selector:
    app: ngnx-pod
  clusterIP: None
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80
    
---

apiVersion: v1
kind: Service
metadata:
  name: tomcat-service
  namespace: test
spec:
  selector:
    app: tomcat-pod
  clusterIP: None
  type: ClusterIP
  ports:
  - port: 8080
    targetPort: 8080

# 创建
[root@k8s-master ~]# kubectl create -f nginx-tomcat.yaml 
deployment.apps/nginx-deployment created
deployment.apps/tomcat-deployment created
service/nginx-service created
service/tomcat-service created

# 查看
[root@k8s-master ~]# kubectl get svc,deploy,pod -n test -o wide
[root@k8s-master ~]# kubectl get svc,deploy,pod -n test -o wide
NAME                     TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE   SELECTOR
service/nginx-service    ClusterIP   None         <none>        80/TCP     7s    app=nginx-pod
service/tomcat-service   ClusterIP   None         <none>        8080/TCP   7s    app=tomcat-pod

NAME                                READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                  SELECTOR
deployment.apps/nginx-deployment    3/3     3            3           7s    nginx        nginx:1.17.1            app=nginx-pod
deployment.apps/tomcat-deployment   3/3     3            3           7s    tomcat       tomcat:8.5-jre10-slim   app=tomcat-pod

NAME                                     READY   STATUS    RESTARTS   AGE   IP             NODE         NOMINATED NODE   READINESS GATES
pod/nginx-deployment-6696798b78-c2dfz    1/1     Running   0          7s    10.244.1.166   k8s-node01   <none>           <none>
pod/nginx-deployment-6696798b78-ld5cp    1/1     Running   0          7s    10.244.1.165   k8s-node01   <none>           <none>
pod/nginx-deployment-6696798b78-pfbfg    1/1     Running   0          7s    10.244.2.139   k8s-node02   <none>           <none>
pod/tomcat-deployment-58467d5474-bs9r4   1/1     Running   0          7s    10.244.1.167   k8s-node01   <none>           <none>
pod/tomcat-deployment-58467d5474-csxzc   1/1     Running   0          7s    10.244.2.140   k8s-node02   <none>           <none>
pod/tomcat-deployment-58467d5474-fwm2n   1/1     Running   0          7s    10.244.2.141   k8s-node02   <none>           <none>


# 编写基于https的ingress资源清单文件
[root@k8s-master ~]# vim ingress-https.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-https
  namespace: test
spec:
  tls:
    - hosts:
      - nginx.test.com
      - tomcat.test.com
      secretName: tls-secret #指定秘钥
  rules:
  - host: nginx.test.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-service
          servicePort: 80
  - host: tomcat.test.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-service
          servicePort: 8080

# 创建
[root@k8s-master ~]# kubectl create -f ingress-https.yaml 
ingress.extensions/ingress-https created

# 查看
[root@k8s-master ~]# kubectl get ing -n test 
NAME            HOSTS                            ADDRESS   PORTS     AGE
ingress-https   nginx.test.com,tomcat.test.com             80, 443   14s
[root@k8s-master ~]# kubectl describe ing ingress-https -n test 
Name:             ingress-https
Namespace:        test
Address:          10.104.194.71
Default backend:  default-http-backend:80 (<none>)
TLS:
  tls-secret terminates nginx.test.com,tomcat.test.com
Rules:
  Host             Path  Backends
  ----             ----  --------
  nginx.test.com   
                   /   nginx-service:80 (10.244.1.165:80,10.244.1.166:80,10.244.2.139:80)
  tomcat.test.com  
                   /   tomcat-service:8080 (10.244.1.167:8080,10.244.2.140:8080,10.244.2.141:8080)
Annotations:
Events:
  Type    Reason  Age   From                      Message
  ----    ------  ----  ----                      -------
  Normal  CREATE  30s   nginx-ingress-controller  Ingress test/ingress-https
  Normal  UPDATE  14s   nginx-ingress-controller  Ingress test/ingress-https

#接下来,在本地电脑上配置host文件,解析上面的两个域名到master主机上

C:\Windows\System32\drivers\etc\hosts
192.168.126.100 nginx.test.com
192.168.126.100 tomcat.test.com

查看服务对应的端口

[root@k8s-master ~]# kubectl get svc -n ingress-nginx -o wide
NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE     SELECTOR
ingress-nginx   NodePort   10.104.194.71   <none>        80:32578/TCP,443:32553/TCP   3h28m   app.kubernetes.io/name=ingress-nginx,app.kubernetes.io/part-of=ingress-nginx
# https对外提供的端口为32553

测试
K8S之Ingress资源_第6张图片
K8S之Ingress资源_第7张图片

你可能感兴趣的:(Kubernetes)