kubernetes實戰(二十八):Kubernetes一鍵式資源管理平臺Ratel安裝及使用

1. Ratel是什麼?html

  

  Ratel是一個Kubernetes資源平臺,基於管理Kubernetes的資源開發,能夠管理Kubernetes的Deployment、DaemonSet、StatefulSet、Service、Ingress、Pods、Nodes、Role、ClusterRole、Rolebinding、ClusterRoleBinding、Secret、ConfigMap、PV、PVC等。主要用於以圖形化的方式管理k8s的各種資源,提升維護k8s集羣的效率及下降出錯的機率。node

 

2. 安裝Ratelnginx

 

2.1 配置文件解析git

  Ratel默認是安裝到須要管理的k8s集羣中的任意一個集羣(須要和其餘集羣可以通訊)。因此須要現有一個集羣,能夠參kubernetes實戰(二十六):kubeadm 安裝 高可用 k8s 1.16.x dashboard 2.x快速搭建一個高可用k8s集羣。github

 

  Ratel的配置文件採用yaml格式,參數以下:web

 servers.yaml是ratel的配置文件, 格式以下:
        - serverName: 'test1'
          serverAddress: 'https://1.1.1.1:8443'
          #serverAdminUser: 'test1'
          #serverAdminPassword: 'test1#'
          serverAdminToken: 'null'
          serverDashboardUrl: "https://k8s.test1.com.cn"
          production: 'false'
          kubeConfigPath: "/mnt/test1.config"
        其中管理的方式有兩種(Token暫不支持): 
            帳號密碼和kubeconfig形式, 只需配置一種便可, kubeconfig優先級高

  參數解析:docker

        serverName: 集羣別名
        serverAddress: Kubernetes APIServer地址
        serverAdminUser: Kubernetes管理員帳號(須要配置basic auth)
        serverAdminPassword: Kubernetes管理員密碼
        serverAdminToken: Kubernetes管理員Token // 暫不支持
        serverDashboardUrl: Kubernetes官方dashboard地址
        kubeConfigPath: Kubernetes kube.config路徑(絕對路徑)
    kubeConfigPath 經過secret掛載到容器的/mnt目錄或者其餘目錄

 

2.2 建立Ratel Secretapi

  其中test1.config是集羣test1的配置文件,test2.config是集羣test2的配置文件,須要一一對應session

kubectl create secret generic ratel-config  --from-file=test1.config --from-file=test2.config --from-file=servers.yaml -n kube-system

 

2.3 部署Ratelapp

  部署Ratel的Yaml文件以下:

        apiVersion: apps/v1
        kind: Deployment
        metadata:
          labels:
            app: ratel
          name: ratel
          namespace: kube-system
        spec:
          replicas: 1
          selector:
            matchLabels:
              app: ratel
          strategy:
            rollingUpdate:
              maxSurge: 1
              maxUnavailable: 0
            type: RollingUpdate
          template:
            metadata:
              creationTimestamp: null
              labels:
                app: ratel
            spec:
              containers:
                - command:
                    - sh
                    - -c
                    - ./ratel -c /mnt/servers.yaml
                  env:
                    - name: TZ
                      value: Asia/Shanghai
                    - name: LANG
                      value: C.UTF-8
                    - name: ProRunMode
                      value: prod
                    - name: ADMIN_USERNAME
                      value: admin
                    - name: ADMIN_PASSWORD
                      value: ratel_password
                  image: dotbalo/ratel:v0.1alpha
                  imagePullPolicy: Always
                  livenessProbe:
                    failureThreshold: 2
                    initialDelaySeconds: 10
                    periodSeconds: 60
                    successThreshold: 1
                    tcpSocket:
                      port: 8888
                    timeoutSeconds: 2
                  name: ratel
                  ports:
                    - containerPort: 8888
                      name: web
                      protocol: TCP
                  readinessProbe:
                    failureThreshold: 2
                    initialDelaySeconds: 10
                    periodSeconds: 60
                    successThreshold: 1
                    tcpSocket:
                      port: 8888
                    timeoutSeconds: 2
                  resources:
                    limits:
                      cpu: 1000m
                      memory: 520Mi
                    requests:
                      cpu: 100m
                      memory: 100Mi
                  volumeMounts:
                    - mountPath: /mnt
                      name: ratel-config
              dnsPolicy: ClusterFirst
              imagePullSecrets:
                - name: myregistrykey
              restartPolicy: Always
              schedulerName: default-scheduler
              securityContext: {}
              terminationGracePeriodSeconds: 30
              volumes:
                - name: ratel-config
                  secret:
                    defaultMode: 420
                    secretName: ratel-config 

  建立Ratel的Service和Ingress的yaml文件以下:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: ratel
  name: ratel
  namespace: kube-system
