K8s:使用 kubectl 插件 kube-score 對YAML資源文件進行合規(guī)分析


kube-score 用于 k8s 中 YAML 資源文件合規(guī)分析

它會輸出是一個建議列表,其中列出了可以改進的內(nèi)容

博文內(nèi)容涉及:

下載安裝、使用 kube-score

靜態(tài) YAML 文件分析

生成 YAML 文件分析

現(xiàn)有集群導出YAML文件的分析

理解不足小伙伴幫忙指正

靡不有初,鮮克有終?!洞笱拧な帯?/p>


對于初學者來講 ,在集群中定義了一些 API 資源對象,雖然對象可以正常創(chuàng)建并且使用,但是確認定義的 API 對象是最優(yōu)的,最合適。需要對每個對象有深入的了解,長期使用 k8s,否則這是一件不容易做到的事。


這里和小伙伴們分享的 kubectl 插件 kube-socre 即可以替我們完成這樣一件事。kube-score 是一個對 Kubernetes 對象定義執(zhí)行靜態(tài)代碼分析的工具。它會輸出 可以改進的建議列表,以使應用程序更安全、更有彈性。



下載安裝

如果你可以科學上網(wǎng),并且安裝了 krew , 可以通過下面的方式


kubectl krew install score

否則,可以通過下面的鏈接下載 二進制包,離線安裝


┌──[root@vms81.liruilongs.github.io]-[~/ansible/krew]

└─$wget https://github.com/zegl/kube-score/releases/download/v1.16.1/kube-score_1.16.1_linux_amd64.tar.gz

解壓之后,配置為 kubectl 插件。


┌──[root@vms81.liruilongs.github.io]-[~/ansible/krew]

└─$tar -zxvf kube-score_1.16.1_linux_amd64.tar.gz

LICENSE

kube-score

┌──[root@vms81.liruilongs.github.io]-[~/ansible/krew]

└─$mv kube-score kubectl-score

┌──[root@vms81.liruilongs.github.io]-[~/ansible/krew]

└─$mv kubectl-score /usr/local/bin/

做簡單測試,查看版本信息


┌──[root@vms81.liruilongs.github.io]-[~/ansible/krew]

└─$kubectl score version

kube-score version: 1.16.1, commit: 74e551f58e9009e70012773a68dcd6424cf6379d, built: 2022-12-16T12:54:40Z

使用 kube-score

靜態(tài) YAML 文件分析

分析指定目錄的 YAML 資源文件,并給出建議


kube-score score my-app/*.yaml

kube-score score my-app/deployment.yaml my-app/service.yaml

┌──[root@vms81.liruilongs.github.io]-[~/ansible]

└─$kubectl score score k8s-daemonset-create/*.yaml

apps/v1/DaemonSet myds1                                                       ??

    [CRITICAL] Container Security Context ReadOnlyRootFilesystem

        · nginx -> Container has no configured security context

            Set securityContext to run the container in a more secure context.

    [CRITICAL] Container Resources

        · nginx -> CPU limit is not set

            Resource limits are recommended to avoid resource DDOS. Set resources.limits.cpu

        · nginx -> Memory limit is not set

            Resource limits are recommended to avoid resource DDOS. Set resources.limits.memory

        · nginx -> CPU request is not set

            Resource requests are recommended to make sure that the application can start and run

            without crashing. Set resources.requests.cpu

        · nginx -> Memory request is not set

            Resource requests are recommended to make sure that the application can start and run

            without crashing. Set resources.requests.memory

    [CRITICAL] Container Security Context User Group ID

        · nginx -> Container has no configured security context

            Set securityContext to run the container in a more secure context.

..........

這里為了了直觀一點,我們翻譯一下看看。


  [關鍵] 容器安全上下文ReadOnlyRootFilesystem

        - nginx -> 容器沒有配置安全上下文

            設置securityContext以在一個更安全的環(huán)境中運行容器。

    [危急] 容器資源

        - nginx -> 沒有設置CPU限制

            推薦使用資源限制來避免資源DDOS。設置resources.limit.cpu

        - nginx -> 沒有設置內(nèi)存限制

            建議進行資源限制,以避免資源DDOS。設置resources.limit.memory

        - nginx -> 未設置CPU請求

            推薦使用資源請求,以確保應用程序能夠啟動和運行而不崩潰。

            而不崩潰。設置resources.request.cpu

        - nginx -> 未設置內(nèi)存請求

            推薦使用資源請求,以確保應用程序能夠啟動和運行而不崩潰。

            而不崩潰。設置resources.request.memory

..............

生成 YAML 文件分析

kube-score 也可以在您的 CI/CD 環(huán)境中運行,如果發(fā)現(xiàn)嚴重錯誤,將以退出代碼 1 退出。可以使用 --exit-one-on-warning 參數(shù)將觸發(fā)級別更改為警告。


kube-score 可以用于分析不同方式生成的 YAML 資源文件


可以是 Helm 對應的 charts 包 導出的 YAML 文件


helm template my-app | kube-score score -

也可以是 Kustomize 生成的合并的 YANL 資源文件


kustomize build . | kube-score score -

┌──[root@vms81.liruilongs.github.io]-[~/kustomize]

└─$kubectl kustomize  ./ | kubectl score score -

apps/v1/Deployment web                                                        ??

    [CRITICAL] Container Resources

        · nginx-web -> CPU limit is not set

            Resource limits are recommended to avoid resource DDOS. Set

            resources.limits.cpu

        · nginx-web -> Memory limit is not set

            Resource limits are recommended to avoid resource DDOS. Set

            resources.limits.memory

        · nginx-web -> CPU request is not set

            Resource requests are recommended to make sure that the application

            can start and run without crashing. Set resources.requests.cpu

        · nginx-web -> Memory request is not set

            Resource requests are recommended to make sure that the application

            can start and run without crashing. Set resources.requests.memory

    [CRITICAL] Pod NetworkPolicy

        · The pod does not have a matching NetworkPolicy

            Create a NetworkPolicy that targets this pod to control who/what

            can communicate with this pod. Note, this feature needs to be

            supported by the CNI implementation used in the Kubernetes cluster

            to have an effect.

    [CRITICAL] Container Ephemeral Storage Request and Limit

        · nginx-web -> Ephemeral Storage limit is not set

            Resource limits are recommended to avoid resource DDOS. Set

            resources.limits.ephemeral-storage

    [CRITICAL] Container Security Context User Group ID

        · nginx-web -> Container has no configured security context

            Set securityContext to run the container in a more secure context.

。。。。。。。。

現(xiàn)有集群的分析

可以通過下面的命令對 現(xiàn)有的集群的 API 對應 的 YAML 文件進行分析


┌──[root@vms81.liruilongs.github.io]-[~/awx-operator]

└─$kubectl api-resources --verbs=list --namespaced -o name   | xargs -n1 -I{} bash -c "kubectl get {} --all-namespaces -oyaml && echo ---"   | kubectl score score -

apps/v1/DaemonSet calico-node in kube-system                                  ??

    [CRITICAL] Pod NetworkPolicy

        · The pod does not have a matching NetworkPolicy

            Create a NetworkPolicy that targets this pod to control who/what

            can communicate with this pod. Note, this feature needs to be

            supported by the CNI implementation used in the Kubernetes cluster

            to have an effect.

.............



博文參考

https://github.com/zegl/kube-score



作者:山河已無恙


歡迎關注微信公眾號 :山河已無恙