一、为什么需要Ingress

一个重要的原因是每个LoadBalancer服务都需要自己的负载均衡器,以及独有的公有IP地址,而Ingress只需要一个公网IP就能为许多服务提供访问。当客户端向Ingress发送HTTP请求时,Ingress会根据请求的主机名和路径决定请求转发到服务。

二、创建一个简单的Ingress服务

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx
  namespace: test
spec:
  rules:
    - host: mingkang-web.cedarhd.com        #ingress将域名mingkang-web映射到你的服务
      http:
        paths:
          - backend:
              serviceName: nginx-svc       #将所有的请求发送到nginx-svc服务的80端口
              servicePort: 80

[root@test-nodes1 k8s-yaml-file]# kubectl get pod -o wide -n test
NAME    READY   STATUS    RESTARTS   AGE     IP           NODE                      NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          6d22h   172.7.21.6   test-nodes1.cedarhd.com              
[root@test-nodes1 k8s-yaml-file]# kubectl get svc -n test
NAME        TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)   AGE
nginx-svc   ClusterIP   192.168.222.228           80/TCP    22m
[root@test-nodes1 k8s-yaml-file]# ipvsadm -ln | grep 192.168.222.228 -C 2
TCP  192.168.212.130:8080 nq
  -> 172.7.21.6:80                Masq    1      0          0         
TCP  192.168.222.228:80 nq
  -> 172.7.21.6:80                Masq    1      0          0         
TCP  192.168.237.252:8080 nq
[root@test-nodes1 k8s-yaml-file]# kubectl get ingress -n test
NAME    HOSTS                      ADDRESS   PORTS   AGE
nginx   mingkang-web.cedarhd.com             80      16m

三、通过相同的Ingress暴露多个服务

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx
  namespace: test
spec:
  rules:
    - host: mingkang-web.cedarhd.com        #ingress将域名mingkang-web映射到你的服务
      http:
        paths:
          - path: /abc
            backend:
              serviceName: nginx-svc  #将mingkang-web.cedarhd.com/abc的请求将会转发至ngins-svc服务
              servicePort: 80
          - path: /def
            backend:
              serviceName: nginx-dd   #将mingkang-web.cedarhd.com/def的请求将会转发至nginx-dd服务  
              servicePort: 81