Kubernetes漸入佳境(Service和Ingress詳解)

一、Service介紹

在Kubernetes中,Pod是應用程序的載體,咱們能夠經過Pod的IP來訪問應用程序,可是Pod的IP地址不是固定的,這就意味着不方便直接採用Pod的IP對服務進行訪問。html

爲了解決這個問題,Kubernetes提供了Service資源,Service會對提供同一個服務的多個Pod進行聚合,而且提供一個統一的入口地址,經過訪問Service的入口地址就能訪問到後面的Pod服務。
node

Service在不少狀況下只是一個概念,真正起做用的實際上是kube-proxy服務進程,每一個Node節點上都運行了一個kube-proxy的服務進程。當建立Service的時候會經過API Server向etcd寫入建立的Service的信息,而kube-proxy會基於監聽的機制發現這種Service的變化,而後它會將最新的Service信息轉換爲對應的訪問規則。nginx

kube-proxy目前支持三種工做模式:git

  • userspace模式:github

    userspace模式下,kube-proxy會爲每個Service建立一個監聽端口,發向Cluster IP的請求被iptables規則重定向到kube-proxy監聽的端口上,kube-proxy根據LB算法(負載均衡算法)選擇一個提供服務的Pod並和其創建鏈接,以便將請求轉發到Pod上。算法

    該模式下,kube-proxy充當了一個四層負載均衡器的角色。因爲kube-proxy運行在userspace中,在進行轉發處理的時候會增長內核和用戶空間之間的數據拷貝,雖然比較穩定,可是效率很是低下。shell

  • iptables模式後端

    iptables模式下,kube-proxy爲Service後端的每一個Pod建立對應的iptables規則,直接將發向Cluster IP的請求重定向到一個Pod的IP上。api

    該模式下kube-proxy不承擔四層負載均衡器的角色,只負責建立iptables規則。該模式的優勢在於較userspace模式效率更高,可是不能提供靈活的LB策略,當後端Pod不可用的時候沒法進行重試。瀏覽器

  • ipvs模式

    ipvs模式和iptables相似,kube-proxy監控Pod的變化並建立相應的ipvs規則。ipvs相對iptables轉發效率更高,除此以外,ipvs支持更多的LB算法。

    開啓ipvs(必須安裝ipvs內核模塊,不然會降級爲iptables)

    kubectl edit cm kube-proxy -n kube-system
    
    # 找到mode,添加"ipvs"

# 刪除原來標籤爲kube-proxy的pod
kubectl delete pod -l k8s-app=kube-proxy -n kube-system

# 測試ipvs模塊是否開啓成功
ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.17.0.1:32176 rr
  -> 192.168.104.2:80             Masq    1      0          0         
TCP  192.168.209.140:32176 rr
  -> 192.168.104.2:80             Masq    1      0          0         
TCP  192.168.219.64:32176 rr
  -> 192.168.104.2:80             Masq    1      0          0         
TCP  10.96.0.1:443 rr
  -> 192.168.209.140:6443         Masq    1      0          0         
TCP  10.96.0.10:53 rr
  -> 10.244.0.2:53                Masq    1      0          0         
  -> 10.244.0.3:53                Masq    1      0          0         
TCP  10.96.0.10:9153 rr
  -> 10.244.0.2:9153              Masq    1      0          0         
  -> 10.244.0.3:9153              Masq    1      0          0         
TCP  10.96.178.15:80 rr
  -> 192.168.104.2:80             Masq    1      0          0            
TCP  10.244.0.0:32176 rr
  -> 192.168.104.2:80             Masq    1      0          0         
TCP  10.244.0.1:32176 rr
  -> 192.168.104.2:80             Masq    1      0          0         
TCP  127.0.0.1:32176 rr
  -> 192.168.104.2:80             Masq    1      0          0         
UDP  10.96.0.10:53 rr
  -> 10.244.0.2:53                Masq    1      0          0         
  -> 10.244.0.3:53                Masq    1      0

二、Service類型

Service的資源清單:

apiVersion: v1 # 版本
kind: Service # 類型
metadata: # 元數據
  name: # 資源名稱
  namespace: # 命名空間
