k8s Ingress和ingress控制器

 

ingress架構圖簡介

咱們知道service的表現形式爲IP:PORT,即工做在第四層傳輸層(TCP/IP層),那麼對於不一樣的URL地址常常對應用不一樣的後端服務或者虛擬服務器,這些應用層的轉發機制僅經過kubernetes的service機制是沒法實現的,這種狀況我麼可使用ingress策略定義和一個具體的ingress Controller,二者結合實現一個完整的Ingress 負載均衡,這個負載均衡是基於nginx七層反向代理來實現的,ingress工做原理以下圖:前端

 

外部客戶端經過訪問負載均衡器,而後調度到service上,而後在調度到IngressController,IngressController經過Ingress規則(域名或虛擬主機)訪問到後端pod,而在Ingress規則當中對應的主機是又service分組來設定的,能夠看到,這幅圖有2種service,最上面的service是用來對外提供服務的,而下面2個service僅僅是用來分pod組的node

 ingress安裝和配置

在github上下載相關yaml文件

下載mandatory.yaml文件nginx

wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.20.0/deploy/mandatory.yaml
wget  https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/provider/baremetal/service-nodeport.yaml      #對外提供服務,若是不須要能夠不下載

由於mandatory文件中默認用的是谷歌地址,你懂得,因此替換defaultbackend-amd64和nginx-ingress-controller鏡像地址,速度也會更快!git

[root@master ingress-nginx]# sed -i 's#k8s.gcr.io/defaultbackend-amd64#registry.cn-qingdao.aliyuncs.com/kubernetes_xingej/defaultbackend-amd64#g' mandatory.yaml
[root@master ingress-nginx]# sed -i 's#quay.io/kubernetes-ingress-controller/nginx-ingress-controller#registry.cn-qingdao.aliyuncs.com/kubernetes_xingej/nginx-ingress-controller#g' mandatory.yaml

若是想手動修改訪問的端口能夠添加service-nodeport文件中nodePort,若是採起隨機分配這一步能夠忽略github

[root@master ingress-nginx]# cat 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

執行service-nodeport.yaml和mandatory.yaml兩個文件web

[root@master ingress-nginx]# kubectl apply -f mandatory.yaml 
[root@master ingress-nginx]# kubectl apply -f service-nodeport.yaml

查看pod狀態後端

[root@master ingress-nginx]# kubectl get pods -A
NAMESPACE       NAME                                       READY   STATUS    RESTARTS   AGE
default nginx-7bb7cd8db5-98wvj                     1/1     Running   0          62m
ingress-nginx   default-http-backend-7fccc47f44-qcfhh      1/1     Running   0          58m
ingress-nginx   nginx-ingress-controller-d786fc9d4-w5nrc   1/1     Running   0 58m
kube-system     coredns-bccdc95cf-8sqzn                    1/1     Running   2          4d2h
kube-system     coredns-bccdc95cf-vt8nz                    1/1     Running   2          4d2h
kube-system     etcd-master                                1/1     Running   1          4d2h
kube-system     kube-apiserver-master                      1/1     Running   1          4d2h
kube-system     kube-controller-manager-master             1/1     Running   2          4d2h
kube-system     kube-flannel-ds-amd64-c97wh                1/1     Running   1          4d1h
kube-system     kube-flannel-ds-amd64-gl6wg                1/1     Running   2          4d1h
kube-system     kube-flannel-ds-amd64-npsqf                1/1     Running   1          4d1h
kube-system     kube-proxy-gwmx8                           1/1     Running   2          4d2h
kube-system     kube-proxy-phqk2                           1/1     Running   1          4d1h
kube-system     kube-proxy-qtt4b                           1/1     Running   1          4d1h
kube-system     kube-scheduler-master                      1/1     Running   2          4d2h

查看svc狀態api

[root@master ingress-nginx]# kubectl get svc -n ingress-nginx
NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
default-http-backend   ClusterIP   10.105.81.131   <none>        80/TCP                       59m
ingress-nginx          NodePort    10.105.53.207   <none>        80:30080/TCP,443:30443/TCP   58m

mandatory部署介紹

1.namespace.yaml 
建立一個獨立的命名空間 ingress-nginx

2.configmap.yaml 
ConfigMap是存儲通用的配置變量的,相似於配置文件,使用戶能夠將分佈式系統中用於不一樣模塊的環境變量統一到一個對象中管理;而它與配置文件的區別在於它是存在集羣的「環境」中的,而且支持K8S集羣中全部通用的操做調用方式。
從數據角度來看,ConfigMap的類型只是鍵值組,用於存儲被Pod或者其餘資源對象(如RC)訪問的信息。這與secret的設計理念有殊途同歸之妙,主要區別在於ConfigMap一般不用於存儲敏感信息,而只存儲簡單的文本信息。
ConfigMap能夠保存環境變量的屬性,也能夠保存配置文件。
建立pod時,對configmap進行綁定,pod內的應用能夠直接引用ConfigMap的配置。至關於configmap爲應用/運行環境封裝配置。
pod使用ConfigMap,一般用於:設置環境變量的值、設置命令行參數、建立配置文件。

3.default-backend.yaml 
若是外界訪問的域名不存在的話,則默認轉發到default-http-backend這個Service,其會直接返回404:

4.rbac.yaml 
負責Ingress的RBAC受權的控制,其建立了Ingress用到的ServiceAccount、ClusterRole、Role、RoleBinding、ClusterRoleBinding

5.with-rbac.yaml 
是Ingress的核心,用於建立ingress-controller。ingress-controller的做用是將新加入的Ingress進行轉化爲Nginx的配置

 打開瀏覽器驗證瀏覽器

 

 上面提示的404是由於後端服務尚未配置,這是OK的tomcat

建立後端服務

這裏咱們已nginx爲服務爲例,建立一個nginx和跟nginx對應的service,這裏要注意metadata.name要和後面建立的ingress中的serviceName一致,切記!

[root@master myself]# cat mypod.yaml 
apiVersion: v1
kind: Service
metadata:
  name: service-nginx
  namespace: default
spec:
  selector:
    app: mynginx
  ports:
  - name: http
    port: 80
    targetPort: 80

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mydepoy
  namespace: default
spec:
  replicas: 5 selector: matchLabels: app: mynginx
  template:
    metadata:
      labels: app: mynginx
    spec:
      containers:
      - name: mycontainer
        image: lizhaoqwe/nginx:v1
        imagePullPolicy: IfNotPresent
        ports:
        - name: nginx 
          containerPort: 80

有了前端了,也有後端了,那麼接下來就該建立ingress規則了

ingress配置

[root@master myself]# cat ingress-nginx.yaml 
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-mynginx
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: mynginx.fengzi.com
    http:
      paths:
      - path:
        backend:
          serviceName: service-nginx
          servicePort: 80

在打開瀏覽器的主機添加一條hosts記錄(mynginx.fengzi.com   192.168.254.13)而後打開瀏覽器驗證

咱們能夠去查看nginx的配置文件,去查看咱們所建立的規則有沒有注入到ingress中

#查看ingress-controller中的規則
[root@master myself]# kubectl get pods -n ingress-nginx NAME READY STATUS RESTARTS AGE default-http-backend-7fccc47f44-sgj6g 1/1 Running 0 140m nginx-ingress-controller-d786fc9d4-4vb5z 1/1 Running 0 140m
[root@master myself]# kubectl exec
-it nginx-ingress-controller-d786fc9d4-4vb5z -n ingress-nginx -- /bin/bash

www-data@nginx-ingress-controller-d786fc9d4-4vb5z:/etc/nginx$ cat nginx.conf

結果以下:

 

 咱們能夠看到nginx配置文件中已經有了咱們所定義的反代規則

 ok,成功!!!

 

咱們還能夠用ingress實現更多的服務,好比tomcat,下面代碼是給tomcat服務添加5個pod和1個service分組

[root@master ingress]# cat tomcat.yaml 
apiVersion: v1
kind: Service
metadata:
  name: tomcat
  namespace: default
spec:
  selector:
    app: tomcat
  ports:
  - name: http
    port: 8080
    targetPort: 8080
  - name: ajp
    port: 8009
    targetPort: 8009
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat
  namespace: default
spec:
  replicas: 5
  selector:
    matchLabels:
      app: tomcat
  template:
    metadata:
      labels:
        app: tomcat
    spec:
      containers:
      - name: tomcat
        image: tomcat:7-alpine
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 8080
        - name: ajp
          containerPort: 8009

 

將tomcat服務添加至ingress-nginx中

[root@master ingress]# cat ingress-tomcat.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-mytomcat
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: mytomcat.fengzi.com
    http:
      paths:
      - path:
        backend:
          serviceName: tomcat
          servicePort: 8080

在瀏覽器宿主機上添加hosts記錄(mytomcat.fengzi.com    192.168.254.13),而後打開瀏覽器驗證

 

 

 這樣咱們就能夠實現利用nginx的反向代理,對於web服務針對主機名的不一樣顯示不通的網站

基於ssl協議的訪問

建立私有證書及secret

[root@master myself]# openssl genrsa -out tls.key 2048

#這裏CN=後面要寫域名
[root@master myself]# openssl req -new -x509 -key tls.key -out tls.crt -subj /C=CN/ST=Beijing/L=Beijing/O=DevOps/CN=mytomcat.fengzi.com

#建立secret
[root@master myself]# kubectl create secret tls mytomcat-ingress-secret --cert=tls.crt --key=tls.key

查看證書

[root@master myself]# kubectl describe secret mytomcat-ingress-secret
Name:         mytomcat-ingress-secret
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  kubernetes.io/tls

Data
====
tls.crt:  1302 bytes
tls.key:  1675 bytes

將證書添加到tomcat中,執行ingress-tomcat-tls.yaml文件,ingress-tomcat-tls.yaml文件內容以下

[root@master myself]# cat ingress-tomcat-tls.yaml 
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-mytomcat-tls
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  tls: - hosts:
    - mytomcat.fengzi.com    #這裏寫域名
    secretName: mytomcat-ingress-secret #這裏寫secret證書名稱
  rules:
  - host: mytomcat.fengzi.com
    http:
      paths:
      - path:
        backend:
          serviceName: tomcat
          servicePort: 8080

驗證

 

 至此,所有結束!!!

相關文章
相關標籤/搜索