cks 试题(1)

简介: cks 试题(1)

文章目录

1.镜像扫描ImagePolicyWebhook

2. sysdig检测pod

3. clusterrole

4. AppArmor

5. PodSecurityPolicy

6. 网络策略

7. dockerfile检测及yaml文件问题

8. pod安全

9. 创建SA

10. trivy检测镜像安全

11. 创建secret

12. kube-bench

13. gVsior

14. 审计

15. 默认网络策略

16. falco 检测输出日志格式

kubernetes exam in action考试信息

2小时

15-20题目

预约时间同CKA,32小时出成绩

满分不到100分,87分或93分,但67分及格

模拟环境

4套环境 1个控制台

NAT网段192.168.26.0

模拟考题

1.镜像扫描ImagePolicyWebhook

切换集群 kubectl config use-context k8s

context

A container image scanner is set up on the cluster,but It’s not yet fully integrated into the cluster’s configuration When complete,the container image scanner shall scall scan for and reject the use of vulnerable images.

task:

You have to complete the entire task on the cluster’s master node,where all services and files have been prepared and placed

Glven an incomplete configuration in directory /etc/kubernetes/aa and a functional container image scanner with HTTPS sendpitont http://192.168.26.60:1323/image_policy


1.enable the necessary plugins to create an image policy

2.validate the control configuration and chage it to an implicit deny

3.Edit the configuration to point the provied HTTPS endpoint correctiy

Finally,test if the configurateion is working by trying to deploy the valnerable resource /csk/1/web1.yaml

解题思路

ImagePolicyWebhook

关键字:image_policy,deny
1. 切换集群,查看master,sshmaster
2. ls /etc/kubernetes/xxx
3. vi /etc/kubernetes/xxx/xxx.yaml 更改 true 为 false
vi /etc/kubernetes/xxx/xxx.yaml 中 https的地址
volume需要挂载进去
4. 启用ImagePolicyWebhook和- --admission-control-config-file=
5. systemctl restart kubelet
6.kubectl run pod1 --image=nginx

案例:


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

添加ImagePolicyWebhook相关策略

重启api-server,systemctl restart kubelet

验证镜像创建pod失败

修改/etc/kubernetes/admission/admission_config.yaml 策略defaultAllow: true

重新验证镜像创建pod

$ ls /etc/kubernetes/aa/
admission_config.yaml  apiserver-client-cert.pem  apiserver-client-key.pem  external-cert.pem  external-key.pem  kubeconf
$ cd /etc/kubernetes/aa
$ cat kubeconf 
apiVersion: v1
kind: Config
# clusters refers to the remote service.
clusters:
- cluster:
    certificate-authority: /etc/kubernetes/aa/external-cert.pem  # CA for verifying the remote service.
    server: http://192.168.26.60:1323/image_policy                   # URL of remote service to query. Must use 'https'.
  name: image-checker
contexts:
- context:
    cluster: image-checker
    user: api-server
  name: image-checker
current-context: image-checker
preferences: {}
# users refers to the API server's webhook configuration.
users:
- name: api-server
  user:
    client-certificate: /etc/kubernetes/aa/apiserver-client-cert.pem     # cert for the webhook admission controller to use
    client-key:  /etc/kubernetes/aa/apiserver-client-key.pem             # key matching the cert
$ cat admission_config.yaml 
apiVersion: apiserver.config.k8s.io/v1
kind: AdmissionConfiguration
plugins:
  - name: ImagePolicyWebhook
    configuration:
      imagePolicy:
        kubeConfigFile: /etc/kubernetes/aa/kubeconf
        allowTTL: 50
        denyTTL: 50
        retryBackoff: 500
        defaultAllow: false
