如何在 k8s 的环境中设置数组结构 属性?
How to set array structure property in env for k8s?
要在secret中定义clients来存储client name和password,如何在env和secret中定义一个client数组来存储这些数据?
在application.yml中应该是这样的:
app:
clients:
client1: password1
client2: password2
......
我试图在app-deployment.yml中定义它,但似乎不允许
env:
- name: APPLICATION_CLIENTS
value:
- name: client1
valueFrom:
secretKeyRef:
name: clients
key: client1-password
- name: client2
valueFrom:
secretKeyRef:
name: clients
key: client2-password
错误:
invalid type for io.k8s.api.core.v1.EnvVar.value: got "array" expected "string"
创建一个 secret 并将其作为 env 安装在 pod 中,然后在代码中读取该 env var 以获取密码值
和用户名的方法,而不是创建配置映射或秘密直接将其定义为 pod
中的 env var
Create a config map and mount that in dev as env and then in deployment.yaml format gives it is as reference :
config map creation with overriding application.yml/application.properties format as both strings or array or lists :
values.yaml :
------------------------------
spring:
profiles: default
server:
port: 8085
management:
endpoints:
loggers:
enabled: true
web:
exposure:
include: loggers, health, prometheus
sites:
- publications: test1
- publications: test2
config.yaml :
-------------------------
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "project.fullname" . }}
labels:
app.kubernetes.io/name: {{ include "project.fullname" . }}
helm.sh/chart: {{ include "project.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
data:
application.yml: |-
spring:
profiles: {{ .Values.spring.profiles }}
server:
port: {{ .Values.server.port }}
management:
endpoints:
loggers:
enabled: {{ .Values.management.endpoints.loggers.enabled }}
web:
exposure:
include: {{ .Values.management.endpoints.web.exposure.include }}
sites:
{{ toYaml .Values.sites | indent 10 }} #list of items in values.yaml
deployment.yaml
-----------------------
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.name }}
namespace: {{ .Values.namespace }}
labels:
app: {{ .Values.name }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Values.name }}
template:
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/path: "/actuator/prometheus"
prometheus.io/port: {{ .Values.server.port | quote }}
sidecar.istio.io/inject: "false"
labels:
app: {{ .Values.name }}
spec:
volumes:
- name: config
configMap:
name: {{ include "project.fullname" . }}
containers:
- name: {{ .Chart.Name }}
volumeMounts:
- name: config
mountPath: "/app/config"
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
resources:
requests:
memory: {{ .Values.image.resources.requests.memory }}
cpu: {{ .Values.image.resources.requests.cpu }}
limits:
memory: {{ .Values.image.resources.limits.memory }}
cpu: {{ .Values.image.resources.limits.cpu }}
imagePullSecrets:
- name: "{{ .Values.image.imagePullSecrets }}"
now while running application from docker command or pom, change the path from where the application is picking config file while running.
Example in pom :
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<to>
<image><image-url></image>
</to>
<container>
<creationTime>${maven.build.timestamp}</creationTime>
<mainClass>mypackage.MainApplication</mainClass>
<args>
<arg>--spring.config.location=/app/config/application.yml</arg>
</args>
</container>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
要在secret中定义clients来存储client name和password,如何在env和secret中定义一个client数组来存储这些数据?
在application.yml中应该是这样的:
app:
clients:
client1: password1
client2: password2
......
我试图在app-deployment.yml中定义它,但似乎不允许
env:
- name: APPLICATION_CLIENTS
value:
- name: client1
valueFrom:
secretKeyRef:
name: clients
key: client1-password
- name: client2
valueFrom:
secretKeyRef:
name: clients
key: client2-password
错误:
invalid type for io.k8s.api.core.v1.EnvVar.value: got "array" expected "string"
创建一个 secret 并将其作为 env 安装在 pod 中,然后在代码中读取该 env var 以获取密码值 和用户名的方法,而不是创建配置映射或秘密直接将其定义为 pod
中的 env varCreate a config map and mount that in dev as env and then in deployment.yaml format gives it is as reference :
config map creation with overriding application.yml/application.properties format as both strings or array or lists :
values.yaml :
------------------------------
spring:
profiles: default
server:
port: 8085
management:
endpoints:
loggers:
enabled: true
web:
exposure:
include: loggers, health, prometheus
sites:
- publications: test1
- publications: test2
config.yaml :
-------------------------
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "project.fullname" . }}
labels:
app.kubernetes.io/name: {{ include "project.fullname" . }}
helm.sh/chart: {{ include "project.chart" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
data:
application.yml: |-
spring:
profiles: {{ .Values.spring.profiles }}
server:
port: {{ .Values.server.port }}
management:
endpoints:
loggers:
enabled: {{ .Values.management.endpoints.loggers.enabled }}
web:
exposure:
include: {{ .Values.management.endpoints.web.exposure.include }}
sites:
{{ toYaml .Values.sites | indent 10 }} #list of items in values.yaml
deployment.yaml
-----------------------
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.name }}
namespace: {{ .Values.namespace }}
labels:
app: {{ .Values.name }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ .Values.name }}
template:
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/path: "/actuator/prometheus"
prometheus.io/port: {{ .Values.server.port | quote }}
sidecar.istio.io/inject: "false"
labels:
app: {{ .Values.name }}
spec:
volumes:
- name: config
configMap:
name: {{ include "project.fullname" . }}
containers:
- name: {{ .Chart.Name }}
volumeMounts:
- name: config
mountPath: "/app/config"
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
resources:
requests:
memory: {{ .Values.image.resources.requests.memory }}
cpu: {{ .Values.image.resources.requests.cpu }}
limits:
memory: {{ .Values.image.resources.limits.memory }}
cpu: {{ .Values.image.resources.limits.cpu }}
imagePullSecrets:
- name: "{{ .Values.image.imagePullSecrets }}"
now while running application from docker command or pom, change the path from where the application is picking config file while running.
Example in pom :
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>2.2.0</version>
<configuration>
<to>
<image><image-url></image>
</to>
<container>
<creationTime>${maven.build.timestamp}</creationTime>
<mainClass>mypackage.MainApplication</mainClass>
<args>
<arg>--spring.config.location=/app/config/application.yml</arg>
</args>
</container>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>