spec:
  ports:
    - name: container-1-web-1
      port: 8888
      protocol: TCP
      targetPort: 8888
  selector:
    app: ratel
  type: ClusterIP
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ratel
  namespace: kube-system
spec:
  rules:
  - host: krm.test.com
    http:
      paths:
      - backend:
          serviceName: ratel
          servicePort: 8888
        path: /

  

  配置參數說明:

        ProRunMode: 區別在於dev模式打印的是debug日誌, 其餘模式是info級別的日誌, 實際使用時應該配置爲非dev
        ADMIN_USERNAME: ratel本身的管理員帳號
        ADMIN_PASSWORD: ratel本身的管理員密碼
        實際使用時帳號密碼應知足複雜性要求,由於ratel能夠直接操做所配置的資源。
        其餘無需配置, 端口配置暫不支持。

  部署完畢後能夠經過krm.test.com進行訪問(域名按需配置)

 

2.4 訪問Ratel

  

 

  登陸後能夠查看到集羣列表

 

  點擊Cluster Name能夠查看到當前集羣部分信息

 

  單擊資源數量能夠查看資源列表,好比Node

 

  或者Deployment

  

  更多詳情查看:https://github.com/dotbalo/ratel-doc

3. 使用Ratel建立Deployment

  點擊Deployment --> 建立

 

3.1 基本信息填寫

  

   填寫說明

    •   選擇集羣: 選擇以前配置的集羣, 按serverName區分,選擇集羣后,會自動彈出Namespace的選擇框。
    •   Namespace: 當前Deployment須要建立在哪一個Namespace,Namespace的選擇框支持搜索功能。
    •   儘可能部署至不一樣宿主機: 此選項至關於添加了一個Pod的Affinity的軟策略,基於selector實現。
    •   部署至不一樣宿主機: 此選項至關於添加了一個Pod的Affinity的硬性策略策略,基於selector實現。
    •   更新策略: Deployment的更新策略,此處和DaemonSet和StatefulSet更新策略不一致。
    •   副本數: Pod的個數,可使用鼠標滾輪或者直接鍵入的方式更改。
    •   私有倉庫Secret: 用於私有倉庫鏡像下載的帳號密碼,須要提早建立,點擊刷新會自動獲取集羣的docker registry類型的secret。

 

   可選參數配置

 

   填寫說明

    •   Labels:Labels是Deployment的label標籤,當屬於Deployment名字時,會自動填寫一個Labels和Selector,能夠按需修改、添加和刪除。
    •   Selectors: Selectors是Pod的Labels和Deployment的Selector,按需修改、添加和刪除。
    •   HostAliases: HostAliases是hosts文件配置,按需修改、添加和刪除。
    •   NodeSelector: 節點選擇器,按需修改、添加和刪除。
    •   內核配置: Pod中的內核配置。
    •   Taint: 容忍配置。

 

3.2 Volume配置

  基本配置填寫完成之後,點擊Next(必須)進入到填寫Volume的視圖

 

   填寫說明

    如上圖所示,目前所支持的Volume配置有HostPath、Secret、ConfigMap、EmptyDir、PVC。
    上述的Secret、ConfigMap、PVC、StorageClass無需手動輸入,單擊刷新後便可自動自動獲取到當前集羣的相關信息。

  填寫內容以下

 

3.3 Container配置

  填寫完Volume配置之後,點擊Next填寫Container配置

 

