Loop through Kubernetes Ingress and Invoke curl
Here is a bash script that can loop through the hosts in a Kubernetes ingress and invoke the curl command for each host to send a request and capture the response code:
#!/bin/bash
# Set the ingress name and namespace
ingress_name="my-ingress"
namespace="default"
# Get the list of hosts for the ingress
hosts=$(kubectl get ingress "$ingress_name" -n "$namespace" -o jsonpath='{.spec.rules[*].host}')
# Loop through each host and send a request with curl
for host in $hosts; do
# Send the request and capture the response code
response_code=$(curl -s -o /dev/null -w "%{http_code}" "http://$host")
# Print the host and response code
echo "Host: $host, Response code: $response_code"
done
This script uses the kubectl command to get the list of hosts for the specified ingress. It then loops through each host and sends a request using the curl command to get the response code. The script prints the host and the response code for each request.
You can run this script by saving it to a file (e.g. check_ingress_hosts.sh) and running it with the bash command
bash check_ingress_hosts.sh
Note that this script assumes that the ingress is in the default namespace. You will need to modify the namespace variable in the script to specify the correct namespace if your ingress is in a different namespace. Additionally, you may need to modify the curl command in the script to specify a different URL or request method if needed.
To query kubernetes endpoints for all services in a namespace, use the following command:
kubectl get endpoints -n <namespace>
To invoke a curl command for each service endpoint, use the following command:
for endpoint in $(kubectl get endpoints -n <namespace> -o jsonpath='{.items[*].subsets[*].addresses[*].ip}');
do curl http://$endpoint;
done
Replace <namespace> with the name of the namespace where the services are located. This will iterate through all endpoints in the specified namespace and execute a curl command on each endpoint.