spec:
  selector: # 標籤選擇器,用於肯定當前Service代理那些Pod
    app: nginx
  type: NodePort # Service的類型,指定Service的訪問方式
  clusterIP: # 虛擬服務的IP地址
  sessionAffinity: # session親和性,支持ClientIP、None兩個選項,默認值爲None
  ports: # 端口信息
    - port: 8080 # Service端口
      protocol: TCP # 協議
      targetPort : # Pod端口
      nodePort:  # 主機端口

spec.type說明:

  • ClusterIP:默認值,它是Kubernetes系統自動分配的虛擬IP,只能在集羣內部訪問。

  • NodePort:將Service經過指定的Node上的端口暴露給外部,經過此方法,就能夠在集羣外部訪問服務。

  • LoadBalancer:使用外接負載均衡器完成到服務的負載分發,注意此模式須要外部雲環境的支持。

  • ExternalName:把集羣外部的服務引入集羣內部,直接使用。

三、Service使用

3.一、實驗環境準備

在使用Service以前,首先利用Deployment建立三個Pod,爲pod設置app=ngxinx-pod標籤。

# 建立deployment.yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pc-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

建立和查看Pod的信息

# 建立
kubectl create -f deployment.yaml

# 查看
kubectl get pods -n dev -o wide 
NAME                             READY   STATUS    RESTARTS   AGE   IP                NODE    NOMINATED NODE   READINESS GATES
pc-deployment-7d7dd5499b-6gmxf   1/1     Running   0          9m    192.168.104.3     node2   <none>           <none>
pc-deployment-7d7dd5499b-xfn7w   1/1     Running   0          9m    192.168.104.1     node2   <none>           <none>
pc-deployment-7d7dd5499b-zdllp   1/1     Running   0          9m    192.168.166.130   node1   <none>           <none>

爲了後面測試方便,修改三個Pod中Nginx的index.html

# 此處只展現了一個,其餘兩個操做相同。
# 進入容器內部
kubectl exec -it pc-deployment-7d7dd5499b-6gmxf -n dev /bin/sh

# 修改index.html,將本來內容替換成Pod對應的ip
echo "192.168.104.3 > /usr/share/nginx/html/index.html

修改完畢後,進行測試訪問

curl 192.168.104.3
> 192.168.104.3

curl 192.168.104.1
> 192.168.104.1

curl 192.168.166.130
> 192.168.166.130

3.二、ClusterIP類型的Service

1)、建立Service

# 建立service-clusterip.yaml文件

apiVersion: v1
kind: Service
metadata:
  name: service-clusterip
  namespace: dev
spec:
  selector:
    app: nginx-pod
  clusterIP: 10.97.97.97 # service的IP地址,若是不寫,默認會生成一個
  type: ClusterIP
  ports:
    - port: 80 # Service的端口
      targetPort: 80 # Pod的端口

2)、建立Service以及查看

# 建立
kubectl create -f service-clusterip.yaml

# 查看
kubectl get svc -n dev -o wide
NAME                TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE   SELECTOR
service-clusterip   ClusterIP   10.97.97.97   <none>        80/TCP    44s   app=nginx-pod

# 查看詳細信息
kubectl describe svc service-clusterip -n dev

Name:              service-clusterip
Namespace:         dev
Labels:            <none>
Annotations:       <none>
Selector:          app=nginx-pod
Type:              ClusterIP
IP:                10.97.97.97
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         192.168.104.1:80,192.168.104.3:80,192.168.166.130:80
# Endpoints列表裏就是當前Service能夠負載到的服務入口
Session Affinity:  None
Events:            <none>

Endpoint是Kubernetes中的一個資源對象,存儲在etcd中,用來記錄一個service對應的全部Pod的訪問地址,它是根據service配置文件中的selector描述產生的。

一個service由一組Pod組成,這些Pod經過Endpoints暴露出來,Endpoints是實現實際服務的端點集合。換言之,service和Pod之間的聯繫是經過Endpoints實現的。