3.3.1 Container 1配置

  

 

 

 

3.3.2 Container 2配置

 

 

 

 

   填寫說明

    •   Container配置目前幾乎支持全部常見配置,Container的名稱會在Deployment名稱鍵入後自動填寫一個默認的,能夠按需修改。
    •   能夠配置多個Container,按需添加、修改和刪除。
    •   啓動命令和啓動參數按需修改,逗號分隔,若是啓動命令或者啓動參數含有逗號,須要以變量的方式傳入,好比啓動命令是--configEnv=test1,prd
    •   此時test1,prd須要配置成變量,以後經過--configEnv=$ConfigEnv注入。
    •   集羣的CPU和內存資源按需配置,直接拖動便可。
    •   健康檢查按需配置,目前支持httpGet、tcpSocket、exec方式。
    •   preStop和postStart按需配置。
    •   高權限運行是添加privilege=true參數至Container。
    •   容器端口按需配置和添加,目前支持三種協議配置。
    •   容器環境變量,支持三種方式的容器變量配置,按需添加、修改和刪除。
      •     字符變量: key value變量,不能留空。
      •     EnvFrom: 從ConfigMap和Secret中獲取變量配置。
      •     ValueFrom: 目前支持FieldRef、ConfigMapRef、SecretRef和ResourceFieldRef配置。
    •   文件掛載配置:
      •     文件掛載配置必須先添加Volume配置,不然沒法建立文件掛載。
      •     Volume名稱自動獲取建立Volume。

 

3.4 Init Container配置

  Init Container配置和Container大體相同

 

3.5 Service配置

  建立Deployment、DaemonSet和StatefulSet的頁面,嵌入了簡單的Service和Ingress配置。
  如圖所示,在配置完Container後,若是須要添加Service(默認不添加),在開啓service配置後,會根據container的端口配置自動生成Service的配置,能夠按需修改、添加和刪除。此頁面Service僅支持ClusterIP和NodePort兩種類型。

 

3.6 Ingress配置

  如圖所示,在配置完Service後,若是須要添加Ingress(默認不添加),在開啓Ingress後,會根據Service配置默認生成一個Ingress配置,能夠按需修改、添加和刪除。
  如需開啓https,須要提早添加tls類型的域名證書,點擊刷新後便可自動讀取當前集羣的當前Namespace的tls類型的證書列表,無tls類型的證書沒法開啓https。
  去除前綴的意思是: 訪問www.test1.com/a/test.html 會自動變成www.test1.com/test.html。

 

3.7 建立資源

  上述有不規範填寫,好比名稱不能包含大寫的,Ratel會自動轉換爲小寫。建立成功頁面以下

  

 

 

 

4. 建立資源查看

  相對於手動建立,不只下降了出錯機率,同時也無需掌握yaml文件的每一個參數含義,大大提升了k8s集羣的管理效率。

 

4.1 查看建立的Deployment

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2019-11-18T10:19:57Z"
  generation: 1
  labels:
    app: nginx
  name: nginx
  namespace: default
  resourceVersion: "33645965"
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/nginx
  uid: f857546a-09ec-11ea-bf14-f80f41f365d0
