入門級實操教程!從概念到部署,全方位瞭解K8S Ingress!

Kubernetes Ingress用於添加規則,以將流量從外部路由到Kubernetes集羣的服務中。在本文中你將瞭解ingress 的概念,以及用於路由外部流量到Kubernetes deployment的ingress controller。nginx

 

一般狀況下,自定義Nginx或HAproxy Kubernetes部署將做爲服務被暴露,它們用於將外部流量代理到內部集羣的服務中。其中,路由規則將會bake到Pod中,並做爲configmap添加。Kubernetes ingress的行爲與此相似,只是路由規則將做爲Kubernetes ingress對象維護。它具備動態路由規則配置的巨大優點,所以無需從新部署proxy pods。
 
入門級實操教程!從概念到部署,全方位瞭解K8S Ingress!git

 

Kubernetes Ingress入門淺析

 

想要順利開始使用Kubernetes Ingress,你須要瞭解如下兩個關鍵概念:github

 

一、 Kubernetes Ingressapi

二、 Kubernetes Ingress Controller架構

 

讓咱們來逐一瞭解。app

 

Kubernetes Ingress

 

Kubernetes Ingress是一個原生的Kubernetes資源,你能夠設置規則來從外部路由流量到集羣內部的服務端點。它須要一個Ingress Controller來路由ingress對象所指定的規則。Ingress 對象以下所示:
 負載均衡

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: dev
spec:
  rules:
  - host: test.apps.example.com
    http:
      paths:
      - backend:
          serviceName: hello-service
          servicePort: 80

 
上面的聲明意味着,對test.apps.example.com的全部調用都應該hit名爲hello-service的服務,這一服務位於dev命名空間中。ide

 

關於Ingress對象,你須要瞭解的關鍵事項以下:測試

 

  1. 你應該在你所部署服務的命名空間內建立ingress規則。若是在其餘沒有ingress對象的命名空間中,你將沒法路由流量到其中的服務內。google

  2. 一個ingress對象須要一個ingress controller來路由流量

  3. 外部流量將不會hit ingress API,而是hit ingress controller服務。

 

Kubernetes Ingress Controller

 

Ingress controller是一個典型的部署在集羣中的代理服務,它只是暴露給服務的Kubernetes部署。如下是可用於Kubernetes的Ingress Controller:

 

  • Nginx Ingress Controller

  • Traefik

  • HAproxy

  • Contour

  • GKE Ingress Controller
     

目前,Nginx是大多數企業的選擇。如下是Nginx Ingress Controller的工做原理:
 

  1. 在Nginx controller pod內部的nginx.conf文件是一個go 模板,它能夠與Kubernetes Ingress API通訊並實時得到流量路由的最新值。

  2. Nginx controller與Kubernetes ingress API 通訊以檢查是否爲流量路由建立了規則。

  3. 若是它發現了任何ingress規則,它將應用到Nginx Controller配置,也就是使用go模板在pod內的nginx.conf文件。

 

若是你使用exec鏈接到pod並檢查/etc/nginx/nginx.conf文件,則能夠看到在conf文件中應用的ingress對象中指定的全部規則。
 

如下的架構圖將解釋在一個Kubernetes集羣上的ingress設置。
 

入門級實操教程!從概念到部署,全方位瞭解K8S Ingress!
 
接下來,咱們詳細看看如何使用Nginx Ingress Controller在Kubernetes中設置Ingress。
 

前期準備

 

  • 一個Kubernetes集羣

  • 安裝好的kubectl並已對Kubernetes集羣進行身份驗證

  • Kubernetes集羣的管理員訪問權限

  • 指向ingress controller負載均衡器的有效域

 

若是你在谷歌雲上,請爲你的帳戶分配管理員權限以啓用集羣角色。
 

ACCOUNT=$(gcloud info --format='value(config.account)')
kubectl create clusterrolebinding owner-cluster-admin-binding \
    --clusterrole cluster-admin \
    --user $ACCOUNT

 

請注意:本教程已在Google Cloud GKE集羣上嘗試過。理論上,它可在全部雲環境中使用。若是你真的遇到任何錯誤,則可能須要在設置中進行一些調整。
  

設置Nginx Ingress Controller

 

有兩個nginx ingress controller:

 

 

咱們將使用Kubernetes社區的nginx controller。

 

Ingress controller須要特定的命名空間、服務帳戶、集羣角色綁定、configmap等。所以,你須要使用官方ingress repo中的yaml文件來建立所提到的Kubernetes對象。
 
官方repo:

https://github.com/kubernetes/ingress-nginx/tree/master/deploy

 

讓咱們使用mandatory.yaml文件部署ingress controller,你能夠在官方repo找到它。它有nginx所需的Kubernetes對象列表。

 