# 查看Endpoint
kubectl get endpoints -n dev -o wide

NAME                ENDPOINTS                                              AGE
service-clusterip   192.168.104.1:80,192.168.104.3:80,192.168.166.130:80   9m24s

3)、查看ipvs的映射規則

ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.17.0.1:32176 rr
  -> 192.168.104.2:80             Masq    1      0          0         
TCP  192.168.209.140:32176 rr
  -> 192.168.104.2:80             Masq    1      0          0         
TCP  192.168.219.64:32176 rr
  -> 192.168.104.2:80             Masq    1      0          0         
TCP  10.96.0.1:443 rr
  -> 192.168.209.140:6443         Masq    1      0          0         
TCP  10.96.0.10:53 rr
  -> 10.244.0.2:53                Masq    1      0          0         
  -> 10.244.0.3:53                Masq    1      0          0         
TCP  10.96.0.10:9153 rr
  -> 10.244.0.2:9153              Masq    1      0          0         
  -> 10.244.0.3:9153              Masq    1      0          0         
TCP  10.96.178.15:80 rr
  -> 192.168.104.2:80             Masq    1      0          0         
# 這塊是否是很眼熟了,rr表示輪詢
TCP  10.97.97.97:80 rr 
  -> 192.168.104.1:80             Masq    1      0          0         
  -> 192.168.104.3:80             Masq    1      0          0         
  -> 192.168.166.130:80           Masq    1      0          0      
# -------------------------------
TCP  10.244.0.0:32176 rr
  -> 192.168.104.2:80             Masq    1      0          0         
TCP  10.244.0.1:32176 rr
  -> 192.168.104.2:80             Masq    1      0          0         
TCP  127.0.0.1:32176 rr
  -> 192.168.104.2:80             Masq    1      0          0         
UDP  10.96.0.10:53 rr
  -> 10.244.0.2:53                Masq    1      0          0         
  -> 10.244.0.3:53                Masq    1      0          0

4)、訪問10.97.97.97.80

while true;do curl 10.97.97.97:80; sleep 2;done;

# 輪詢效果
192.168.104.3
192.168.104.1
192.168.166.130
192.168.104.3
192.168.104.1
192.168.166.130
192.168.104.3
......

5)、負載分發策略

對Service的訪問被分發到了後端的Pod上去,目前Kubernetes提供了兩種負載分發策略:

  • 若是不定義,默認使用kube-proxy的策略,好比隨機、輪詢等。

  • 基於客戶端地址的會話保持模式,即來自同一個客戶端發起的全部請求都會轉發到固定的一個Pod上,這對於傳統基於Session的認證項目來講很友好,此模式能夠在spec中添加sessionAffinity: ClusterIP選項。

修改分發策略:

apiVersion: v1
kind: Service
metadata:
  name: service-clusterip
  namespace: dev
spec:
  selector:
    app: nginx-pod
  clusterIP: 10.97.97.97 # service的IP地址,若是不寫,默認會生成一個
  type: ClusterIP
  sessionAffinity: ClientIP # 修改分發策略爲基於客戶端地址的會話保持模式
  ports:
    - port: 80 # Service的端口
      targetPort: 80 # Pod的端口

更新svc以及訪問

# 更新
kubectl apply -f service-clusterip.yaml

# 訪問
while true;do curl 10.97.97.97:80; sleep 2;done;

192.168.166.130
192.168.166.130
192.168.166.130
192.168.166.130
192.168.166.130
192.168.166.130
....

3.三、HeadLiness類型的Srvice

在某些場景中,開發人員可能不想使用Service提供的負載均衡功能,而但願本身來控制負載均衡策略,針對這種狀況,Kubernetes提供了HeadLinesss Service,這類Service不會分配Cluster IP,若是想要訪問Service,只能經過Service的域名進行查詢。

1)、建立Service

# 建立service-headliness.yaml

apiVersion: v1
kind: Service
metadata:
  name: service-headliness
  namespace: dev
spec:
  selector:
    app: nginx-pod
  clusterIP: None # 將clusterIP設置爲None,便可建立headliness Service
  type: ClusterIP
  ports:
    - port: 80 # Service的端口
      targetPort: 80 # Pod的端口

