[英]How to mount a configMap as a volume mount in a Stateful Set
I don't see an option to mount a configMap as volume in the statefulset, as per https://kube.netes.io/docs/reference/generated/kube.netes-api/v1.17/#statefulset-v1-apps only PVC can be associated with "StatefulSet".根据https://kube.netes.io/docs/reference/generated/kube.netes-api/v1.17/#statefulset-v1- ,我没有看到将 configMap 作为卷挂载到 statefulset 中的选项apps only PVC 可以与“StatefulSet”相关联。 But PVC does not have option for configMaps.但是 PVC 没有 configMaps 选项。
Here is a minimal example:这是一个最小的例子:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: example
spec:
selector:
matchLabels:
app: example
serviceName: example
template:
metadata:
labels:
app: example
spec:
containers:
- name: example
image: nginx:stable-alpine
volumeMounts:
- mountPath: /config
name: example-config
volumes:
- name: example-config
configMap:
name: example-configmap
---
apiVersion: v1
kind: ConfigMap
metadata:
name: example-configmap
data:
a: "1"
b: "2"
In the container, you can find the files a
and b
under /config
, with the contents 1
and 2
, respectively.在容器中,您可以在/config
下找到文件a
和b
,内容分别为1
和2
。
Some explanation: You do not need a PVC to mount the configmap as a volume to your pods.一些解释:您不需要 PVC 来将 configmap 作为卷挂载到您的 pod。 PersistentVolumeClaim
s are persistent drives, which you can read from/write to. PersistentVolumeClaim
是永久性驱动器,您可以从中读取/写入。 An example for their usage is a database, such as Postgres.它们的使用示例是数据库,例如 Postgres。
ConfigMap
s on the other hand are read-only key-value structures that are stored inside Kubernetes (in its etcd store), which are to store the configuration for your application.另一方面, ConfigMap
是存储在 Kubernetes 内部(在其 etcd 存储中)的只读键值结构,用于存储应用程序的配置。 Their values can be mounted as environment variables or as files, either individually or altogether.它们的值可以单独或全部挂载为环境变量或文件。
I have done it in this way.我已经这样做了。
apiVersion: v1
kind: ConfigMap
metadata:
name: rabbitmq-configmap
namespace: default
data:
enabled_plugins: |
[rabbitmq_management,rabbitmq_shovel,rabbitmq_shovel_management].
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: rabbitmq
labels:
component: rabbitmq
spec:
serviceName: "rabbitmq"
replicas: 1
selector:
matchLabels:
component: rabbitmq
template:
metadata:
labels:
component: rabbitmq
spec:
initContainers:
- name: "rabbitmq-config"
image: busybox:1.32.0
volumeMounts:
- name: rabbitmq-config
mountPath: /tmp/rabbitmq
- name: rabbitmq-config-rw
mountPath: /etc/rabbitmq
command:
- sh
- -c
- cp /tmp/rabbitmq/rabbitmq.conf /etc/rabbitmq/rabbitmq.conf && echo '' >> /etc/rabbitmq/rabbitmq.conf;
cp /tmp/rabbitmq/enabled_plugins /etc/rabbitmq/enabled_plugins
volumes:
- name: rabbitmq-config
configMap:
name: rabbitmq-configmap
optional: false
items:
- key: enabled_plugins
path: "enabled_plugins"
- name: rabbitmq-config-rw
emptyDir: {}
containers:
- name: rabbitmq
image: rabbitmq:3.8.5-management
env:
- name: RABBITMQ_DEFAULT_USER
value: "username"
- name: RABBITMQ_DEFAULT_PASS
value: "password"
- name: RABBITMQ_DEFAULT_VHOST
value: "vhost"
ports:
- containerPort: 15672
name: ui
- containerPort: 5672
name: api
volumeMounts:
- name: rabbitmq-data-pvc
mountPath: /var/lib/rabbitmq/mnesia
volumeClaimTemplates:
- metadata:
name: rabbitmq-data-pvc
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 2Gi
---
apiVersion: v1
kind: Service
metadata:
name: rabbitmq
spec:
selector:
component: rabbitmq
ports:
- protocol: TCP
port: 15672
targetPort: 15672
name: ui
- protocol: TCP
port: 5672
targetPort: 5672
name: api
type: ClusterIP
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.