kubernetes 部署ingress

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

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

用kubernetes Ingress  是因爲它是7層調度,能夠直接卸載https會話,代理的後端的pod能夠直接使用明文的http協議。git

而Service NodePort得類型,是4層得調度,作不到這點,然而如今https是一種趨勢,因此在kubernetes 對外暴露服務得時候咱們仍是要選擇Ingress。github

下面咱們來看下Ingress得部署:vim

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

建立後端代理得pod得yaml文件,以下:api

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,bash

部署Ingress Controllerapp

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

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部署完成,哪裏有不對的地方但願各位朋友指出,你們相互學習!

相關文章
相關標籤/搜索