<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: omkar kulkarni</title>
    <description>The latest articles on DEV Community by omkar kulkarni (@omkarstwts).</description>
    <link>https://dev.to/omkarstwts</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F745672%2F2b2b58a7-e2f3-4e19-be60-e3e1dc6cecf3.jpg</url>
      <title>DEV Community: omkar kulkarni</title>
      <link>https://dev.to/omkarstwts</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/omkarstwts"/>
    <language>en</language>
    <item>
      <title>Taking Backup of your Kubernetes etcd Data: A step-by-step guide</title>
      <dc:creator>omkar kulkarni</dc:creator>
      <pubDate>Wed, 01 Nov 2023 14:35:32 +0000</pubDate>
      <link>https://dev.to/everythingdevops/taking-backup-of-your-kubernetes-etcd-data-a-step-by-step-guide-4dpa</link>
      <guid>https://dev.to/everythingdevops/taking-backup-of-your-kubernetes-etcd-data-a-step-by-step-guide-4dpa</guid>
      <description>&lt;p&gt;This article was originally posted on &lt;a href="https://everythingdevops.dev/backup-kubernetes-etcd-data/" rel="noopener noreferrer"&gt;Everything DevOps&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In the ever-evolving landscape of container orchestration, Kubernetes (K8s) has emerged as the gold standard for managing and scaling containerized applications. At the heart of every K8s cluster lies a critical component known as &lt;a href="https://etcd.io/" rel="noopener noreferrer"&gt;etcd&lt;/a&gt;. etcd is a distributed key-value store that stores and manages all of the K8s' configuration data, ensuring the system's reliability and consistency.&lt;/p&gt;

&lt;p&gt;While K8s provides a robust platform for deploying and managing applications, the need to safeguard the etcd data cannot be overstated. This is where the importance of taking regular backups comes into play.&lt;/p&gt;

&lt;p&gt;In this article, we'll dive into the essential part of etcd backup in Kubernetes, understanding why it's crucial for the stability and recoverability of your cluster.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Relationship between Kubernetes and etcd&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;At the core of Kubernetes, etcd — an open-source distributed key-value store that acts as Kubernetes' primary database for storing configuration data and ensuring cluster consistency.&lt;br&gt;
Etcd serves as the single source of truth, storing information about the cluster's state, configuration, and secrets. Kubernetes components, including the API server, controller manager, and scheduler, rely heavily on etcd to synchronize and manage containerized workloads across the cluster.&lt;/p&gt;

