What will be the equivalent ConfigMap YAML code for the following command line?
kubectl create configmap mongo-initdb --from-file=init-mongo.js
There is actually a kubectl command that lets you get the generated yaml for a created config map. In your case:
kubectl get configmaps mongo-initdb -o yaml
You can use the command as suggested by Mo Xue, however here is YAML,
Example :
apiVersion: v1
kind: ConfigMap
metadata:
name: mongo-initdb
data:
init-mongo.js: |
<Content>
Read more about : https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#create-configmaps-from-directories
Create the configmap from a file.
Just create a sample file like ui.properties
example:
cat ui.properties
1. name=x
2. rollno=y
Command to create a configmap from above file
kubectl create configmap ui-configmap --from-file=ui.properties
Verify the data.
kubectl get configmap ui-configmap -o yaml
apiVersion: v1
kind: ConfigMap
metadata:
creationTimestamp: <>
name: ui-configmap
namespace: default
data:
name: x
role: y
Related
I am trying to add namespace to a multi document kubernetes yaml file if it doesn't exist but if the namespace field already exists don't update it using yq but i can't seem to get it to work. Is this possible?
e.g.
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: serviceaccount1
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: serviceaccount2
namespace: namespace2
should end up something like this:
apiVersion: v1
kind: ServiceAccount
metadata:
name: serviceaccount1
namespace: namespace1
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: serviceaccount2
namespace: namespace2
UPDATE:
After successfully updating the yaml file thanks to:
https://stackoverflow.com/a/74736018/16126608
I am getting the namespace populated however due to the fact my yaml file was produced as an output of a helm template I am seeing the following issue where an empty document is having {} added as a result of the yq command, e.g.
---
# Source: path/to/chart/templates/configmap.yaml
---
# Source: ppath/to/chart/templates/network-policy.yaml
---
Is coming out like this afterwards:
---
# Source: path/to/chart/templates/configmap.yaml
---
{}
# Source: path/to/chart/templates/network-policy.yaml
---
{}
Is it possible to have yq to not add the {} and ignore the empty documents?
I am using mikefarah/yq
You can use has to check the existence of a field, and not to negate, then select it and add the new field.
Which implementation of yq are you using?
Using kislyuk/yq:
yq -y 'select(.metadata | has("namespace") | not).metadata.namespace = "namespace1"'
Using mikefarah/yq:
yq 'select(.metadata | has("namespace") | not) |= .metadata.namespace = "namespace1"'
Well, I'm using minikube v1.26.0 and kubectl to manage Kubernetes on my local machine, when I decide to create a ConfigMap with kubectl apply -f ConfigMapFile.yaml I'm getting error no matches for kind "configmap" in version "apps/v1"
ConfigMapFile.yaml
apiVersion: apps/v1
kind: ConfigMap
metadata:
name: test-config-map
data:
.........
Seems Like ConfigMap is not allowed or deprecated in Kubernetes apps/v1, but cannot find any solution or tips that would help me with this problem.
you need to use apiVersion: v1 for configmap. You can also check the version of any resource using:
kubectl api-resources |grep -i configmap
configmaps cm v1 true ConfigMap
I was wondering if anyone has any ideas on the best way to create 200 namespaces within a cluster.
Ideally a simple bash loop to create kubectl create namespaces would be good.
You can dynamically create a YAML file using any programming language you are most comfortable with (bash or python), consisting of a list of k8s namespaces with the following format:
$ cat namespaces-list.yaml
---
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Namespace
metadata:
name: namespace-list1
- apiVersion: v1
kind: Namespace
metadata:
name: namespace-list2
- apiVersion: v1
kind: Namespace
metadata:
name: namespace-list3
Then execute the following command to create them all in one shot! kubectl apply -f namespaces-list.yaml
Hope this helped!
I eventually came up with something as simple this
#!/usr/bin/env bash
for i in $(seq 100); do
kubectl create namespace test-${i}
done
I have a need to define a standalone patch as YAML.
More specifically, I want to do the following:
kubectl patch serviceaccount default -p '{"imagePullSecrets": [{"name": "registry-my-registry"}]}'
The catch is I can't use kubectl patch. I'm using a GitOps workflow with flux, and that resource I want to patch is a default resource created outside of flux.
In other terms, I need to do the same thing as the command above but with kubectl apply only:
kubectl apply patch.yaml
I wasn't able to figure out if you can define such a patch.
The key bit is that I can't predict the name of the default secret token on a new cluster (as the name is random, i.e. default-token-uudge)
Fields set and deleted from Resource Config are merged into Resources by Kubectl apply:
If a Resource already exists, Apply updates the Resources by merging the
local Resource Config into the remote Resources
Fields removed from the Resource Config will be deleted from the remote Resource
You can learn more about Kubernetes Field Merge Semantics.
If your limitation is not knowing the secret default-token-xxxxx name, no problem, just keep that field out of your yaml.
As long as the yaml has enough fields to identify the target resource (name, kind, namespace) it will add/edit the fields you set.
I created a cluster (minikube in this example, but it could be any) and retrieved the current default serviceAccount:
$ kubectl get serviceaccount default -o yaml
apiVersion: v1
kind: ServiceAccount
metadata:
creationTimestamp: "2020-07-01T14:51:38Z"
name: default
namespace: default
resourceVersion: "330"
selfLink: /api/v1/namespaces/default/serviceaccounts/default
uid: a9e5ff4a-8bfb-466f-8873-58c2172a5d11
secrets:
- name: default-token-j6zx2
Then, we create a yaml file with the content's that we want to add:
$ cat add-image-pull-secrets.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: default
namespace: default
imagePullSecrets:
- name: registry-my-registry
Now we apply and verify:
$ kubectl apply -f add-image-pull-secrets.yaml
serviceaccount/default configured
$ kubectl get serviceaccount default -o yaml
apiVersion: v1
imagePullSecrets:
- name: registry-my-registry
kind: ServiceAccount
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"v1","imagePullSecrets":[{"name":"registry-my-registry2"}],"kind":"ServiceAccount","metadata":{"annotations":{},"name":"default","namespace":"default"}}
creationTimestamp: "2020-07-01T14:51:38Z"
name: default
namespace: default
resourceVersion: "2382"
selfLink: /api/v1/namespaces/default/serviceaccounts/default
uid: a9e5ff4a-8bfb-466f-8873-58c2172a5d11
secrets:
- name: default-token-j6zx2
As you can see, the ImagePullPolicy was added to the resource.
I hope it fits your needs. If you have any further questions let me know in the comments.
Let say, your service account YAML looks like bellow:
$ kubectl get sa demo -o yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: demo
namespace: default
secrets:
- name: default-token-uudge
Now, you want to add or change the imagePullSecrets for that service account. To do so, edit the YAML file and add imagePullSecrets.
apiVersion: v1
kind: ServiceAccount
metadata:
name: demo
namespace: default
secrets:
- name: default-token-uudge
imagePullSecrets:
- name: myregistrykey
And finally, apply the changes:
$ kubectl apply -f service-account.yaml
lets take this example of a config map
apiVersion: v1
kind: ConfigMap
data:
abc.yml: |-
<yml here>
Getting an error like failed to parse yaml to Json.
Yes you can do that, but you should care about the syntax. You can also follow techniques for yaml from here.
If you use kubectl create configmap myconfig --from-file=abc.yml, then it is ok.
But if you write the whole yaml file for your configmap in myconfig.yaml and then run kubectl create -f myconfig.yaml, then you should care about syntax.
Say your abc.yml file is as followings:
a:
b: b1
c: c1
d: d1
Then write your myconfig.yaml file:
apiVersion: v1
kind: ConfigMap
data:
abc.yml: |
a:
b: b1
c: c1
d: d1
Now just run kubectl create -f myconfig.yaml.
That's it.
Happy Kubernetes!!!.
Create ConfigMap from file.
kubectl create configmap myconfig --from-file=youfile.yml.
You can check more examples on kubernetes docs
These could be the problems
1. most likely the issue could with the indentation.
2. remove '-' from abc.yml: |- and check
I followed the below steps and was able to load yaml file into configmap. it worked fine.
master $ cat c.yaml
apiVersion: v1
data:
redis-config: |
maxmemory 2mb
maxmemory-policy allkeys-lru
kind: ConfigMap
metadata:
name: example-redis-config
master $ kubectl create configmap testcfg --from-file=./c.yaml
master $ kubectl get cm testcfg -oyaml
apiVersion: v1
data:
c.yaml: |
apiVersion: v1
data:
redis-config: |
maxmemory 2mb
maxmemory-policy allkeys-lru
kind: ConfigMap
metadata:
name: example-redis-config
kind: ConfigMap
metadata:
creationTimestamp: 2019-03-07T08:35:18Z
name: testcfg
namespace: default
resourceVersion: "7520"
selfLink: /api/v1/namespaces/default/configmaps/testcfg
uid: f033536d-40b3-11e9-a67d-0242ac11005b