#修改api-server配置
$ cat /etc/kubernetes/manifests/kube-apiserver.yaml 
...............
  - command:
    - kube-apiserver
    - --admission-control-config-file=/etc/kubernetes/aa/admission_config.yaml #添加此行
    - --advertise-address=192.168.211.40
    - --allow-privileged=true
    - --authorization-mode=Node,RBAC
    - --client-ca-file=/etc/kubernetes/pki/ca.crt
    - --enable-admission-plugins=NodeRestriction,ImagePolicyWebhook  # #修改此行
    - --enable-bootstrap-token-auth=true
    - --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt
  ...........
    - mountPath: /etc/kubernetes/pki
      name: k8s-certs
      readOnly: true
    - mountPath: /etc/kubernetes/aa  #添加此行
      name: k8s-admission    #添加此行
      readOnly: true    #添加此行
..............
  - hostPath:    #添加此行
      path: /etc/kubernetes/aa   #添加此行
      type: DirectoryOrCreate    #添加此行
    name: k8s-admission    #添加此行
  - hostPath:
      path: /usr/local/share/ca-certificates
      type: DirectoryOrCreate
    name: usr-local-share-ca-certificates
  - hostPath:
      path: /usr/share/ca-certificates
      type: DirectoryOrCreate
    name: usr-share-ca-certificates
status: {}
$ k get nodes
NAME     STATUS   ROLES                  AGE   VERSION
master   Ready    control-plane,master   9d    v1.20.1
node1    Ready    <none>                 9d    v1.20.1
node2    Ready    <none>                 9d    v1.20.1
#创建pod失败
$ k run test --image=nginx
Error from server (Forbidden): pods "test" is forbidden: Post "https://external-service:1234/check-image?timeout=30s": dial tcp: lookup external-service on 8.8.8.8:53: no such host
#修改admission_config.yaml 配置
$ vim /etc/kubernetes/aa/admission_config.yaml 
apiVersion: apiserver.config.k8s.io/v1
kind: AdmissionConfiguration
plugins:
  - name: ImagePolicyWebhook
    configuration:
      imagePolicy:
        kubeConfigFile: /etc/kubernetes/aa/kubeconf
        allowTTL: 50
        denyTTL: 50
        retryBackoff: 500
        defaultAllow: true    #修改此行为true
#重启api-server
$ ps -ef |grep api
root      78871  39023  0 20:17 pts/3    00:00:00 grep --color=auto api
$ mv ../kube-apiserver.yaml .
#创建pod成功
$ k run test --image=nginx
pod/test created

2. sysdig检测pod

切换集群 kubectl config use-context k8s

you may user you brower to open one additonal tab to access sysdig’s documentation ro Falco’s documentaion

Task:

user runtime detection tools to detect anomalous processes spawning and executing frequently in the sigle container belorging to Pod redis.

Tow tools are avaliable to use:


sysdig

falico

the tools are pre-installed on the cluster’s worker node only;the are not avaliable on the base system or the master node.

using the tool of you choice(including any non pre-install tool) analyse the container’s behaviour for at lest 30 seconds,using filers that detect newly spawing and executing processes store an incident file at /opt/2/report,containing the detected incidents one per line in the follwing format:

[timestamp],[uid],[processName]

解题思路

Sysdig User Guide

关键字:sysdig
0. 记住使用sysdig -l |grep 搜索相关字段
1. 切换集群,查询对应的pod,ssh到pod对应的node主机上
2. 使用sysdig,注意要求格式和时间,结果重定向到对应的文件
3. sysdig -M 30 -p "*%evt.time,%user.uid,%proc.name" container.id=容器id >/opt/2/report

案例

3. clusterrole

切换集群 kubectl config use-context k8s

context

A Role bound to a pod’s serviceAccount grants overly permissive permission

Complete the following tasks to reduce the set of permissions.

task

Glven an existing Pod name web-pod running in the namespace monitoring Edit the Roleebound to the Pod’s serviceAccount sa-dev-1 to only allow performing list operations,only on resources of type Endpoints

create a new Role named role-2 in the namespaces monitoring which only allows performing update operations,only on resources of type persistentvoumeclaims.

