sealos 神奇功能 serverless kubernetes 之 cloud terminal

sealyun發表於2023-03-07

何為 serverless kubernetes

顧名思義,就是不需要安裝直接開啟網頁就可以直接使用的 kubernetes,是一個多租戶共享 kubernetes 的租戶模型,這樣做的好處是對於使用者的使用成本極低,而且無需安裝 kubernetes, 且天生對多租戶的隔離性做的很好。
劣勢是使用者沒有叢集級別的管理許可權,比如訪問節點,或者掛載主機目錄,共享主機埠等,其實這也不是劣勢,因為開放這些東西本身就是不安全的。

sealos 在這塊做了非常多的最佳化,直接能做到在公網這種不可信的環境中讓多租戶安全的共享一個 kubernetes 叢集,從而把 IT 成本打到骨折。

這是一個系列文章,我們從 sealos 的一些基礎功能開始介紹,本篇介紹的是 sealos 的 terminal 能力。

file

cloud terminal 使用教程

cloud terminal 是一個跑在容器內的 web terminal, terminal 中已經配置好了使用者的 .kube/config,
也包含了一些常用命令,幾乎可以用來訪問所有 sealos 已經提供的應用的 API.

tips: 目前 terminal 中不支援持久化檔案,請務必儲存好在裡面編輯的一些檔案。

訪問 API server

terminal 中已經安裝 kubectl, 也配置好使用者的 kubeconfig.

root@thpqxr0bh:~# kubectl get pod
NAME                                                             READY   STATUS    RESTARTS   AGE
acid-test-0                                                      1/1     Running   0          37m
terminal-8b66134e-5294-480f-b6c4-00243fc2488e-5cdc66697c-d5tss   1/1     Running   0          40m

檢視 .kube/config

root@thpqxr0bh:~# cat .kube/config 
apiVersion: v1
clusters:
- cluster:
    server: https://kubernetes.default.svc.cluster.local:443
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: 8b66134e-5294-480f-b6c4-00243fc2488e
    namespace: ns-8b66134e-5294-480f-b6c4-00243fc2488e
  name: 8b66134e-5294-480f-b6c4-00243fc2488e
current-context: 8b66134e-5294-480f-b6c4-00243fc2488e
kind: Config
preferences: {}
users:
- name: 8b66134e-5294-480f-b6c4-00243fc2488e
  user:
    token: eyJhbGciOiJSUzI1NiIsImtpZCI6IjItUWxyLUc3X0FiNjF...

這裡如果把 server 地址修改成 https://cloud.sealos.io:6443 就可以遠端訪問 apiserver, 如使用你本地的 terminal,
或者整合到任意的 CI/CD 系統中,只需要把檔案內容複製到客戶端的 ~/.kube/config 目錄即可

許可權控制

當你嘗試越權行為時:

root@thpqxr0bh:~# kubectl get node
Error from server (Forbidden): nodes is forbidden: 
User "system:serviceaccount:user-system:8b66134e-5294-480f-b6c4-00243fc2488e" 
cannot list resource "nodes" in API group "" at the cluster scope

使用者是沒有許可權訪問 node 的.

訪問其它資源

可以透過 terminal 訪問 資料庫例項, 或者 sealos cloud provider 提供的多叢集

root@thpqxr0bh:~# kubectl get infra
NAME    STATUS    AGE     AZ
aaa     Running   3d23h   cn-north-1b
fanux   Running   4d3h    cn-north-1b
test    Running   13d     cn-north-1b
root@thpqxr0bh:~# kubectl get cluster
NAME    AGE
aaa     3d23h
fanux   4d3h
test    13d
root@thpqxr0bh:~# kubectl get infra
NAME    STATUS    AGE     AZ
aaa     Running   3d23h   cn-north-1b
fanux   Running   4d3h    cn-north-1b
test    Running   13d     cn-north-1b

當然也可以刪除或者編輯這些資源。

除錯

terminal 還是非常好的除錯工具,因為其本質也是執行在使用者 ns 下的一個 pod,而且裡面自帶很多網路工具.

root@thpqxr0bh:~# nslookup acid-test
Server:         10.96.0.10
Address:        10.96.0.10#53

Name:   acid-test.ns-8b66134e-5294-480f-b6c4-00243fc2488e.svc.cluster.local
Address: 10.96.2.134

root@thpqxr0bh:~# ping 10.96.2.134
PING 10.96.2.134 (10.96.2.134) 56(84) bytes of data.
64 bytes from 10.96.2.134: icmp_seq=1 ttl=64 time=0.048 ms
64 bytes from 10.96.2.134: icmp_seq=2 ttl=64 time=0.069 ms

甚至可以在 terminal 中寫程式碼

root@thpqxr0bh:~# vim main.go
root@thpqxr0bh:~# go run main.go 
hello sealos!

sealos 以kubernetes為核心的雲作業系統發行版,讓雲原生簡單普及

laf 寫程式碼像寫部落格一樣簡單,什麼docker kubernetes統統不關心,我只關心寫業務!

相關文章