<p>I am trying to get ETCD metrics like the number of ETCD keys and size as well as the number of requests made to ETCD through exec (ing) into a kubernetes pod (etcdctl) and am not sure what command to use for this.</p>

<p>An alternative (such as cUrl) would help as well. </p>

<p>Thanks for the help!</p>

Shrey Baid
<p>You need to extract the information from etcd and filter what you want. 
To illustrate, I will show you how to get the number of total keys from etcd.</p>

<blockquote>
  <p><strong>NOTE</strong>: Tested in kubernetes 1.18.2. </p>
</blockquote>

<pre class="lang-sh prettyprint-override"><code># Getting etcd pod IP and set a local variable:
ADVERTISE_URL="https://$(kubectl get pods -n kube-system -l=component=etcd -o=jsonpath='{ .items[*].status.podIP }'):2379"

# Getting ectd pod name and set a variable ETCD_POD
ETCD_POD=$(kubectl get pods -n kube-system -l=component=etcd -o=jsonpath='{ .items[*].metadata.name}')

# Extracting all etcd keys/values to a file called "etcd-kv.json":
kubectl exec $ETCD_POD -n kube-system -- sh -c \
"ETCDCTL_API=3 etcdctl \
--endpoints $ADVERTISE_URL \
--cacert /etc/kubernetes/pki/etcd/ca.crt \
--key /etc/kubernetes/pki/etcd/server.key \
--cert /etc/kubernetes/pki/etcd/server.crt \
get \"\" --prefix=true -w json" &gt; etcd-kv.json
</code></pre>

<p>Now you have all the keys/values pairs from etcd, you just need to filter to extract the information you need. For example, to list all keys you can use the command:</p>

<pre><code>for k in $(cat etcd-kv.json | jq '.kvs[].key' | cut -d '"' -f2);  do echo $k | base64 --decode;  echo;  done
</code></pre>

<p>and to count the number of keys, just use the command <code>wc -l</code> on the end of this command, like:</p>

<pre><code>for k in $(cat etcd-kv.json | jq '.kvs[].key' | cut -d '"' -f2);  do echo $k | base64 --decode; echo; done | echo "Total keys=$(wc -l)"
Total keys=308
</code></pre>

<p><strong>References:</strong></p>

<p><a href="https://medium.com/better-programming/a-closer-look-at-etcd-the-brain-of-a-kubernetes-cluster-788c8ea759a5" rel="nofollow noreferrer">A closer look at etcd: The brain of a kubernetes cluster</a></p>