create a new Rolebind name role role-2-bindding binding the newly created Roleto the Pod’s serviceAccount


解题思路

RBAC

关键字:role,rolebindding
1. 查找rollebind对应的rolle修改权限为list 和 endpoints
$ kubectl edit role role-1 -n monitoring
2. 记住 --verb是权限 --resource是对象
$ kubectl create role role-2 --verb=update --resource=persistentvolumeclaims -n
monitoring
3. 创建绑定 绑定为对应的sa
$ kubectl create rolebinding role-2-bindding --role=role-2 --
serviceaccount=monitoring:sa-dev-1 -n monitoring

4. AppArmor

切换集群 kubectl config use-context k8s

Context

AppArmor is enabled on the cluster’s worker node. An AppArmor profile is prepared, but not enforced yet. You may use your browser to open one additional tab to access

theAppArmor documentation. Task

On the cluster’s worker node, enforce the prepared AppArmor profile located at /etc/apparmor.d/nginx_apparmor . Edit the prepared manifest file located at /cks/4/pod1.yaml to apply the AppArmor profile. Finally, apply the manifest file and create the pod specified in it


解题思路

apparmor

关键字:apparmor
1. 切换结群,记住查看nodes,ssh到node节点
2. 查看对应的配置文件和名字
$ cd /etc/apparmor.d
$ vi nginx_apparmor
$ apparmor_status |grep nginx-profile-3 # 没有grep到说明没有启动
$ apparmor_parser -q nginx_apparmor # 加载启用这个配置文件
3. 修改对应yaml应用这个规则 ,打开官网的网址复制例子,修改容器名字和本地的配置名
$ vi /cks/4/pod1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: hello-apparmor
  annotations:
    container.apparmor.security.beta.kubernetes.io/hello: localhost/nginx-profile-3
spec:
  containers:
  - name: hello
    image: busybox
    command: [ "sh", "-c", "echo 'Hello AppArmor!' && sleep 1h" ]
4. 修改后创建出来
$ kubectl apply -f /cks/4/pod1.yaml

5. PodSecurityPolicy

切换集群 kubectl config use-context k8s63

context

A PodsecurityPolicy shall prevent the create on of privileged Pods in a specific

namespace. Task


Create a new PodSecurityPolicy named prevent-psp-policy , which prevents the creation of privileged Pods.

Create a new ClusterRole named restrict-access-role , which uses the newly created PodSecurityPolicy prevent-psp-policy .

Create a new serviceAccount named pspdenial-sa in the existing namespace development .

Finally, create a new clusterRoleBinding named dany-access-bind ,which binds the newly created ClusterRole restrict-access-role to the newly created serviceAccount

解题思路

PodSecurityPolicy

关键词: psp policy privileged
0. 切换结群,查看是否启用
$ vi /etc/kubernetes/manifests/kube-apiserver.yaml
- --enable-admission-plugins=NodeRestriction,PodSecurityPolicy
$ systemctl restart kubelet
1. 官方网址复制psp,修改拒绝特权
$ cat psp.yaml
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: prevent-psp-policy
spec:
privileged: false
seLinux:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
runAsUser:
rule: RunAsAny
fsGroup:
rule: RunAsAny
volumes:
- '*'
$ kubectl create -f psp.yaml
2. 创建对应的clusterrole
$ kubectl create clusterrole restrict-access-role --verb=use --resource=podsecuritypolicy --resource-name=prevent-psp-policy
3. 创建sa 看对应的ns
$ kubectl create sa psp-denial-sa -n development
4. 创建绑定关系
$ kubectl create clusterrolebinding dany-access-bind --clusterrole=restrict-access-role --serviceaccount=development:psp-denial-sa

6. 网络策略

切换集群 kubectl config use-context k8s

create a NetworkPolicy named pod-access to restrict access to Pod products-service running in namespace development . only allow the following Pods to connect to Pod productsservice :


Pods in the namespace testing