2)、查看詳情

kubectl describe svc service-headliness -n dev

Name:              service-headliness
Namespace:         dev
Labels:            <none>
Annotations:       Selector:  app=nginx-pod
Type:              ClusterIP
IP:                None
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         192.168.104.1:80,192.168.104.3:80,192.168.166.130:80
Session Affinity:  None
Events:            <none>

3)、查看域名解析狀況

# 查看pod
kubectl get pod -n dev

NAME                             READY   STATUS    RESTARTS   AGE
pc-deployment-7d7dd5499b-6gmxf   1/1     Running   0          26m
pc-deployment-7d7dd5499b-xfn7w   1/1     Running   0          26m
pc-deployment-7d7dd5499b-zdllp   1/1     Running   0          26m

# 進入Pod中,執行cat /etc/resolv.conf命令
kubectl exec -it pc-deployment-7d7dd5499b-6gmxf -n dev /bin/sh

# cat /etc/resolv.conf
nameserver 10.96.0.10
search dev.svc.cluster.local svc.cluster.local cluster.local
options ndots:5

4)、經過Service的域名進行查詢

yum -y install bind-utils

dig @10.96.0.10 service-headliness.dev.svc.cluster.local

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.5 <<>> @10.96.0.10 service-headliness.dev.svc.cluster.local
; (1 server found)
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 13705
;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;service-headliness.dev.svc.cluster.local. IN A

;; ANSWER SECTION:
service-headliness.dev.svc.cluster.local. 30 IN	A 192.168.104.3
service-headliness.dev.svc.cluster.local. 30 IN	A 192.168.104.1
service-headliness.dev.svc.cluster.local. 30 IN	A 192.168.166.130

;; Query time: 1 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Tue Aug 10 17:27:25 CST 2021
;; MSG SIZE  rcvd: 237

3.四、NodePort類型的Service

在以前的案例中,建立的Service的IP地址只能在集羣內部才能夠訪問,若是但願Service暴露給集羣外部使用,那麼就須要使用到另一種類型的Service,稱爲NodePort類型的Service。NodePort的工做原理就是將Service的端口映射到Node的一個端口上,而後就能夠經過NodeIP:NodePort來訪問Service了。

1)、建立Service

apiVersion: v1
kind: Service
metadata:
  name: service-nodeport
  namespace: dev
spec:
  selector:
    app: nginx-pod
  type: NodePort # Service類型爲NodePort
  ports:
    - port: 80 # Service的端口
      targetPort: 80 # Pod的端口
      nodePort: 30002 # 指定綁定的node的端口
      			 #(默認取值範圍是30000~32767),若是不指定,會默認分配

2)、查看Service

kubectl get svc service-nodeport -n dev -o wide
NAME               TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   SELECTOR
service-nodeport   NodePort   10.102.12.251   <none>        80:30002/TCP   17s   app=nginx-pod

3)、訪問

經過瀏覽器訪問:http://192.168.209.140:30002

3.五、LoadBalancer類型

LoadBalancer和NodePort很類似,目的都是向外部暴露一個端口,區別在於LoadBalancer會在集羣的外部再來作一個負載均衡設備,而這個設備須要外部環境的支持,外部服務發送到這個設備上的請求,會被設備負載以後轉發到集羣中。

3.六、ExternalName類型的Service

ExternalName類型的Service用於引入集羣外部的服務,它經過externalName屬性指定一個服務的地址,而後在集羣內部訪問此Service就能夠訪問到外部的服務了。

1)、建立service-externalname.yaml

apiVersion: v1
kind: Service
metadata:
  name: service-externalname
  namespace: dev
spec:
  type: ExternalName # Service類型爲ExternalName
  externalName: www.baidu.com # 改爲IP地址也能夠

2)、域名解析

dig @10.96.0.10 service-externalname.dev.svc.cluster.local

<<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.5 <<>> @10.96.0.10 service-externalname.dev.svc.cluster.local
; (1 server found)
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 65339
;; flags: qr aa rd; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;service-externalname.dev.svc.cluster.local. IN A

