1. Ratel是什么?
Ratel是一個Kubernetes資源平台,基於管理Kubernetes的資源開發,可以管理Kubernetes的Deployment、DaemonSet、StatefulSet、Service、Ingress、Pods、Nodes、Role、ClusterRole、Rolebinding、ClusterRoleBinding、Secret、ConfigMap、PV、PVC等。主要用於以圖形化的方式管理k8s的各類資源,提高維護k8s集群的效率及降低出錯的概率。
2. 安裝Ratel
請查看最新文檔: https://github.com/dotbalo/ratel-doc
2.1 配置文件解析
Ratel默認是安裝到需要管理的k8s集群中的任意一個集群(需要和其他集群能夠通信)。所以需要現有一個集群,可以參照https://www.cnblogs.com/dukuan/p/12104600.html快速搭建一個高可用k8s 1.17.x集群。
Ratel的配置文件采用yaml格式,參數如下:
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優先級高
參數解析:
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 Secret
其中test1.config是集群test1的配置文件,test2.config是集群test2的配置文件,需要一一對應
kubectl create secret generic ratel-config --from-file=test1.config --from-file=test2.config --from-file=servers.yaml -n kube-system
2.3 部署Ratel
部署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