i have lots of deployments yaml files and lots of services yaml files, i want to know if it is possible to execute all the yaml files at once ?
i tried this command:
kubectl create -f *.yaml
But it's not working
Thanks
If you are inside any directory and you want to apply all the YAML
kubectl apply -f ./
or else you merge many YAML files in single YAML file
YAML1 content
---
YAML 2 content
---
YAML 3 contnet
Addition to #Harsh answer.
Apply resources from a directory:
kubectl apply -k dir/
Include the sub directories to include all manifest files from sub-directories.
kubectl apply -R -f dir/
-R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests
organized within the same directo
merge those by using
---
three dashes in one file
Related
I'm working under some restrictions, outside of my control, namely the CI/CD pipeline and the infrastructure in general.
I've been testing my kustomizations on my local console which has v1.21, but my pipeline is running v1.17.
So components are out, and apparently so is patching multiple resources with the patches object: https://github.com/kubernetes-sigs/kustomize/issues/1373#issuecomment-618439078
And no, directly invoking kustomize (of any version) is not available either.
Not using components, I can deal with, but multi-patching support seems incredibly basic.
Here's a basic example of what I was doing:
root
|- kube
|- kustomize
|- base
| |- job_dir
| | |- job1.yml
| | |- ..
| |- job.patch.yml
| | # config that applies to some jobs
| |- kustomization.yml
| # resources:
| # - job_dir/job1.yml
| # - ..
| #
| # patches:
| # - target:
| # kind: Job
| # patch: |-
| # - op: add
| # ..
| # - path: job.patch.yml
| # target:
| # kind: Job
| # labelSelector: patchWith=job.patch
|- overlays
|- dev
| |- kustomization.yml
| # images: ..
|- prod
|- kustomization.yml
# images: ..
executed with kubectl 1.17:
$ kubectl apply -k kube/kustomize/base
error: json: cannot unmarshal object into Go struct field Kustomization.patchesStrategicMerge of type patch.StrategicMerge
executed with kubectl 1.20+:
kubectl apply -k kube/kustomize/base
job.batch/job1 created
job.batch/..
Without multi-patching, can I emulate the same behavior somehow?
Preferably with using some templating tool like jinja2.
Worth noting that the infrastructure's actual Kubernetes instance is running v1.20, so there's some restrictions there, too. (for instance, why I'm not just using indexed jobs ಠ_ಠ) So, if there's a way to
I found a way. This probably isn't the best way.
You can use kubectl patch to patch specific files -f PATH, directories -f DIR_PATH, kustomizations -k DIR_PATH, or directories recursively -Rf DIR_PATH.
You should be careful, though, as this applies the patch to everything in scope.
For instance, this is what happens when a ConfigMap is in the directory where you're applying a patch meant for jobs:
$ ls kube/kustomize/base
job.yml config.yml
$ patch='[{op: add, path: /spec/template/spec/restartPolicy, value: Never}]'
$ kubectl patch --local=true --type=json -p "$patch" -o name -R kube/kustomize/base
job.batch/job1
error: add operation does not apply: doc is missing path: "/spec/template/spec/restartPolicy": missing value
In this case, it's not too bad since nothing unexpected got the patch, but if you're not careful you might apply a patch to the wrong resources. Globs don't work, so you won't be able to patch based on a file pattern unless there's only one resource that matches.
So, -f PATH is the best, and safest.
You can compile a list of the correct files using something like this, depending on the system:
$ kust_dir="kube/kustomize/base"
$ kust_file="${kust_dir}/kustomization.yml"
$ sed -n '/^resources:\s*$/,/^[^ \t-]/p' ${kust_file} | tail -n +2 | head -n -1 | sed -i "s#\s*-\s\+#$kust_dir#" > manifest_list.tmp
It gets pretty complicated if you want a bulletproof way to use label selectors, but this flawed approach works for most things:
# recursive look for label anywhere in $kust_dir, output filenames, filter output by the existing list of filenames
$ grep -lre "$label:\s*$value" $kust_dir | grep -Ff manifest_list.tmp > filtered_by_label.tmp
As stated, it matches any label in the file. If you know the whitespace string that manifests are indented with, and they're uniform, can do this:
# indent_str=" " if it's two spaces
$ grep -lre "\($indent_str\)\{2\}$label:\s*$value" $kust_dir | grep -Ff manifest_list.tmp > filtered_by_label.tmp
which should only match the first label. Use -z and -P options to match multiple lines to get a better result, but it may not work on every system, and you still might need to know the number of tabs, or else use some find -exec awk or find -exec sed.
once you have a file list you're confident of, do something like this:
$ for file in $file_list; do
> kubectl patch --local=true --type=json -p '$patch_str' -o yaml -f $file > $file
> done
$ kubectl apply -k kube/kustomize/base
job/job1 created
You can use -R -f kube/kustomize/base/jobs or -k kube/kustomize/base to apply the patch to multiple resources, but the output won't include the --- separator, so you can't write the output to a file and then apply it without some processing. But you can tinker with that as a possible alternative.
I have multiple pods running as below. I want to delete them all except the one having minimum age. How to do it?
Something like this? Perhaps also add -l app=value to filter for a specific app
kubectl get pods --sort-by=.metadata.creationTimestamp -o name | head -n -1 | xargs echo kubectl delete
(Remove echo to do it for realz)
You could just use PowerShell and pipe the output to the Foreach-Object, which then loops over each line:
kubectl get pods -n default -o name | ForEach-Object { kubectl delete $_ }
I'm accessing Kubernetes through the CLI tool kubectl and I'm trying to get a list of all context names, one per line.
I know that JSONPath can be used to extract and format specific output. I get really close to what I want with
kubectl config view -o=jsonpath="{.contexts[*].name}"
but this puts all the names on the same line. I'm trying to use range to list all names separated by newlines:
kubectl config view -o=jsonpath='{range .contexts[*]}{.name}{"\n"}{end}'
But this just gives me an error:
error: unexpected arguments: [.contexts[*]}{.name}{"\n"}{end}]
See 'kubectl config view -h' for help and examples.
I've reviewed the kubectl documentation and what I'm doing is really similar to https://kubernetes.io/docs/tasks/access-application-cluster/list-all-running-container-images/#list-containers-by-pod, where the command is
kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' |\
sort
but I can't see where I'm going wrong.
Your command works for me in kubectl 1.9.2
If it still doesn't work, you can use tr in bash to replace spaces with new lines:
kubectl config view -o=jsonpath="{.contexts[*].name}" | tr " " "\n"
I figured it out. I had been using #ahmetb's kubectl-aliases script, which works fine with no problem, but one of the suggestions in the README was:
Print the full command before running it: Add this to your .bashrc or .zshrc file:
function kubectl() { echo "+ kubectl $#"; command kubectl $#; }
I had that function declaration in my .bashrc and it was stripping off the quotes for my jsonpath argument. As soon as I commented out that declaration and opened a new shell, the command worked correctly.
Have you seen a comma in a kubectl command?
Like the following command:
kubectl get deployments,ing -n my-system
Is it like a pipe grap or?
It is just a separator between k8s resources, that you would like to do something with (get/describe/etc...). Your command will output the list of Ingress resources next to the list of Deployments
I suppose I could compare the number of files in the source directory to the number of files in the target directory as cp progresses, or perhaps do it with folder size instead? I tried to find examples, but all bash progress bars seem to be written for copying single files. I want to copy a bunch of files (or a directory, if the former is not possible).
You can also use rsync instead of cp like this:
rsync -Pa source destination
Which will give you a progress bar and estimated time of completion. Very handy.
To show a progress bar while doing a recursive copy of files & folders & subfolders (including links and file attributes), you can use gcp (easily installed in Ubuntu and Debian by running "sudo apt-get install gcp"):
gcp -rf SRC DEST
Here is the typical output while copying a large folder of files:
Copying 1.33 GiB 73% |##################### | 230.19 M/s ETA: 00:00:07
Notice that it shows just one progress bar for the whole operation, whereas if you want a single progress bar per file, you can use rsync:
rsync -ah --progress SRC DEST
You may have a look at the tool vcp. Thats a simple copy tool with two progress bars: One for the current file, and one for overall.
EDIT
Here is the link to the sources: http://members.iinet.net.au/~lynx/vcp/
Manpage can be found here: http://linux.die.net/man/1/vcp
Most distributions have a package for it.
Here another solution: Use the tool bar
You could invoke it like this:
#!/bin/bash
filesize=$(du -sb ${1} | awk '{ print $1 }')
tar -cf - -C ${1} ./ | bar --size ${filesize} | tar -xf - -C ${2}
You have to go the way over tar, and it will be inaccurate on small files. Also you must take care that the target directory exists. But it is a way.
My preferred option is Advanced Copy, as it uses the original cp source files.
$ wget http://ftp.gnu.org/gnu/coreutils/coreutils-8.21.tar.xz
$ tar xvJf coreutils-8.21.tar.xz
$ cd coreutils-8.21/
$ wget --no-check-certificate wget https://raw.githubusercontent.com/jarun/advcpmv/master/advcpmv-0.8-8.32.patch
$ patch -p1 -i advcpmv-0.8-8.32.patch
$ ./configure
$ make
The new programs are now located in src/cp and src/mv. You may choose to replace your existing commands:
$ sudo cp src/cp /usr/local/bin/cp
$ sudo cp src/mv /usr/local/bin/mv
Then you can use cp as usual, or specify -g to show the progress bar:
$ cp -g src dest
A simple unix way is to go to the destination directory and do watch -n 5 du -s . Perhaps make it more pretty by showing as a bar . This can help in environments where you have just the standard unix utils and no scope of installing additional files . du-sh is the key , watch is to just do every 5 seconds.
Pros : Works on any unix system Cons : No Progress Bar
To add another option, you can use cpv. It uses pv to imitate the usage of cp.
It works like pv but you can use it to recursively copy directories
You can get it here
There's a tool pv to do this exact thing: http://www.ivarch.com/programs/pv.shtml
There's a ubuntu version in apt
How about something like
find . -type f | pv -s $(find . -type f | wc -c) | xargs -i cp {} --parents /DEST/$(dirname {})
It finds all the files in the current directory, pipes that through PV while giving PV an estimated size so the progress meter works and then piping that to a CP command with the --parents flag so the DEST path matches the SRC path.
One problem I have yet to overcome is that if you issue this command
find /home/user/test -type f | pv -s $(find . -type f | wc -c) | xargs -i cp {} --parents /www/test/$(dirname {})
the destination path becomes /www/test/home/user/test/....FILES... and I am unsure how to tell the command to get rid of the '/home/user/test' part. That why I have to run it from inside the SRC directory.
Check the source code for progress_bar in the below git repository of mine
https://github.com/Kiran-Bose/supreme
Also try custom bash script package supreme to verify how progress bar work with cp and mv comands
Functionality overview
(1)Open Apps
----Firefox
----Calculator
----Settings
(2)Manage Files
----Search
----Navigate
----Quick access
|----Select File(s)
|----Inverse Selection
|----Make directory
|----Make file
|----Open
|----Copy
|----Move
|----Delete
|----Rename
|----Send to Device
|----Properties
(3)Manage Phone
----Move/Copy from phone
----Move/Copy to phone
----Sync folders
(4)Manage USB
----Move/Copy from USB
----Move/Copy to USB
There is command progress, https://github.com/Xfennec/progress, coreutils progress viewer.
Just run progress in another terminal to see the copy/move progress. For continuous monitoring use -M flag.