Pods with label environment: staging , in any namespace Make sure to apply the NetworkPolicy. You can find a skelet on manifest file at/cks/6/p1.yaml

解题思路

NetworkPolicy

关键字:NetworkPolicy
1. 主机查看pod的标签
$ kubectl get pod -n development --show-labels
2. 查看对应ns的标签,没有需要设置一下
$ kubectl label ns testing name=testing
3. 编排networkpolicy策略
$ cat /cks/6/p1.yaml
kind: NetworkPolicy
metadata:
name: "pod-access"
namespace: "development"
spec:
  podSelector:
    matchLabels:
      environment: staging
  policyTypes:
  - Ingress
  ingress:
  - from:
    - namespaceSelector:
        matchLabels:
          name: testing
  - from:
    - namespaceSelector:
        matchLabels:
      podSelector:
        matchLabels:
          environment: staging
$ kubectl create -f /cks/6/p1.yaml

7. dockerfile检测及yaml文件问题

切换集群 kubectl config use-context k8s

Task

Analyze and edit the given Dockerfile (based on the ubuntu:16.04 image) /cks/7/Dockerfile fixing two instructions present in the file being prominent security/best-practice issues.


Analyze and edit the given manifest file /cks/7/deployment.yaml

fixing two fields present in the file being prominent security/best-practiceissues.


解题思路

关键字:Dockerfile issues
1.注意dockerfile提示的错误数量
注释:USER root
2.yaml问题:注意api版本问题,和特权网络,镜像版本,也是要看题目中说的错误是几处

案例:

Dockerfile

