Kubernetes学习笔记(二十八):Service Accounts、Image Security
Service Accounts
kubectl create serviceaccount dashboard-sa
kubectl get serviceaccount
kubectl describe serviceaccount dashboard-sa
当创建Service Account时,首先创建 Service Account 对象,然后为服务账户生成令牌
查看secret token:kubectl describe secret dashboard-sa-token-kbbdm
利用 Service Account 创建 token:kubectl create token sa-name
默认采用 default 的 Service Account,可以在deployment指明 Service Account,在 pod spec 下添加 serviceAccountName: sa-name
使用curl访问:
curl https://192.168.56.70:6443/api -insecure --header "Authorization: Bearer <dashboard-sa-token-kbbdm>"
每个namespace都会创建 default 的 Service Account:kubectl exec -it my-kubernetes-dashboard ls /var/run/secrets/kubernetes.io/serviceaccount,默认只有运行基本的Kubernetes API 查询的权限
无法编辑pod中的Service Account,只能删除后新建,在deployment中可以。
如果不想自动挂载Service Account,可以设置:
spec:
automountServiceAccountToken: false
Image Security
image: docker.io/library/nginx:不指定用户名或账户名,则会假定为library。Registry,User/Account,Image/Repository
docker login private-registry.io
docker run private-registry.io/apps/internal-app
kubectl create secret docker-registry regcred \ ## 取名为regcred
--docker-server= private-registry.io \
--docker-username= registry-user \ ## 类型为docker-registry
--docker-password= registry-password \
--docker-email= registry-user@org.com
deployment 应用 secret 需要添加以下设置:
spec:
imagePullSecrets:
- name: regcred
查看哪个user在运行pod:kubectl exec pod-name -- whoami
修改 securityContext
spec:
securityContext:
runAsUser: 1010 ## 设置在pod或container level
spec:
securityContext:
capabilities: ## 设置在container level
add: ["SYS_TIME"]

浙公网安备 33010602011771号