kubeaudit

網址:https://github.com/Shopify/kubeaudit

可以用來稽核 kubernetes 叢集的工具,檢查的項目有

  • 確保容器以非 root 身份執行
  • 確保容器的根目錄是唯讀
  • 移除有危險的 capability
  • 不執行 privileged
  • 陸續增加中

安裝

直接從 github release 下載二進位檔案,然後放到 /usr/local/bin 就可以了。

使用

有三種模式

  • Manifest mode
  • Local mode
  • Cluster mode

Manifest mode

Manifest mode 就是掃檔案,意思是如果你有寫好的 yaml 檔案,kubeaudit 可以直接掃 yaml 檔案

kubeaudit all -f "/path/to/manifest.yml"

掃完會輸出掃描結果。

需要修正的話,可以用 autofix 來自動修正。

kubeaudit autofix -f "/path/to/manifest.yml"

怕改爛的話,可以輸出到另外一個檔案

kubeaudit autofix -f "/path/to/manifest.yml" -o "/path/to/fixed"

Cluster mode

Cluster mode 就是掃描當前的 Kubernetes 叢集,不要加上 -f 參數就可以

kubeaudit all

Local mode

Local mode 是在你管理多座 kubernetes 叢集時,可以指定 kubeconfig 跟 context,例如

kubeaudit all --kubeconfig "/path/to/config" --context my_cluster

設定

大部份掃描工具都會提供設定,讓開發者可以決定要掃描哪些項目,kubeaudit 也有,格式是 yaml 。

enabledAuditors:
  # Auditors are enabled by default if they are not explicitly set to "false"
  apparmor: false
  asat: false
  capabilities: true
  deprecatedapis: true
  hostns: true
  image: true
  limits: true
  mounts: true
  netpols: true
  nonroot: true
  privesc: true
  privileged: true
  rootfs: true
  seccomp: true
auditors:
  capabilities:
    # add capabilities needed to the add list, so kubeaudit won't report errors
    allowAddList: ['AUDIT_WRITE', 'CHOWN']
  deprecatedapis:
    # If no versions are specified and the'deprecatedapis' auditor is enabled, WARN
    # results will be genereted for the resources defined with a deprecated API.
    currentVersion: '1.22'
    targetedVersion: '1.25'
  image:
    # If no image is specified and the 'image' auditor is enabled, WARN results
    # will be generated for containers which use an image without a tag
    image: 'myimage:mytag'
  limits:
    # If no limits are specified and the 'limits' auditor is enabled, WARN results
    # will be generated for containers which have no cpu or memory limits specified
    cpu: '750m'
    memory: '500m'
  • enabledAuditors 是要啟用的稽核項目
  • auditors 下方則是每個稽核項目的設定

總結

透過使用 kubeaudit 這個工具,可以稽核 kubernetes 叢集,避免放上有危險的設定,是很實用的工具。

microk8s使用自建的mirror registry

文件在這邊:How to work with a private registry

設定方法很簡單,設定檔案位置在 /var/snap/microk8s/current/args/certs.d/ ,每個 registry server 都有一個對應的目錄,裏面會有 hosts.toml 。例如 docker.io ,目錄就是

/var/snap/microk8s/current/args/certs.d/docker.io ,hosts.toml 的內容是
server = "https://docker.io"

[host."https://registry-1.docker.io"]
  capabilities = ["pull", "resolve"]

要改為 mirror registry,就變為

server = "https://docker.io"

[host."http://your_registry_server/v2"]
  capabilities = ["pull", "resolve"]
  override_path = true

關鍵在於 [host.””] 裡的 server 位置還有 override_path = true

修改完成以後,重新啟動 microk8s 即可。

sudo snap restart microk8s

基本設定方法就是這樣。之前在看的時候因為眼花,一直想說為什麼會有 “‘ | sudo tee -a /var/snap/microk8s/current/args/certs.d/k8s.gcr.io/hosts.toml” 這一段,後來仔細看,才知道文件裡貼的是一個指令。

以後改為這樣,就可以不用去外部拉取,加快拉取 image 速度了。

trivy-operator

Trivy 是 AquaSecurity 開發的工具,它可以掃描漏洞與有問題的設定檔,而且這工具有開源出來,真的是很佛心。

Trivy 有提供 CLI ,可以搭配 CI 作映像檔掃描,它也有提供 Operator ,可以安裝到 Kubernetes/OpenShift 裡,在佈署容器以後,他會自動的掃描,並產出漏洞報告。

安裝

安裝相當簡單,這邊使用 helm 來安裝,主要是因為這樣好管理。安裝步驟是參考這份文件:Helm – Trivy Operator

加入 helm repository

helm repo add aqua https://aquasecurity.github.io/helm-charts/
helm repo update

安裝

helm install trivy-operator aqua/trivy-operator \
  --namespace trivy-system \
  --create-namespace \
  --set="trivy.ignoreUnfixed=true" \
  --version 0.10.1

檢查

helm list -n trivy-system
kubectl get deployment -n trivy-system

應該會看到以下的輸出

$ helm list -n trivy-system
NAME                 NAMESPACE           REVISION    UPDATED                                 STATUS      CHART                       APP VERSION
trivy-operator   trivy-system    1           2021-01-27 20:09:53.158961 +0100 CET    deployed    trivy-operator-0.10.1   0.10.1

$ kubectl get deployment -n trivy-system
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
trivy-operator   1/1     1            1           11m

使用

安裝完成以後,Trivy 會自動開始對所有 deployment/daemonset/pod 等的 image 去做掃描。

可以用以下指令去看掃描報告的列表,報告有兩種,一種是安全漏洞的 (vulnerabilityreports) ,一種是稽核的 (configauditreports)

kubectl get vulnerabilityreports -A -o wide
kubectl get configauditreports -A -o wide

後續佈署上去的,也會自動掃描。

要看報告內容,可以用 get/describe 來察看

kubectl get vulnerabilityreport replicaset-nginx-5fbc65fff-nginx -o yaml
kubectl describe configauditreport replicaset-nginx-5fbc65fff

報告預設是 24 小時後刪除,然後重新掃描,如果要調整,可以修改 trivy-operator 這個 deployment 裡的 OPERATOR_SCANNER_REPORT_TTL 環境變數。

後續

後續延伸的議題是 trivy 的掃描依據是什麼呢?若在封閉的環境下,能否使用?若可以使用,又該如何更新呢?

結論

感謝 AquaSecurity 提供的這個工具,讓容器平台能更安全。

kubectl scale

看完這篇 Using Kubectl Scale 所整理的。

基本使用方式

kubectl scale --replicas=3 deployment/nginx-hello

有條件的 scale,可以指定只有在已經有 n 個 pod 的情況下,才去 scale,例如

kubectl scale --current-replicas=3 --replicas=5 deployment/demo-deployment

這就是說,只有在目前已經有 3 個 pod 的情況下,才做 scale。這可以避免在資源已經不足的情況下還去做 scale,讓整個 k8s 叢集更加不穩定。

對多個資源做 scale

kubectl scale --replicas=5 deployment/app deployment/database

對 default 這個 namespace 裡所有可以 scale 的資源去做 scale

kubectl scale --all --replicas=5 --namespace=default deployment

也可以用 selector 去篩選,例如針對所有 app-name=demo-app 的 deployment 去做 scale

kubectl scale --replicas=5 --selector=app-name=demo-app deployment

要做 scale 還有其他方法:

  1. 更動 yaml 裡的 spec.replicas
  2. 使用 HPA (Horizontal Pod Autoscaling)

最佳實踐

  • 避免經常性的做 scale
  • scale 到 0 的時候,表示應用程式會停止服務
  • 確定選到正確的資源做 scale
  • 使用 --current-replicas 來避免意外。

yq

jq 這個工具可以用查詢語法去查詢 json 檔案裡的指定內容,很方便。基本上 yaml 可以直接轉換為 json ,因此若先把 yaml 轉換為 json,再用 jq 查詢也是可以的。

只是多了一道轉換,是稍嫌麻煩一點,那麼 yaml 檔案是不是也有類似的工具呢?

答案是有的,我找到兩個實作,一個是使用 python 實作,一個是使用 go 實作。

查詢語法跟 jq 很相似,這部份可以參考 jq https://stedolan.github.io/jq/

microk8s升級

使用過 OpenShift 跟 Kubernetes ,對於升級這件事情,就會比較小心。關於升級這件事情,可以參考 Kubernetes 升級 FAQ

microk8s 這部份蠻簡單的,官方的文件很清楚:Upgrading MicroK8s

單個節點

用 snap 更新就可以

sudo snap refresh microk8s --channel=1.21/stable

多個節點

Kubernetes 本身允許某些節點的版本較低 (service skew),這邊就一個節點、一個節點處理。

文件沒有提到順序,我的想法是先升級 master 節點,再升級 worker 節點,步驟都一樣。

先把 pod 都趕出來

microk8s kubectl drain <node> --ignore-daemonsets

用以下指令確認除了 daemonset 以外的 pod 都已經被終止。

microk8s kubectl get po -A -o wide

然後更新

sudo snap refresh microk8s --channel=1.21/stable

再用以下指令去確認 node 都升級完成

microk8s.kubectl get node

最後再將 node 設置為可以調度

