进入K8S Pod执行操作,遭遇Operation not permitted错误

前置需求

  • POD 名称ks-installer-58b9cf7c4c-qtgrn
  • 进入Pod修改/etc/resolv.conf文件权限(文件原为只读)

遭遇问题

1、通过k8s exec命令进入pod

kubectl exec -it ks-installer-58b9cf7c4c-qtgrn -n kubesphere-system /bin/bash

2、修改resolv.conf文件权限,提示

chmod 666 /etc/resolv.conf
chmod: /etc/resolv.conf: Operation not permitted

解决方法

         通过K8S EXEC进入Pod的用户是普通用户,而修改文件我们需要root权限。K8S EXEC命令目前没有找到指定用户的相关参数,但是Docker的EXEC命令可以通过-u指定root用户,那么可以通过docker命令进入Pod进行操作。

1、查询Pod的Docker容器Id

kubectl describe pod ks-installer-58b9cf7c4c-qtgrn -n kubesphere-system

image

2、通过docker exec 用管理员权限进入容器

docker exec -u root 1a0dfe3d4c75f30512a14756f63a200226aff2b157b0fdfa80e14f455cb40e69 -/bin/bash

注意:要注意pod是被k8s调度到那个节点上边,需要在对应的节点上执行docker命令

3、修改resolv.conf文件权限

原文地址:https://www.cnblogs.com/lswweb/p/13883742.html