设置服务账号Service Accounts(sa)的token不挂载到pod - 人生的哲理
source link: https://www.cnblogs.com/renshengdezheli/p/17650021.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
一.系统环境
本文主要基于Kubernetes1.22.2和Linux操作系统Ubuntu 18.04。
服务器版本 | docker软件版本 | Kubernetes(k8s)集群版本 | CPU架构 |
---|---|---|---|
Ubuntu 18.04.5 LTS | Docker version 20.10.14 | v1.22.2 | x86_64 |
Kubernetes集群架构:k8scludes1作为master节点,k8scludes2,k8scludes3作为worker节点。
服务器 | 操作系统版本 | CPU架构 | 进程 | 功能描述 |
---|---|---|---|---|
k8scludes1/192.168.110.128 | Ubuntu 18.04.5 LTS | x86_64 | docker,kube-apiserver,etcd,kube-scheduler,kube-controller-manager,kubelet,kube-proxy,coredns,calico | k8s master节点 |
k8scludes2/192.168.110.129 | Ubuntu 18.04.5 LTS | x86_64 | docker,kubelet,kube-proxy,calico | k8s worker节点 |
k8scludes3/192.168.110.130 | Ubuntu 18.04.5 LTS | x86_64 | docker,kubelet,kube-proxy,calico | k8s worker节点 |
在我们使用Kubernetes进行容器化部署时,经常需要为Pod设置特定的权限。这种权限管理通常通过Kubernetes的服务账号(Service Accounts)实现。然而,默认情况下,Service Account的token会自动挂载到对应的Pod中,这可能引发一些安全问题。因此,本文将介绍如何设置Service Account的token不自动挂载到Pod中。
设置Kubernetes(k8s)服务账号Service Accounts(sa)的token不挂载到pod的前提是已经有一套可以正常运行的Kubernetes集群,关于Kubernetes(k8s)集群的安装部署,可以查看博客《Ubuntu 安装部署Kubernetes(k8s)集群》https://www.cnblogs.com/renshengdezheli/p/17632858.html。
三.Service Accounts(sa)简介
Service Accounts(服务账户)通常用于计算机程序或操作系统中。它们不是普通用户账户,而是被某些应用程序或服务用于特定的、通常自动化的任务。
在Kubernetes(一个开源的容器编排平台)中,Service Accounts主要为Pod中运行的进程提供身份。与面向人类的普通用户不同,Service Accounts由Kubernetes集群内的Pods/服务使用。
关于Service Accounts的关键点:
-
它们是有命名空间的:可以为每个命名空间创建Service Accounts,并且不能跨命名空间访问(除非被赋予明确的权限)。
-
默认情况下,当创建一个Pod时,Kubernetes会自动为其分配一个"default" Service Account,但你也可以创建并指定特定的Service Account。
-
Service Account 被绑定到一组角色(Roles),这些角色定义了 Service Account 的权限,例如对某些资源的读写权限等。
总的来说,Service Accounts 是一种安全和权限管理的机制,让我们能更好地控制在复杂环境中的应用程序和服务的访问权限。
默认情况下,Service Account的token会自动挂载到Pod内的应用程序使用。但在某些情况下,我们可能不希望token自动挂载,以避免潜在的安全风险。Service Accounts(sa)的token自动挂载到pod的方式具有安全隐患,假如我们拉取的镜像存有恶意代码,如果给了sa一个不正当的权限,pod使用这个sa权限的时候就可能干坏事,避免的方法有:
- 授予的权限最小化,够用就行 ;
- 如果没有必要 的话,不需要把sa的token挂载到pod里,不然恶意程序使用token干坏事。
关于Service Accounts(sa)的更多内容,请查看博客《Kubernetes(k8s)服务账号Service Accounts》。
四.在pod里设置sa的token不挂载到pod
不把Service Accounts(sa)的token挂载到pod有两种方法:
- 在pod里设置 ;
- 在sa里设置。
我们先使用第一种方法,现在没有pod。
root@k8scludes1:~/authorization# kubectl get pod
No resources found in authorization namespace.
pod.spec下有automountServiceAccountToken字段。
root@k8scludes1:~/authorization# kubectl explain pod.spec | grep automountService
automountServiceAccountToken <boolean>
查看automountServiceAccountToken参数的含义,automountServiceAccountToken字段的功能是:是否应自动挂载服务账户token。
root@k8scludes1:~/authorization# kubectl explain pod.spec.automountServiceAccountToken
KIND: Pod
VERSION: v1
FIELD: automountServiceAccountToken <boolean>
DESCRIPTION:
AutomountServiceAccountToken indicates whether a service account token
should be automatically mounted.
如下是pod的yaml文件,功能为:使用hub.c.163.com/library/nginx:latest镜像创建一个名为podtest的pod,不自动挂载sa的token到pod上。
root@k8scludes1:~/authorization# vim pod.yaml
root@k8scludes1:~/authorization# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: podtest
name: podtest
spec:
#automountServiceAccountToken: false:表示不会自动挂载服务账户token到Pod中。通常情况下,这个令牌用于授权Pod访问Kubernetes API。
automountServiceAccountToken: false
#terminationGracePeriodSeconds: 0:表示当需要关闭容器时,立即杀死容器而不等待默认的30秒优雅停机时长。
terminationGracePeriodSeconds: 0
containers:
- image: hub.c.163.com/library/nginx:latest
#imagePullPolicy: IfNotPresent:这表示如果本地不存在该镜像,则从远程仓库拉取。
imagePullPolicy: IfNotPresent
name: podtest
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
创建pod。
root@k8scludes1:~/authorization# kubectl apply -f pod.yaml
pod/podtest created
pod创建成功。
root@k8scludes1:~/authorization# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
podtest 1/1 Running 0 9s 10.244.218.143 k8scludes2 <none> <none>
进入pod,此时sa的token就没有挂载到pod上,应用程序就不可以使用token干坏事了。
root@k8scludes1:~/authorization# kubectl exec -it podtest -- bash
root@podtest:/# df -Th
Filesystem Type Size Used Avail Use% Mounted on
overlay overlay 97G 5.3G 87G 6% /
tmpfs tmpfs 64M 0 64M 0% /dev
tmpfs tmpfs 1.4G 0 1.4G 0% /sys/fs/cgroup
/dev/mapper/tom--vg-root ext4 97G 5.3G 87G 6% /etc/hosts
shm tmpfs 64M 0 64M 0% /dev/shm
tmpfs tmpfs 1.4G 0 1.4G 0% /proc/acpi
tmpfs tmpfs 1.4G 0 1.4G 0% /proc/scsi
tmpfs tmpfs 1.4G 0 1.4G 0% /sys/firmware
#退出pod
root@podtest:/# exit
exit
删除pod。
root@k8scludes1:~/authorization# kubectl delete pod podtest
pod "podtest" deleted
五.在sa里设置sa对应的token不挂载到pod上
默认存在一个名为default的sa。
root@k8scludes1:~/authorization# kubectl get sa
NAME SECRETS AGE
default 1 3d4h
生成创建sa的yaml文件。
root@k8scludes1:~/authorization# kubectl create sa satest --dry-run=client -o yaml >satest.yaml
sa的yaml文件如下:sa的名字为satest。
root@k8scludes1:~/authorization# cat satest.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
creationTimestamp: null
name: satest
sa有一个automountServiceAccountToken字段。
root@k8scludes1:~/authorization# kubectl explain sa | grep -A4 automountService
automountServiceAccountToken <boolean>
AutomountServiceAccountToken indicates whether pods running as this service
account should have an API token automatically mounted. Can be overridden
at the pod level.
查看sa的automountServiceAccountToken参数的含义,表示作为此服务账户运行的Pod是否应自动挂载API token,这可以在Pod级别被覆盖。
root@k8scludes1:~/authorization# kubectl explain sa.automountServiceAccountToken
KIND: ServiceAccount
VERSION: v1
FIELD: automountServiceAccountToken <boolean>
DESCRIPTION:
AutomountServiceAccountToken indicates whether pods running as this service
account should have an API token automatically mounted. Can be overridden
at the pod level.
修改sa的yaml文件,设置automountServiceAccountToken: false,这样设置之后,pod以satest的身份运行之后,默认是不挂载satest的token到pod上的。
root@k8scludes1:~/authorization# vim satest.yaml
root@k8scludes1:~/authorization# cat satest.yaml
apiVersion: v1
kind: ServiceAccount
automountServiceAccountToken: false
metadata:
creationTimestamp: null
name: satest
创建sa。
root@k8scludes1:~/authorization# kubectl apply -f satest.yaml
serviceaccount/satest created
sa创建成功。
root@k8scludes1:~/authorization# kubectl get sa
NAME SECRETS AGE
default 1 3d4h
satest 1 4s
如下是pod的yaml文件,功能为:使用hub.c.163.com/library/nginx:latest镜像创建一个名为podtest的pod,并且pod以satest的身份运行。
root@k8scludes1:~/authorization# vim pod.yaml
root@k8scludes1:~/authorization# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: podtest
name: podtest
spec:
#terminationGracePeriodSeconds: 0 表示当需要关闭容器时,立即杀死容器而不等待默认的30秒优雅停机时长。
terminationGracePeriodSeconds: 0
containers:
- image: hub.c.163.com/library/nginx:latest
#imagePullPolicy: IfNotPresent:表示如果本地已经存在该镜像,则不重新下载;否则从远程 Docker Hub 下载该镜像
imagePullPolicy: IfNotPresent
name: podtest
resources: {}
#serviceAccount: satest表示pod以satest的身份运行
serviceAccount: satest
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
创建pod。
root@k8scludes1:~/authorization# kubectl apply -f pod.yaml
pod/podtest created
pod创建成功。
root@k8scludes1:~/authorization# kubectl get pod
NAME READY STATUS RESTARTS AGE
podtest 1/1 Running 0 5s
进入pod,sa的token没有挂载到pod。
root@k8scludes1:~/authorization# kubectl exec -it podtest -- bash
#sa的token没有挂载到pod
root@podtest:/# df -hT
Filesystem Type Size Used Avail Use% Mounted on
overlay overlay 97G 5.3G 87G 6% /
tmpfs tmpfs 64M 0 64M 0% /dev
tmpfs tmpfs 1.4G 0 1.4G 0% /sys/fs/cgroup
/dev/mapper/tom--vg-root ext4 97G 5.3G 87G 6% /etc/hosts
shm tmpfs 64M 0 64M 0% /dev/shm
tmpfs tmpfs 1.4G 0 1.4G 0% /proc/acpi
tmpfs tmpfs 1.4G 0 1.4G 0% /proc/scsi
tmpfs tmpfs 1.4G 0 1.4G 0% /sys/firmware
root@podtest:/# exit
exit
本文介绍了如何在Kubernetes中设置Service Account的token不自动挂载到Pod中,以增强系统的安全性。希望本文能够帮助你更好地理解和使用Kubernetes的Service Account。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK