Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to kubectl apply all the files in a directory?

I am writing an ansible playbook right now that deploys a dockerized application in kubernetes. However, for molecularity purposes I would rather not hard code the files that need to be apply after doing kompose convert -f docker-compose.yaml --volumes hostPath Is there a way to apply all the files in a directory?

like image 954
James Ukilin Avatar asked Dec 26 '19 17:12

James Ukilin


People also ask

What is kubectl apply command?

The command set kubectl apply is used at a terminal's command-line window to create or modify Kubernetes resources defined in a manifest file. This is called a declarative usage. The state of the resource is declared in the manifest file, then kubectl apply is used to implement that state.

Does kubectl apply overwrite?

kubectl apply .. will use various heuristics to selectively update the values specified within the resource. kubectl replace ... will replace / overwrite the entire object with the values specified. This should be preferred as you're avoiding the complexity of the selective heuristic update.

How do I deploy multiple YAML files in Kubernetes?

Using Helm, you can deploy multiple resources (bunch of YAMLs) in one go. Helm Charts help you define, install, and upgrade even the most complex Kubernetes application. Helm uses a packaging format called charts. A chart is a collection of files that describe a related set of Kubernetes resources.

What is the opposite of kubectl apply?

It's really important to remember that technically there isn't an inverse to kubectl apply . This is because of how k8s works, it converges desired state against current state. By running apply , you are telling the cluster to "make it look like this".


3 Answers

You can apply all files in a folder with

kubectl apply -f <folder> 

You may also be interested in parameterization of your manifest files using Kustomize e.g. use more replicas in a prod-namespace than in a test-namespace. You can apply parameterized manifest files with

kubectl apply -k <folder>
like image 147
Jonas Avatar answered Oct 17 '22 18:10

Jonas


The above command to give the directory as an option to "-f" works perfectly if you want to apply all the files in the given directory.

The following should apply the yaml files in the current directory matching the given criteria (e.g. here all yamls starting with test)

kubectl apply $(ls test*.yaml | awk ' { print " -f " $1 } ')
like image 17
pr-pal Avatar answered Oct 17 '22 18:10

pr-pal


An ansible-specific answer, using the ansible k8s module, would be:

      - name: Apply all manifests in a given folder
        k8s:
          state: present
          definition: "{{ lookup('template', '{{ item }}') }}"
          namespace: default
        with_fileglob:
          - "manifests/*.yaml"
          - "manifests/*.j2"

This would also allow you to template your manifests and parse them on-the-fly when applying the whole folder.

Please note that this ansible task expects that the manifests folder is in the same path as the running playbook is.

like image 5
iomv Avatar answered Oct 17 '22 16:10

iomv