Last active
September 21, 2023 21:19
-
-
Save dalmosantos/31649bb8da89dfcdcaeaba5a23cb1374 to your computer and use it in GitHub Desktop.
change nodegroup.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Verificar se o kubectl está instalado | |
if ! command -v kubectl &> /dev/null; then | |
echo "O kubectl não está instalado. Por favor, instale-o antes de continuar." | |
exit 1 | |
fi | |
# Defina o namespace do seu aplicativo ou o namespace padrão | |
NAMESPACE="default" | |
# Obtenha uma lista de todos os serviços em todos os namespaces | |
SERVICE_LIST=$(kubectl get svc -A -o custom-columns=NAME:.metadata.name --no-headers) | |
# Lista de nomes de serviços a serem verificados | |
SERVICE_NAMES=("service1" "service2" "service3") | |
# Defina o nome do pod temporário | |
TEMP_POD_NAME="dnsutils-pod" | |
# Criar um pod temporário com a imagem dnsutils | |
kubectl run $TEMP_POD_NAME -n $NAMESPACE --image=debian:stretch-slim --restart=Never --command -- sleep infinity | |
# Aguarde o pod ficar em execução | |
kubectl wait --for=condition=Ready pod/$TEMP_POD_NAME -n $NAMESPACE | |
# Iterar pelos nomes dos serviços e verificar a resolução de nomes para cada um | |
for SERVICE_NAME in "${SERVICE_NAMES[@]}"; do | |
# Verificar se o serviço existe | |
if echo "$SERVICE_LIST" | grep -q "$SERVICE_NAME"; then | |
echo "Verificando a resolução de nomes para $SERVICE_NAME..." | |
kubectl exec -it -n $NAMESPACE $TEMP_POD_NAME -- nslookup $SERVICE_NAME | |
echo "" | |
else | |
echo "Serviço $SERVICE_NAME não encontrado." | |
fi | |
done | |
# Encerrar o pod temporário após a solução de problemas | |
kubectl delete pod -n $NAMESPACE $TEMP_POD_NAME |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -xe | |
NAMESPACE="$1" | |
TMP_DIR=$(mktemp -d -t "${NAMESPACE}-XXXX") | |
TAINT_KEY="nodegroup" | |
TAINT_OPERATOR="Equal" | |
TAINT_VALUE="spot" | |
TAINT_EFFECT="NoSchedule" | |
NODE_SELECTOR_KEY="eks.amazonaws.com/nodegroup" | |
NODE_SELECTOR_VALUE="example" | |
# Function to check for the existence of a command and install it if necessary | |
install_command() { | |
local cmd_name="$1" | |
if ! command -v "$cmd_name" &>/dev/null; then | |
local download_url="$2" | |
local install_path="$3" | |
sudo wget "$download_url" -O "$install_path" && \ | |
sudo chmod +x "$install_path" | |
fi | |
} | |
# Function to log messages to the console | |
log() { | |
echo "$(date +"%Y-%m-%d %H:%M:%S"): $1" | |
} | |
# Check and install jq and yq when necessary | |
install_command "jq" "https://github.com/stedolan/jq/releases/download/jq-1.7/jq-linux-amd64" "/usr/local/sbin/jq" | |
install_command "yq" "https://github.com/mikefarah/yq/releases/download/4.13.2/yq_linux_amd64" "/usr/local/sbin/yq" | |
# Check if the temporary directory was created successfully | |
if [ ! -d "$TMP_DIR" ]; then | |
log "Error creating the temporary directory." | |
exit 1 | |
fi | |
# Function to manipulate YAML manifests | |
modify_yaml() { | |
local file="$1" | |
local operation="$2" | |
if yq eval --exit-status "$operation" "$file"; then | |
log "Operation executed on $file" | |
fi | |
} | |
# kubectl sanitize manifests | |
kubectl_yaml() { | |
kubectl get "$@" -o json | jq 'del(.metadata.managedFields,.metadata.resourceVersion,.metadata.uid,.metadata.selfLink,.metadata.creationTimestamp,.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration",.metadata.annotations."deployment.kubernetes.io/revision",.metadata.generation,.metadata.ownerReferences,.metadata.generateName,.metadata.conditions,.status)' | yq eval -P | |
} | |
# Extract YAML manifests | |
extract_manifests() { | |
local deployments=($(kubectl get deployments -n "$NAMESPACE" -o jsonpath='{.items[*].metadata.name}')) | |
for deployment in "${deployments[@]}"; do | |
local output_file="$TMP_DIR/${deployment}-deployment.yaml" | |
log "Extracting $deployment..." | |
kubectl_yaml deployment "$deployment" -n "$NAMESPACE" > "$output_file" | |
done | |
log "YAML manifests extracted and saved in the folder $TMP_DIR." | |
} | |
add_nodegroup_and_tolerations() { | |
local DEPLOYMENTS_FOLDER=$TMP_DIR | |
if [ ! -d "$DEPLOYMENTS_FOLDER" ]; then | |
echo "Deployments folder not found: $DEPLOYMENTS_FOLDER" | |
exit 1 | |
fi | |
# Iterate over files in the folder | |
for DEPLOYMENT_FILE in $DEPLOYMENTS_FOLDER/*.yaml; do | |
if [ -f "$DEPLOYMENT_FILE" ]; then | |
# Check if the toleration field already exists in the file | |
if yq eval ".spec.template.spec.tolerations != null" "$DEPLOYMENT_FILE" >/dev/null 2>&1; then | |
# Modify the TAINT_VALUE variable | |
yq eval -i ".spec.template.spec.tolerations += [{\"key\": \"$TAINT_KEY\", \"operator\": \"$TAINT_OPERATOR\", \"value\": \"$TAINT_VALUE\", \"effect\": \"$TAINT_EFFECT\"}]" "$DEPLOYMENT_FILE" | |
else | |
# Add the toleration field | |
yq eval -i ".spec.template.spec.tolerations = [{\"key\": \"$TAINT_KEY\", \"operator\": \"$TAINT_OPERATOR\", \"value\": \"$TAINT_VALUE\", \"effect\": \"$TAINT_EFFECT\"}]" "$DEPLOYMENT_FILE" | |
fi | |
log "Toleration added/modified successfully in $DEPLOYMENT_FILE" | |
fi | |
done | |
} | |
add_node_selector() { | |
local DEPLOYMENTS_FOLDER="$TMP_DIR" | |
local NODE_SELECTOR_KEY="eks.amazonaws.com/nodegroup" | |
local NODE_SELECTOR_VALUE_ADD="example" | |
local NODE_SELECTOR_VALUE_UPDATE="ondemand" | |
local LOG_FILE="log.txt" | |
if [ ! -d "$DEPLOYMENTS_FOLDER" ]; then | |
echo "Deployments folder not found: $DEPLOYMENTS_FOLDER" | |
exit 1 | |
fi | |
# Iterate over files in the folder | |
for DEPLOYMENT_FILE in "$DEPLOYMENTS_FOLDER"/*.yaml; do | |
if [ -f "$DEPLOYMENT_FILE" ]; then | |
# Check if the nodeSelector field already exists in the file | |
if yq eval ".spec.nodeSelector != null" "$DEPLOYMENT_FILE" >/dev/null 2>&1; then | |
# Update the NODE_SELECTOR_VALUE | |
yq eval -i ".spec.nodeSelector.$NODE_SELECTOR_KEY = \"$NODE_SELECTOR_VALUE_UPDATE\"" "$DEPLOYMENT_FILE" | |
log "Updated nodeSelector in $DEPLOYMENT_FILE" | |
else | |
# Add the nodeSelector field | |
yq eval -i ".spec.nodeSelector = { \"$NODE_SELECTOR_KEY\": \"$NODE_SELECTOR_VALUE_ADD\" }" "$DEPLOYMENT_FILE" | |
log "Added nodeSelector in $DEPLOYMENT_FILE" | |
fi | |
fi | |
done | |
} | |
# Extract and manipulate the manifests | |
extract_manifests | |
add_nodegroup_and_tolerations | |
add_or_update_node_selector |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Verificar se o kubectl está instalado | |
if ! command -v kubectl &> /dev/null; then | |
echo "O kubectl não está instalado. Por favor, instale-o antes de continuar." | |
exit 1 | |
fi | |
# Defina o namespace do seu aplicativo ou o namespace padrão | |
NAMESPACE="default" | |
# Nome do pod para depuração | |
POD_NAME="seu-pod-aqui" | |
# Nome do serviço que deseja resolver | |
SERVICE_NAME="nome-do-servico-aqui" | |
# Executar um shell dentro do pod | |
kubectl exec -it -n $NAMESPACE $POD_NAME -- /bin/sh | |
# Verificar a resolução de nomes dentro do pod | |
echo "Verificando a resolução de nomes dentro do pod..." | |
nslookup $SERVICE_NAME | |
# Verificar a configuração do DNS do cluster | |
echo "Verificando a configuração de DNS do cluster..." | |
kubectl get cm -n kube-system coredns -o yaml | |
# Verificar o status do serviço CoreDNS | |
echo "Verificando o status do serviço CoreDNS..." | |
kubectl get svc -n kube-system coredns | |
# Verificar os registros DNS do cluster | |
echo "Verificando os registros DNS do cluster..." | |
kubectl get endpoints -n kube-system kube-dns | |
# Verificar o status dos pods do CoreDNS | |
echo "Verificando o status dos pods do CoreDNS..." | |
kubectl get pods -n kube-system -l k8s-app=kube-dns | |
# Encerrar o shell interativo no pod | |
exit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
NAMESPACE=$1 | |
TMP_DIR=$(mktemp -d -t ${NAMESPACE}-XXXX) | |
# Ensure the temporary folder exists | |
mkdir -p $TMP_DIR | |
# install jq | |
install_jq() { | |
if ! command -v jq &>/dev/null; then | |
sudo wget https://github.com/jqlang/jq/releases/download/jq-1.7/jq-linux-amd64 -O /usr/local/sbin/jq && \ | |
sudo chmod +x /usr/local/sbin/jq | |
fi | |
} | |
# install yq | |
install_yq() { | |
if ! command -v yq &>/dev/null; then | |
sudo wget https://github.com/mikefarah/yq/releases/download/v4.35.1/yq_linux_amd64 -O /usr/local/sbin/yq && \ | |
sudo chmod +x /usr/local/sbin/yq | |
fi | |
} | |
# kubectl santinize manifests | |
kubectl_yaml() { | |
kubectl get "$@" -o json | jq 'del(.metadata.resourceVersion,.metadata.generateName,.metadata.uid,.metadata.selfLink,.metadata.creationTimestamp,.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration",.metadata.annotations."kubectl.kubernetes.io/last-applied-configuration",.metadata.generation,.metadata.ownerReferences,.status)' | yq eval . --prettyPrint | |
} | |
# Get a list of all deployments in the specified namespace | |
DEPLOYMENTS=$(kubectl get deployments -n $NAMESPACE -o jsonpath='{.items[*].metadata.name}') | |
# Loop through the deployments and extract their YAML manifests | |
for DEPLOYMENT in $DEPLOYMENTS; do | |
echo "Extracting $DEPLOYMENT..." | |
kubectl_yaml deployment $DEPLOYMENT -n $NAMESPACE > "$TMP_DIR/${DEPLOYMENT}_deployment.yaml" | |
done | |
echo "YAML manifests extracted and saved in the $TMP_DIR folder." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment