模拟题目:

设置配置环境:

[candidate@node-1] $ kubectl config use-context k8s

Task

test 命名空间,有一个名为 secnginx 的 pod,修改此 pod,为容器添加CAP_NET_ADMINCAP_SYS_TIME 权能

参考:

https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/security-context/#set-capabilities-for-a-container

apiVersion: v1
kind: Pod
metadata:
  name: security-context-demo-4
spec:
  containers:
  - name: sec-ctx-4
    image: gcr.io/google-samples/node-hello:1.0
    securityContext:
      capabilities:
        add: ["NET_ADMIN", "SYS_TIME"]

解答:

切换环境

kubectl config use-context k8s

通过模板修改现在运行pod的权限后重建

kubectl -n test get pod secnginx -oyaml > secnginx.yaml
cp secnginx.yaml secnginx.yaml-bak
kubectl delete -f secnginx.yaml

编辑yaml文件添加

vim secnginx.yaml
    securityContext:
      capabilities:
        add: ["CAP_NET_ADMIN","CAP_SYS_TIME"]

file

创建pod

kubectl apply -f secnginx.yaml