spec:
  progressDeadlineSeconds: 600
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - nginx
              namespaces:
              - default
              topologyKey: kubernetes.io/hostname
            weight: 1
      containers:
      - args:
        - sleep 3600
        command:
        - sh
        - -c
        env:
        - name: TZ
          value: Asia/Shanghai
        - name: LANG
          value: C.UTF-8
        - name: testValueFrom
          valueFrom:
            resourceFieldRef:
              containerName: nginx
              divisor: "0"
              resource: requests.memory
        envFrom:
        - secretRef:
            name: default-token-hgfm8
        image: nginx
        imagePullPolicy: IfNotPresent
        lifecycle: {}
        livenessProbe:
          exec:
            command:
            - echo 0
          failureThreshold: 2
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 2
        name: nginx
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        readinessProbe:
          exec:
            command:
            - echo 0
          failureThreshold: 2
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 2
        resources:
          limits:
            cpu: 164m
            memory: 273Mi
          requests:
            cpu: 10m
            memory: 10Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /usr/share/zoneinfo/Asia/Shanghai
          name: tz-config
        - mountPath: /etc/localtime
          name: tz-config
        - mountPath: /etc/timezone
          name: timezone
        - mountPath: /mnt/testSecret
          name: testsecret
        - mountPath: /mnt/testConfigMap
          name: testconfigmap
        - mountPath: /mnt/testempty
          name: testemptydir
      - command:
        - sh
        - -c
        - sleep 3600
        env:
        - name: TZ
          value: Asia/Shanghai
        - name: LANG
          value: C.UTF-8
        - name: test
          value: a
        - name: testFieldRef
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
        envFrom:
        - configMapRef:
            name: runner-xxx-gitlab-runner
        image: nginx
        imagePullPolicy: IfNotPresent
        lifecycle: {}
        name: nginx2
        resources:
          limits:
            cpu: 48m
            memory: 52Mi
          requests:
            cpu: 10m
            memory: 10Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /usr/share/zoneinfo/Asia/Shanghai
          name: tz-config
        - mountPath: /etc/localtime
          name: tz-config
        - mountPath: /etc/timezone
          name: timezone
        - mountPath: /mnt/testConfigMap
          name: testconfigmap
        - mountPath: /mnt/testPVC
          name: testpvc
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: myregistrykey
      initContainers:
      - command:
        - sh
        - -c
        - sleep3
        env:
        - name: TZ
          value: Asia/Shanghai
        - name: LANG
          value: C.UTF-8
        image: nginx
        imagePullPolicy: Always
        name: initcontainer
        resources:
          limits:
            cpu: 100m
            memory: 100Mi
          requests:
            cpu: 10m
            memory: 10Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /usr/share/zoneinfo/Asia/Shanghai
          name: tz-config
        - mountPath: /etc/localtime
          name: tz-config
        - mountPath: /etc/timezone
          name: timezone
      nodeSelector:
        env: test
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext:
        sysctls:
        - name: net.core.somaxconn
          value: "16384"
        - name: net.ipv4.tcp_max_syn_backlog
          value: "16384"
      terminationGracePeriodSeconds: 30
      tolerations:
      - effect: NoSchedule
        key: node-role.kubernetes.io/master
        operator: Exists
      volumes:
      - hostPath:
          path: /usr/share/zoneinfo/Asia/Shanghai
          type: ""
        name: tz-config
      - hostPath:
          path: /etc/timezone
          type: ""
        name: timezone
      - name: testsecret
        secret:
          defaultMode: 420
          secretName: default-token-hgfm8
      - configMap:
          defaultMode: 420
          name: runner-xxx-gitlab-runner
        name: testconfigmap
      - emptyDir: {}
        name: testemptydir
      - name: testpvc
        persistentVolumeClaim:
          claimName: gitrunner-workspace

 

4.2 查看建立Service

[root@k8s-master01 ~]# kubectl get svc nginx -oyaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2019-11-18T10:19:56Z"
  labels:
    app: nginx
  name: nginx
  namespace: default
  resourceVersion: "33645941"
  selfLink: /api/v1/namespaces/default/services/nginx
  uid: f833a327-09ec-11ea-bf14-f80f41f365d0
spec:
  clusterIP: 50.104.173.155
  ports:
  - name: container-1-web-1
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: nginx
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

 

4.3 Ingress配置

[root@k8s-master01 ~]# kubectl get ingress nginx-strip-path -o yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  creationTimestamp: "2019-11-18T10:25:39Z"
  generation: 1
  name: nginx-strip-path
  namespace: default
  resourceVersion: "33646594"
  selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/nginx-strip-path
  uid: c42679fe-09ed-11ea-bf14-f80f41f365d0
spec:
  rules:
  - host: www.test.com
    http:
      paths:
      - backend:
          serviceName: nginx
          servicePort: 8080
        path: /test(/|$)(.*)
status:
  loadBalancer: {}

 

  Ratel項目地址:https://github.com/dotbalo/ratel-doc

相關文章
相關標籤/搜索