Kubernetes Ingress API 對象

Ingress 它是什麼html

如何暴露您Kubernetes集羣內部 "應用服務" 並向外(互聯網)提供訪問服務!!!前端

  • 一般狀況下集羣內部Service和Pod僅可在集羣內部網絡中經過IP地址訪問。全部到達邊界路由器的流量或被丟棄或被轉發到其它地方。(Ingress 的存在便是完成以上之目的)
  • 不直接使用Ingress資源,也可有多種方法暴露Service。
    • 使用 Service.Type=LoadBalancer
    • 使用 Service.Type=NodePort
    • 有幾個是廢棄的
  • 未定義Ingress的狀況下,外部請求訪問內部服務時可能這樣
Internet ---------> Service
  • 定義Ingress受權請求服務入站鏈接到達集羣的規訪問則可能這樣
Internet ------> Ingress ------> Service

Ingress 它能作什麼node

  • 能夠將 Ingress 配置爲提供服務外部可訪問的 URL、負載均衡流量、終止 SSL / TLS 並提供基於名稱的虛擬主機。Ingress 控制器一般負責經過負載均衡器來實現 Ingress,儘管它也能夠配置邊緣路由器或其餘前端來幫助處理流量。
  • Ingress 不會公開任意端口或協議。 將 HTTP 和 HTTPS 之外的服務公開到 Internet 時,一般使用 Service.Type=NodePort 或者 Service.Type=LoadBalancer 類型的服務,典型的訪問方式是 "HTTP"。
  • 先決條件:
    • 您必須具備 ingress 控制器才能知足 Ingress 的要求。僅建立 Ingress 資源無效,好比ingress-nginx。
    • 必定要檢查一下控制器的 beta 限制。 在 GCE/GKE 以外的環境中,須要將控制器部署 爲 Pod。
  • 默認後段:
    • 若是沒有主機或路徑與 Ingress 對象中的 HTTP 請求匹配,則流量將路由到您的默認後端。
    • 默認後端一般是 Ingress 控制器的配置選項,而且未在 Ingress 資源中指定。
    • 更多更詳細的Ingress描述及使用方法 參考中文官方 Ingress 文檔

Ingress 控制器nginx

  • 想讓Ingress資源工做您單獨部署個Ingress沒有用,您得部署一個Ingress Controller來實現Ingress。
  • 與做爲 kube-controller-manager 可執行文件的一部分運行的其餘類型的控制器不一樣,Ingress 控制器不是隨集羣自動啓動的。 基於此頁面,您可選擇最適合您的集羣的 Ingress 控制器實現。
  • Kubernetes 做爲一個項目,目前支持和維護 GCE 和 nginx 控制器。
  • 您能夠在集羣中部署和使用多個 Ingress 控制器,建立Ingress時使用 "ingress.class" 進行註釋。
  • 更多更詳細的控制器使用可 參考中文官方 Ingress controller 描述

多個Ingress控制器並存的示例
多個控制器切換使用,修改紅色字體便可git

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  tls:
  - secretName: tls-secret
  backend:
    serviceName: echoheaders-https
    servicePort: 80

haproxy-ingress參考 haproxy-ingress in github github

在使用kubeadm工具部署的集羣下部署Ingress-nginx後端

  • Github站點:
https://github.com/kubernetes/ingress-nginx
https://github.com/kubernetes/ingress-nginx/blob/master/docs/deploy/index.md
  • Kubernetes站點:
https://kubernetes.github.io/ingress-nginx/
https://kubernetes.github.io/ingress-nginx/deploy/
  • 命令幫助
    rules:規則的對象列表,誰調度到誰那裏去(路徑調度、主機調度)
    backend:調度到後端相關pod資源,關聯後段Pod,serviceName、servicePort
[root@node1 ~]# kubectl explain ingress.spec
[root@node1 ~]# kubectl explain ingress.spec.rules
  • 下載安裝nginx-ingress-controller
    需提早下載鏡像到本地,或者改用阿里雲鏡像
[root@node1 ingress]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.27.1/deploy/static/mandatory.yaml
[root@node1 ingress]# kubectl apply -f mandatory.yaml
[root@node1 ingress]# kubectl get pods -n ingress-nginx
NAME                                       READY   STATUS    RESTARTS   AGE
nginx-ingress-controller-948ffd8cc-9nd4c   1/1     Running   0          10m
[root@node1 ingress]# 
[root@node1 ingress]# kubectl describe pods -n ingress-nginx
  • 建立一個Pod類型爲NodePort的service用來接入互聯網請求
    修改yaml文件可添加nodePort來指定端口,這裏採用默認
