kubernetes的Service Account和secret
Service Account
Service Account概念的引入是基于这样的使用场景:运行在pod里的进程需要调用Kubernetes API以及非Kubernetes API的其它服务。Service Account它并不是给kubernetes集群的用户使用的,而是给pod里面的进程使用的,它为pod提供必要的身份认证。
kubectl get sa --all-namespaces
NAMESPACE NAME SECRETS AGE
default build-robot 1 1d
default default 1 32d
default kube-dns 1 31d
kube-public default 1 32d
kube-system dashboard 1 31d
kube-system default 1 32d
kube-system heapster 1 30d
kube-system kube-dns 1 31d
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
如果kubernetes开启了ServiceAccount(–admission_control=…,ServiceAccount,… )那么会在每个namespace下面都会创建一个默认的default的sa。
如下,其中最重要的就是secrets,它是每个sa下面都会拥有的一个加密的token,这个在下面的secret会详细介绍。
kubectl get sa default -o yaml
apiVersion: v1
kind: ServiceAccount
metadata:
creationTimestamp: 2017-05-02T06:39:12Z
name: default
namespace: default
resourceVersion: "175"
selfLink: /api/v1/namespaces/default/serviceaccounts/default
uid: 0de23575-2f02-11e7-98d0-5254c4628ad9
secrets:
- name: default-token-rsf8r
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
当用户再该namespace下创建pod的时候都会默认使用这个sa,下面是get pod 截取的部分,可以看到kubernetes会把默认的sa挂载到容器内。
volumes:
- name: default-token-rsf8r
secret:
defaultMode: 420
secretName: default-token-rsf8r
- 1
- 2
- 3
- 4
- 5
具体看一下secret
kubectl get secret default-token-rsf8r -o yaml
apiVersion: v1
data:
ca.crt: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUR2akNDQXFhZ0F3SUJBZ0lVZlpvZDJtSzNsa3JiMzR3NDhhUmtOc0pVVDJjd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1pURUxNQWtHQTFVRUJoTUNRMDR4RURBT0JnTlZCQWdUQjBKbGFVcHBibWN4RURBT0JnTlZCQWNUQjBKbAphVXBwYm1jeEREQ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UwTG5WcFpDSTZJakJrWlRJek5UYzFMVEptTURJdE1URmxOeTA1T0dRd0xUVXlOVFJqTkRZeU9HRmtPU0lzSW5OMVlpSTZJbk41YzNSbGJUcHpaWEoyYVdObFlXTmpiM1Z1ZERwa1pXWmhkV3gwT21SbFptRjFiSFFpZlEuSmxuamM0Y0xNYkZrRlJVQjIyWGtFN2t4bTJ1dS1aQm9sUTh4VEdDNmdLOTdSZTVOMzBuY2V0SWJsanVOVWFlaDhtMDk2R19nMHE3cmRvWm5XMTV2OFBVXzNyM1hWWlBNc1lxbGRpZlNJbWtReXFqeEphVlBka3Izam5GWVBkVWNaTmk3MFF3cWtEdm5sMXB4SFRNZTZkTVNPTlExbUVoMHZSbHBhRTdjVWtTVlg5blRzaFVJVTVXWE9wRUxwdTVjVjBHV3ZGeDRDSzR6Umt3clNMdlV5X2d5UGZwLWdYVFZQWU80NkJKSWZtaVhlZGhVaW9nempPN285eGxDbUxQVkhyNkFIZGViNExiTVA1dkJ2MlBSZ2RrMW9keTR0VEdxLVRGU3M2VkNoMTZ4dk5IdTRtRVN5TjZmcXVObzJwYUFOelY4b251aTJuaU4yNTU1TzN4SFdR
kind: Secret
metadata:
annotations:
kubernetes.io/service-account.name: default
kubernetes.io/service-account.uid: 0de23575-2f02-11e7-98d0-5254c4628ad9
creationTimestamp: 2017-05-02T06:42:07Z
name: default-token-rsf8r
namespace: default
resourceVersion: "12551"
selfLink: /api/v1/namespaces/default/secrets/default-token-rsf8r
uid: 75c0a236-2f02-11e7-98d0-5254c4628ad9
type: kubernetes.io/service-account-token
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
上面的内容是经过base64加密过后的,我们直接进入容器内:
~ # ls -l /var/run/secrets/kubernetes.io/serviceaccount/
total 0
lrwxrwxrwx 1 root root 13 May 4 23:57 ca.crt -> ..data/ca.crt
lrwxrwxrwx 1 root root 16 May 4 23:57 namespace -> ..data/namespace
lrwxrwxrwx 1 root root 12 May 4 23:57 token -> ..data/token
- 1
- 2
- 3
- 4
- 5
可以看到已将ca.crt 、namespace和token放到容器内了,那么这个容器就可以通过https的请求访问apiserver了。
Secret
Kubernetes提供了Secret来处理敏感信息,目前Secret的类型有3种:
Opaque(default): 任意字符串
kubernetes.io/service-account-token: 作用于ServiceAccount,就是上面说的。
kubernetes.io/dockercfg: 作用于Docker registry,用户下载docker镜像认证使用。
Opaque Secret
Opaque Secret就是字符串
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: MWYyZDFlMmU2N2Rm
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
在使用的时候可以选择已volume方式或者是已环境变量的方式放到容器内使用。
{
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "mypod",
"namespace": "default"
},
"spec": {
"containers": [{
"name": "mypod",
"image": "busybox",
"command": ["sleep","3600"],
"imagePullPolicy": "IfNotPresent",
"volumeMounts": [{
"name": "foo",
"mountPath": "/etc/foo",
"readOnly": true
}]
}],
"volumes": [{
"name": "foo",
"secret": {
"secretName": "mysecret"
}
}]
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
这样就可以通过文件的方式挂载到容器内,在/etc/foo目录下回生成这个文件。
如果是环境变量当然也是ok的
apiVersion: v1
kind: Pod
metadata:
name: secret-env-pod
spec:
containers:
- name: mycontainer
image: busybox
imagePullPolicy: IfNotPresent
command:
- sleep
- "3600"
env:
- name: SECRET_USERNAME
valueFrom:
secretKeyRef:
name: mysecret
key: username
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
进入容器通过env命令,你将可以看到这两个环境变量被注入到容器内。
imagePullSecrets
当在需要安全验证的环境中拉取镜像的时候,需要通过用户名和密码。
apiVersion: v1
kind: Secret
metadata:
name: myregistrykey
namespace: awesomeapps
data:
.dockerconfigjson: UmVhbGx5IHJlYWxseSByZWVlZWVlZWVlZWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGx5eXl5eXl5eXl5eXl5eXl5eXl5eSBsbGxsbGxsbGxsbGxsbG9vb29vb29vb29vb29vb29vb29vb29vb29vb25ubm5ubm5ubm5ubm5ubm5ubm5ubm5ubmdnZ2dnZ2dnZ2dnZ2dnZ2dnZ2cgYXV0aCBrZXlzCg==
type: kubernetes.io/dockerconfigjson
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
或者直接通过命令创建
kubectl create secret docker-registry myregistrykey --docker-server=DOCKER_REGISTRY_SERVER --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL
- 1
接下来拉取镜像的时候,就可以使用了
apiVersion: v1
kind: Pod
metadata:
name: foo
namespace: awesomeapps
spec:
containers:
- name: foo
image: janedoe/awesomeapp:v1
imagePullSecrets:
- name: myregistrykey
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
其实本质上还是kubelet把这个认证放到了docker的目录下面,如下:
cat ~/.docker/config.json
{
"auths": {
"10.39.0.118": {
"auth": "Y2hlbm1vOmNtMTM4MTE2NjY3ODY="
},
"10.39.0.12:5000": {
"auth": "dXNlcjAxOjEyMzQ1YQ=="
},
"http://10.39.0.12:5000": {
"auth": "dXNlcjAxOjEyMzQ1YQ=="
}
}
}
推荐阅读
-
Android 启动 Service(startservice和bindservice) 两种方式的区别
-
Android 启动 Service(startservice和bindservice) 两种方式的区别
-
Spring 中 @Service 和 @Resource 注解的区别
-
安装程序在正在设置 reporting service 和 share point 排除路径期间遇到错误的解决方
-
安装程序在正在设置 reporting service 和 share point 排除路径期间遇到错误的解决方
-
android使用service和activity获取屏幕尺寸的方法
-
如何申请百度文字识别apikey和Secret Key的方法步骤
-
浅析kubernetes的控制器和标签
-
详解kubernetes pod的编排和生命周期
-
Android实现音乐播放进度条传递信息的两种方式(在service和activity中)