{"id":759,"date":"2018-11-08T13:11:58","date_gmt":"2018-11-08T13:11:58","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=759"},"modified":"2018-11-08T13:11:58","modified_gmt":"2018-11-08T13:11:58","slug":"setup-etcd-cluster-linux","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=759","title":{"rendered":"How To Setup a etcd Cluster on Linux &#8211; Beginners Guide"},"content":{"rendered":"<p>This blog will teach you the basics of etcd and then set up an etcd cluster on Linux using step-by-step guides<\/p>\n<p>Before you begin, make sure you have the following setup.<\/p>\n<ol>\n<li>Three Linux servers (Can be an odd quorum of 5, 7, etc based on the needs)<\/li>\n<li>A valid hostname for all the servers<\/li>\n<li>Firewall rules enabled all the servers on the following ports for client requests and peer-to-peer communication. <\/li>\n<\/ol>\n<pre><code>2380 2379<\/code><\/pre>\n<h2 id=\"setup-etcd-cluster\">Setup etcd Cluster<\/h2>\n<p>etcd setup is fairly easy and this guide follows the static bootstrap method, which means you need to know the IPs of your nodes for bootstrapping. This guide covers all the necessary steps to set up a cluster on Linux servers. It is a multimode setup with systemd files to run etcd as a service.<\/p>\n<p>Following are the etcd server hostname and IP details used in this guide. Change the IPs mentioned in the guide with your IPs where ever needed.<\/p>\n<ol>\n<li><strong>etcd-1<\/strong>: 10.128.0.2<\/li>\n<li><strong>etcd-2<\/strong>: 10.128.0.4<\/li>\n<li><strong>etcd-3: <\/strong>10.128.0.3<\/li>\n<\/ol>\n<p>Let&#8217;s get started with the setup.<\/p>\n<h3 id=\"on-all-the-3-nodes\">On All the 3 Nodes<\/h3>\n<p>Perform steps 1 to 6 on all three nodes.<\/p>\n<p><strong>Step 1<\/strong>: CD into the local src folder<\/p>\n<pre><code>cd \/usr\/local\/src<\/code><\/pre>\n<p><strong>Step 2: <\/strong>Download the latest etcd release from the etcd <a href=\"https:\/\/github.com\/etcd-io\/etcd\/releases?ref=devopscube.com\">Github Releases<\/a>. At the time of writing this article, the latest version is 3.3.10<\/p>\n<pre><code>sudo wget \"https:\/\/github.com\/coreos\/etcd\/releases\/download\/v3.3.9\/etcd-v3.3.9-linux-amd64.tar.gz\"<\/code><\/pre>\n<p><strong>Step 3<\/strong>: Untar the binary.<\/p>\n<pre><code>sudo tar -xvf etcd-v3.3.9-linux-amd64.tar.gz<\/code><\/pre>\n<p><strong>Step 4:<\/strong> Move the extracted etcd executables (etcd &amp; ectdctl) to local bin.<\/p>\n<pre><code>sudo mv etcd-v3.3.9-linux-amd64\/etcd* \/usr\/local\/bin\/<\/code><\/pre>\n<p><strong>Step 5: <\/strong>Create relevant etcd folders, user &amp; group. We will be running the etcd service as an etcd user.<\/p>\n<pre><code>sudo mkdir -p \/etc\/etcd \/var\/lib\/etcd\ngroupadd -f -g 1501 etcd\nuseradd -c \"etcd user\" -d \/var\/lib\/etcd -s \/bin\/false -g etcd -u 1501 etcd\nchown -R etcd:etcd \/var\/lib\/etcd<\/code><\/pre>\n<p><strong>Step 6:<\/strong> Perform the following as root user.<\/p>\n<p>Set two environment variables. One to fetch the system IP and another to get the system hostname.<\/p>\n<pre><code>ETCD_HOST_IP=$(ip addr show eth0 | grep \"inet\\b\" | awk '{print $2}' | cut -d\/ -f1)\nETCD_NAME=$(hostname -s)<\/code><\/pre>\n<p>Create a systemd service file for etcd. Replace <code>--listen-client-urls<\/code> with your server IPs<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><i><b><strong class=\"italic\" style=\"white-space: pre-wrap;\">Note:<\/strong><\/b><\/i> &#8211;name , &#8211;initial-advertise-peer-urls, &#8211;listen-peer-urls, &#8211;listen-client-urls will be different for each server. ETCD_NAME &amp; ETCD_HOST_IP variables will automatically replace it.<\/div>\n<\/div>\n<pre><code>cat &lt;&lt; EOF &gt; \/lib\/systemd\/system\/etcd.service\n[Unit]\nDescription=etcd service\nDocumentation=https:\/\/github.com\/coreos\/etcd\n \n[Service]\nUser=etcd\nType=notify\nExecStart=\/usr\/local\/bin\/etcd \\\\\n --name ${ETCD_NAME} \\\\\n --data-dir \/var\/lib\/etcd \\\\\n --initial-advertise-peer-urls http:\/\/${ETCD_HOST_IP}:2380 \\\\\n --listen-peer-urls http:\/\/${ETCD_HOST_IP}:2380 \\\\\n --listen-client-urls http:\/\/${ETCD_HOST_IP}:2379,http:\/\/127.0.0.1:2379 \\\\\n --advertise-client-urls http:\/\/${ETCD_HOST_IP}:2379 \\\\\n --initial-cluster-token etcd-cluster-1 \\\\\n --initial-cluster etcd-1=http:\/\/10.142.0.2:2380,etcd-2=http:\/\/10.142.0.4:2380,etcd-3=http:\/\/10.142.0.3:2380 \\\\\n --initial-cluster-state new \\\\\n --heartbeat-interval 1000 \\\\\n --election-timeout 5000\nRestart=on-failure\nRestartSec=5\n \n[Install]\nWantedBy=multi-user.target\nEOF<\/code><\/pre>\n<h3 id=\"bootstrap-the-etcd-cluster\">Bootstrap The etcd Cluster<\/h3>\n<p>Once all the configurations are applied on the three servers, start and enable the newly created etcd service on all the nodes. The first server will act as a bootstrap node. One node will be automatically elected as a leader once the service is started in all the three nodes.<\/p>\n<pre><code>systemctl daemon-reload\nsystemctl enable etcd\nsystemctl start etcd.service\nsystemctl status -l etcd.service<\/code><\/pre>\n<h3 id=\"verify-etcd-cluster-status\">Verify etcd Cluster Status<\/h3>\n<p><code>ectdctl<\/code> is the utility to interact with the etcd cluster. You can find this utility in the folder<code>\/usr\/local\/bin<\/code> of all the nodes.<\/p>\n<p>You can use any one of the cluster nodes to perform the following checks.<\/p>\n<p>Check the cluster health using the following command<\/p>\n<pre><code>etcdctl cluster-health<\/code><\/pre>\n<p>Verify cluster membership status using the following command. It will show the leader&#8217;s status.<\/p>\n<pre><code>etcdctl  member list<\/code><\/pre>\n<p>By default, etcdctl uses etcd v2. So you need to explicitly use a variable <code>ETCDCTL_API=3<\/code> to access etcd v3 functionalities.<\/p>\n<p>You can set it as an environment variable or pass it along with each etcdctl command as shown below.<\/p>\n<p>Let&#8217;s write a few key-value pairs in the cluster and verify it.<\/p>\n<pre><code>ETCDCTL_API=3 etcdctl put name1 batman\nETCDCTL_API=3 etcdctl put name2 ironman\nETCDCTL_API=3 etcdctl put name3 superman\nETCDCTL_API=3 etcdctl put name4 spiderman<\/code><\/pre>\n<p>Now you can try getting the value of name3 using the following command.<\/p>\n<pre><code>ETCDCTL_API=3 etcdctl get name3<\/code><\/pre>\n<p>You can list all the keys using ranges and prefixes<\/p>\n<pre><code>ETCDCTL_API=3 etcdctl get name1 name4 # lists range name1 to name 4 ETCDCTL_API=3 etcdctl get --prefix name # lists all keys with name prefix<\/code><\/pre>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/setup-etcd-cluster-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">How To Setup a etcd Cluster on Linux &#8211; Beginners Guide \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/setup-etcd-cluster-linux\/<\/p>\n","protected":false},"author":1,"featured_media":760,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-759","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/759","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=759"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/759\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/760"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=759"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=759"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=759"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}