;; ANSWER SECTION:
service-externalname.dev.svc.cluster.local. 30 IN CNAME	www.baidu.com.
www.baidu.com.		30	IN	CNAME	www.a.shifen.com.
www.a.shifen.com.	30	IN	A	14.215.177.38
www.a.shifen.com.	30	IN	A	14.215.177.39

;; Query time: 29 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Tue Aug 10 17:51:15 CST 2021
;; MSG SIZE  rcvd: 247

四、Ingress介紹

咱們已經知道,Service對集羣以外暴露服務的主要方式有兩種:NodePort和LoadBalancer,可是這兩種方式,都有必定的缺點:

  • NodePort方式的缺點是會佔用不少集羣機器的端口,那麼當集羣服務變多的時候,這個缺點就愈發明顯。
  • LoadBalancer的缺點是每一個Service都須要一個LB,浪費,麻煩,而且須要Kubernetes以外的設備的支持。

基於這種現狀,Kubernetes提供了Ingress資源對象,Ingress只須要一個NodePort或者一個LB就能夠知足暴露多個Service的需求,工做機制大體以下圖所示:

實際上,Ingress至關於一個七層的負載均衡器,是Kubernetes對反向代理的一個抽象,它的工做原理相似於Nginx,能夠理解爲Ingress裏面創建了諸多映射規則,Ingress Controller經過監聽這些配置規則並轉化爲Nginx的反向代理配置,而後對外提供服務。

  • Ingress:Kubernetes中的一個對象,做用是定義請求如何轉發到Service的規則。

  • Ingress Controller:具體實現反向代理及負載均衡的程序,對Ingress定義的規則進行解析,根據配置的規則來實現請求轉發,實現的方式有不少,好比Nginx,Contour,Haproxy等。

Ingress(以Nginx)的工做原理以下:

​ 一、用戶編寫Ingress規則,說明那個域名對應Kubernetes集羣中的那個Service。

​ 二、Ingress控制器動態感知Ingress服務規則的變化,而後生成一段對應的Nginx的反向代理配置。

​ 三、Ingress控制器會將生成的Nginx配置寫入到一個運行着的Nginx服務中,並動態更新。

​ 四、到此爲止,其實真正在工做的就是一個Nginx了,內部配置了用戶定義的請求規則。

五、Ingress使用

5.一、環境準備

一、搭建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

三、建立Ingress-nginx以及查看:

# 建立
kubeclt apply -f ./

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

NAME                                        READY   STATUS    RESTARTS   AGE
nginx-ingress-controller-5bb8fb4bb6-mn8xp   1/1     Running   0          37s

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

NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx   NodePort   10.110.119.41   <none>        80:30994/TCP,443:30693/TCP   2m27s

5.二、準備Service和Pod

爲了後面的實驗比較方便,建立以下圖所示的模型:

1)、建立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

2)、建立Service和Pod以及查看

# 建立
kubectl create -f tomcat-ngxin.yaml 

deployment.apps/nginx-deployment created
deployment.apps/tomcat-deployment created
service/nginx-service created
service/tomcat-service created

# 查看
kubectl get svc,pod -n dev

NAME                           TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
service/nginx-service          ClusterIP      None            <none>          80/TCP         2m18s
service/service-externalname   ExternalName   <none>          www.baidu.com   <none>         118m
service/service-headliness     ClusterIP      None            <none>          80/TCP         157m
service/service-nodeport       NodePort       10.102.12.251   <none>          80:30002/TCP   132m
service/tomcat-service         ClusterIP      None            <none>          8080/TCP       2m18s

