这里介绍Kubernetes卷中的Projected Volumes投射卷
实践
所谓Projected Volumes投射卷,可以实现为容器提供预先定义好的数据。故从容器的角度来看,卷中的数据是被K8s投射到容器当中的。即,Projected Volumes投射卷可以将若干现有的卷源映射到同一个目录下。当前,投射卷支持以下几种类型的卷源
- Secret
- ConfigMap
- DownwardAPI
- ServiceAccountToken
故在演示投射卷之前,我们先通过Secret、ConfigMap定义一些数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| apiVersion: v1
kind: ConfigMap metadata: name: my-configmap-1 data: mysql.ip: 196.168.1.2 mysql.password: "123456"
---
apiVersion: v1 kind: Secret
type: Opaque metadata: name: my-secret-1
stringData: es.url: localhost:3307 es.user: Aaron es.password: Bob
|
效果如下所示
然后我们使用投射卷将ConfigMap、Secret、DownwardAPI中的数据投射到容器下。配置如下所示
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| apiVersion: apps/v1
kind: ReplicaSet metadata: name: my-mongodb-app spec: replicas: 1 selector: matchLabels: app: db template: metadata: labels: app: db spec: containers: - name: my-mongodb-app image: mongo volumeMounts: - name: my-config-data mountPath: /usr/MyData volumes: - name: my-config-data projected: sources: - configMap: name: my-configmap-1 - secret: name: my-secret-1 items: - key: es.user path: MyEsUser - key: es.password path: MyEsPassword - downwardAPI: items: - path: myPodName fieldRef: fieldPath: metadata.name
|
效果如下所示
参考文献
- Kubernetes in Action中文版 Marko Luksa著
- 深入剖析Kubernetes 张磊著