Sudi Krishnakumar:
hello all, This is definitely something super silly I am doing, but I cannot figure out what I am doing wrong for the life of me
so I have the following aliases set up
alias k=kubectl
alias doyaml=" --dry-run=client -oyaml"
now I do
k run nginx-pod --image=nginx:alpine doyaml > nginx-pod.yaml
and when I open the nginx-pod.yaml
I see the result in it which is pod/nginx-pod created
However when I do
k run nginx-pod --image=nginx:alpine --dry-run=client -oyaml > nginx-pod.yaml
The correct yaml pod spec is created.
I do not understand why the alias doyaml does not seem to work in the first case? Appreciate if anyone can enlighten me! Thanks
Alistair Mackay:
@Sudi Krishnakumar you need to do it a a shell variable
Alistair Mackay:
export doyaml='--dry-run=client -o yaml'
k run nginx-pod --image=nginx:alpine $doyaml > nginx-pod.yaml
Note the $
to expand the shell variable
Alistair Mackay:
Save yourself extra typing by simply calling it y
instead of doyaml
Sudi Krishnakumar:
Thanks mate!
Pallavi Sharma:
Does this mean we need to always use export instead of Alias directly?
Pallavi Sharma:
@Alistair Mackay
Alistair Mackay:
export and alias are different things.
• alias
creates an alias for a runnable command (optionally with some of its arguments)
• export
creates a shell variable which is visible to any program or subshell you run from the terminal.
--dry-run-client -o yaml
is not a command in its own right, therefore we use a shell variable for it which can be expanded in the context of a longer command line. So
alias k=kubectl
export y='--dry-run=client -o yaml`
k run nginx-pod --image=nginx:alpine $y > nginx-pod.yaml
k
is an alias. Note that it is pre-existing in the exam so you don’t have to create it.
y
is a shell variable. Note the use of quotes above. This is necessary since the value includes spaces.
The shell therefore interprets the above as
kubectl run nginx-pod --image=nginx:alpine --dry-run=client -o yaml > nginx-pod.yaml
This is basic Linux stuff. Anyone doing Kubernetes is recommended to also do a basic Linux course, like https://kodekloud.com/courses/the-linux-basics-course/
Alistair Mackay:
A good alias to create is this
alias kgp='kubectl get pods'
Then you can do
kgp
or
kgp -o wide
etc
also
alias kcn='kubectl config set-context --curreent --namespace'
so
kcn my-namespace
Time savers in exam!