kubernetes 部署ingress


kubernetes Ingess 是有2部分組成,Ingress Controller 和Ingress服務組成,常用的Ingress Controller 是ingress-nginx,工作的原理是:

Ingress Controller 會動態感知集群中的Ingress的規則變化,然后讀取,動態生成Nginx的配置文件,最后注入到運行nginx的pod的中,然后會自動reload,配置生效。

用kubernetes Ingress  是由於它是7層調度,可以直接卸載https會話,代理的后端的pod可以直接使用明文的http協議。

而Service NodePort得類型,是4層得調度,做不到這點,然而現在https是一種趨勢,所以在kubernetes 對外暴露服務得時候我們還是要選擇Ingress。

下面我們來看下Ingress得部署:

首先創建一個文件夾專門放置Igress得yaml得文件,mkdir ingress

創建后端代理得pod得yaml文件,如下:

apiVersion: v1
kind: Service
metadata:
  name: myapp-ding
  namespace: default
spec:
  selector:
    app: myapp
    release: ding
  ports:
    - name: http
      port: 80
      targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-ding
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
      release: ding
  template:
    metadata:
      labels:
        app: myapp
        release: ding
    spec:
      containers:
        - name: myapp-ding
          image: ikubernetes/myapp:v2
          ports:
            - name: http
              containerPort: 80

pod得yaml文件一定要有Service,

部署Ingress Controller

在kubernetes 得github上下載ingress得yaml文件,地址:https://github.com/kubernetes/ingress-nginx/tree/master/deploy

ingress得所需得文件:configmap.yaml ,namespace.yaml,rbac.yaml,tcp-services-configmap.yaml,with-rbac.yaml  

同時在ingress得官方文檔中介紹到,需要下載service-nodeport.yaml文件,這個文件得目的是為Ingress Controller 接入外部得流量,如果沒有這個文件,是無法通過

外部訪問得。這個文件其實就是為Ingress Controller 創建一個NodePort 類型得Service,這里我稍微修改了下service-nodeport.yaml,如下:

apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  type: NodePort
  ports:
    - name: http
      port: 80
      targetPort: 80
      protocol: TCP
      nodePort: 30080   添加了這行,固定下外部訪問的端口
    - name: https
      port: 443
      targetPort: 443
      protocol: TCP
      nodePort: 30443   添加了這行
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

 

開始安裝,應用namespace.yaml   kubectl apply -f namespace.yaml

其他得yaml為文件可以一起應用   cd  ingress ,kubectl apply -f .     應用所有的文件

Ingress Controller 部署部署好了,現在要寫ingress的規則,注入到ingress-nginx pod的配置文件中

vim ingress-myapp.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-ding
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"   這里是說明ingress的類型使用的nginx,一定要說明這點,否則ingress Controller 不知道是配置成那種類型的配置文件
spec:
  rules:
  - host: test.ding.com     使用的是虛擬主機的來訪問的
    http:
      paths:
      - path:
        backend:
          serviceName: myapp-ding  代理的后端的pod的service,通過這個service來生成nginx的upstrm 
          servicePort: 80   

kubectl apply -f ingress-myapp.yaml

 

訪問的客戶端的機器配置下域名解析

現在我們可以通過test.ding.com:30080來訪問到后端代理的pod了

 

這里是使用http訪問的,如果要用https,首先我們要創建一個證書,步驟如下

[root@master ingrss]# openssl genrsa -out tls.key 2048
Generating RSA private key, 2048 bit long modulus
.........................................................................................................................................................................................................................................................+++
..............................................................+++
e is 65537 (0x10001)
[root@master ingrss]# openssl req -new -x509 -key tls.key -out tls.crt
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Hefei
Locality Name (eg, city) [Default City]:Hefei
Organization Name (eg, company) [Default Company Ltd]:test
Organizational Unit Name (eg, section) []:test
Common Name (eg, your name or your server's hostname) []:test.ding.com
Email Address []:

證書生成好了,然后把證書轉成secret,

kubectl create secret tls ding-ingress-secret --cert=tls.crt --key=tls.key

修改下 ingress-myapp.yaml 加入剛剛添加的secret,修改后的文件如下:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-ding
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  tls:    添加了tls這一段
  - hosts:
    - test.ding.com
    secretName: ding-ingress-secret  這里結束
  rules:
  - host: test.ding.com
    http:
      paths:
      - path:
        backend:
          serviceName: myapp-ding
          servicePort: 80

現在我們可以通過https訪問了

總結下,部署ingress,首先要部署下后端代理的pod,這組pod必須要有service,service的作用是用於ingress規則代理到后端pod的,通俗點就是這個service僅僅是給這組pod分組的,沒有其他的左右。接着部署Ingress Controller,最后是寫ingress的規則,讓Ingress Controller 發現注入到ingress-nginx的pod中生成配置文件

最后補一張ingress-nginx pod里nignx的配置文件的圖:

kubectl exec -n ingress-nginx -it nginx-ingress-controller-6dc8769b5-zljbw -- /bin/bash

好了,ingress部署完成,哪里有不對的地方希望各位朋友指出,大家相互學習!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM