Skip to content

Instantly share code, notes, and snippets.

@mjbright
Last active November 18, 2025 07:43
Show Gist options
  • Select an option

  • Save mjbright/0b12daf273a77a2925e2bf7cbf2be31b to your computer and use it in GitHub Desktop.

Select an option

Save mjbright/0b12daf273a77a2925e2bf7cbf2be31b to your computer and use it in GitHub Desktop.
Easy access to etcdctl
# Function to simplify use of etcdctl from cp Node
#
# - set POD to the Pod name for etcd on a Node:
# - then try one of the commands:
# etcdctl member list
# etcdctl member list -w table
# etcdctl member list -w table
# etcdctl endpoint health
# etcdctl endpoint status -w table
#
function etcdctl {
[ -z "$1" ] && set -- endpoint status -w table
echo "Running on $(hostname) [$(hostname -i)]"
# SET this value as appropriate:
POD=etcd-cp
case $(hostname) in
cp) POD=etcd-cp;;
secondcp|cp2) POD=etcd-secondcp;;
thirdcp|cp3) POD=etcd-thirdcp;;
esac
# NOTE: ETCDCTL_API=3 is no longer needed after Kubernetes 1.34
kubectl --request-timeout='1' -n kube-system exec -it $POD -- sh -c \
"ETCDCTL_API=3 ETCDCTL_CACERT=/etc/kubernetes/pki/etcd/ca.crt ETCDCTL_CERT=/etc/kubernetes/pki/etcd/server.crt ETCDCTL_KEY=/etc/kubernetes/pki/etcd/server.key etcdctl $*"
}
@mjbright
Copy link
Author

mjbright commented Nov 18, 2025

etcdcl_all.fn

Here's a version for use with an HA cluster.

Note the -l option which loops every 2 seconds

# Function to simplify use of etcdctl from cp Node showing all endpoints status:
# - set POD to the Pod name for etcd on a Node:
# - set ENDPOINTS list
# - then try one of the commands:
#      etcdctl_all
#      etcdctl_all member list
#      etcdctl_all member list -w table
#      etcdctl_all member list -w table
#      etcdctl_all endpoint health
#      etcdctl_all -l endpoint status -w table
#
function etcdctl_all {
    local LOOP=0
    [ "$1" = "-x" ] && { shift; set -x; }
    [ "$1" = "-l" ] && { shift; LOOP=1; }
    [ -z "$1"      ]  &&  set -- endpoint status -w table

    POD=etcd-cp
    case $(hostname) in
                  cp) POD=etcd-cp;;
        secondcp|cp2) POD=etcd-secondcp;;
         thirdcp|cp3) POD=etcd-thirdcp;;
    esac

    ENDPOINTS="$( kubectl -n kube-system get pods -l component=etcd -o wide -o custom-columns=IP:.status.podIP --no-headers | sed -e 's/$/:2379/' -e 's/ /,/g' )"

    # NOTE: ETCDCTL_API=3 is no longer needed after Kubernetes 1.34

    echo "-- $(date) Running on $(hostname) [$(hostname -i)] -----------------------------"
    while true; do
        kubectl --request-timeout='1' -n kube-system exec -it $POD -- sh -c "ETCDCTL_API=3 ETCDCTL_CACERT=/etc/kubernetes/pki/etcd/ca.crt ETCDCTL_CERT=/etc/kubernetes/pki/etcd/server.crt ETCDCTL_KEY=/etc/kubernetes/pki/etcd/server.key etcdctl --command-timeout=2s --endpoints $ENDPOINTS $*"
        [ $LOOP -eq 0 ] && { set +x; return; }
        sleep 2
    done
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment