1. 程式人生 > >Kubernetes部署(九):CoreDNS、Dashboard、Ingress部署

Kubernetes部署(九):CoreDNS、Dashboard、Ingress部署

建立CoreDNS

  • kubernetes內部的服務發現以及pod之間的域名解析服務都是通過dns來實現,所以DNS對kubernets叢集來說非常重要。目前dns有2種,一種是kube dns,一種是core dns,本次我們安裝的是Coredns。

所有的軟體及配置檔案都儲存在前面文章提到的百度網盤裡 :百度共享連結在此文章裡

[[email protected] k8s]# kubectl create -f coredns/coredns.yaml 
serviceaccount/coredns created
clusterrole.rbac.authorization.k8s.io/system:coredns created
clusterrolebinding.rbac.authorization.k8s.io/system:coredns created
configmap/coredns created
deployment.extensions/coredns created
service/coredns created

[
[email protected]
yaml]# kubectl get pod -n kube-system NAME READY STATUS RESTARTS AGE coredns-5f94b495b5-58t47 1/1 Running 0 6m coredns-5f94b495b5-wvcsg 1/1 Running 0 6m

然後我們就可以隨便進入一個pod,去ping域名看dns是否可以正常解析。

[[email protected]
yaml]# kubectl get pod NAME READY STATUS RESTARTS AGE tomcat-7666b9764-mfgpb 1/1 Running 0 11h [[email protected] yaml]# kubectl exec -it tomcat-7666b9764-mfgpb -- /bin/sh # ping baidu.com PING baidu.com (220.181.57.216) 56(84) bytes of data. 64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=1 ttl=54 time=37.2 ms 64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=2 ttl=54 time=37.0 ms 64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=3 ttl=54 time=36.6 ms 64 bytes from 220.181.57.216 (220.181.57.216): icmp_seq=4 ttl=54 time=37.9 ms ^C --- baidu.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3000ms rtt min/avg/max/mdev = 36.629/37.230/37.958/0.498 ms

建立Dashboard

[[email protected] yaml]# kubectl create -f dashboard/

[[email protected] yaml]# kubectl cluster-info
Kubernetes master is running at https://10.31.90.200:6443
CoreDNS is running at https://10.31.90.200:6443/api/v1/namespaces/kube-system/services/coredns:dns/proxy
kubernetes-dashboard is running at https://10.31.90.200:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

訪問Dashboard

https://10.31.90.200:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy

使用者名稱:admin 密碼:admin 選擇令牌模式登入。

獲取Token

kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')

操作完以上步驟就可以看到dashboard了。
Kubernetes部署(九):CoreDNS、Dashboard、Ingress部署

Ingress部署

  • Kubernetes中,Service資源和Pod資源的IP地址僅能用於叢集網路內部的通訊,所有的網路流量都無法穿透邊界路由器(Edge Router)以實 現叢集內外通訊。儘管可以為Service使用NodePort或LoadBalancer型別通過節點引入外部流量,但它依然是4層流量轉發,可用的負載均衡器也 為傳輸層負載均衡機制。

  • Ingress是Kubernetes API的標準資源型別之一,它其實就是一組基於DNS名稱(host)或URL路徑把請求轉發至指定的Service資源的規則, 用於將叢集外部的請求流量轉發至叢集內部完成服務釋出。然而,Ingress資源自身並不能進行“流量穿透”,它僅是一組路由規則的集合,這些 規則要想真正發揮作用還需要其他功能的輔助,如監聽某套接字,然後根據這些規則的匹配機制路由請求流量。這種能夠為Ingress資源監聽套 接字並轉發流量的元件稱為Ingress控制器(Ingress Controller)。
    Kubernetes部署(九):CoreDNS、Dashboard、Ingress部署
  • Ingress控制器可以由任何具有反向代理(HTTP/HTTPS)功能的服務程式實現,如Nginx、Envoy、HAProxy、Vulcand和Traefik等。Ingress 控制器自身也是運行於叢集中的Pod資源物件,它與被代理的執行為Pod資源的應用運行於同一網路中,如上圖中ingress-nginx與pod1、pod3等 的關係所示。
  • 另一方面,使用Ingress資源進行流量分發時,Ingress控制器可基於某Ingress資源定義的規則將客戶端的請求流量直接轉發至與Service對應 的後端Pod資源之上,這種轉發機制會繞過Service資源,從而省去了由kube-proxy實現的埠代理開銷。如上圖所示,Ingress規則需要由一個 Service資源物件輔助識別相關的所有Pod物件,但ingress-nginx控制器可經由api.ilinux.io規則的定義直接將請求流量排程至pod3或pod4,而無須 經由Service物件API的再次轉發,WAP相關規則的作用方式與此類同。
  • 首先需要說明的是此次我們部署的是v0.21.0版本的ingress,在最新的v0.21.0版本歷沒有了default backend。
    Kubernetes部署(九):CoreDNS、Dashboard、Ingress部署

建立Ingress Controller

可以下載官方的mandatory.yaml到本地安裝

[[email protected] ingress]# kubectl create -f mandatory.yaml 

或者

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

由於官方的ingress也只是一個pod並沒有對外暴露IP和埠,所以我們需要為ingress建立一個對外暴露的service,暴露nodePort 20080和20443埠。對於想部署在生產環境的,可以單獨拿2個node伺服器來單獨部署ingress controller,然後暴露80和443埠就可以了。

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
      nodePort: 20080
      port: 80
      targetPort: 80
      protocol: TCP
    - name: https
      nodePort: 20443
      port: 443
      targetPort: 443
      protocol: TCP
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx

然後在haproxy的backend中加入3個node 的20080和20443埠,不明白我的網路架構的請看這裡,然後將www.cnlinux.clubA記錄解析到10.31.90.200

listen ingress-80
        bind 10.31.90.200:80
        mode tcp
        balance roundrobin
        timeout server 15s
        timeout connect 15s

        server apiserver01 10.31.90.204:20080 check port 20080 inter 5000 fall 5
        server apiserver02 10.31.90.205:20080 check port 20080 inter 5000 fall 5
        server apiserver03 10.31.90.206:20080 check port 20080 inter 5000 fall 5

listen ingress-443
        bind 10.31.90.200:443
        mode tcp
        balance roundrobin
        timeout server 15s
        timeout connect 15s

        server apiserver01 10.31.90.204:20443 check port 20443 inter 5000 fall 5
        server apiserver02 10.31.90.205:20443 check port 20443 inter 5000 fall 5
        server apiserver03 10.31.90.206:20443 check port 20443 inter 5000 fall 5

建立測試的tomcat demo

[[email protected] yaml]# kubectl create -f tomcat-demo.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat
  labels:
    app: tomcat
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tomcat
  template: 
    metadata:
      labels:
        app: tomcat
    spec:
      containers:
      - name: tomcat
        image: tomcat:latest
        ports:
        - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: tomcat
spec:
  selector:
    app: tomcat
  ports:
  - name: tomcat
    protocol: TCP
    port: 8080
    targetPort: 8080
  type: ClusterIP 

建立ingress

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - host: www.cnlinux.club
      http:
        paths:
          - path: 
            backend:
              serviceName: tomcat
              servicePort: 8080

至此ingress就已建立完成。在瀏覽器開啟www.cnlinux.club 就可以看到tomcat的頁面。
Kubernetes部署(九):CoreDNS、Dashboard、Ingress部署

後續會陸續更新所有的k8s相關文件,如果你覺得我寫的不錯,希望大家多多關注點贊,非常感謝!