'Need some explaination of kubectl stdin and pipe
I'm daily user of kubectl, but not expert of linux. Recently I need edit some service type after deployment, so searched and used kubectl replace and it worked well.
cat yaml | kubectl replace -f -
service/tracs-pool-1sv replaced
But I don't understand why add a short dash - at the last.
The doc only says:
Replace a pod based on the JSON passed into stdin.
I searched and found this SO question, and learned kubectl command may be that kind of command that does not read stdin(am I right?).
I tried
cat yaml |xargs kubectl replace -f
but error returned:
the path "apiVersion:" does not exist
So is the ending short dash(-) syntax built for kubectl ONLY? or is some more common syntax of linux bash stdin pipe? Can some one explain why xargs not work here and I must place a short dash(-) at the end?
Solution 1:[1]
Short dash(-) here represents stdin. This is a kubectl specific implementation. There are many other linux utilities as well that have similar implementation.
Apart from David's answer, there is another common format that we see used with kubectl. Its called Heredoc.
kubectl apply -f - <<EOF
apiVersion: v1
kind: ServiceAccount
metadata:
name: kubectl-actions
namespace: my-v2-restore
EOF
OR
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ServiceAccount
metadata:
name: kubectl-actions
namespace: my-v2-restore
EOF
Heredoc: is a way to send multiline string to the tool/command for processing.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