microk8s kubectl uncordon <node>

這邊有篇實例可以參考:https://microk8s.io/docs/upgrade-cluster

git-delta

git-delta 是帶有語法高亮的比對工具。

你知道的,git 預設是使用 diff,diff 沒有語法高亮,而且在比對的顯示需要一點時間習慣。

git-delta :delta

安裝

安裝蠻簡單的,提供各種作業系統的安裝方法:Installation – delta

以 Debian/Ubuntu 來說,從下載網頁下載 debian package以後,用 dpkg -i 安裝就可以。

wget https://github.com/dandavison/delta/releases/download/0.15.1/git-delta_0.15.1_amd64.deb
sudo dpkg -i git-delta_0.15.1_amd64.deb

設定

編輯 ~/.gitconfig ,加上以下設定

[core]
    pager = delta

[interactive]
    diffFilter = delta --color-only --features=interactive

[delta]
    features = decorations

[delta "interactive"]
    keep-plus-minus-markers = false

[delta "decorations"]
    commit-decoration-style = blue ol
    commit-style = raw
    file-style = omit
    hunk-header-decoration-style = blue box
    hunk-header-file-style = red
    hunk-header-line-number-style = "#067a00"
    hunk-header-style = file line-number syntax

使用

設定加好以後,在使用以下 git 指令時,就會看到比對了

  • git diff
  • git show
  • git log -p
  • git stash show -p
  • git reflog -p
  • git add -p

也可以直接拿來替代 diff 使用

delta a.txt b.txt

其他還有很多設定可以調整,可以依照自己的需求來調整:Usage – delta

結語

這種可以提升開發速度的工具,要列到建立開發環境的步驟裡,這樣在開發上可以提升效率。

Windows Server 空間清理

用 Microsoft Windows Server 提供的工具比較保險,查到有兩種方式,一種是需要安裝桌面體驗,一種是下指令。

安裝桌面體驗會要求額外安裝其他的元件,但空間就已經不太夠了,就不選擇這個方案。

下指令的方式是使用 dism 指令,先用以下指令分析

dism.exe /online /Cleanup-Image /AnalyzeComponentStore

分析完畢會有狀態報告,接下來就可以進行清理

dism.exe /online /Cleanup-Image /StartComponentCleanup

看起來是有排程進行清理,所以應該也不需要特別使用這指令。

參考資料

s3fs

Ubuntu 可以用 apt 安裝,安裝以後,不需要 root 權限就可以掛載。可以用這個掛載 AWS S3, minio 這類有相同 API 的 object storage。

首先把 ACCESS_KEY_ID 跟 SECRET_ACCESS_KEY 放到 ~/.passwd-s3fs 或 /etc/passwd-s3fs 檔案裡。

<ACCESS_KEY_ID>:<SECRET_ACCESS_KEY>

掛載時就用

s3fs <bucket_name> /mnt -o passwd_file=/home/<user>/.passwd-s3fs,use_path_request_style,url=<minio_url>

使用上蠻簡單的。

參考資料

vim基本款設定與常用指令、按鍵整理

去客戶那邊的時候,vim 設定一定都要重來過的,如何在最快的時間設定成自己想要的樣子?

目前自己的基本設定

set et
set ts=2
set sw=2
set autoindent
set nu
colorscheme desert

這樣就足夠了,太多也記不住。

再來是常用指令

指令說明
:set wrap超過螢幕寬度就換行
:set nowrap超過螢幕寬度不換行
/\ccopyright
:set ignorecase
搜尋不分大小寫,可參閱 :help ignorecase 或 :help smartcase
:set nu顯示行號
:set nu!隱藏行號
:tabedit filename在新頁籤開啟指定檔案
:tabnew開新頁籤
:ls列出所有開啟的檔案 (buffer)
:b1切到第一個檔案 (buffer)
:set paste設定為貼上,不要管 auto indent
:set nopaste不要設定為貼上,這樣若有 autoindent 時,會自動縮排
:wq最重要的指令,存檔然後離開。若加上 ! 表示強制存檔後離開。
:set mouse= 或 :set mouse=r 或 :set mouse=v 設定不要為 a 就可以使用標準系統的選取、複製模式

常用按鍵

Mode按鍵說明
Normalctrl+w s分割畫面-橫切
Normalctrl+w v分割畫面-直切
Normalctrl+w c關閉當前窗格
Normalctrl+w j / ctrl+w k / ctrl+w h / ctrl+w l移到下面/上面/左邊/右邊的窗格
Normalgt到下個頁籤
NormalgT到上個頁籤
Insertctrl+x ctrl+p表示往前找有輸入過的來作自動完成
Insertctrl+x ctrl+f自動完成檔名

參考資料