&lt;p&gt;This tight integration makes etcd indispensable in maintaining the stability and reliability of a Kubernetes cluster, underlining the need for regular backups to safeguard this vital component.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why is it crucial to take a backup of your Kubernetes cluster?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Taking regular backups of etcd in the Kubernetes cluster is crucial for several reasons, as it ensures the reliability, recoverability, and security of your K8s cluster. Here are key points explaining why regular etcd backups are essential:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Data Recovery&lt;/strong&gt;: In the event of data loss or cluster-wide failures, etcd backups serve as a lifeline to restore your K8s cluster to a previously known state. This minimizes downtime and ensures business continuity.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Configuration History&lt;/strong&gt;: Etcd stores the entire configuration history of your K8s cluster. Regular backups provide a historical record of changes, enabling you to trace and understand configuration modifications and troubleshoot issues over time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rollback and Versioning&lt;/strong&gt;: Etcd backups enable you to roll back to previous cluster configurations or versions, which is essential for testing new configurations or reverting to a stable state in case of issues with updates or changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Prerequisites&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Before you learn how to take a backup of the etcd cluster, ensure you have the following prerequisites:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Kubernetes Cluster using &lt;a href="https://kubernetes.io/docs/reference/setup-tools/kubeadm/" rel="noopener noreferrer"&gt;Kubeadm&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;An &lt;code&gt;etcd&lt;/code&gt; server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For demo purposes, I used the &lt;a href="https://killercoda.com/playgrounds/scenario/kubernetes" rel="noopener noreferrer"&gt;Killerkoda Kubernetes playground&lt;/a&gt;.&lt;br&gt;
To communicate with etcd, you’ll need &lt;a href="https://etcd.io/docs/v3.5/dev-guide/interacting_v3/" rel="noopener noreferrer"&gt;etcdctl&lt;/a&gt;, a command line utility for communicating with the etcd database, as it comes with the Kubeadm cluster by default.&lt;br&gt;
etcdctl supports two versions of the etcd server's API. When making server calls, it defaults to version 2 of the API. In version 2, some operations are either undefined or have different arguments.&lt;br&gt;
Next, you will tell &lt;code&gt;etcdctl&lt;/code&gt; to use the V3 API, which is required for the snapshot functionality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting up&lt;/strong&gt; &lt;code&gt;**ETCDCTL_API**&lt;/code&gt; &lt;strong&gt;to VERSION 3&lt;/strong&gt;&lt;br&gt;
To make &lt;code&gt;etcdctl&lt;/code&gt; use the V3 API; you can either set the environment variable with each call as in the following commands.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ETCDCTL_API=3 etcdctl snapshot save ...  
$ ETCDCTL_API=3 etcdctl snapshot restore ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;or the entire terminal session.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ export ETCDCTL_API=3
$ etcdctl snapshot save ...
$ etcdctl snapshot restore ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;How to Backup your Kubernetes etcd Data&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To take a backup of the etcd database, you run the following command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ etcdctl snapshot save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For executing this operation, you’ll need a few flags (arguments)  of certificates, which are mandatory for verification of the etcd server. This is because you must authenticate with the etcd server before it will expose its sensitive data. The authentication scheme is called &lt;a href="https://www.cloudflare.com/en-gb/learning/access-management/what-is-mutual-tls/" rel="noopener noreferrer"&gt;Mutual TLS (mTLS)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To learn more about the flags, run:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ etcdctl snapshot save -h
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The output of the above command should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropboxusercontent.com%2Fs_5A155DA51EAA199DDD1E46BA3F8D568BE2AB2C2BAED904AC0571647D68BA185D_1694419531046_image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropboxusercontent.com%2Fs_5A155DA51EAA199DDD1E46BA3F8D568BE2AB2C2BAED904AC0571647D68BA185D_1694419531046_image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You’ll need 4 important arguments to successfully backup etcd:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;--cacert&lt;/li&gt;
&lt;li&gt;--cert&lt;/li&gt;
&lt;li&gt;--key&lt;/li&gt;
&lt;li&gt;--endpoints (Optional)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s look into these arguments, what they are, and why you should pass them.&lt;br&gt;
&lt;strong&gt;1. --cacert&lt;/strong&gt;&lt;br&gt;
This provides the path to the Certificate Authority (CA). The CA certificate is used to verify the authenticity of the TLS certificate sent to &lt;code&gt;etcdctl&lt;/code&gt; by the etcd server. The server's certificate found must be signed by the CA. Creating the CA is one of the tasks you need to do when building a cluster. Kubeadm does it automatically.&lt;br&gt;
&lt;strong&gt;2. --cert&lt;/strong&gt;&lt;br&gt;
This is the path to the TLS certificate that &lt;code&gt;etcdctl&lt;/code&gt; sends to the etcd server. The etcd server will verify that this certificate is also signed by the same CA. Certificates of this type contain a &lt;strong&gt;&lt;em&gt;public key&lt;/em&gt;&lt;/strong&gt; that can be used to encrypt data. The public key is used by the server to encrypt data being sent back to &lt;code&gt;etcdctl&lt;/code&gt; during the authentication steps.&lt;br&gt;
&lt;strong&gt;3. --key&lt;/strong&gt;&lt;br&gt;
This is the path to the private key that is used to decrypt data sent to &lt;code&gt;etcdctl&lt;/code&gt; by the etcd server during the authentication steps. The key is &lt;em&gt;only&lt;/em&gt; used by the &lt;code&gt;etcdctl&lt;/code&gt; process. It is never sent to the server.&lt;br&gt;
&lt;strong&gt;4. --endpoints (optional)&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;--endpoints&lt;/code&gt; argument on &lt;code&gt;etcdctl&lt;/code&gt; is used to tell it where to find the etcd server. If you are running the command on the same host where etcd service is running &lt;em&gt;and&lt;/em&gt; there is only one instance of etcd, then you do not need to provide this argument, as it has a default value of &lt;code&gt;https://127.0.0.1:2379&lt;/code&gt;.&lt;br&gt;
If your etcd service is running on the different port you need to provide that different port number instead of &lt;code&gt;2379&lt;/code&gt;  -  &lt;code&gt;https://127.0.0.1:port&lt;/code&gt;&lt;br&gt;
OR&lt;br&gt;
If your etcd service is running on the remote host then you need to pass  -&lt;br&gt;
&lt;code&gt;--endpoints https://host-ip:port&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where to find the values of these arguments?&lt;/strong&gt;&lt;br&gt;
As etcd is running as a pod in the Kubernetes namespace called &lt;code&gt;kube-system&lt;/code&gt;. You can describe the same pod, and you will able to see all the arguments and their values.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl describe -n kube-system pod etcd-controlplane
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropboxusercontent.com%2Fs_5A155DA51EAA199DDD1E46BA3F8D568BE2AB2C2BAED904AC0571647D68BA185D_1694420994895_image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropboxusercontent.com%2Fs_5A155DA51EAA199DDD1E46BA3F8D568BE2AB2C2BAED904AC0571647D68BA185D_1694420994895_image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As this contains a lot of information that we don't need right now, we can use &lt;code&gt;grep&lt;/code&gt; command to extract only what we need.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl describe -n kube-system pod etcd-controlplane | grep -i file 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropboxusercontent.com%2Fs_5A155DA51EAA199DDD1E46BA3F8D568BE2AB2C2BAED904AC0571647D68BA185D_1694421202409_image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropboxusercontent.com%2Fs_5A155DA51EAA199DDD1E46BA3F8D568BE2AB2C2BAED904AC0571647D68BA185D_1694421202409_image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can observe here the path of these all certificates is at the location &lt;code&gt;/etc/kubernetes/pki/etcd&lt;/code&gt; so you can find them as well from controlplane node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Final backup command will be:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ETCDCTL_API=3 etcdctl snapshot save \
      --cacert /etc/kubernetes/pki/etcd/ca.crt \
      --cert /etc/kubernetes/pki/etcd/server.crt \
      --key /etc/kubernetes/pki/etcd/server.key \
      /opt/etcd-backup.db
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;/opt/etcd-backup.db&lt;/code&gt; is the path for storing etcd backup data.&lt;br&gt;
You should see output similar to this&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropboxusercontent.com%2Fs_5A155DA51EAA199DDD1E46BA3F8D568BE2AB2C2BAED904AC0571647D68BA185D_1694422240643_image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropboxusercontent.com%2Fs_5A155DA51EAA199DDD1E46BA3F8D568BE2AB2C2BAED904AC0571647D68BA185D_1694422240643_image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Restoring from a backup&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Normally you will restore this to another directory, and then point the &lt;code&gt;etcd&lt;/code&gt; service at the new location. For restores, the certificate and endpoints arguments are not required, as we are doing creating files in directories and not talking to the &lt;code&gt;etcd&lt;/code&gt; API, so the only argument required is &lt;code&gt;--data-dir&lt;/code&gt; to tell &lt;code&gt;etcdctl&lt;/code&gt; where to put the restored files.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ etcdctl snapshot restore -h
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropboxusercontent.com%2Fs_5A155DA51EAA199DDD1E46BA3F8D568BE2AB2C2BAED904AC0571647D68BA185D_1694422587581_image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropboxusercontent.com%2Fs_5A155DA51EAA199DDD1E46BA3F8D568BE2AB2C2BAED904AC0571647D68BA185D_1694422587581_image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can pass any value as the path to the argument &lt;code&gt;-- data-dir&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The final restore command will be:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ETCDCTL_API=3 etcdctl snapshot restore \
      --data-dir /var/lib/etcd-from-backup \
      /opt/etcd-backup.db
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above command will output the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropboxusercontent.com%2Fs_5A155DA51EAA199DDD1E46BA3F8D568BE2AB2C2BAED904AC0571647D68BA185D_1694422840813_image.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpaper-attachments.dropboxusercontent.com%2Fs_5A155DA51EAA199DDD1E46BA3F8D568BE2AB2C2BAED904AC0571647D68BA185D_1694422840813_image.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;This article described how you can take a backup of etcd in the Kubernetes cluster and restore it safely to avoid data loss and cluster-wide failures.&lt;br&gt;
There is much more to learn about Kubernetes and etcd. Check out the following resources to explore more:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://etcd.io/docs" rel="noopener noreferrer"&gt;etcd documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://kubernetes.io/search/?q=etcd" rel="noopener noreferrer"&gt;Kubernetes resources on etcd&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>kubernetes</category>
      <category>etcd</category>
      <category>database</category>
    </item>
  </channel>
</rss>
