iT邦幫忙

2025 iThome 鐵人賽

DAY 28
0

今天以及明天主要會針對在準備考試的時候在網路上以及網站學習時遇到的問題進行整理。

1. apparmor

考試時對 apparmor 的題目主要是有幾個重點:
(1) 設定對 read, write 的限制
(2) 取得 apparmor 的名稱
(3) pod 指定 apparmor

(1) 設定對 read, write 的限制

使用時可以在 k8s.io 中查詢 apparmor 當中就含有如何配置 deny read write 的權限

(2) 取得 apparmor 的名稱

在配置完後可以透過指令建立 profile
apparmor_parser -q <file_name>
查看建立後的 profile
aa-status

(3) pod 指定 apparmor

apiVersion: v1
kind: Pod
metadata:
  name: hello-apparmor
spec:
  containers:
  - name: hello
    image: busybox:1.28
    command: [ "sh", "-c", "echo 'Hello AppArmor!' && sleep 1h" ]
    securityContext:
        appArmorProfile:
        type: Localhost
        localhostProfile: k8s-apparmor-example-deny-write    # 將apparmor 的 profile 設定於此

2. Dockerfile 以及 yaml security

主要是在檢查 Dockfile 以及 pod/deployment 的 yaml 有無安全問題

有幾個重點

  • Dockerfile 不能將密碼在 build 的時候加入,需要在後續透過 secret 導入
  • pod/deployment 會在 securityContext 中做設定
    • 使用 non root user => 不使用 user 0
    • readOnlyRootFilesystem
    • allowPrivilegeEscalation: false

3. kube-bench 合規檢測

前言 - 似乎 kube-bench 的指令有修改,以往只要kube-bench就會自動執行,這次在準備時發現會需要指定benchmark

(1) 檢查內容

kube-bench --benchmark cis-1.9

(2) 按照需求進行修改
會需要大概記一下各個 config 檔的位置,剩下的可以直接照說明修改

  • kube-controller-manager /etc/kubernetes/manifests/kube-controller-manager.yaml
  • kube-scheduler /etc/kubernetes/manifests/kube-scheduler.yaml
  • kubelet /var/lib/kubelet/
  • etcd /var/lib/etcd

4. Falco

Falco 主要是會考 log 的篩選以及輸出
需要記一下 Falco 的網站 https://falco.org/

(1) 規則尋找
falco 會提供 default 的規則做參考所有的配置檔會放在 /etc/falco
例如尋找Directory traversal monitored file read對應的規則在哪

grep "Directory traversal monitored file read" -A 10 -B 10 -r ./

-A 顯示該行的後面幾行
-B 顯示該行的前面幾行
-r 針對哪個資料夾做尋找

(2) ouput 修改

  • 內容修改
    可以至 Falco 的官網看輸出 位置會在 Reference > Falco Rules > Fields for Conditions and Outputs
  • 輸出檔案位置
    可以至 Concept > Output > Channels 查看配置

(3) 重啟
修改完後 systemctl daemon-reload 以及 systemctl restart falco
或者直接執行 falco

5. ImagePolicyWebhook

如果是針對 image 的 policy 就會是透過 ImagePolicyWebhook 設定,可以直接至 k8s.io 中查詢 ImagePolicyWebhook

Configuration

ImagePolicyWebhook 的 Configuration 會有兩種形式

  • 形式1 兩段式
imagePolicy:
  kubeConfigFile: /path/to/kubeconfig/for/backend
  # time in s to cache approval
  allowTTL: 50
  # time in s to cache denial
  denyTTL: 50
  # time in ms to wait between retries
  retryBackoff: 500
  # determines behavior if the webhook backend fails
  defaultAllow: true
apiVersion: apiserver.config.k8s.io/v1
kind: AdmissionConfiguration
plugins:
  - name: ImagePolicyWebhook
    path: imagepolicyconfig.yaml
...
  • 形式2 合併
apiVersion: apiserver.config.k8s.io/v1
kind: AdmissionConfiguration
plugins:
  - name: ImagePolicyWebhook
    configuration:
      imagePolicy:
        kubeConfigFile: <path-to-kubeconfig-file>
        allowTTL: 50
        denyTTL: 50
        retryBackoff: 500
        defaultAllow: true

需要特別注意要修改 defaultAllow: true

kube-apiserver 修改

/etc/kubernetes/manifests/kube-apiserver.yaml增加配置

  • - --enable-admission-plugins=加上ImagePolicyWebhook
  • 加上 - --admission-control-config-file 位置
  • 確定 Volume 以及 Volume Mount 是否有設定

設定完後 crictl pod 查看 control plane 是否有重啟
重啟後可以透過 kubectl get pods查看是否修改成功,若配置有錯誤會無法呼叫 kube-apiserver The connection to the server controlplane:6443 was refused - did you specify the right host or port?

6. ServiceAccount security

(1) Security account automatically mounted 關閉
可在 k8s.io 查詢 servcieaccount pod

apiVersion: v1
kind: ServiceAccount
metadata:
  name: build-robot
automountServiceAccountToken: false

7. Ingress

在 ingress 中主要會考的是 tls 以及 https 的 ingress 建立

(1) tls secret 建立

tls secret 建立有三種

  • 透過 kubectl create secret tls 開頭建立
  • 透過 yaml 並指定檔案路徑建立
  • 透過 yaml 並直接使用明碼建立 (使用明碼建立時需要手動 base64 encode)

(2) ingress 透過 https 連線

詳細內容可以至 k8s.io 中搜尋 ingress 並尋找 TLS 相關的說明

不同的配置會依據不同雲所使用的 controller 而有所不同,但是在考試中主要考的是 nginx 的 controller

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: tls-example-ingress
spec:
  tls:
  - hosts:
      - https-example.foo.com
    secretName: testsecret-tls
  rules:
  - host: https-example.foo.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: service1
            port:
              number: 80

8. audit log

audit log 以考試而言會比較單純,主要是考:
(1) config 檔配置:需要注意類別會是複數 ex. namespaces, deployments, secrets...
(2) log 儲存:需要注意要使用 volume 以及 volume mount
(3) 單一檔案大小限制、儲存天數、儲存檔案數

相關配置可以在 k8s.io 中查詢audit取得

9. Pod Security Standard

可以在 k8s.io 中以關鍵字 pod security standard namespace 查詢,
考試時主要會是需要在指定的 namespace 上加上 label

10. istio

istio 會是透過 mesh 的形式在每個 pod 建立 sidecar container,考試時會有兩個可以注意:
(1) 在 namesapce 上加上 label

  • 需記住 istio 的官方網站為 istio.io
  • 在 Quickstart 找到 sidecar mode 然後找到 Sidecar or ambient?
  • istio-injection=enabled配置到 namespace 上
    (2) 開始 STRICT mode
  • 在 istio 官方網站中找到 Task > Authentication > Mutual TLS Migration
  • 照以下 yaml 內容進行配置
apiVersion: security.istio.io/v1
kind: PeerAuthentication
metadata:
  name: default
  namespace: default # 可按照需求加上 namespace
spec:
  mtls:
    mode: STRICT

上一篇
[Day27] 6-5. Use Kubernetes audit logs to monitor access
下一篇
[Day29] Exam Question Review2
系列文
我在 CKS 考完只拿 47% 後痛定思痛決定好好準備內容30
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言