讓咱們使用kubectl建立Nginx controller deployment:
 

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml

 
檢查ingress controller pod以確保它是否正確設置:
 

kubectl get pods -n ingress-nginx

 

爲Ingress Controller設置 LoadBalancer 服務

 

下一步是建立一個LoadBalancer類型的服務,以在集羣外部暴露nginx controller部署。

 

Step1:在本地建立項目目錄,而後切換到該目錄。
 

mkdir ingress-deployment && cd ingress-deployment

 
Step2:建立一個名爲nginx-ingress.yaml的文件
 

vi nginx-ingress.yaml

 
Step3:複製如下內容到文件
 

請注意:label下的annotation對於nginx controller部署集成很是重要
 

kind: Service
apiVersion: v1
metadata:
name: ingress-nginx
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
spec:
externalTrafficPolicy: Local
type: LoadBalancer
selector:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
ports:
- name: http
port: 80
targetPort: http
- name: https
port: 443
targetPort: https

 
Step4:建立ingress 服務
 

kubectl apply -f nginx-ingress.yaml

 
Step5:檢查已建立的服務是否已鏈接到外部負載均衡器
 

kubectl get svc -n ingress-nginx

 

將域名映射到Loadbalancer IP

 

爲了讓咱們的ingress的設置運轉起來,咱們須要映射一個域名到負載均衡器IP。你能夠用兩種方式,完成此操做。

 

單個DNS映射

 

你能夠將單個域做爲A record直接映射到負載均衡器IP,使用這一功能,你只能爲ingress controller提供一個域,並能夠基於多個路徑進行流量路由。

 

例如:
 

www.example.com --> Loadbalancer IP

 
您可使用此模型進行基於路徑的路由。

 

如下有幾個例子:
 

http://www.example.com/app1
http://www.example.com/app2
http://www.example.com/app1/api
http://www.example.com/app2/api

 

通配符DNS映射

 
若是你映射一個通配符DNS到負載均衡器,你就能夠經過ingress擁有動態DNS端點。

 

例如:
 

*.apps.example.com

 
這樣,你能夠經過單個ingress controller擁有多個動態子域,而且每一個DNS有本身基於路徑的路由。

 

例如:
 

#URL one

http://demo1.apps.example.com/api
http://demo1.apps.example.com/api/v1
http://demo1.apps.example.com/api/v2

#URL two

http://demo2.apps.example.com/api
http://demo2.apps.example.com/api/v1
http://demo2.apps.example.com/api/v2

 
出於演示目的,咱們已將通配符DNS映射到LoadBalancer IP。你能夠根據你的DNS提供商進行此設置。
 

設置一個Demo 應用程序

 
出於測試的目的,咱們將部署一個demo應用程序而且添加一個ClusterIP服務到應用程序上。

 

Step1:建立一個名爲dev的命名空間
 

kubectl create namespace dev

 
Step2:建立一個名爲hello-app.yaml的文件

 

Step3:複製如下內容到文件並保存
 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-app
  namespace: dev
spec:
  selector:
    matchLabels:
      app: hello
  replicas: 3
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
      - name: hello
        image: "gcr.io/google-samples/hello-app:2.0"

 
Step4:使用kubectl建立deployment
 

kubectl create -f hello-app.yaml

 
檢查deployment狀態

 

Step5:建立一個名爲hello-app-service.yaml的文件

 

Step6:複製如下內容到文件並保存
 

apiVersion: v1
kind: Service
metadata:
  name: hello-service
  namespace: dev
  labels:
    app: hello
spec:
  type: ClusterIP
  selector:
    app: hello
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP

 
Step7:使用kubectl建立服務
 

kubectl create -f hello-app-service.yaml

 
檢查服務狀態
 

kubectl get svc -n dev

 

建立Kubernetes Ingress對象

 

如今讓咱們使用一個DNS建立一個Ingress對象來訪問咱們的hello app。Ingress對象能夠設置路由規則。

 

Ingress controller pod會鏈接到Ingress API來檢查規則,而且會相應地更新其nginx.conf。

 

Step1:建立一個名爲ingress.yaml的文件

 
Step2:複製如下內容到文件並保存

 

使用你的域名替換test.apps.example.info。此處,咱們假設你已經有*.apps.example.info格式的通配符域名。
 

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: dev
spec:
  rules:
  - host: test.apps.example.info
    http:
      paths:
      - backend:
          serviceName: hello-service
          servicePort: 80

 
Step3:描述已建立的ingress對象,它用於檢查配置
 

kubectl describe ingress  -n dev

 
如今,若是你嘗試訪問test.apps.example.info域(用你的域名代替它),你應該可以訪問咱們部署的app。
 

原文連接:

https://devopscube.com/kubernetes-ingress-tutorial/

https://devopscube.com/setup-ingress-kubernetes-nginx-controller/

相關文章
相關標籤/搜索