# build container stage 1
FROM ubuntu:20.04
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y golang-go=2:1.13~1ubuntu2
COPY app.go .
RUN pwd
RUN CGO_ENABLED=0 go build app.go
# app container stage 2
FROM alpine:3.12.0
RUN addgroup -S appgroup && adduser -S appuser -G appgroup -h /home/appuser
RUN rm -rf /bin/*
COPY --from=0 /app /home/appuser/
USER appuser
CMD ["/home/appuser/app"]

8. pod安全

切换集群 kubectl config use-context k8s

context

lt is best-practice to design containers to best teless and immutable. Task

lnspect Pods running in namespace testing and delete any Pod that is either not stateless or not immutable. use the following strict interpretation of stateless and immutable:

Pods being able to store data inside containers must be treated as not stateless.

You don’t have to worry whether data is actually stored inside containers or not already. Pods being configured to be privileged in any way must be treated as potentially not stateless and not immutable.


解题思路

关键字:stateless immutable
1. get 所有pod
2. 查看是否有特权 privi*
3. 查看是否有volume
4. 把特权网络和volume都删除
$ kubectl get pod pod1 -n testing -o jsonpath={.spec.volumes} | jq
$ kubectl get pod sso -n testing -o yaml |grep "privi.*: true"
$ kubectl delete pod xxxxx -n testing
相关实践学习
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。 &nbsp; &nbsp; 相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
相关文章
|
网络协议 数据可视化 Unix
Xshell 7(SSH远程终端工具) v7.0.0109 官方中文正式版(附文件+安装教程)
Xshell 7(SSH远程终端工具) v7.0.0109 官方中文正式版(附文件+安装教程)
15216 0
Xshell 7(SSH远程终端工具) v7.0.0109 官方中文正式版(附文件+安装教程)
|
23天前
|
存储 弹性计算 人工智能
阿里云轻量应用服务器2核2G200M峰值带宽到底多少钱?38元、68元、79元、459元哪个准?
阿里云轻量应用服务器2核2G在同一页面上会同时出现38元、68元、79元、459元四个价格,对应四类账号身份。38元与68元均为新用户价,前者每日10点、15点两场限量抢购、限购1次、不可叠券、续费恢复常规价,后者无需抢购;79元适用于买过其他云产品但未开通过服务器的产品新用户;459元为老用户常规价。身份按账号有无云产品付费记录判定,同一实名主体下的账号会合并判定,该配置为200M峰值带宽。
阿里云轻量应用服务器2核2G200M峰值带宽到底多少钱?38元、68元、79元、459元哪个准?
|
3月前
|
人工智能 JSON API
AI编程工具Codex对接DeepSeek V4‑Flash实操指南:一键脚本部署、手动修改配置、验证排错完整手册
Codex作为面向开发者的本地AI编程Agent工具,兼具CLI命令行终端、桌面客户端、IDE插件多种形态,能够直接读取本地项目代码,完成代码生成、文件修改、Bug排查、项目重构、单元测试编写等工程任务。原生环境默认绑定官方模型,很多开发者希望替换为DeepSeek V4‑Flash作为底层推理基座,该模型具备优秀代码生成能力、强大工具调用效果,同时拥有不错的响应速度与可控调用成本。
366 0
|
4月前
|
人工智能 缓存 监控
阿里云百炼三大订阅套餐全解:Token Plan、Coding Plan、Night Plan计费规则、API调用与业务选型
随着大模型应用从原型验证走向规模化生产,按量付费模式会出现账单不可预测、高频调用成本居高不下等现实问题。订阅类套餐以固定周期费用换取配额额度,把大模型调用成本从“按次不可控消耗”转变为“预算可预估”,已经成为个人开发者、工作室以及中小企业构建AI应用的重要选择。百炼平台推出Token Plan、Coding Plan、Night Plan三套相互独立的订阅产品,三套套餐额度互不通用,API凭证、调用链路、配额约束、适配模型与工具各有区别。很多开发者在初次接入时,容易混淆三套套餐的使用边界,误用普通API Key导致订阅额度无法抵扣,或是选错套餐造成额度浪费。本文从产品定位、额度计量逻辑、套餐档
336 0
|
11月前
|
存储 Shell Linux
玩转Linux Shell数组(从零开始掌握Shell数组初始化技巧)
本教程详解Linux Shell数组初始化,涵盖空数组声明、赋值初始化、指定索引、命令输出填充等方法,助你掌握Bash数组基本操作,提升脚本编写效率,适合Shell初学者快速入门。
|
6月前
|
存储 人工智能 安全
OpenHuman:本地优先的开源Agent
OpenHuman 展示了一条本地优先的 Agent 路线:把个人记忆、数据连接、自动更新结合,让 Agent 更懂你,也更尊重数据边界。
710 1
|
8月前
|
人工智能 自然语言处理 安全
Open Claw 2.6.4 Windows 一键部署完整教程(技术分享)
OpenClaw(昵称“小龙虾”)是2026年热门开源AI智能体,GitHub星标超28万。支持本地运行、零代码操作、跨平台部署,可理解自然语言指令,自动完成文件管理、数据处理、浏览器自动化等任务,一键安装,隐私安全。
|
12月前
|
JavaScript 前端开发 Java
Bootstrap5 快速上手指南
Bootstrap5 快速上手指南:告别 jQuery,拥抱原生 JS!新增 xxL 断点与 RTL 支持,优化栅格系统与组件样式。提供 CDN 引入、核心布局、常用组件及完整博客实例,零基础也能快速搭建美观响应式博客,含按钮、卡片、导航栏、表单等实战代码,即拷即用。
Bootstrap5 快速上手指南
|
11月前
|
存储 人工智能 自然语言处理
从 RAG 到智能体:解决大模型“幻觉”的工程化路径探讨 (智能体来了——西南总部)
本文探讨如何通过RAG与AI Agent结合,工程化解决大模型“幻觉”难题。从原理到实践,详解检索增强、智能决策与生产部署路径,助力构建可信、可追溯、高准确性的企业级AI应用,释放大模型真实生产力。(238字)
833 0
|
存储 消息中间件 Kubernetes
在K8S中,deploy和Statefulset有何区别?
在K8S中,deploy和Statefulset有何区别?

热门文章

最新文章