我们为开发环境使用一个命名空间,为登台环境使用一个命名空间。在每个命名空间中,我们都有几个 configMaps 和 secrets,但是这两个环境之间有很多共享变量,因此我们希望为它们创建一个公共文件。
有没有办法将基本 configMap 放入默认命名空间并使用以下内容引用它:
- envFrom:
- configMapRef:
name: default.base-config-map
Run Code Online (Sandbox Code Playgroud)
如果这是不可能的,除了通过命名空间复制变量之外,还有其他方法吗?
根据 v1.6 及更高版本的 Kubernetes 文档K8s Docs,我们可以使用:
envFrom:
- configMapRef:
name: <config-file>
Run Code Online (Sandbox Code Playgroud)
将所有 configMaps 数据定义为容器环境变量。使用它与将其设置为具有名称和键的环境变量的用例是什么:
env:
# Define the environment variable
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
# The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
name: special-config
# Specify the key associated with the value
key: special.how
Run Code Online (Sandbox Code Playgroud)
如果文件中有多个变量但只想提取单个键=值对,您会使用第二个示例吗?我试图了解我在 CKAD 考试中可能遇到的场景的不同用例。
我正在尝试从 k8s 命名空间中删除 configmap .. 我使用以下命令创建了 configmap
kubectl -n namespacename create -f configmap.yaml
检查 k8s 备忘单https://kubernetes.io/docs/reference/kubectl/cheatsheet/我没有找到任何相关的东西.. 请建议如何做到这一点?
我在家庭实验室中设置了 Kubernetes,并且能够从部署中运行 nginx 的普通实现。
下一步是有一个自定义的 nginx.conf 文件用于 nginx 的配置。为此,我使用了 ConfigMap。
当我这样做时,当我导航到http://192.168.1.10:30008(运行 nginx 服务器的节点的本地 IP 地址)时,我不再收到 nginx 索引页面。如果我尝试使用 ConfigMap,我会收到 nginx 404 页面/消息。
我无法看到我在这里做错了什么。任何方向将不胜感激。
nginx-deploy.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-conf
data:
nginx.conf: |
user nginx;
worker_processes 1;
events {
worker_connections 10240;
}
http {
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata: …Run Code Online (Sandbox Code Playgroud) 是否有使用 kubectl 从 Kubernetes 配置映射中删除特定键的好方法?
现在我运行:
$ kubectl edit configmap myconfigmap
Run Code Online (Sandbox Code Playgroud)
然后我删除了该条目,但我想要一个可以作为脚本运行的解决方案。
我正在尝试了解掌舵,我想知道是否有人可以 ELI5 给我一些东西或帮助我做一些事情。
所以我确实在下面运行:
helm repo add coreos https://s3-eu-west-1.amazonaws.com/coreos-charts/stable/
Run Code Online (Sandbox Code Playgroud)
然后我使用以下方法安装了 kube-prometheus:
helm install coreos/kube-prometheus --name kube-prometheus -f values.yaml --namespace monitoringtest
Run Code Online (Sandbox Code Playgroud)
一切正常,但我正在尝试从 json 文件添加一些自定义仪表板,我正在努力理解如何做到这一点。
我正在关注这个: https://blogcodevalue.wordpress.com/2018/09/16/automate-grafana-dashboard-import-process/
在我的 values.yaml 中,我在下面添加了
serverDashboardConfigmaps:
- example-dashboards
Run Code Online (Sandbox Code Playgroud)
我明白,如果我这样做:
helm upgrade --install kube-prometheus -f values.yaml --namespace monitoringtest coreos/kube-prometheus
Run Code Online (Sandbox Code Playgroud)
这应该会导致 grafana 获取下面的 configmap 调用example-dashboards并从custom-dashboards文件夹加载 * .json文件。
apiVersion: v1
kind: ConfigMap
metadata:
name: example-dashboards
data:
{{ (.Files.Glob "custom-dashboards/*.json").AsConfig | indent 2 }}
# Or
#
# data:
# custom-dashboard.json: |-
# {{ (.Files.Get "custom.json") | …Run Code Online (Sandbox Code Playgroud) 所以我正在处理这样的结构:
\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 1\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 env-vars\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 2\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 env-vars\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 env-vars\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 shared\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 env-vars\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 kustomization.yaml\nRun Code Online (Sandbox Code Playgroud)\n而每个级别内的环境变量都有一些环境变量并且
\n$cat kustomization.yaml\nbases:\n- 1/\n- 2/\n\nnamePrefix: toplevel-\n\nconfigMapGenerator:\n - name: env-cm\n behavior: merge\n envs:\n - env-vars\nRun Code Online (Sandbox Code Playgroud)\n$cat 1/kustomization.yaml\nbases:\n- ./../shared\n\nnamePrefix: first-\n\nconfigMapGenerator:\n - name: env-cm\n behavior: merge\n envs:\n - env-vars\nRun Code Online (Sandbox Code Playgroud)\n$cat 2/kustomization.yaml\nbases:\n- ./../shared\n\nnamePrefix: second-\n\nconfigMapGenerator:\n - name: env-cm\n behavior: merge\n envs:\n - env-vars\nRun Code Online (Sandbox Code Playgroud)\n$cat shared/kustomization.yaml\nconfigMapGenerator:\n - name: env-cm\n behavior: create\n envs:\n - env-vars\nRun Code Online (Sandbox Code Playgroud)\n我本质上是试图创建 2 个具有一些共享值的配置映射(它们是从不同的资源注入的:从shared目录和顶级目录)
我们有一个具有巨大配置的应用程序(这只是一部分):
apiVersion: v1
kind: ConfigMap
metadata:
name: my-app
data:
application.yaml: |-
config:
app: MY-APP
my-custom-map:
KEY1: value1
KEY2: value2
KEY3: value3
KEY4: value4
something1: true
something2: 123
something3: string123
something4: null
subclass:
anotherMap:
"[AAA:0.0.1,BBB:CCC]": "DDD:EEEE"
subclass2:
something4: AAAA
anotherMap2:
0.0.3: 0.0.3
Run Code Online (Sandbox Code Playgroud)
我按照这个示例将 configmap 与 spring boot 配置绑定,但仍然存在一些问题,例如如何解决 yaml 中的 null ,其中 spring yaml 后处理器将其解析为空字符串:问题
第二个问题是如何处理这个configmap。我知道我可以编辑然后使用 apply,但这可能会导致一些错误。是否有一些工具可以用来编辑这个 yaml 并制作一些 bash 脚本进行编辑?例如: ./my-script.sh -function addMyCustomMapValue -args "KEY5:value5" 。我尝试探索yq但我认为存在一些限制,并且很难用于某些用例,然后kustomize我认为这对于创建 configmap 很有用,但不适合编辑现有的 configmap。
这个用例已经有一些很好的例子了吗?
目前,我正在通过执行以下操作从文件config.json创建一个configmap:
kubectl create configmap jksconfig --from-file=config.json
Run Code Online (Sandbox Code Playgroud)
我希望将ConfigMap 创建为部署的一部分,并尝试执行以下操作:
apiVersion: v1
kind: ConfigMap
metadata:
name: jksconfig
data:
config.json: |-
{{ .Files.Get "config.json" | indent 4 }}
Run Code Online (Sandbox Code Playgroud)
但似乎不起作用。应该怎么做才能进入configmap.yaml,以便创建相同的configmap?
-更新-
当我执行头盔安装空运行时:
# Source: mychartv2/templates/jks-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: jksconfig
data:
config.json: |
Run Code Online (Sandbox Code Playgroud)
注意:我正在使用minikube作为我的kubernetes集群
我有关于 configmap 的文档:
https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#define-container-environment-variables-using-configmap-data
Run Code Online (Sandbox Code Playgroud)
据我所知,我可以从两个文件(game.properties 和 ui.properties)创建一个配置映射(game-config-2)使用
kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/kubectl/game.properties --from-file=configure-pod-container/configmap/kubectl/ui.properties
Run Code Online (Sandbox Code Playgroud)
现在我看到了配置图
kubectl describe configmaps game-config-2
Name: game-config-2
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
game.properties: 158 bytes
ui.properties: 83 bytes
Run Code Online (Sandbox Code Playgroud)
我如何使用该配置映射?我试过这种方式:
envFrom:
- configMapRef:
name: game-config-2
Run Code Online (Sandbox Code Playgroud)
但这不起作用,env 变量没有从 configmap 中选择。或者我可以在 envFrom 下有两个 configMapRef 吗?
configmap ×10
kubernetes ×10
kubectl ×2
config ×1
grafana ×1
java ×1
kustomize ×1
minikube ×1
namespaces ×1
nginx ×1
spring-boot ×1