NAME                                     READY   STATUS    RESTARTS   AGE
pod/nginx-deployment-7d7dd5499b-nv7n6    1/1     Running   0          2m18s
pod/nginx-deployment-7d7dd5499b-p7ndx    1/1     Running   0          2m18s
pod/nginx-deployment-7d7dd5499b-qfqpp    1/1     Running   0          2m18s
pod/pc-deployment-7d7dd5499b-6gmxf       1/1     Running   0          3h1m
pod/pc-deployment-7d7dd5499b-xfn7w       1/1     Running   0          3h1m
pod/pc-deployment-7d7dd5499b-zdllp       1/1     Running   0          3h1m
pod/tomcat-deployment-7d5fcd4756-82xd4   1/1     Running   0          2m18s
pod/tomcat-deployment-7d5fcd4756-8gz84   1/1     Running   0          2m18s
pod/tomcat-deployment-7d5fcd4756-pv7zw   1/1     Running   0          2m18s

5.三、HTTP

1)、建立ingress-http.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-http
  namespace: dev
spec:
  rules:
  - host: nginx.negan.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-service
          servicePort: 80
  - host: tomcat.negan.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-service
          servicePort: 8080

2)、建立以及查看

# 建立
kubectl apply -f ingress-http.yaml 

# 查看
kubectl get ingress -n dev
NAME           CLASS    HOSTS                              ADDRESS   PORTS   AGE
ingress-http   <none>   nginx.negan.com,tomcat.negan.com             80      5s

# 查看詳情
kubectl describe ingress ingress-http -n dev

Name:             ingress-http
Namespace:        dev
Address:          10.110.119.41
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host              Path  Backends
  ----              ----  --------
  nginx.negan.com   
                    /   nginx-service:80 (192.168.104.1:80,192.168.104.3:80,192.168.104.5:80 + 3 more...)
  tomcat.negan.com  
                    /   tomcat-service:8080 (192.168.104.4:8080,192.168.104.6:8080,192.168.166.134:8080)
Annotations:        Events:
  Type              Reason  Age   From                      Message
  ----              ------  ----  ----                      -------
  Normal            CREATE  90s   nginx-ingress-controller  Ingress dev/ingress-http
  Normal            UPDATE  60s   nginx-ingress-controller  Ingress dev/ingress-http

3)、測試訪問

在本機的hosts文件中添加以下的規則 (ip爲主機master)

# C:\Windows\System32\drivers\etc\hosts
192.168.209.140 nginx.negan.com
192.168.209.140 tomcat.negan.com

查看ingress-nginx暴露的端口

kubectl get svc -n ingress-nginx

 kubectl get svc -n ingress-nginx
NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx   NodePort   10.110.119.41   <none>        80:30994/TCP,443:30693/TCP   23m

經過瀏覽器訪問:http://nginx.negan.com:30994http://tomcat.negan.com:30994

5.四、HTTPS

1)、生成證書

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=xudaxian.com"

2)、建立密鑰

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

3)、建立ingress-https.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-https
  namespace: dev
spec:
  tls:
    - hosts:
      - nginx.xudaxian.com
      - tomcat.xudaxian.com
      secretName: tls-secret # 指定祕鑰
  rules:
  - host: nginx.negan.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-service
          servicePort: 80
  - host: tomcat.negan.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-service
          servicePort: 8080

4)、建立和查看

# 建立
kubectl create -f ingress-https.yaml

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

NAME            CLASS    HOSTS                              ADDRESS   PORTS     AGE
ingress-https   <none>   nginx.negan.com,tomcat.negan.com             80, 443   3s

# 查看詳情
Name:             ingress-https
Namespace:        dev
Address:          
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
TLS:
  tls-secret terminates nginx.negan.com,tomcat.negan.com
Rules:
  Host              Path  Backends
  ----              ----  --------
  nginx.negan.com   
                    /   nginx-service:80 (192.168.104.1:80,192.168.104.3:80,192.168.104.5:80 + 3 more...)
  tomcat.negan.com  
                    /   tomcat-service:8080 (192.168.104.4:8080,192.168.104.6:8080,192.168.166.134:8080)
Annotations:        <none>
Events:
  Type    Reason  Age   From                      Message
  ----    ------  ----  ----                      -------
  Normal  CREATE  41s   nginx-ingress-controller  Ingress dev/ingress-https

5)、訪問

經過瀏覽器訪問:https://nginx.negan.com:30693和 https://tomcat.negan.com:30693

相關文章
相關標籤/搜索