{"id":2803,"date":"2022-08-29T11:33:29","date_gmt":"2022-08-29T11:33:29","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=2803"},"modified":"2023-08-04T16:18:41","modified_gmt":"2023-08-04T16:18:41","slug":"install-cockroach-db-cluster","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/install-cockroach-db-cluster","title":{"rendered":"How to Install Cockroach DB Cluster?"},"content":{"rendered":"<p class=\"ahmdropcap\"><b>Cockroach DB<\/b> is an elastic<a href=\"https:\/\/www.checkmateq.com\/full-stack-development\"> SQL\u00a0<\/a> \u00a0database described as\u00a0 &#8220;almost impossible&#8221; to take down and used for modern applications, It is developed by Cockroach Labs and it&#8217;s designed to store copies of data in multiple locations\/stores\u00a0 in order to enable\u00a0 speedy access for the users\u00a0 ,you can easily add or remove a node to Cockroach DB cluster and it&#8217;s easy to\u00a0 scale a Cockroach DB cluster from a single node on your laptop to thousands of server nodes, In this blog\u00a0 let&#8217;s install Cockroach DB on three nodes and create\u00a0 a\u00a0 cluster.<\/p>\n<h3>Install Cockroach DB on all the Nodes<\/h3>\n<pre>sudo yum install curl tar -y<\/pre>\n<p>Execute below given command to download the Cockroach DB archive for Linux and its supporting libraries, and then copy the binary into\u00a0 \/usr\/local\/bin\/ so you can execute cockroach commands from any shell.<\/p>\n<pre>sudo curl https:\/\/binaries.cockroachdb.com\/cockroach-v22.1.6.linux-amd64.tgz | tar -xz &amp;&amp; sudo cp -i cockroach-v22.1.6.linux-amd64\/cockroach \/usr\/local\/bin\/<\/pre>\n<p><img loading=\"lazy\" class=\" wp-image-2805 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Hire-DevOps-Engineer-3-300x33.jpeg\" alt=\"\" width=\"1164\" height=\"128\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Hire-DevOps-Engineer-3-300x33.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Hire-DevOps-Engineer-3-768x84.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Hire-DevOps-Engineer-3-1536x167.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Hire-DevOps-Engineer-3-1200x131.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Hire-DevOps-Engineer-3.jpeg 1892w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>CockroachDB uses custom-built versions of the\u00a0<strong>GEOS<\/strong> libraries, it is an open-source geometry library used by CockroachDB, by default, CockroachDB looks for external libraries in <strong>\/usr\/local\/lib\/cockroach<\/strong> , so create a folder named cockroach at\u00a0 \/usr\/local\/lib and copy libgeos.so,\u00a0 libgeos_c.so to it, use below given commands to do this.<\/p>\n<pre>sudo mkdir -p \/usr\/local\/lib\/cockroach\r\nsudo cp -i cockroach-v22.1.6.linux-amd64\/lib\/libgeos.so \/usr\/local\/lib\/cockroach\/\r\nsudo cp -i cockroach-v22.1.6.linux-amd64\/lib\/libgeos_c.so \/usr\/local\/lib\/cockroach\/\r\n<\/pre>\n<p>Now use below given command on your terminal to check the installation.<\/p>\n<pre>cockroach demo<\/pre>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2808 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Devops-Support-2-300x217.jpeg\" alt=\"\" width=\"869\" height=\"629\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Devops-Support-2-300x217.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Devops-Support-2-1024x741.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Devops-Support-2-768x556.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Devops-Support-2-1200x868.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Devops-Support-2.jpeg 1291w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>use command\u00a0<strong>exit<\/strong> to come out of the shell, once you are done with the installation on all the nodes follow the next steps for creating a cluster.<\/p>\n<h3><strong>Configuring Name Resolution for Cluster Nodes<\/strong><\/h3>\n<p>Configuring Name resolution is very important while setting up a cluster, because, if a node is unable to resolve the hostname of the other nodes it will give an error while initializing a cluster, I am configuring the local DNS resolver for hostname resolution of the cluster nodes.<\/p>\n<p>Edit \/<strong>etc\/hosts<\/strong> file on all nodes and give your node IP addresses and hostnames as shown in below given snapshot, do it on all the nodes.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2813 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-amazon-web-services-1-300x87.jpeg\" alt=\"\" width=\"786\" height=\"228\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-amazon-web-services-1-300x87.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-amazon-web-services-1-1024x297.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-amazon-web-services-1-768x223.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-amazon-web-services-1-1200x349.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-amazon-web-services-1.jpeg 1439w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<h2>Setup Cockroach DB Cluster<\/h2>\n<p><strong>Generate Certificates for secure connections\u00a0<\/strong><\/p>\n<p>Configuration of Cockroach DB cluster in Insecure mode is very simple but it does not perform any encryption of inter-cluster communication, but secure mode uses certificates to enforce encryption of inter-cluster communication and authorization, first create a Certificate Authority-(CA-key), this will be used to digitally sign certificates that you will generate for configuring your Cockroach DB secure cluster<\/p>\n<p>Create two directories &#8220;certs&#8221; and &#8220;my-safe-directory&#8221; on<b> all the nodes<\/b>,use certs to store CA certificate and all node and client certificates and keys, use my-safe-directory to keep your CA key and then reference the key when generating node and client certificates.<\/p>\n<pre>mkdir {certs,my-safe-directory}<\/pre>\n<h3>Configure First Node<\/h3>\n<p>Now use the following <strong>cockroach<\/strong> command to create a certificate authority certificate and a key on the <strong>first node<\/strong>.<\/p>\n<pre>cockroach cert create-ca \\ --certs-dir=certs \\ --ca-key=my-safe-directory\/ca.key<\/pre>\n<p>Generate a certificate for <strong>2nd<\/strong> <strong>CockroachDB node<\/strong> using the following command on node one , use your node&#8217;s IP address, hostname here<\/p>\n<pre>cockroach cert create-node \\\r\n  172.31.2.22  \\\r\n  ip-172-31-2-22.us-west-2.compute.internal  \\\r\n  127.0.0.1 \\\r\n  localhost \\\r\n  --certs-dir=certs \\\r\n  --ca-key=my-safe-directory\/ca.key\r\n<\/pre>\n<p><img loading=\"lazy\" class=\" wp-image-2814 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Screenshot-795-300x36.png\" alt=\"\" width=\"808\" height=\"97\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Screenshot-795-300x36.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Screenshot-795-1024x123.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Screenshot-795-768x92.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Screenshot-795.png 1053w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Here\u00a0 172.31.2.22\u00a0 is my 2nd node IP and ip-172-31-2-22.us-west-2.compute.internal is its hostname ,please replace them with your node details, once the certificates are generated, copy the certs folder on to the 2nd node certs folder (CA certificate, node certificate and key) from node cert folder, and after copying use below given command on the first node to remove them and create certificates for first node.<\/p>\n<pre>rm certs\/node.crt certs\/node.key<\/pre>\n<p>as the certificate and keys are copied to the second node and you have to generate it for the first node, use the same command on the first with <strong>3rd node<\/strong> node IP and hostname and copy them on to node 3 then use\u00a0 &#8220;rm certs\/node.crt certs\/node.key&#8221; to delete them on node one, and finally use the cockroach create command on node one to generate the certifiacte.<\/p>\n<p>Now move the certs directory to the cockroach directory (&#8220;\/var\/lib\/cockroach&#8221;)on each CockroachDB node and change the ownership of Cockroach directory to the user cockroach<\/p>\n<pre>sudo mkdir \/var\/lib\/cockroach\r\nsudo cp -rv certs \/var\/lib\/cpckroach\r\nsudo useradd cockroach\r\nsudo chown -R cockroach.cockroach \/var\/lib\/cockroach\/\r\n<\/pre>\n<h3>Configure Systemd Service for Cockroach DB<\/h3>\n<p>To enable auto-start of CockroachDB server during Linux startup, you are required to create a systemd service unit, do the following on all the CockroachDB nodes.<\/p>\n<p>Create a systemd service unit file.<\/p>\n<pre>vi \/etc\/systemd\/system\/cockroachdb.service<\/pre>\n<p>vi is a editor here<\/p>\n<pre>[Unit]\r\nDescription=Cockroach Database cluster node\r\nRequires=network.target\r\n[Service]\r\nType=notify\r\nWorkingDirectory=\/var\/lib\/cockroach\r\nExecStart=\/usr\/local\/bin\/cockroach start --certs-dir=certs --advertise-addr=ip-172-31-11-237.us-west-2.compute.internal --join=ip-172-31-2-22.us-west-2.compute.internal,ip-172-31-11-229.us-west-2.compute.internal, --cache=.25 --max-sql-memory=.25\r\nTimeoutStopSec=60\r\nRestart=always\r\nRestartSec=10\r\nStandardOutput=syslog\r\nStandardError=syslog\r\nSyslogIdentifier=cockroach\r\nUser=cockroach\r\n[Install]\r\nWantedBy=default.target\r\n<\/pre>\n<p>Repeat creating the systemd file on each node and you need to replace the &#8211;advertise-addr with the hostname of the CockroachDB node on which you are creating this systemd service and then start CockroachDB service using below given command.<\/p>\n<pre>sudo systemctl start cockroachdb<\/pre>\n<p>Then check the status of the service on each Node.<\/p>\n<pre>sudo systemctl status cockroachdb<\/pre>\n<p><img loading=\"lazy\" class=\" wp-image-2821 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Cloud-Engineering-Services-5-300x53.jpeg\" alt=\"\" width=\"973\" height=\"172\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Cloud-Engineering-Services-5-300x53.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Cloud-Engineering-Services-5-1024x182.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Cloud-Engineering-Services-5-768x137.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Cloud-Engineering-Services-5-1536x273.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Cloud-Engineering-Services-5-1200x213.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Cloud-Engineering-Services-5.jpeg 1793w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>Before initializing the cluster open the required ports, 26257\/tcp is CockroachDB default service port for SQL interface, execute the below given commands on each node.<\/p>\n<pre>sudo yum install firewalld -y\r\nsudo systemctl enable --now firewalld \r\nsudo firewall-cmd --permanent --add-port={8080,26257}\/tcp\r\nsudo firewall-cmd --reload\r\n<\/pre>\n<p><img loading=\"lazy\" class=\" wp-image-2822 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Hire-DevOps-Engineer-4-300x37.jpeg\" alt=\"\" width=\"851\" height=\"105\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Hire-DevOps-Engineer-4-300x37.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Hire-DevOps-Engineer-4-1024x125.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Hire-DevOps-Engineer-4-1200x146.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Hire-DevOps-Engineer-4.jpeg 1290w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p><strong>Initialize CockroachDB Cluster\u00a0<\/strong><\/p>\n<p>As all the required configuration is done intialize\u00a0 cockroachDB cluster using below given command.<\/p>\n<pre>cockroach init --certs-dir=certs --host=<strong>&lt;address of any node&gt;<\/strong>\r\ncockroach init --certs-dir=certs --host=localhost:26257<\/pre>\n<p>Replace host value with address of any Node, and if the cluster is initialized successfully, you will get a success message as given below.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2824 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Database-Management-Company-300x34.jpeg\" alt=\"\" width=\"882\" height=\"100\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Database-Management-Company-300x34.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Database-Management-Company-768x88.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Database-Management-Company-1200x138.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/08\/Checkmate-Database-Management-Company.jpeg 1827w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>Please Contact our technical team for any offshore infrastructure management services <a href=\"https:\/\/www.checkmateq.com\/contact-us\">website<\/a>, <a href=\"https:\/\/www.linkedin.com\/company\/checkmatglobal\/\">LinkedIn<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cockroach DB is an elastic SQL\u00a0 \u00a0database described as\u00a0 &#8220;almost impossible&#8221; to take down and used for modern applications, It is developed by Cockroach Labs and it&#8217;s designed to store copies of data in multiple locations\/stores\u00a0 in order to enable\u00a0 speedy access for the users\u00a0 ,you can easily add or remove a node to Cockroach &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/install-cockroach-db-cluster\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Install Cockroach DB Cluster?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":2845,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[79,2,71,70,69,68,59,11,78,63,23,16,24,80,8],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2803"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=2803"}],"version-history":[{"count":21,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2803\/revisions"}],"predecessor-version":[{"id":4174,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2803\/revisions\/4174"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/2845"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=2803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=2803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=2803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}