This question already has an answer here:
Kubernetes (kubectl) get running pods
(1 answer)
Closed 4 months ago.
I want to extract a pod name from a list of pods available on kubernetes.
For example, for the following command
kubectl get pods -n namespace
NAME READY STATUS RESTARTS AGE
pod1 1/1 Running 2 46d
pod2 1/1 Running 0 46d
test-pod3-yy 0/1 ImagePullBackOff 0 338d
test-pod3-xx 1/1 Running 0 255d
I want to extract pod test-pod3-xx using shell script. Currently this is the command that I'm using
POD_NAME=$(kubectl get pods -n namespace | grep testpod-3 | cut -d' ' -f1)
With this I get both the pods test-pod3-yy and test-pod3-xx but I want to extract the pod that is in running state. How can I do that?
You can use the field-selector and check for running only:
--field-selector=status.phase=Running
You could also use the -o name flag, to get only the names. With that you'll get:
$ kubectl get pods -n namespace -o name --field-selector=status.phase=Running
pod/pod1
pod/pod2
pod/test-pod3-xx
#!/bin/sh -x
kubectl get pods -n namespace > stack
while read line
do
[[ -n $(sed -n '/Running/p' "${line}" ]] && echo "${line}"
done < stack
Related
I have few pods running in my kubernetes cluster. I am developing a shell script and I want to grep for few pods and want to select each pod from the grep result to execute a command.
Lets say I grep few pods by command :
kubectl get pods | grep test
the results are:
Test-0
Test-1
Test-2
From the result, I want to select each pod and execute a command for it in a loop.
for example:
for first pod:
kubectl exec -it Test-0 -- mysqldump.......
after finishing the first pod, it has to process the second pod and so on
for pod in $(kubectl get pod -oname |grep -i Test ); do
kubectl exec "$pod" -- ls -ltr ;
done
Replace ls -ltr with mysqldump .....
Get pods name and then use "for" to execute command in each pod
#!/bin/bash
pods=$(kubectl get pods | awk '{print $2}' | grep -i test)
for i in $pods
do
kubectl exec -it $i -- echo "test"
done
Select your target pods using labels is less error prone and can do multiple matching:
kubectl get pods --selector <key>=<value>,<key>=<value> --namespace <name> -oname | xargs -I{} kubectl exec -it {} --namespace <name> -- mysqldump ...
This question already has an answer here:
kubectl wait for a pod to complete
(1 answer)
Closed 1 year ago.
In a simple bash script I want to run multiple kubectl and helm commands, like:
helm install \
cert-manager jetstack/cert-manager \
--namespace cert-manager \
--create-namespace \
--version v1.5.4 \
--set installCRDs=true
kubectl apply -f deploy/cert-manager/cluster-issuers.yaml
My problem here is, that after the helm install command I have to wait until the cert-manager pod is running, then the kubectl apply command can be used. Right now the script is calling it too early, so it will fail.
As stated in the comments kubectl wait is the way to go.
Example from the kubectl wait --help
Examples:
# Wait for the pod "busybox1" to contain the status condition of type "Ready"
kubectl wait --for=condition=Ready pod/busybox1
This way your script will pause until specified pod is Running, and kubectl will output
<pod-name> condition met
to STDOUT.
kubectl wait is still in experimental phase. If you want to avoid experimental features, you can achieve similar result with bash while loop.
By pod name:
while [[ $(kubectl get pods <pod-name> -o 'jsonpath={..status.conditions[?(#.type=="Ready")].status}') != "True" ]]; do
sleep 1
done
or by label:
while [[ $(kubectl get pods -l <label>=<label-value> -o 'jsonpath={..status.conditions[?(#.type=="Ready")].status}') != "True" ]]; do
sleep 1
done
I'm trying to make a backup from Mongo / K8S with this script
export POD=$(kubectl get pods -l app=mongo-client -o custom-columns=:metadata.name -n espace-client)
kubectl exec "$POD" sh -c 'mongodump --archive' > ~/backup/mongo/$(date +%F).db.dump
I get this error:
Error from server (NotFound): pods "\nmongo-client-79667cc85d-tsg72" not found
When I check the pods, I can see mongo-client-79667cc85d-tsg72
When I put the name without variable, it works well, so it might be because of initial \n. How can I avoid it ?
How can I remove it from name ?
Your kubectl get pods command is constrained with a namespace selector -n espace-client. Your kubectl exec command also needs the namespace flag.
The output of your kubectl get pods command has a newline before the pod name because the first line of the output is the column header (which is empty in your case).
To prevent this and get only the name as output, you can suppress the column headers with the --no-headers flag:
kubectl get pods -l app=mongo-client -o custom-columns=:metadata.name -n espace-client --no-headers
I am trying to find a command or a sample shell snippet where I can wait until the list of Kubernetes pods is successful. I have checked the answer but it was not giving any output. Can someone guide me or suggest an approach, I am completely new to kubernetes.
kubectl -n test-ns get jobs -w
NAME DESIRED SUCCESSFUL AGE
test-1 1 1 2d
test-2 1 1 2d
test-3 1 1 2d
test-4 1 1 2d
until kubectl get jobs -n test-ns -o jsonpath='{.status.conditions[?(#.type=="Complete")].status}' | grep True ; do sleep 1 ; done
This is not giving any output
To wait until your pods are running, check for "condition=ready" and filter by app label, for example:
$ kubectl wait --for=condition=ready pod -l app=netshoot
pod/netshoot-58785d5fc7-xt6fg condition met
you need to use this command
kubectl rollout status
If you want to use kubectl as described here where it gets all the jobs, you need to use .items[*]... in your JSONpath (That answer is for just one specific job). For example:
kubectl -n test-ns get jobs \
-o jsonpath='{.items[*].status.conditions[?(#.type=="Complete")].status}' \
| grep True
I'm working with fleetctl and kubectl and would like to extract an IP from kubectl get pod app-etcd:
POD IP CONTAINER(S) IMAGE(S) HOST LABELS STATUS CREATED MESSAGE
app-etcd 10.10.0.1 k8s-socius-node-1/100.100.100.100 name=app-etcd Running 3 days
app-etcd xyz/etcd-discovery Running 3 days
The closest I got to get the IP address is:
kubectl get pod app-etcd | grep -Eo '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
But this gives me both IP addresses (10.10.0.1 and 100.100.100.100); I only want/need the first one to run sed over a config file afterwards.
How do I get only the first address to store it in a variable for further processing?
What if you use awk to get the second column output like
kubectl get pod app-etcd | awk '{print $2}'
kubectl offers json output with --output / -o:
kubectl get -o json pod app-etcd | jsawk 'return this.status.podIP'