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 來避免意外。

microk8s+okd web console

看到這篇:Running the OpenShift console in plain Kubernetes

剛好手頭上有 microk8s ,就裝來試試看。

環境

使用 Ubuntu 22.04

步驟

先安裝 nodejs 16,注意,17 以後的版本因為用了 OpenSSL3 ,所以編譯過程會有錯誤。

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs

取得 web console 原始碼

git clone https://github.com/openshift/console.git

進入 console 資料夾,進行編譯

./build.sh

再來是啟動

先檢查 default 這個 service account 有沒有 token,這個 service account 是在 kube-system 這個 namespace 裡。

kubectl get serviceaccount default --namespace=kube-system -o jsonpath='{.secrets[0].name}'

如果沒有,就建立 token

kubectl create token default -n kube-system

複製輸出結果,然後建立 secret

kubectl create secret generic default-token-0621 --from-literal=token=<前面的輸出結果>

好了以後,編輯 service account

kubectl edit secret default-token-0621 -n kube-system

在 yaml 裡加入 secrets

apiVersion: v1
kind: ServiceAccount
metadata:
  name: default
  namespace: kube-system
secrets:
- name: default-token-0621

給權限

kubectl create clusterrolebinding default-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:default

最後要啟動了,因為我是用 microk8s ,先取得 kubeconfig

microk8s config > ~/.kube/config
export KUBECONFIG=~/.kube/config
source ./contrib/environment.sh
./bin/bridge

預設會啟動在 :9000