如何經過 JumpServer 堡壘機管控 Kubernetes 集羣

提出一個問題每每比解決一個更重要。-- 愛因斯坦

1. 跳轉原理

Jumpserver是個好東西,特別是對於線上設備的管控,基本跳轉原理以下圖所示。詳細的系統設計,能夠參考其文檔linux

http                       ssh
    [user] <---------> [jumpserver] <----------> [remote machine]

然而,隨着kubernetes的普及,愈來愈多的線上服務採用了kubernetes集羣部署。如何經過Jumpserver原理進行kubernetes集羣管控就是本文要解決的問題。git

2. K8S跳轉

kubernetes的管控原理,和管控遠程機器的原理基本相似。只是須要在集羣內部部署一個持久的POD, 針對 Jumpserver 該POD可以提供 SSHD 服務,其次該POD內部應該自帶 kubectl 工具。github

2.1 簡單的網絡架構圖

http                       ssh      /------------------------------------------------\
    [user] <---------> [jumpserver] <---------->  |    [kubectl pod] <=> [ kubernetes resource ]   |
                                                  \------------------------------------------------/

2.2 構建POD的IMAGE

按以上原理,構建中間跳轉POD的IMAGE。具體Dockerfile以下:centos

FROM sickp/centos-sshd:latest

#安裝kubectl
RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
RUN chmod +x ./kubectl
RUN mv ./kubectl /usr/local/bin/kubectl

#提供默認的ssh key
RUN usermod -p "!" root
ADD id_rsa /root/.ssh/id_rsa
ADD id_rsa.pub /root/.ssh/id_rsa.pub
RUN cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys

按此Dockerfile請提早準備好對應的ssh key。並將次IMAGE推送的本身的Docker Registry中。api

2.3 在K8S集羣中部署

有了中間POD的IMAGE,部署具體的K8S服務很簡單。具體定義文件,參考如下manifest定義:bash

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: jump
  labels:
    app: jump
spec:
  serviceName: jump
  replicas: 1
  selector:
    matchLabels:
      app: jump
  template:
    metadata:
      labels:
        app: jump
    spec:
      imagePullSecrets:
        - name: <YOUR-PULL-SECRET>
      serviceAccountName: jump
      containers:
        - name: jump
          image: <YOUR-POD-IMAGE>
          ports:
          - name: ssh
            containerPort: 22
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: jump
imagePullSecrets: 
  - name: <YOUR-PULL-SECRET>
---
apiVersion: v1
kind: Service
metadata:
  name: jump
spec:
  type: LoadBalancer
  selector:
    app: jump
  ports:
    - name: ssh
      port: 22
      targetPort: ssh
      protocol: TCP

注意替換相應的集羣參數配置。從定義文件中能夠看出,中間POD是以LoadBalancer的方式對外提供服務的。須要查出具體的外網服務IP。網絡

$: kubectl get service | grep jump
jump      LoadBalancer   [內網IP]   [外網IP]   22:30525/TCP     75d

該[外網IP]就是JumpServer鏈接的地址了。架構

2.4 配置JumpServer

JumpServer中的配置和普通的遠程主機配置基本一至,再也不贅述了。app

更多文章可直接訪問我的BLOG:GitDiG.comssh

相關文章
相關標籤/搜索