本实验实现不给服务账号自动挂载 API 凭据并应用到Pod
Service AccountKubernetes 区分用户账户和服务账户的概念主要基于以下原因:
• 用户账户是针对人而言的。 服务账户是针对运行在 pod 中的进程而言的。
• 用户账户是全局性的。 其名称在集群各 namespace 中都是全局唯一的,未来的用户资源不会做 namespace 隔离, 服务账户是 namespace 隔离的。
• 通常情况下,集群的用户账户可能会从企业数据库进行同步,其创建需要特殊权限,并且涉及到复杂的业务流程。 服务账户创建的目的是为了更轻量,允许集群用户为了具体的任务创建服务账户 ( 即权限最小化原则 )。
• 对人员和服务账户审计所考虑的因素可能不同。
• 针对复杂系统的配置可能包含系统组件相关的各种服务账户的定义。 因为服务账户可以定制化地创建,并且有 namespace 级别的名称,这种配置是很轻量的。
对 pod 的改动通过一个被称为 Admission Controller 的插件来实现。它是 apiserver 的一部分。 当 pod 被创建或更新时,它会同步地修改 pod。 当该插件处于激活状态 ( 在大多数发行版中都是默认的 ),当 pod 被创建或更新时它会进行以下动作:
1. 如果该 pod 没有 ServiceAccount 设置,将其 ServiceAccount 设为 default。
2. 保证 pod 所关联的 ServiceAccount 存在,否则拒绝该 pod。
3. 如果 pod 不包含 ImagePullSecrets 设置,那么 将 ServiceAccount 中的 ImagePullSecrets 信息添加到 pod 中。
4. 将一个包含用于 API 访问的 token 的 volume 添加到 pod 中。
5. 将挂载于 /var/run/secrets/kubernetes.io/serviceaccount 的 volumeSource 添加到 pod 下的每个容器中。
在特定namesapce种替换pod的serviceaccount,该sa不应该挂载任何的Secret。
实验步骤- 实验环境
[root@master-01 tmp]# kubectl get node
NAME STATUS ROLES AGE VERSION
master-01 Ready control-plane,master 60d v1.21.3
worker-01 Ready 60d v1.21.3
worker-02 Ready 60d v1.21.3
[root@master-01 tmp]# kubectl get ns
NAME STATUS AGE
default Active 60d
kube-node-lease Active 60d
kube-public Active 60d
kube-system Active 60d
qa Active 39h
[root@master-01 tmp]# kubectl get sa -n qa
NAME SECRETS AGE
default 1 39h
[root@master-01 tmp]# kubectl get po -n qa
No resources found in qa namespace.
- 准备一个pod
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx-qa
name: nginx-qa
namespace: qa
spec:
containers:
- image: nginx
name: nginx-qa
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
[root@master-01 tmp]# kubectl apply -f 1-pod.yaml
pod/nginx-qa created
[root@master-01 tmp]# kubectl get po -n qa
NAME READY STATUS RESTARTS AGE
nginx-qa 1/1 Running 0 13s
[root@master-01 tmp]# kubectl edit pod nginx-qa -n qa
#Please edit the object below. Lines beginning with a ‘#’ will be ignored,
#and an empty file will abort the edit. If an error occurs while saving this file will be
#reopened with the relevant failures.apiVersion: v1
kind: Pod
metadata:
…
labels:
run: nginx-qa
name: nginx-qa
namespace: qa
resourceVersion: “485702”
uid: ade61ce9-518d-4505-8d62-c492454a9872
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx-qa
resources: {}
…
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: default
serviceAccountName: default
terminationGracePeriodSeconds: 30
tolerations:- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
volumes:
- name: kube-api-access-mbsqc
projected:
defaultMode: 420
sources:
- serviceAccountToken:
expirationSeconds: 3607
path: token`
- configMap:
items:
- key: ca.crt
path: ca.crt
name: kube-root-ca.crt
- downwardAPI:
items:
- fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
path: namespace
可以看到,现在pod:nginx-qa挂载了defult的serviceaccount。
查看sa:default
[root@master-01 tmp]# kubectl describe sa default -n qa
Name: default
Namespace: qa
Labels:
Annotations:
Image pull secrets:
Mountable secrets: default-token-7jmmb
Tokens: default-token-7jmmb
Events:
- 自定义一个ServiceAccount并不自动挂载 API 凭据
apiVersion: v1 kind: ServiceAccount metadata: creationTimestamp: null name: backend-sa namespace: qa automountServiceAccountToken: false
自动生成yaml:
kubectl create sa backend-sa -n qa --dry-run -o yaml > ./tmp/1.yaml
参考链接:https://kubernetes.io/zh/docs/tasks/configure-pod-container/configure-service-account/
查找路径:kubernetes.io–>任务–>配置Pod和容器–>为 Pod 配置服务账户
[root@master-01 tmp]# kubectl apply -f 1.yaml
serviceaccount/backend-sa created
[root@master-01 tmp]# kubectl get sa -n qa
NAME SECRETS AGE
backend-sa 1 10s
default 1 39h
[root@master-01 tmp]# kubectl describe sa backend-sa -n qa
Name: backend-sa
Namespace: qa
Labels:
Annotations:
Image pull secrets:
Mountable secrets: backend-sa-token-q5fpx
Tokens: backend-sa-token-q5fpx
Events:
对于ServiceAccount backend-sa,依然有secrets/token:backend-sa-token-q5fpx
- 修改pod是yaml文件
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx-qa
name: nginx-qa
namespace: qa
spec:
serviceAccountName: backend-sa #add the serviceaccount backend-sa
containers:
- image: nginx
name: nginx-qa
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
- 使用yaml文件更新pod
[root@master-01 tmp]# kubectl apply -f 1-pod.yaml --force
pod/nginx-qa configured
- 查看pod情况
[root@master-01 tmp]# kubectl edit po nginx-qa -n qa
#Please edit the object below. Lines beginning with a ‘#’ will be ignored,
#and an empty file will abort the edit. If an error occurs while saving this file will be
#reopened with the relevant failures.
apiVersion: v1
kind: Pod
metadata:
annotations:
…
labels:
run: nginx-qa
name: nginx-qa
namespace: qa
…
spec:
containers:
…
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
serviceAccount: backend-sa
serviceAccountName: backend-sa
terminationGracePeriodSeconds: 30
tolerations:
- effect: NoExecute
key: node.kubernetes.io/not-ready
operator: Exists
tolerationSeconds: 300- effect: NoExecute
key: node.kubernetes.io/unreachable
operator: Exists
tolerationSeconds: 300
status:
conditions:- lastProbeTime: null
lastTransitionTime: “2021-10-11T06:30:51Z”
status: “True”
…
pod使用了ServiceAccount:backend-sa,没有挂载任何secret/token



