默认情况下,系统会为每个servcieaccount(sa)创建一个对应的secret,这个secret里包含一个token,一般默认是ca证书。
https://v1-10.docs.kubernetes.io/docs/concepts/configuration/secret/ 文章说可以通过将sa挂载到pod上实现secret的自动挂载,这个地方其实有些问题,缺省自动产生的secret是可以自动被挂载到pod的。但是如果额外为这个sa创建的secret是不能自动挂载到pod上的(因为系统都不知道你要挂到哪里去),默认情况下pod只自动挂载sa中的第一个mountable的secret-一般也就是系统自动产生的那个secret。
测试步骤:
- 创建一个sa
kubectl create sa ljtest
12345678910[root@k8s-master cka]# kubectl describe sa ljtestName: ljtestNamespace: defaultLabels: <none>Annotations: <none>Image pull secrets: <none>Mountable secrets: ljtest-token-s7x8kTokens: ljtest-token-s7x8kEvents: <none> - 为该sa额外创建一个secret
1234567891011[root@k8s-master cka]# cat sa-ljtest-secert.yamlapiVersion: v1kind: Secretmetadata:name: ljtestsecrtannotations:kubernetes.io/service-account.name: ljtesttype: kubernetes.io/service-account-tokendata:username: YWRtaW4=password: MWYyZDFlMmU2N2Rm - 创建完毕,这个secret不会自动关联到sa上,需要手工编辑sa kubectl edit sa ljtest
12345678910111213141516# 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: v1kind: ServiceAccountmetadata:creationTimestamp: 2018-08-11T08:44:31Zname: ljtestnamespace: defaultresourceVersion: "446346"selfLink: /api/v1/namespaces/default/serviceaccounts/ljtestuid: c42e7e67-9d42-11e8-86b0-000c29850765secrets:- name: ljtest-token-s7x8k- name: ljtestsecrt <<<<<<<<增加这个
1234567891011[root@k8s-master cka]# kubectl describe sa ljtestName: ljtestNamespace: defaultLabels: <none>Annotations: <none>Image pull secrets: <none>Mountable secrets: ljtest-token-s7x8kljtestsecrtTokens: ljtest-token-s7x8kljtestsecrtEvents: <none> - 创建pod,并关联这个sa
123456789101112[root@k8s-master cka]# cat pod-with-sa-ljtest.yamlkind: PodapiVersion: v1metadata:name: pod-with-sa-ljtestlabels:name: pod-with-sa-ljtestspec:serviceAccount: ljtestcontainers:- name: nginx-sa-ljtestimage: nginx
创建完毕,会发现只是mount了系统自动产生的那个
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576[root@k8s-master cka]# kubectl get pods pod-with-sa-ljtest -o yamlapiVersion: v1kind: Podmetadata:creationTimestamp: 2018-08-11T09:08:51Zlabels:name: pod-with-sa-ljtestname: pod-with-sa-ljtestnamespace: defaultresourceVersion: "446503"selfLink: /api/v1/namespaces/default/pods/pod-with-sa-ljtestuid: 2a1b6688-9d46-11e8-86b0-000c29850765spec:containers:- image: nginximagePullPolicy: Alwaysname: nginx-sa-ljtestresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /var/run/secrets/kubernetes.io/serviceaccountname: ljtest-token-s7x8kreadOnly: truednsPolicy: ClusterFirstnodeName: k8s-masterrestartPolicy: AlwaysschedulerName: default-schedulersecurityContext: {}serviceAccount: ljtestserviceAccountName: ljtestterminationGracePeriodSeconds: 30tolerations:- effect: NoExecutekey: node.kubernetes.io/not-readyoperator: ExiststolerationSeconds: 300- effect: NoExecutekey: node.kubernetes.io/unreachableoperator: ExiststolerationSeconds: 300volumes:- name: ljtest-token-s7x8ksecret:defaultMode: 420secretName: ljtest-token-s7x8kstatus:conditions:- lastProbeTime: nulllastTransitionTime: 2018-08-11T09:08:51Zstatus: "True"type: Initialized- lastProbeTime: nulllastTransitionTime: 2018-08-11T09:08:57Zstatus: "True"type: Ready- lastProbeTime: nulllastTransitionTime: 2018-08-11T09:08:51Zstatus: "True"type: PodScheduledcontainerStatuses:- containerID: docker://7bbecc0852475ab6bcbf6dc619691909d401c3f3848fa1d325e1bf0e5fbae543image: nginx:latestimageID: docker-pullable://nginx@sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424lastState: {}name: nginx-sa-ljtestready: truerestartCount: 0state:running:startedAt: 2018-08-11T09:08:57ZhostIP: 172.16.199.100phase: RunningpodIP: 10.244.0.36qosClass: BestEffortstartTime: 2018-08-11T09:08:51Z - 修改pod yaml文件,手工关联
1234567891011121314151617181920[root@k8s-master cka]# cat pod-with-sa-ljtest.yamlkind: PodapiVersion: v1metadata:name: pod-with-sa-ljtestlabels:name: pod-with-sa-ljtestspec:volumes:- name: ljtestsecrtvolsecret:secretName: ljtestsecrtserviceAccount: ljtestcontainers:- name: nginx-sa-ljtestimage: nginxvolumeMounts:- name: ljtestsecrtvolreadOnly: truemountPath: /usr/local/www
产生的最后效果,可以看到被挂载了。
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990[root@k8s-master cka]# kubectl get pods pod-with-sa-ljtest -o yamlapiVersion: v1kind: Podmetadata:annotations:kubectl.kubernetes.io/last-applied-configuration: |{"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"name":"pod-with-sa-ljtest"},"name":"pod-with-sa-ljtest","namespace":"default"},"spec":{"containers":[{"image":"nginx","name":"nginx-sa-ljtest","volumeMounts":[{"mountPath":"/usr/local/www","name":"ljtestsecrtvol","readOnly":true}]}],"serviceAccount":"ljtest","volumes":[{"name":"ljtestsecrtvol","secret":{"secretName":"ljtestsecrt"}}]}}creationTimestamp: 2018-08-11T09:27:06Zlabels:name: pod-with-sa-ljtestname: pod-with-sa-ljtestnamespace: defaultresourceVersion: "447837"selfLink: /api/v1/namespaces/default/pods/pod-with-sa-ljtestuid: b6c92928-9d48-11e8-86b0-000c29850765spec:containers:- image: nginximagePullPolicy: Alwaysname: nginx-sa-ljtestresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- mountPath: /usr/local/wwwname: ljtestsecrtvolreadOnly: true- mountPath: /var/run/secrets/kubernetes.io/serviceaccountname: ljtest-token-s7x8kreadOnly: truednsPolicy: ClusterFirstnodeName: k8s-masterrestartPolicy: AlwaysschedulerName: default-schedulersecurityContext: {}serviceAccount: ljtestserviceAccountName: ljtestterminationGracePeriodSeconds: 30tolerations:- effect: NoExecutekey: node.kubernetes.io/not-readyoperator: ExiststolerationSeconds: 300- effect: NoExecutekey: node.kubernetes.io/unreachableoperator: ExiststolerationSeconds: 300volumes:- name: ljtestsecrtvolsecret:defaultMode: 420secretName: ljtestsecrt- name: ljtest-token-s7x8ksecret:defaultMode: 420secretName: ljtest-token-s7x8kstatus:conditions:- lastProbeTime: nulllastTransitionTime: 2018-08-11T09:27:06Zstatus: "True"type: Initialized- lastProbeTime: nulllastTransitionTime: 2018-08-11T09:27:14Zstatus: "True"type: Ready- lastProbeTime: nulllastTransitionTime: 2018-08-11T09:27:06Zstatus: "True"type: PodScheduledcontainerStatuses:- containerID: docker://44f2780069c75aacd0f0a2fae451c83608aa159d4d502547ae51161b6c06698eimage: nginx:latestimageID: docker-pullable://nginx@sha256:d85914d547a6c92faa39ce7058bd7529baacab7e0cd4255442b04577c4d1f424lastState: {}name: nginx-sa-ljtestready: truerestartCount: 0state:running:startedAt: 2018-08-11T09:27:14ZhostIP: 172.16.199.100phase: RunningpodIP: 10.244.0.37qosClass: BestEffortstartTime: 2018-08-11T09:27:06Z[root@k8s-master cka]# kubectl exec pod-with-sa-ljtest sh[root@k8s-master cka]# kubectl exec -it pod-with-sa-ljtest sh# ls /usr/local/wwwca.crt namespace password token username
参考:
文章评论