启用基于预共享秘钥的l2tp连接功能
解决连接lt2p后能ping但是所有端口都无法访问的问题,关闭网络checksum功能。ip-up是会在lt2p连接建立时自动运行的脚本
在断开l2tp连接后会将相关配置还原为系统默认。ip-down是会在l2tp连接断开时自动运行的脚本
启用基于预共享秘钥的l2tp连接功能
解决连接lt2p后能ping但是所有端口都无法访问的问题,关闭网络checksum功能。ip-up是会在lt2p连接建立时自动运行的脚本
在断开l2tp连接后会将相关配置还原为系统默认。ip-down是会在l2tp连接断开时自动运行的脚本
Tab:用4个空格替换;
分号:可要可不要,但 Rust 风格是需要;
代码文件命名:用下划线分割不同单词;
编译与运行
使用cargo比rustc的优势
常用命令
安装测试集群
1 | curl https://raw.githubusercontent.com/tigera/ccol1/main/control-init.yaml | multipass launch -n control -m 2048M 20.04 --cloud-init - |
calico 的四种安装方式
以Operator的方式来安装calico
1 | multipass shell host1 |
安装测试所需的模拟业务
1 | multipass shell host1 |
原生网络策略
默认配置
命名空间中没有 policy 时,全部开放;
拒绝/允许所有流量
1 | apiVersion: networking.k8s.io/v1 |
选择流量的3种方式
DNS 记录
Calico Network Policy
比原生网络策略的优势
CKA 和 CKS 是 LINUX 基金会联合 CNCF社区组织的云原生技术领域权威认证,考试采用实操方式进行。CKS全称是Kubernetes安全专家认证,它在一个模拟真实的环境中测试考生对Kubernetes和云安全的知识。在参加CKS考试之前,必须已经通过CKA(Kubernetes管理员认证),在获得CKA认证之后才可以预约CKS考试。CKS 的考试难度相对于 CKA 提高了很多,2个小时的考试时间很紧张,因为考试是在外网上进行,这两个考试又是实操考试,网络条件不好,很影响效率,如果不抓紧的话,很可能做不完所有实操题。提醒备考的同学善用考试软件提供的 notepad 功能,先把 yaml 文件或命令写到notepad里,再粘贴到 terminal里。
我因为上次 CKA 考试还是比较顺利,94 高分通过,所以这次的 CKS 考试有点疏忽了,搞忘带身份证和护照,CKA/CKS 考试需要身份证+护照/信用卡,因此跟监考老师沟通了很久时间,最后修改了考试人姓名为中文,是用驾驶证完成的考试。意外之喜是 CKS 给我的证书是中文名的。
我这次考试的 kubernetes 版本是 1.22,特意记录了一下考试会考到的知识点,分享给需要的同学。
通常使用标签选择器来选择pod,控制流量。所以要对 kubectl label的使用方法熟悉起来。
1 | kubectl label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N |
网络策略的实用方法见注释
1 | apiVersion: networking.k8s.io/v1 |
查看当前节点加载的 apparmor profile ,如果没有加载,要手工加载
cks 考试的 apparmor profile 文件内容:
1 | #include <tunables/global> |
注意: nginx-profile-3 这一行要确保注释掉,考试环境提供的可能没有注释,加载配置文件按时会报错
1 | root@node01:~# apparmor_parser /etc/apparmor.d/nginx_apparmor |
修改 pod yaml 文件,在注释里设置为 podx 加载 apparmor profile
yaml 文件内容如下:
1 | apiVersion: v1 |
kube-bench 是一个 CIS 评估工具,扫描 kubernetes 集群存在的安全问题,基本上按照 扫描结果的修复建议进行修复就可以了,系统会给出很具体的修复措施。
1 | # 修复 kube-apiserver 安全问题 |
这个题要注意 serviceaccount 有个选项 automountServiceAccountToken, 这个选项决定是否自动挂载 secret 到 pod。
有这个选项,我们可以控制 pod 创建并绑定 serviceaccount 时,不自动挂载对应的 secret,这样 pod 就没有权限访问 apiserver,提高了业务 pod 的安全性。
可以在 serviceaccount 和 pod 的 spec 里设置,pod的设置优先于 serviceaccount 里的设置。
1 | apiVersion: v1 |
删除未使用的 serviceaccount
这道题是送分题,设置默认拒绝所有出站和入站的 pod 流量,基本上可以参考官网的案例直接改一下名字就可以了
默认网络策略
这道题也基本是送分题,参考官网文档,根据题目要求,设置 role 的 资源访问权限,绑定到 serviceaccount 就可以了。
RBAC
这道题稍复杂,需要按照要求启动日志审计,包括两个步骤:
(1) 编写日志审计策略文件
日志审计策略
1 | apiVersion: audit.k8s.io/v1 |
(2) 修改 kube-apiserver.yaml配置文件,启用日志审计策略,日志策略配置文件位置、日志文件存储位置、循环周期。
启动日志配置
vi /etc/kubernetes/manifests/kube-apiserver.yaml
1 | # 设置日志审计策略文件在 pod 里的 mount 位置 |
重启 kubelet
这道题考解码 secret 的 base64 编码信息,创建新的 secret 并 mount 到 pod 的特定位置。
解码 secret
1 | kubectl get secrets -n istio-system db1-test -o jsonpath={.data.username} | base64 -d > /cks/sec/user.txt |
1 | kubectl create secret generic db2-test -n istio-system --from-literal=username=production-instance --from-literal=password=KvLftKgs4aVH |
1 | apiVersion: v1 |
这道题也是送分题,主要是把 dockerfile里两个 使用了 root 用户的指令删除,把添加特定能力的 securityContext 安全上下文注释掉。
1 | # 删除两处 |
给出了 支持安全沙箱容器运行时 handler runsc
, 我们需要创建一个 RuntimeClass 并在 pod spec里指定是用该 RuntimeClass
参考资料
1 | 注意:运行中的 pod 只能修改有限的几个属性,不支持修改 RuntimeClass,需要将所有 pod 的 yaml 解析出来,修改 yaml 后,再重新创建 pod |
这道题考察对于镜像扫描工具 trivy 的使用
1 | # 获取镜像名 |
本体考察是否掌握 sysdig 的基本用法,记住两个帮助命令:
另外 sysdig 支持指定 containerid 分析特定容器
1 | # 查看容器id |
这道题考察是否掌握 psp 的用法,包括5步骤
(1) 创建 psp
参考链接
1 | apiVersion: policy/v1beta1 |
(2) 创建 clusterrole,使用 psp
1 | kubectl create clusterrole restrict-access-role --verb=use --resource=psp --resource-name=restrict-policy |
(3) 创建 serviceaccount
(4) 绑定 clusterrole 到 serviceaccount
1 | kubectl create clusterrolebinding dany-access-bind --clusterrole=restrict-access-role --serviceaccount=staging:psp-denial-sa |
(5) 启用 PodSecurityPolicy
1 | vi /etc/kubernetes/manifests/kube-apiserver.yaml |
这道题同前面 kube-bench 的考核内容有点重合,题目中是用 kubeamd创建的 kubernetes服务器权限设置有问题,允许未经授权的访问。
参考链接
需要进行以下修改:
1 | vi /etc/kubernetes/manifests/kube-apiserver.yaml |
这道题考察 ImagePolicyWebhook 准入控制器的使用,分4个步骤
vi /etc/kubernetes/epconfig/admission_configuration.json
1 | { |
1 | vi /etc/kubernetes/epconfig/kubeconfig.yaml |
1 | # 启用 ImagePolicyWebhook |
From: 2022年2月我的CKS备考记录 - scwang18 - 博客园 (cnblogs.com)
CKS 考试链接
Important Instructions: CKS
Certifications- expire 36 months from the date that the Program certification requirements are met by a candidate.
The following tools and resources are allowed during the exam as long as they are used by candidates to work independently on exam tasks (i.e. not used for 3rd party assistance or research) and are accessed from within the Linux server terminal on which the Exam is delivered.
During the exam, candidates may:
You’re only allowed to have one other browser tab open with:
kubectl config use-context <cluster/context name>
ssh <nodename>
−ssh
is not supported.kubectl
and the appropriate context to work on any cluster from the base node. When connected to a cluster member via ssh, you will only be able to work on that particular cluster via kubectl.kubectl
with kalias and Bash autocompletionyq
and jq
for YAML/JSON processingtmux
for terminal multiplexingcurl
and wget
for testing web servicesman
and man pages for further documentationv1.25
有几个练习库,建议将每个题目都自己亲自操作一遍,一定要操作。
1 | # 不熟 |
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 the AppArmor documentation.
AppArmor 已在 cluster 的工作节点上被启用。一个 AppArmor 配置文件已存在,但尚未被实施。
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 /home/candidate/KSSH00401/nginx-deploy.yaml
to apply the AppArmor profile.
Finally, apply the manifest file and create the pod specified in it.
在 cluster 的工作节点上,实施位于 /etc/apparmor.d/nginx_apparmor
的现有 AppArmor 配置文件。
编辑位于 /home/candidate/KSSH00401/nginx-deploy.yaml
的现有清单文件以应用 AppArmor 配置文件。
最后,应用清单文件并创建其中指定的 Pod 。
搜索 apparmor(使用 AppArmor 限制容器对资源的访问),接着再搜索字符串 “parser”
https://kubernetes.io/zh/docs/tutorials/security/apparmor/
1 | ### 远程登录到指定工作节点 |
A CIS Benchmark tool was run against the kubeadm-created cluster and found multiple issues that must be addressed immediately.
针对 kubeadm 创建的 cluster 运行 CIS 基准测试工具时, 发现了多个必须立即解决的问题。
Fix all issues via configuration and restart theaffected components to ensure the new settings take effect.
通过配置修复所有问题并重新启动受影响的组件以确保新的设置生效。
Fix all of the following violations that were found against the API server:
修复针对 API 服务器发现的所有以下违规行为:
Ensure that the 1.2.7 –authorization-mode FAIL argument is not set to AlwaysAllow
Ensure that the 1.2.8 –authorization-mode FAIL argument includes Node
Ensure that the 1.2.9 –authorization-mode FAIL argument includes RBAC
Ensure that the 1.2.18 –insecure-bind-address FAIL argument is not set
Ensure that the 1.2.19 –insecure-port FAIL argument is set to 0
Fix all of the following violations that were found against the kubelet:
修复针对 kubelet 发现的所有以下违规行为:
Ensure that the 4.2.1 –anonymous-auth FAIL argument is set to false
Ensure that the 4.2.2 –authorization-mode FAIL argument is not set to AlwaysAllow
Use Webhook
authn/authz where possible. 注意:尽可能使用 Webhook authn/authz。
Fix all of the following violations that were found against etcd:
修复针对 etcd 发现的所有以下违规行为:
Ensure that the 4.2.1 –client-cert-auth FAIL argument is set to true
1 | $ ssh root@vms65.rhce.cc |
使用 Trivy 开源容器扫描器检测 namespace kamino 中 Pod 使用的具有严重漏洞的镜像。
查找具有 High 或 Critical 严重性漏洞的镜像,并删除使用这些镜像的 Pod。
注意:Trivy 仅安装在 cluster 的 master 节点上,在工作节点上不可使用。你必须切换到 cluster 的 master 节点才能使用 Trivy 。
Use the Trivy open-source container scanner to detect images with severe vulnerabilities used by Pods in the namespace kamino
.
Look for images with High
or Critical
severity vulnerabilities, and delete the Pods that use those images.
Trivy is pre-installed on the cluster’s master
node only; it is not available on the base system or the worker nodes. You’ll have to connect to the cluster’s master node to use Trivy
.
搜索 kubectl images(列出集群中所有运行容器的镜像)
https://kubernetes.io/zh-cn/docs/tasks/access-application-cluster/list-all-running-container-images/
1 | ### 需登录到控制节点操作 |
you may use you brower to open one additonal tab to access sysdig’s documentation or Falco’s documentaion
Use runtime detection tools to detect anomalous processes spawning and executing frequently in the sigle container belonging to Pod redis
. Two tools are avaliable to use:
使用运行时检测工具来检测 Pod tomcat 单个容器中频发生成和执行的异常进程。有两种工具可供使用:
The tools are pre-installed on the cluster’s worker node only, they 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 behavior for at least 30
seconds, using filers that detect newly spawing and executing processes, store an incident file at /opt/KSR00101/incidents/summary
, containing the detected incidents one per line in the follwing format:
注:这些工具只预装在 cluster 的工作节点,不在 master 节点。
使用工具至少分析 30 秒,使用过滤器检查生成和执行的进程,将事件写到 /opt/KSR00101/incidents/summary
文件中,其中包含检测的事件, 每个单独一行
格式如下:
保持工具的原始时间戳格式不变。
注:确保事件文件存储在集群的工作节点上。
1 | # 方法一 sysdig |
Context
A Pod fails to run because of an incorrectly specified ServiceAcccount.
create a new ServiceAccount named backend-sa
in the existing namespace qa
, which must not have access to any secrets.
Inspect the Pods in the namespace
.
backend-sa
.Finally, clean-up and delete the now unused serviceAccount in the namespace qa
.
在现有 namespace qa
中创建一个名为 backend-sa
的新 ServiceAccount, 确保此 ServiceAccount 不自动挂载secrets。
使用 /cks/9/pod9.yaml
中的清单文件来创建一个 Pod。
最后,清理 namespace qa
中任何未使用的 ServiceAccount。
搜索 serviceaccount(为Pod配置服务账号),接着再搜索字符串 “automount”
https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-service-account/
1 | ### 创建 ServiceAccount |
2023的最新考试已经没有这道题了,替代的是Pod Security Standard
A PodsecurityPolicy shall prevent the creation on of privileged Pods in a specific namespace.
PodSecurityPolicy 应防止在特定 namespace 中特权 Pod 的创建。
Create a new PodSecurityPolicy
named restrict-policy
, which prevents the creation of privileged Pods.
Create a new ClusterRole
named restrict-access-role
, which uses the newly created PodSecurityPolicy restrict-policy
.
Create a new serviceAccount
named psp-denial-sa
in the existing namespace staging
.
Finally, create a new clusterRoleBinding
named dany-access-bind
, which binds the newly created ClusterRole restrict-access-role
to the newly created serviceAccount psp-denial-sa
.
创建一个名为 restrict-policy
的新的 PodSecurityPolicy,以防止特权 Pod 的创建。
创建一个名为 restrict-access-role
并使用新创建的 PodSecurityPolicy restrict-policy
的 ClusterRole。
在现有的 namespace staging
中创建一个名为 psp-denial-sa
的新 ServiceAccount 。
最后,创建一个名为 dany-access-bind
的 ClusterRoleBinding,将新创建的 ClusterRole restrict-access-role
绑定到新创建的 ServiceAccount psp-denial-sa
。
你可以在一下位置找到模版清单文件: /cks/psp/psp.yaml
搜索 runasany(Pod Security Policy)
https://kubernetes.io/id/docs/concepts/policy/pod-security-policy/
搜索 clusterrole(使用RBAC鉴权)
https://kubernetes.io/zh-cn/docs/reference/access-authn-authz/rbac/
1 | ### (1)创建 psp |
Task weight: 8%
Use context: kubectl config use-context workload-prod
There is Deployment container-host-hacker
in Namespace team-red
which mounts /run/containerd
as a hostPath volume on the Node where it’s running. This means that the Pod can access various data about other containers running on the same Node.
To prevent this configure Namespace team-red
to enforce
the baseline
Pod Security Standard. Once completed, delete the Pod of the Deployment mentioned above.
Check the ReplicaSet
events and write the event/log lines containing the reason why the Pod isn’t recreated into /opt/course/4/logs
.
Making Namespaces use Pod Security Standards works via labels. We can simply edit it:
Now we configure the requested label:
1 | # kubectl edit namespace team-red |
This should already be enough for the default Pod Security Admission Controller to pick up on that change. Let’s test it and delete the Pod to see if it’ll be recreated or fails, it should fail!
1 | ➜ k -n team-red get pod |
Usually the ReplicaSet of a Deployment would recreate the Pod if deleted, here we see this doesn’t happen. Let’s check why:
1 | ➜ k -n team-red get rs |
There we go! Finally we write the reason into the requested file so that Mr Scoring will be happy too!
1 | # /opt/course/4/logs |
Pod Security Standards can give a great base level of security! But when one finds themselves wanting to deeper adjust the levels like baseline
or restricted
… this isn’t possible and 3rd party solutions like OPA could be looked at.
A default-deny NetworkPolicy avoids to accidentally expose a Pod in a namespace that doesn’t have any other NetworkPolicy defined.
一个默认拒绝(default-deny)的 NetworkPolicy 可避免在未定义任何其他 NetworkPolicy 的 namespace 中意外公开 Pod。
Create a new default-deny NetworkPolicy named denynetwork
in the namespace development
for all traffic of type Ingress
.
The new NetworkPolicy must deny all ingress traffic in the namespace development
.
Apply the newly created default-deny
NetworkPolicy to all Pods running in namespace development
.
You can find a skeleton manifest file at /cks/15/p1.yaml
为所有类型为 Ingress+Egress 的流量在 namespace testing 中创建一个名为 denynetwork
的新默认拒绝 NetworkPolicy。 此新的 NetworkPolicy 必须拒绝 namespace testng 中的所有的 Ingress + Egress 流量。
将新创建的默认拒绝 NetworkPolicy 应用与在 namespace testing 中运行的所有 Pod。
你可以在 /cks/15/p1.yaml
找到一个模板清单文件。
1 | ### 修改模板清单文件 |
create a NetworkPolicy named pod-restriction
to restrict access to Pod products-service
running in namespace dev-team
.
Only allow the following Pods to connect to Pod products-service
:
qa
environment: testing
, in any namespaceMake sure to apply the NetworkPolicy.
You can find a skelet on manifest file at /cks/6/p1.yaml
创建一个名为 pod-restriction
的 NetworkPolicy 来限制对在 namespace dev-team
中运行的 Pod products-service
的访问。只允许以下 Pod 连接到 Pod products-service
:
qa
中的 Podenvironment: testing
的 Pod注意:确保应用 NetworkPolicy。
你可以在/cks/net/po.yaml
找到一个模板清单文件。
搜索 networkpolicy(网络策略)
https://kubernetes.io/zh-cn/docs/concepts/services-networking/network-policies/
1 | ### (1)查看命名空间 qa 具有的标签 |
绑定到 Pod 的 ServiceAccount 的 Role 授予过度宽松的权限。完成以下项目以减少权限集。
A Role bound to a Pod’s serviceAccount grants overly permissive permissions.
Complete the following tasks to reduce the set of permissions.
一个名为 web-pod
的现有 Pod 已在 namespace db
中运行。编辑绑定到 Pod 的 ServiceAccount service-account-web
的现有 Role, 仅允许只对 services 类型的资源执行 get
操作。
db
中创建一个名为 role-2
,并仅允许只对 namespaces
类型的资源执行 delete
操作的新 Role。role-2-binding
的新 RoleBinding,将新创建的 Role 绑定到 Pod 的 ServiceAccount。Given an existing Pod named web-pod
running in the namespace db
. Edit the existing Role bound to the Pod’s serviceAccount sa-dev-1
to only allow performing list
operations, only on resources of type Endpoints
.
role-2
in the namespace db
, which only allows performing update
operations, only on resources of type persistentvolumeclaims
role-2-binding
binding the newly created Role to the Pod’s serviceAccount.Don’t delete the existing RoleBinding
搜索 clusterrole(使用RBAC鉴权)
https://kubernetes.io/zh-cn/docs/reference/access-authn-authz/rbac/
1 | ### 查询 sa 对应的 role 名称(假设是 role-1) |
Enable audit logs in the cluster.
To do so, enable the log backend, and ensure that:
/var/log/kubernetes/audit-logs.txt
10
days2
auditlog files are retained/etc/kubernetes/logpolicy/sample-policy.yaml
. it only specifies what not to log. The base policy is located on the cluster’s master
node.Edit and extend the basic policy to log:
namespaces
changes at RequestResponse
levelfront-apps
configMap
and secret
changes in all namespaces at the Metadata
levelcatch-all
ruie to log all other requests at the Metadata
level.Don’t forget to apply the modifiedpolicy./etc/kubernetes/logpolicy/sample-policy.yaml
在 cluster 中启用审计日志。为此,请启用日志后端,并确保:
/var/log/kubernetes/audit-logs.txt
10
天2
个旧审计日志文件编辑和扩展基本策略以记录:
搜索 audit(审计)
https://kubernetes.io/zh-cn/docs/tasks/debug/debug-cluster/audit/
1 | ### 如果没有 /var/log/kubernetes/,则创建目录 |
Retrieve the content of the existing secret named db1-test
in the istio-system
namespace.
store the username field in a file named /home/candidate/user.txt
, and the password field in a file named /home/candidate/pass
.txt.
You must create both files; they don’t exist yet.
Do not use/modify the created files in the following steps, create new temporary files if needed.
Create a new secret named db2-test
in the istio-system
namespace, with the following
Finally, create a new Pod that has access to the secret db2-test via a volume:
secret-pod
istio-system
dev-container
nginx
secret-volume
/etc/secret
在 namespace istio-system
中获取名为 db1-test
的现有 secret 的内容.
将 username 字段存储在名为 /cks/sec/user.txt
的文件中,并将 password 字段存储在名为 /cks/sec/pass.txt
的文件中。
注意:你必须创建以上两个文件,他们还不存在。
注意:不要在以下步骤中使用/修改先前创建的文件,如果需要,可以创建新的临时文件。
在 istio-system
namespace 中创建一个名为 db2-test
的新 secret,内容如下:
最后,创建一个新的 Pod,它可以通过卷访问 secret db2-test
:
secret-pod
istio-system
dev-container
nginx
secret-volume
/etc/secret
搜索 secret(使用 kubectl 管理 Secret)
https://kubernetes.io/zh-cn/docs/tasks/configmap-secret/managing-secret-using-kubectl/
搜索 secret(Secret)
https://kubernetes.io/zh-cn/docs/concepts/configuration/secret/
1 | ### 检索已存在的 secret,将获取到的用户名和密码字段存储到指定文件 |
/cks/docker/Dockerfile
fixing two instructions present in the file being prominent security/best-practice issues./cks/docker/deployment.yaml
fixing two fields present in the file being prominent security/best-practice issues.Don’t add or remove configuration settings; only modify the existing configuration settings, so that two configuration settings each are no longer security/best-practice concerns.
Should you need an unprivileged user for any of the tasks, use user nobody
with user id 65535
.
- 分析和编辑给定的 Dockerfile
/cks/docker/Dockerfile
(基于 ubuntu:16.04 镜像),并修复在文件中拥有的突出的安全/最佳实践问题的两个指令。- 分析和编辑给定的清单文件
/cks/docker/deployment.yaml
,并修复在文件中拥有突出的安全/最佳实践问题的两个字段。注意:请勿添加或删除配置设置;只需修改现有的配置设置让以上两个配置设置都不再有安全/最佳实践问题。
注意:如果您需要非特权用户来执行任何项目,请使用用户 ID 65535 的用户 nobody 。
答题: 注意,本次的 Dockerfile 和 deployment.yaml 仅修改即可,无需部署。
1 | ### 修复 dockerfile 文件中存在的两个安全/最佳实践指令 |
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 scan for and reject the use of vulnerable images.
cluster 上设置了容器镜像扫描器,但尚未完全集成到 cluster 的配置中。完成后,容器镜像扫描器应扫描并拒绝易受攻击的镜像的使用。
You have to complete the entire task on the cluster’s master
node, where all services and files have been prepared and placed.
Given an incomplete configuration in directory /etc/kubernetes/epconfig
and a functional container image scanner with HTTPS endpoint https://acme.local:8082/image_policy
:
/cks/1/web1.yaml
You can find the container image scanner’s log file at /var/loglimagepolicyiacme.log
注意:你必须在 cluster 的 master 节点上完成整个考题,所有服务和文件都已被准备好并放置在该节点上。 给定一个目录 /etc/kubernetes/epconfig
中不完整的配置以及具有 HTTPS 端点 https://acme.local:8082/image_policy
的功能性容器镜像扫描器:
/cks/img/web1.yaml
来测试配置是否有效。你可以在 /var/log/imagepolicy/roadrunner.log
找到容器镜像扫描仪的日志文件。
搜索 imagepolicywebhook(使用准入控制器),接着再搜索字符串”imagepolicywebhook”
https://kubernetes.io/zh-cn/docs/reference/access-authn-authz/admission-controllers/
1 | # 0 master node |
Context: it is best-practice to design containers to be stateless and immutable
Inspect Pods runnings in namespace production
and delete any Pod that is either not stateless or not immutable.
Use the following strict interpretation of stateless and immutable:
privileged
in any way must be treated as potentially not stateless and not immutable.检查在 namespace production 中运行的 Pod,并删除任何非无状态或非不可变的 Pod。
使用以下对无状态和不可变的严格解释:
1 | ### 在命名空间 dev 中检查 running 状态的 pod |
This cluster uses containerd as CRI runtime.
Containerd’s default runtime handler is runc
. Containerd has been prepared to support an additional runtime handler , runsc
(gVisor).
该 cluster 使用 containerd 作为 CRI 运行时。
containerd 的默认运行时处理程序是runc
。 containerd 已准备好支持额外的运行时处理程序runsc
(gVisor)。
Create a RuntimeClass named untrusted
using the prepared runtime handler named runsc
.
Update all Pods in the namespace server
to run on gvisor, unless they are already running on anon-default
runtime handler.
You can find a skeleton manifest file at /cks/13/rc.yaml
使用名为
runsc
的现有运行时处理程序,创建一个名为untrusted
的 RuntimeClass。
更新 namespaceserver
中的所有 Pod 以在 gVisor 上运行。
您可以在/cks/gVisor/rc.yaml
中找到一个模版清单
搜索 runtimeclass(容器运行时类)
https://kubernetes.io/zh-cn/docs/concepts/containers/runtime-class/
1 | ### 修改模板清单文件 |
由 kubeadm 创建的 cluster 的 Kubernetes API 服务器,出于测试目的,临时配置允许未经身份验证和未经授权的访问,授予匿名用户 cluster-admin
的访问权限。
重新配置 cluster 的 Kubernetes API 服务器,以确保只允许经过身份验证和授权的 REST 请求。
使用授权模式 Node
,RBAC
和准入控制器 NodeRestriction。
删除用户 system:anonymous
的 ClusterRoleBinding 来进行清理。
注意:所有 kubectl 配置环境/文件也被配置使用未经身份验证和未经授权的访问。 你不必更改它,但请注意,一旦完成 cluster 的安全加固, kubectl 的配置将无法工作。 您可以使用位于 cluster 的 master 节点上,cluster 原本的 kubectl 配置文件 /etc/kubernetes/admin.conf ,以确保经过身份验证的授权的请求仍然被允许。
1 | k get nodes |
From: http://liyuankun.top/Kubernates-Certified%20Kubernetes%20Security%20Specialist-CKS.html
1、Pod 指定 ServiceAccount
Task
2、RBAC - RoleBinding
Context 绑定到 Pod 的 ServiceAccount 的 Role 授予过度宽松的权限。完成以下项目以减少权限集。
Task
一个名为 web-pod 的现有 Pod 已在 namespace db 中运行。 编辑绑定到 Pod 的 ServiceAccount service-account-web 的现有 Role, 仅允许只对 services 类型的资源 执行 get 操作。 在namespace db 中创建一个名为 role-2 ,并仅允许只对 namespaces 类型的资源执行 delete 操作的新Role。 创建一个名为 role-2-binding 的新 RoleBinding,将新创建的 Role 绑定到 Pod 的ServiceAccount。 注意:请勿删除现有的 RoleBinding。
3、启用 API server 认证
Context 由 kubeadm 创建的 cluster 的 Kubernetes API 服务器,出于测试目的,临时配置允许未经身份验证和未经授权的访问,授予匿名用户 cluster-admin 的访问权限.
Task
重新配置 cluster 的Kubernetes APl 服务器,以确保只允许经过身份验证和授权的 REST 请求。 使用授权模式 Node,RBAC 和准入控制器
NodeRestriction。 删除用户 system:anonymous 的 ClusterRoleBinding 来进行清理。
注意:所有 kubectl 配置环境/文件也被配置使用未经身份验证和未经授权的访问。 你不必更改它,但请注意,一旦完成 cluster
的安全加固, kubectl 的配置将无法工作。 您可以使用位于 cluster 的 master 节点上,cluster 原本的kubectl 配置文件 /etc/kubernetes/admin.conf ,以确保经过身份验证的授权的请求仍然被允许。
4、Sysdig & falco
Task:
使用运行时检测工具来检测 Pod tomcat 单个容器中频发生成和执行的异常进程。 有两种工具可供使用:
⚫ sysdig
⚫ falco
注: 这些工具只预装在 cluster 的工作节点,不在 master 节点。 使用工具至少分析 30 秒,使用过滤器检查生成和执行的进程,将事件写到 /opt/KSR00101/incidents/summary 文件中,其中包含检测的事件, 格式如下: [timestamp],[uid],[processName] 保持工具的原始时间戳格式不变。
注:确保事件文件存储在集群的工作节点上。
5、 容器安全,删除特权 Pod
Task
检查在 namespace production 中运行的 Pod,并删除任何非无状态或非不可变的 Pod。
使用以下对无状态和不可变的严格解释:
⚫ 能够在容器内存储数据的 Pod 的容器必须被视为非无状态的。
注意:你不必担心数据是否实际上已经存储在容器中。
⚫ 被配置为任何形式的特权 Pod 必须被视为可能是非无状态和非不可变的。
6、沙箱运行容器 gVisor
Context 该 cluster 使用 containerd 作为 CRI 运行时。containerd 的默认运行时处理程序是runc。 containerd 已准备好支持额外的运行时处理程序 runsc (gVisor)。 Task 使用名为 runsc的现有运行时处理程序,创建一个名为 untrusted 的 RuntimeClass。 更新 namespace server 中的所有Pod 以在 gVisor 上运行。
您可以在 /cks/gVisor/rc.yaml 中找到一个模版清单
7、Pod 安全策略-PSP
Context PodSecurityPolicy 应防在特定 namespace 中特权 Pod 的创建。 Task 创建一个名为restrict-policy 的新的PodSecurityPolicy,以防止特权 Pod 的创建。 创建一个名为restrict-access-role 并使用新创建的 PodSecurityPolicy restrict-policy 的ClusterRole。 在现有的 namespace staging 中创建一个名为 psp-denial-sa 的新ServiceAccount 。最后,创建一个名为 dany-access-bind 的 ClusterRoleBinding ,将新创建的 ClusterRole restrict-access-role 绑定到新创建的 ServiceAccount psp-denial-sa。
你可以在一下位置找到模版清单文件: /cks/psp/psp.yaml
8、创建 Secret
Task
在 namespace istio-system 中获取名为 db1-test 的现有 secret 的内容 将 username字段存储在名为 /cks/sec/user.txt 的文件中,并将 password 字段存储在名为 /cks/sec/pass.txt 的文件 中。注意:你必须创建以上两个文件,他们还不存在。 注意:不要在以下步骤中使用/修改先前创建的文件,如果需要,可以创建新的临时文件。
在 istio-system namespace 中创建一个名为 db2-test 的新 secret,内容如下:
username : production-instance
password : KvLftKgs4aVH
最后,创建一个新的 Pod,它可以通过卷访问 secret db2-test :
Pod 名称 secret-pod
Namespace istio-system
容器名 dev-container
镜像 nginx
卷名 secret-volume
挂载路径 /etc/secret
9、AppArmor
Context
APPArmor 已在 cluster 的工作节点上被启用。一个 APPArmor 配置文件已存在,但尚未被实施
Task
在 cluster 的工作节点上,实施位于 /etc/apparmor.d/nginx_apparmor 的现有 APPArmor配置文件。 编辑位于 /home/candidate/KSSH00401/nginx-deploy.yaml 的现有清单文件以应用 AppArmor 配置文件。 最后,应用清单文件并创建其中指定的 Pod 。
10、kube-bench 修复不安全项
Context 针对 kubeadm 创建的 cluster 运行 CIS 基准测试工具时, 发现了多个必须立即解决的问题。
Task
通过配置修复所有问题并重新启动受影响的组件以确保新的设置生效。
修复针对 API 服务器发现的所有以下违规行为:
1.2.7 Ensure that the –authorization-mode argument is not set to AlwaysAllow
1.2.8 Ensure that the –authorization-mode argument includes Node
1.2.9 Ensure that the –authorization-mode argument includes RBAC
1.2.18 Ensure that the –insecure-bind-address argument is not set
1.2.19 Ensure that the –insecure-port argument is set to 0
FAIL FAIL FAIL FAIL FAIL
修复针对 kubelet 发现的所有以下违规行为:
Fix all of the following violations that were found against the kubelet: 4.2.1
Ensure that the anonymous-auth argument is set to false 4.2.2
Ensure that the –authorization-mode argument is not set to AlwaysAllow
FAIL FAIL
注意:尽可能使用 Webhook 身份验证/授权。
修复针对 etcd 发现的所有以下违规行为:
Fix all of the following violations that were found against etcd:
2.2 Ensure that the –client-cert-auth argument is set to true FAIL
11、网络策略 NetworkPolicy
Task
创建一个名为 pod-restriction 的 NetworkPolicy 来限制对在 namespace dev-team 中运行的 Pod products-service 的访问。 只允许以下 Pod 连接到 Pod products-service
⚫ namespace qa 中的 Pod
⚫ 位于任何 namespace,带有标签 environment: testing 的 Pod
注意:确保应用 NetworkPolicy。 你可以在/cks/net/po.yaml 找到一个模板清单文件。
12、Dockerfile 检测
Task
分析和编辑给定的 Dockerfile /cks/docker/Dockerfile(基于 ubuntu:16.04 镜像),
并修复在文件中拥有的突出的安全/最佳实践问题的两个指令。 分析和编辑给定的清单文件 /cks/docker/deployment.yaml
, 并修复在文件中拥有突出的安全/最佳实践问题的两个字段。
注意:请勿添加或删除配置设置;只需修改现有的配置设置让以上两个配置设置都不再有安全/最佳实践问题。
注意:如果您需要非特权用户来执行任何项目,请使用用户 ID 65535 的用户 nobody 。 答题: 注意,本次的 Dockerfile
和 deployment.yaml 仅修改即可,无需部署。
13、ImagePolicyWebhook 容器镜像扫描
Context
cluster 上设置了容器镜像扫描器,但尚未完全集成到 cluster 的配置中。完成后,容器镜像扫描器应扫描并拒绝易受攻击的镜像的使用。
Task
注意:你必须在 cluster 的 master 节点上完成整个考题,所有服务和文件都已被准备好并放置在该节点上。
给定一个目录 /etc/kubernetes/epconfig 中不完整的配置以及具有 HTTPS 端点 https://acme.local:8082/image_policy 的功能性容器镜像扫描器:
启用必要的插件来创建镜像策略
校验控制配置并将其更改为隐式拒绝(implicit deny)
编辑配置以正确指向提供的 HTTPS 端点
最后,通过尝试部署易受攻击的资源 /cks/img/web1.yaml 来测试配置是否有效。
你可以在 /var/log/imagepolicy/roadrunner.log 找到容器镜像扫描仪的日志文件。
14、Trivy 扫描镜像安全漏洞
Task
使用 Trivy 开源容器扫描器检测 namespace kamino 中 Pod 使用的具有严重漏洞的镜像。 查找具有 High 或 Critical 严重性漏洞的镜像,并删除使用这些镜像的 Pod。
注意:Trivy 仅安装在 cluster 的 master 节点上, 在工作节点上不可使用。 你必须切换到 cluster 的 master 节点才能使用 Trivy
15、默认网络策略
Context
一个默认拒绝(default-deny)的 NetworkPolicy 可避免在未定义任何其他 NetworkPolicy 的 namespace 中 意外公开 Pod。
Task
为所有类型为 Ingress+Egress 的流量在 namespace testing 中创建一个名为 denypolicy 的新默认拒绝 NetworkPolicy。 此新的 NetworkPolicy 必须拒绝 namespace testing 中的所有的 Ingress + Egress 流量。 将新创建的默认拒绝 NetworkPolicy 应用与在 namespace testing 中运行的所有 Pod。
你可以在 /cks/net/p1.yaml 找到一个模板清单文件。
16、日志审计 log audit
Task
在 cluster 中启用审计日志。为此,请启用日志后端,并确保:
⚫ 日志存储在 /var/log/kubernetes/audit-logs.txt
⚫ 日志文件能保留 10 天
⚫ 最多保留 2 个旧审计日志文件
/etc/kubernetes/logpolicy/sample-policy.yaml 提供了基本策略。它仅指定不记录的内容。
注意:基本策略位于 cluster 的 master 节点上。 编辑和扩展基本策略以记录:
⚫ RequestResponse 级别的
cronjobs 更改
⚫ namespace front-apps 中 deployment 更改的请求体
⚫ Metadata级别的所有 namespace 中的 ConfigMap 和 Secret 的更改 此外,添加一个全方位的规则以在 Metadata
级别记录所有其他请求。 注意:不要忘记应用修改后的策略
云原生|kubernetes|2022年底cks真题解析(1-10)_cks试题_晚风_END的博客-CSDN博客
云原生|kubernetes|2022年底cks真题解析(11-16)_晚风_END的博客-CSDN博客
Start : 2024.1.15
DDL1:2024.2.3 15:00 (Rescheduled)
DDL2:2024.2.8 20:00 (Failed)
DDL3: 2024.2.23 23:30 (Success)
1 | # Other prerequisites |
There may be lots of impediments to setting up this kubernetes cluster successfully due to network conditions or some misconfigurations, but those above can be solved step by step. Finally, node(s) is(are) ready as follows:
1 | alias k=kubectl # will already be pre-configured |
Function | Description | Example | Result |
---|---|---|---|
text |
the plain text | kind is {.kind} |
kind is List |
@ |
the current object | {@} |
the same as input |
. or [] |
child operator | {.kind} , {['kind']} or {['name\.type']} |
List |
.. |
recursive descent | {..name} |
127.0.0.1 127.0.0.2 myself e2e |
* |
wildcard. Get all objects | {.items[*].metadata.name} |
[127.0.0.1 127.0.0.2] |
[start:end:step] |
subscript operator | {.users[0].name} |
myself |
[,] |
union operator | {.items[*]['metadata.name', 'status.capacity']} |
127.0.0.1 127.0.0.2 map[cpu:4] map[cpu:8] |
?() |
filter | {.users[?(@.name=="e2e")].user.password} |
secret |
range , end |
iterate list | {range .items[*]}[{.metadata.name}, {.status.capacity}] {end} |
[127.0.0.1, map[cpu:4]] [127.0.0.2, map[cpu:8]] |
'' |
quote interpreted string | {range .items[*]}{.metadata.name}{'\t'}{end} |
127.0.0.1 127.0.0.2 |
\ |
escape termination character | {.items[0].metadata.labels.kubernetes\.io/hostname} |
127.0.0.1 |
Examples using kubectl
and JSONPath expressions:
1 | kubectl get pods -o json |
examples
1 | # Read a value |
常规使用
组装命令并执行
https://kubernetes.io/docs/concepts/cluster-administration/logging/#system-component-logs
对 kubelet 组件:journalctl -xefu kubelet
对以容器形式启动的 kubernetes 组件:在/var/log/pods
下(当把kube-apiserver的yaml弄坏起不来之后,应该可以在这个目录下查看启动失败的原因)
group为空时表示core
group,此时的 gv 缩写只有 v,即
1 | $ kubectl api-resources --api-group='' |
常见的控制器资源基本属于apps
group
1 | NAME SHORTNAMES APIVERSION NAMESPACED KIND |
常见的几种需要填充group的地方
rbac
role.rules.apiGroups
只需要填写group
audit policy
rules.resources.group
只需要填写group
客观局限
16
道题,需要在120
分钟内完成,完成一道题的平局时间应该120/16=7
分钟;主观局限
改进措施
67
,粗略估计取得证书,需要做完67/(100/16)=11
道题,可以允许5
道题不做,但每题的平均用时为10
分钟多一点。1
分钟浏览全题,理解题意,并评估是否有把握能完成;Reference: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
创建sa、role、rolebinding
1 | kubectl create sa shs-sa |
使用该ServiceAccount
1 | kubectl patch -p '{"spec":{"template":{"spec":{"serviceAccountName":"shs-saax","serviceAccount":"shs-saax"}}}}' deployment shs-dep |
Tips:
deploy.spec.template.spec.serviceAccount
与 deploy.spec.template.spec.serviceAccountName
都需要修改。Egress
type, then only out connections mentioned in it allowed. If lots of NetworkPolicy select the same pod, then all connections mentoined in those list are allowed. Additive. 1 | apiVersion: networking.k8s.io/v1 |
parameters of to
and from
was the same, as follows(irrelevant informations are omitted):
1 | $ kubectl explain networkpolicy.spec.ingress |
details of NetworkPolicyPeer
are as follows:
1 | $ kubectl explain networkpolicy.spec.egress.to |
As for details of IPBlock
and LabelSelector
, just kubectl explain
before coding yamls.
NetworkPolicy
was namespaced, and only works in the namespace to which it belongs.NetworkPolicy
can define only allowed rules.NetworkPolicy
selects pod by labels only.Deny all in & out bound traffics for a pod
1 | apiVersion: networking.k8s.io/v1 |
Ref: https://kubernetes.io/blog/2019/08/06/opa-gatekeeper-policy-and-governance-for-kubernetes
gatekeeper admission controller 拦截所有资源的创建、更新、删除请求,并针对相关资源,做所配置的校验。
1 | apiVersion: templates.gatekeeper.sh/v1beta1 |
每个命名空间都需要一个标签hr
1 | apiVersion: constraints.gatekeeper.sh/v1beta1 |
Gatekeeper stores audit results as violations
listed in the status
field of the relevant Constraint.
1 | apiVersion: constraints.gatekeeper.sh/v1beta1 |
Confine programs or containers to limited set of resources, such as Linux capabilities, network access, file permissions, etc.
Add annotations to pod which needed to be secured with key, name of container in Pod should be referred in key:
1 | container.apparmor.security.beta.kubernetes.io/<container_name>: <profile_ref> |
The profile_ref
can be one of:
runtime/default
to apply the runtime’s default profilelocalhost/<profile_name>
to apply the profile loaded on the host with the name <profile_name>
unconfined
to indicate that no profiles will be loadedkubectl exec <pod_name> -- cat /proc/1/attr/current
1 | $ apparmor_parser /etc/apparmor.d/nginx_apparmor |
Reference: https://kubernetes.io/docs/reference/config-api/apiserver-audit.v1/#audit-k8s-io-v1-Policy
RequestReceived
- Before handled by handler chainResponseStarted
- After response header sent, but before response body sentResponseComplete
- After response body sentPanic
- After panic occurred.None
- don’t log events that match this ruleMetadata
- log request metadata only(user, timestamp,resource,vert) but not request or response body.Request
- log event metadata plus request bodyRequestResponse
- log event metadata plus request, response bodies.1 | apiVersion: audit.k8s.io/v1 |
Configure it to kube-apiserver, see audit log.
If the Policy
doesn’t work as expected, check kube-apiserver logs as below, make sure the Policy
was loaded successfully. Since it seems to load a default AuditPolicy
when failled to load the AuditPolicy
passed in parameters of kube-apiserver. Logs are as below:
1 | W0122 16:00:29.139016 1 reader.go:81] Audit policy contains errors, falling back to lenient decoding: strict decoding error: unknown field "rules[0].resources[0].resource" |
Reference
The Pod Security Standards define three different policies to broadly cover the security spectrum. These policies are cumulative and range from highly-permissive to highly-restrictive. This guide outlines the requirements of each policy.
3种策略,每种策略只是定义了检查、校验哪些字段、即校验范围。此3种策略,从上至下,校验范围依次增大。具体校验内容,可参考文档。
Profile | Description |
---|---|
Privileged | Unrestricted policy, providing the widest possible level of permissions. This policy allows for known privilege escalations. |
Baseline | Minimally restrictive policy which prevents known privilege escalations. Allows the default (minimally specified) Pod configuration. |
Restricted | Heavily restricted policy, following current Pod hardening best practices. |
有3种针对不符合上述3种Policy的处理方式,即强制要求(否则拒绝创建)、记录到审计日志中、用户可见警告。
Mode | Description |
---|---|
enforce | Policy violations will cause the pod to be rejected. |
audit | Policy violations will trigger the addition of an audit annotation to the event recorded in the audit log, but are otherwise allowed. |
warn | Policy violations will trigger a user-facing warning, but are otherwise allowed. |
在命名空间上打标签
1 | # The per-mode level label indicates which policy level to apply for the mode. |
Reference: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/
总共有两个安全配置的地方,位置分别为
pod.spec.securityContext
属于 PodSecurityContext
这个结构体,表示pod中所有的容器都使用这个配置;pod.spec["initContainers","containers"].securityContext
属于 SecurityContext
这个结构体,只限于当前容器使用此配置,且优先级高于上面的配置。上述两种不同位置的安全配置中,有的字段是重复的,SecurityContext
的优先级更高。两者之间值的差异(都存在的字段已加粗):
PodSecurityContext | SecurityContext |
---|---|
fsGroup | allowPrivilegeEscalation |
fsGroupChangePolicy | capabilities |
runAsGroup | privileged |
runAsNonRoot | procMount |
runAsUser | readOnlyRootFilesystem |
seLinuxOptions | runAsGroup |
seccompProfile | runAsNonRoot |
supplementalGroups | runAsUser |
sysctls | seLinuxOptions |
windowsOptions | seccompProfile |
windowsOptions |
按照如下要求修改 sec-ns
命名空间里的 Deployment secdep
一、用 ID 为 30000 的用户启动容器(设置用户 ID 为: 30000 runAsUser
)
二、不允许进程获得超出其父进程的特权(禁止 allowPrivilegeEscalation
)
三、以只读方式加载容器的根文件系统(对根文件的只读权限readOnlyRootFilesystem
)
注意点:
readOnlyRootFilesystem
和 allowPrivilegeEscalation
只存在于SecurityContext
中,因此需要为各个容器都配置上,需注意容器数量,避免漏配;runAsUser
存在于PodSecurityContext
和SecurityContext
中,可只配 PodSecurityContext
Reference: https://kubernetes.io/docs/concepts/containers/runtime-class/
RuntimeClass
RuntimeClass
in pod.spec.runtimeClassName
Reference: https://kubernetes.io/docs/concepts/configuration/secret
练手速【来源】
在 namespace istio-system
中获取名为 db1-test
的现有 secret 的内容。将 username 字段存储在名为 /cks/sec/user.txt
的文件中,并将 password 字段存储在名为 /cks/sec/pass.txt
的文件中。
注意:你必须创建以上两个文件,他们还不存在。
注意:不要在以下步骤中使用/修改先前创建的文件,如果需要,可以创建新的临时文件。
在istio-system
namespace 中创建一个名为 db2-test
的新 secret,内容如下:
username : production-instance
password : KvLftKgs4aVH
db2-test
Pod 名称 secret-pod
Namespace istio-system
容器名 dev-container
镜像 nginx
卷名 secret-volume
挂载路径 /etc/secret
Reference: https://kubernetes.io/docs/concepts/security/service-accounts/
1 | $ kubectl explain sa.automountServiceAccountToken |
Set one of fields above to false
to prevent auto injection for a pod.
Restrict access to Secrets
Set annotation kubernetes.io/enforce-mountable-secrets
to true
for a ServiceAccount
, then only secrets in the field of sa.secrets
of this ServiceAccount was allowed to use in a pod, such as a secret volume,
envFrom
, imagePullSecrets
.
How to use ServiceAccount to connect to apiserver? reference
1 | curl --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt --header "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -X GET https://kubernetes.default.svc/api/v1/namespaces/default/secrets |
整个kubeAPIServer提供了三类API Resource接口:
/api/v1
下;/apis/$GROUP/$VERSION
;/metrics
、/version
等;而API的URL大致以 /apis/{group}/{version}/namespaces/{namespace}/{resources}/{name}
组成,结构如下图所示:
https://img2020.cnblogs.com/other/2041406/202101/2041406-20210120094608734-1433747602.png
Tips:
在apiserver的URL中,资源需要用复数形式,如:
How to use etcdctl
to get raw data from etcd?
1 | ETCDCTL_API=3 etcdctl --cacert=/etc/kubernetes/pki/etcd/ca.crt \ |
Follow these steps:
k drain controller-plane
apt-mark unhold kubeadm
apt-mark hold kubelet kubectl
apt update && apt upgrade -y
kubeadm upgrade plan
kubeadm upgrade apply v1.2x.x
kubeadm upgrade plan(for check purpose)
apt-mark hold kubeadm
apt-mark unhold kubelet kubectl
apt install kubectl=1.2x.x kubelet=1.2x.x
apt-mark hold kubelet kubectl
systemctl restart kubelet
systemctl status kubelet
k uncordon controller-plane
k drain node
apt update
apt-mark unhold kubeadm
apt-mark hold kubectl kubelet
apt install kubeadm=1.2x.x
kubeadm upgrade plan
kubeadm upgrade node
apt-mark hold kubeadm
apt-mark unhold kubectl kubelet
apt install kubectl=1.2x.x kubelet=1.2x.x
systemctl restart kubelet
systemctl status kubelet
k uncordon kubelet
k get node
https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#imagepolicywebhook
A tool to detect potential security issues and give the specific means to solve the issue.
Reference:
1 | # Simple way in a kubernetes cluster created by kubeadm |
Contents
Consists of the following topics:
Each topic starts with a list of items which was checked with checked status, then a list of remediations to FAIL or WARN items given. You can fix those issues under the given instructions. At last, check summary of this topic.
Here is a output example for topic master
1 | [WARN] 1.1.9 Ensure that the Container Network Interface file permissions are set to 600 or more restrictive (Manual) |
Full contexts can be touch by this link
Reference: https://github.com/aquasecurity/trivy
Scan a docker image
扫描某命名空间下所有pod所使用的镜像包含 HIGH, CRITICAL
类型漏洞,并删除该pod
1 | k get pod -A -ojsonpath="{range .items[*]}{.spec['initContainers','containers'][*].image} {.metadata.name} {'#'} {end}" | sed 's|#|\n|g' | sed 's|^ ||g' | sed 's| $||g' | awk '{cmd="echo "$2"; trivy -q image "$1" --severity HIGH,CRITICAL | grep Total";system(cmd)}' |
该命令的注意点:
jsonpath
rangeawk
system(cmd)sed
replaceReference: https://github.com/draios/sysdig
deb
from sysdig-releasesudo dpkg -i sysdig-0.34.1-x86_64.deb
sudo apt -f install
1 | %evt.num %evt.outputtime %evt.cpu %proc.name (%thread.tid) %evt.dir %evt.type %evt.info |
Notes:
evt.dir
aka event direction, < represents out, > represents in.evt.type
aka event type, perceiving it as a name of system call.predefined function sets based on sysdig events, to implements complex situation. Locates in /usr/share/sysdig/chisels
on Linux machine.
What are those chisels?
1 | # See HTTP log |
1 | $ sysdig -i spy_file |
关于filter可用的字段,可以通过sysdig -l
来查看所有支持的字段。例如查看容器相关的过滤字段,有:
1 | ubuntu@primary:~$ sysdig -l | grep "^container." |
可以看出,container.id
只能取前12个字符,另外也可以用容器id的全名,即container.full_id
。另外k8s可支持的字段有:
1 | ubuntu@primary:~$ sysdig -l | grep "^k8s." |
此处有坑
使用container.id
过滤时,注意id的长度需要为12
,不然数据出不来。通过crictl ps
看到的container id
是13位的,使用sysdig时,需要注意长度。
1 | ubuntu@primary:~$ crictl ps | grep -v ^C | awk '{print $1,$2,$6,$7}' |
Reference: https://falco.org/docs
监控进程的系统调用和信号量,基础的使用方式
strace -p <pid>
strace <binary-name>
strace -e trace=file
Requirments of your computer, microphone, camera, speaker, etc.
Don’t use headphone, earbuds.
Exam Details
Online tests, 15-20 performance-based tasks, 2 hours to complete the tasks.
Don’t cheat, audio,camera,screen capture of the test will be reviewed.
下载镜像:Index of /releases/22.03.5/targets/x86/64/ (openwrt.org)
转换镜像:
1 | qemu-img convert -f raw -O vdi openwrt-22.03.5-x86-64-generic-ext4-combined-efi.img openwrt-22.03.5-x86-64-generic-ext4-combined-efi.img.vdi |
配置虚拟机网卡:
在VirtualBox中新建HostNetwork(Host-Only网络),网段为192.168.56.0/24
在openwrt虚拟机的网络选项中设置:
1)启用网卡1
,连接方式选仅主机网络
,名称选上步创建的HostNetwork
2)启用网卡2
,连接方式选桥接
网卡,名称选本机上能访问外网的网卡
进入openwrt虚拟机,为lan口设置静态IP地址为HostNetwork
中的一个IP,这里用 192.168.56.2
1 | root@OpenWrt:~# cat /etc/config/network |
同时也能使用scp
命令进行拷贝
1 | opkg update |
1 | #iptables |
如果需要强制安装,可以先opkg remove
,再opkg install
。
在Releases · vernesong/OpenClash (github.com)页面,将openclash安装包下载到openwrt虚拟机中,通过 opkg install
安装。
配置手册:Home · vernesong/OpenClash Wiki (github.com)
此处用的是ubuntu server,可以在安装界面时设置,也可以等安装完成后,手动修改配置。手动修改的配置如下:
1 | $ cat /etc/netplan/00-installer-config.yaml |
配置修改后,可以看到默认路由已变成 192.168.56.2
:
1 | $ ip route |