[root@node1 ingress]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.27.1/deploy/static/provider/baremetal/service-nodeport.yaml
[root@node1 ingress]# kubectl apply -f service-nodeport.yaml 
service/ingress-nginx created
[root@node1 ingress]# 
[root@node1 ingress]# kubectl get svc -n ingress-nginx
NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx   NodePort   10.99.146.223   <none>        80:32116/TCP,443:30771/TCP   17s
[root@node1 ingress]#
  • 經過互聯網測試訪問,看nginx的調度器是否配置成功
    任意集羣地址訪問
[root@node1 ingress]# curl 172.12.0.10:32116
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.17.7</center>
</body>
</html>
[root@node1 ingress]#

Nginx調度器自己調度已經能夠正常工做了!!!
Nginx調度器自己調度已經能夠正常工做了!!!
Nginx調度器自己調度已經能夠正常工做了!!!
接下來就能夠手動部署個應用,並經過nginx的調度器發佈出去api

手動部署tomcat應用服務,經過nginx調度器發佈出去tomcat

  • NodePort類型的Tomcat建立
    把資源都寫在一個yaml文件內,使用三條橫線進行分割
[root@node1 pod]# cat tomcat-nodeport.yaml 
apiVersion: v1
kind: Service
metadata:
  name: tomcat
  namespace: default
spec:
  selector: 
    app: tomcat
    release: canary
  type: NodePort
  ports:
  - port: 8080
    targetPort: 8080
    nodePort: 30080
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-demo
  namespace: default
spec:
  replicas: 2
  selector: 
    matchLabels:
      app: tomcat
      release: canary
  template:
    metadata:
      labels:
        app: tomcat
        release: canary
    spec:
      containers:
      - name: tomcat
        image: tomcat
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 8080
        - name: ajp
          containerPort: 8009
[root@node1 pod]#

直接使用集羣內部任意IP地址加30080端口進行訪問便可。網絡

  • 經過Ingress形式發佈Tomcat至外部
    提早下載好tomcat鏡像到本地,service資源和deploy資源寫在一個yaml文件內
[root@node1 pod]# cat deploy-svc-tomcat.yaml 
apiVersion: v1
kind: Service
metadata:
  name: tomcat
spec:
  selector:
    app: tomcat
    release: canary
  ports:
  - name: http
    targetPort: 8080
    port: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-demo
spec:
  replicas: 2
  selector: 
    matchLabels:
      app: tomcat
      release: canary
  template:
    metadata:
      labels:
        app: tomcat
        release: canary
    spec:
      containers:
      - name: tomcat8
        image: tomcat
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 8080
[root@node1 pod]#

建立完成後,接下來就能夠爲剛剛部署的tomcat製做一個ingress服務了

  • 爲ingress類型的tomcat建立ingress服務
    您也能夠建立帶ssl認證證書類型的tomcat服務,只須要購買ssl證書或只爲了測試自建證書也可
[root@node1 pod]# cat tomcat-ingress.yaml 
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-tomcat
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: tomcat.siyou.com
    http:
      paths:
      - path:
        backend:
          serviceName: tomcat
          servicePort: 8080
[root@node1 pod]#

測試吧。配置好"tomcat.siyou.com"的解析,訪問的時候經過ingress調度器映射出去的端口訪問便可

  • 爲了測試會用到的相關命令
    kubectl 的相關資源使用幫助 # kubectl explain pods or kubectl explain pods.spec
# 獲取默認空間下的pods資源詳情
[root@node1 pod]# kubectl get pods -o wide
# 描述默認空間下某個pods資源
[root@node1 pod]# kubectl describe pods tomcat-demo-655c78c49-ctd66
# 查看某個tomcat的日誌,實時查看
[root@node1 pod]# kubectl logs tomcat-demo-655c78c49-ctd66 -f
# 查看默認空間下tomcat應用端口監聽狀態
[root@node1 pod]# kubectl exec tomcat-demo-655c78c49-ctd66 -- ss -tnl
# 進入到一個tomcat內部
[root@node1 pod]# kubectl exec -it tomcat-demo-655c78c49-ctd66 -- /bin/sh
# 查看默認名稱空間下的service詳情
[root@node1 pod]# kubectl get svc -o wide
# 獲取名稱空間爲ingress-nginx下的pod資源狀況
[root@node1 pod]# kubectl get pods -n ingress-nginx
# 查看ingress-nginx空間下pod詳情
[root@node1 pod]# kubectl describe pods -n ingress-nginx nginx-ingress-controller-948ffd8cc-9nd4c
# 查看默認名稱空間下的ingress
[root@node1 pod]# kubectl get ingress

更多 kubectl 命令的使用,"kubectl --help" 或者官方文檔 k8s kubectl overview

相關文章
相關標籤/搜索