I want to make a YAML file with Deployment, Ingress, and Service (maybe with clusterissuer, issuer and cert) on one file, how can I do that? I tried
kubectl apply -f (name_file.yaml)
You can it with three dashes on your yaml file
like this
apiVersion: v1
kind: Service
metadata:
name: mock
spec:
...
---
apiVersion: v1
kind: ReplicationController
metadata:
name: mock
spec:
Source : https://levelup.gitconnected.com/kubernetes-merge-multiple-yaml-into-one-e8844479a73a
Related
In the Base Ingress file I have added the following annotation nginx.ingress.kubernetes.io/auth-snippet and it needs to be removed in one of the environment.
Base Ingress:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress
annotations:
nginx.ingress.kubernetes.io/auth-snippet: test
I created a ingress-patch.yml in overlays and added the below
- op: remove
path: /metadata/annotations/nginx.ingress.kubernetes.io/auth-snippet
But it gives the below error when executing Kustomize Build
Error: remove operation does not apply: doc is missing path: "/metadata/annotations/nginx.ingress.kubernetes.io/auth-snippet": missing value
The path /metadata/annotations/nginx.ingress.kubernetes.io/auth-snippet doesn't work because / is the character that JSONPath uses to separate elements in the document; there's no way for a JSONPath parser to know that the / in nginx.ingress.kubernetes.io/auth-snippet means something different from the / in /metadata/annotations.
The JSON Pointer RFC (which is the syntax used to specify the path component of a patch) tells us that we need to escape / characters using ~1. If we have the following in ingress.yaml:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress
annotations:
example-annotation: foo
nginx.ingress.kubernetes.io/auth-snippet: test
And write our kustomization.yaml like this:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ingress.yaml
patches:
- target:
kind: Ingress
name: ingress
patch: |
- op: remove
path: /metadata/annotations/nginx.ingress.kubernetes.io~1auth-snippet
Then the output of kustomize build is:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
example-annotation: foo
name: ingress
Just currently battling an issue with kustomize and not having much look.
I have my config setup and are using kustomize (v4.5.7) to have separate base, variants and environment configuration. I’m trying to use the setup to deploy a copy of my dev environment onto the same cluster using different namespaces and a suffix.
The idea is that everything would be deployed using a suffix for the name (and got this working but it only does the names and not the namespaces) and drop them into separate namespaces with a suffix.
I’m currently defining all the namspaces with the following config:
apiVersion: v1
kind: Namespace
metadata:
name: mynamespace
Now i want to be able to deploy copies of the NS named mynamespace-mysuffix
I’ve managed to implemented a suffix for the names of the object alongside a PrefixSuffixTransformer to update the namespaces in the objects created to mynamespace-mysuffix
This unfortunately doesn’t update the namespace configuration and leaves things in tact. In short it tries to deploy the objects into namespaces that do not exist.
This is the working PrefixSuffixTransformer amending the namespace set in the various objects:
apiVersion: builtin
kind: PrefixSuffixTransformer
metadata:
name: customSuffixer
suffix: "-mysuffix"
fieldSpecs:
- path: metadata/name
- path: metadata/namespace
trying to target the namespace objects unsuccessfully with the following additional PrefixSuffixTransformer
apiVersion: builtin
kind: PrefixSuffixTransformer
metadata:
name: nsSuffixer
suffix: "-mysuffix"
fieldSpecs:
- kind: Namespace
path: metadata/name
Was hoping on this last part working but no success. Anyone any suggestions as to how I can get the additional namespaces created with a suffix?
If I understand your question correctly, the solution is just to add a namespace: declaration to the kustomization.yaml file in your variants.
For example, if I have a base directory that contains:
namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: example
spec: {}
service.yaml
apiVersion: v1
kind: Service
metadata:
name: example
spec:
ports:
- name: http
port: 80
targetPort: http
kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: example
resources:
- namespace.yaml
- service.yaml
And I create a variant in overlays/example, with this kustomization.yaml:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: example-mysuffix
resources:
- ../../base
nameSuffix: -mysuffix
Then running kustomize build overlays/example results in:
apiVersion: v1
kind: Namespace
metadata:
name: example
spec: {}
---
apiVersion: v1
kind: Service
metadata:
name: example-mysuffix
namespace: example
spec:
ports:
- name: http
port: 80
targetPort: http
As you have described in your question, the Namespace resource wasn't renamed by the nameSuffix configuration. But if I simply add a namespace: declaration to the kustomization.yaml, like this:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: example-mysuffix
resources:
- ../../base
nameSuffix: -mysuffix
Then I get the desired output:
apiVersion: v1
kind: Namespace
metadata:
name: example-mysuffix
spec: {}
---
apiVersion: v1
kind: Service
metadata:
name: example-mysuffix
namespace: example-mysuffix
spec:
ports:
- name: http
port: 80
targetPort: http
Is there a way to combine a namespace creation with a resource quota in one go?
I'm looking for something like:
apiVersion: v1
kind: Namespace
metadata:
name: custom-namespace
quota: {"cpu": "400m", "memory": "1Gi"}
You can combine different documents in the same YAML file using dashes as separator.
For your example it would like
apiVersion: v1
kind: Namespace
metadata:
name: custom-namespace
---
apiVersion: v1
kind: ResourceQuota
metadata:
name: cpu
namespace: custom-namespace
spec:
hard:
limits.cpu: "400m"
limits.memory: 1Gi
You can then apply the file or pipe it from stdin.
$ kubectl apply -f temp.yaml
namespace/custom-namespace created
resourcequota/cpu created
In Azure Kubernetes Service, my goal is to configure both staging and production k8 clusters with a common YAML file, with critical values & environment variables parameterized from a ConfigMap.
I can set container environment variables easily using valueFrom but I would like to use the ConfigMap values in other areas of the YAML file, for example:
staging-config-map.yaml:
kind: ConfigMap
apiVersion: v1
metadata:
name: base-config
data:
ENVIRONMENT_NAME: staging
...
prod-config-map.yaml:
kind: ConfigMap
apiVersion: v1
metadata:
name: base-config
data:
ENVIRONMENT_NAME: prod
...
common-cluster-config.yaml:
apiVersion: v1
kind: Service
metadata:
name: my-amazing-microservice
annotations:
service.beta.kubernetes.io/azure-dns-label-name: "my-amazing-microservice-$ENVIRONMENT_NAME"
spec:
type: LoadBalancer
ports:
- targetPort: 5000
name: port5000
port: 5000
protocol: TCP
selector:
app: my-amazing-microservice
---
...
Note the reference to $ENVIRONMENT_NAME which is where I want to insert something from the ConfigMap.
Can I do this, so I don't have to maintain duplicated manifests for staging and prod?
No you cant with vanilla k8's manifests. ConfigMaps are just resources that get mounted into the container when it starts. Either as env variables or as a file. You cant access a config map at deployment time.
I suggest looking into helm that can do this with templating.
I have below k8s configuration yml file but when run kubectl apply, it gives me the error namespaces "aws-observability" not found.
I understand that the aws-observability namespace is not deployed when deploying the ConfigMap.
It can be solved by split this config to two files and deploy the namespace first then the ConfigMap. But I'd like to put them in one file and deploy them in one go. How can I add a depend between these two configurations?
kind: Namespace
apiVersion: v1
metadata:
name: aws-observability
labels:
aws-observability: enabled
kind: ConfigMap
apiVersion: v1
metadata:
name: aws-logging
namespace: aws-observability
labels:
data:
output.conf: |
[OUTPUT]
Name cloudwatch
Match *
region <ap-southeast-2>
log_group_name elk-fluent-bit-cloudwatch
log_stream_prefix from-elk-fluent-bit-
auto_create_group true
You should add separator (---) between two components. I have tested below YAML on my machine and its working as expected:
kind: Namespace
apiVersion: v1
metadata:
name: aws-observability
labels:
aws-observability: enabled
---
kind: ConfigMap
apiVersion: v1
metadata:
name: aws-logging
namespace: aws-observability
labels:
data:
output.conf: |
[OUTPUT]
Name cloudwatch
Match *
region <ap-southeast-2>
log_group_name elk-fluent-bit-cloudwatch
log_stream_prefix from-elk-fluent-bit-
auto_create_group true