{"id":76,"date":"2021-03-05T01:46:03","date_gmt":"2021-03-05T01:46:03","guid":{"rendered":"https:\/\/stupidtechblog.com\/?p=76"},"modified":"2021-03-05T01:47:41","modified_gmt":"2021-03-05T01:47:41","slug":"setting-up-a-kubernetes-cluster-with-rancher","status":"publish","type":"post","link":"https:\/\/stupidtechblog.com\/?p=76","title":{"rendered":"Setting up a Kubernetes cluster with Rancher"},"content":{"rendered":"\n<p>Getting into Kubernetes can be a little intimidating. In this guide, I&#8217;ll show you how to set up a 6 node cluster using Rancher and some Ubuntu VMs.<\/p>\n\n\n\n<p>To start, you&#8217;re going to need 6 servers:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>rancher-man01 &#8211; First node of the &#8220;management&#8221; cluster for Kubernetes. These will run rancher, and an assortment of system containers.<\/li><li>rancher-man02 &#8211; Second node of the &#8220;management&#8221; cluster.<\/li><li>rancher-man03 &#8211; Third node of the &#8220;management&#8221; cluster.<\/li><li>rancher-work01 &#8211; First node of the &#8220;worker&#8221; cluster for Kubernetes. These will run your actual workloads.<\/li><li>rancher-work02 &#8211; Second node of the &#8220;worker&#8221; cluster.<\/li><li>rancher-work03 &#8211; Third node of the &#8220;worker&#8221; cluster.<\/li><\/ul>\n\n\n\n<p>You&#8217;re also going to need some sort of centralized DNS name that points to each of your rancher-man* servers. That should ideally include a load balancer, but if you don&#8217;t have one of those you can just set things up in DNS. For example, if our domain is .network.local, we can create A type DNS records like this:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>rancher.network.local &#8211; 10.0.0.51 (IP of rancher-man01)<\/li><li>rancher.network.local &#8211; 10.0.0.52 (IP of rancher-man02)<\/li><li>rancher.network.local &#8211; 10.0.0.53 (IP of rancher-man03)<\/li><\/ul>\n\n\n\n<p><strong>For now, only create an entry for rancher-man01. We&#8217;ll add the others later.<\/strong><\/p>\n\n\n\n<p>After those are all online and ready, you need to run a few commands to get the rancher server up and running. Start with rancher-man01.<\/p>\n\n\n\n<p><strong>All commands should be run as root.<\/strong> (sudo su -)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">rancher-man01<\/h2>\n\n\n\n<p>First, create a configuration file and populate it with some basic information:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mkdir -p \/etc\/rancher\/rke2\nvim \/etc\/rancher\/rke2\/config.yaml\n\n#config.yml content below:\ntoken: my-shared-secret # Change this to a nice secure string, you'll share it between your cluster nodes\ntls-san:\n  - rancher.network.local<\/pre>\n\n\n\n<p>Then, install RancherD, and start the service.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">curl -sfL https:\/\/get.rancher.io | sh -\nsystemctl enable rancherd-server.service\nsystemctl start rancherd-server.service<\/pre>\n\n\n\n<p>Use this command to watch the RancherD service start up. It may take a few minutes for everything to stabilize, and you&#8217;ll probably see some error messages. This is normal.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">journalctl -eu rancherd-server -f\n\n# You should see something like this:\nlevel=info msg=\"Handling backend connection request [rancher-man01]\"<\/pre>\n\n\n\n<p>Now you can set some environment variables and check on the kubernetes status<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export KUBECONFIG=\/etc\/rancher\/rke2\/rke2.yaml PATH=$PATH:\/var\/lib\/rancher\/rke2\/bin\nkubectl get daemonset rancher -n cattle-system\nkubectl get pod -n cattle-system<\/pre>\n\n\n\n<p>You should see something like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">root@rancher-man01:~# kubectl get daemonset rancher -n cattle-system\nNAME      DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR                         AGE\nrancher   1         1         1       1            1           node-role.kubernetes.io\/master=true   4m13s\nroot@rancher-man01:~# kubectl get pod -n cattle-system\nNAME                               READY   STATUS      RESTARTS   AGE\nhelm-operation-4vch8               0\/2     Completed   0          2m46s\nhelm-operation-84rfh               0\/2     Completed   0          3m22s\nhelm-operation-dt4gn               0\/2     Completed   0          2m20s\nhelm-operation-xspdr               0\/2     Completed   0          3m2s\nhelm-operation-z9b48               0\/2     Completed   0          2m30s\nrancher-c46b6                      1\/1     Running     0          4m25s\nrancher-webhook-798c5599d9-67ch7   1\/1     Running     0          2m26s\n<\/pre>\n\n\n\n<p>Now that rancher is up and running, you need to reset the admin password so that you can log into the web interface.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">rancherd reset-admin<\/pre>\n\n\n\n<p>That should give you a temporary password that you can use to log into the web interface, which will be at https:\/\/rancher.network.local:8443, or whatever other DNS name you set earlier in this tutorial. For now though, just write it down. We need to set the other cluster members up first.<\/p>\n\n\n\n<p>If you lose that password, you can just run that command again to get a new one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">rancher-man02<\/h2>\n\n\n\n<p>Setup on this node will be pretty similar to the first one, but with a few minor changes. First, we&#8217;re going to create that config.yaml file, but we&#8217;re going to add a reference to the existing node.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">mkdir -p \/etc\/rancher\/rke2\nvim \/etc\/rancher\/rke2\/config.yaml\n\n#config.yml content below:\nserver: https:\/\/rancher.network.local:9345\ntoken: my-shared-secret # Use the same secret that you set on rancher-man01\ntls-san:\n  - rancher.network.local<\/pre>\n\n\n\n<p>Then, <strong>install RancherD<\/strong>, and start the service.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">curl -sfL https:\/\/get.rancher.io | sh -\nsystemctl enable rancherd-server.service\nsystemctl start rancherd-server.service<\/pre>\n\n\n\n<p>Use this command to <strong>watch the RancherD service start up<\/strong>. It may take a few minutes for everything to stabilize, and you&#8217;ll probably see some error messages. This is normal. If you find that it&#8217;s taking a really long time and doesn&#8217;t seem to resolve itself, rebooting the server might help.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">journalctl -eu rancherd-server -f\n\n# You should see something like this:\nlevel=info msg=\"Handling backend connection request [rancher-man02]\"<\/pre>\n\n\n\n<p>Now you can set some environment variables and <strong>check on the Kubernetes status<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">export KUBECONFIG=\/etc\/rancher\/rke2\/rke2.yaml PATH=$PATH:\/var\/lib\/rancher\/rke2\/bin\nkubectl get daemonset rancher -n cattle-system\nkubectl get pod -n cattle-system<\/pre>\n\n\n\n<p>You should see something like this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">root@rancher-man02:~# kubectl get daemonset rancher -n cattle-system\nNAME      DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR                         AGE\nrancher   2         2         2       2            2           node-role.kubernetes.io\/master=true   90m\nroot@rancher-man02:~# kubectl get pod -n cattle-system\nNAME                               READY   STATUS    RESTARTS   AGE\nrancher-bkl5v                      1\/1     Running   0          17m\nrancher-webhook-798c5599d9-5r8zj   1\/1     Running   0          88m\nrancher-z4prp                      1\/1     Running   0          90m\n<\/pre>\n\n\n\n<p>Then, just <strong>do the same thing on rancher-man03<\/strong>.<\/p>\n\n\n\n<p>When both of those are done, update DNS with the extra records I described above, and then you should be able to hit <strong>https:\/\/rancher.network.local:8443<\/strong> and log in with the username &#8220;admin&#8221; and the password that was generated earlier.<\/p>\n\n\n\n<p>Now, we can create the worker cluster.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Rancher-work*<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>First, you need to <strong>install docker <\/strong>on all of your rancher-work nodes. You can do that by running this set of commands:<\/li><\/ul>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">apt update\napt install -y apt-transport-https ca-certificates curl gnupg\ncurl -fsSL https:\/\/download.docker.com\/linux\/ubuntu\/gpg | sudo gpg --dearmor -o \/usr\/share\/keyrings\/docker-archive-keyring.gpg\necho \"deb [arch=amd64 signed-by=\/usr\/share\/keyrings\/docker-archive-keyring.gpg] https:\/\/download.docker.com\/linux\/ubuntu $(lsb_release -cs) stable\" | sudo tee \/etc\/apt\/sources.list.d\/docker.list > \/dev\/null\napt update\napt install -y docker-ce docker-ce-cli containerd.io\n\n# After docker is installed, you can verify the installation with this:\n\ndocker run hello-world<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>In the <strong>Rancher web UI<\/strong>, click the <strong>Add Cluster<\/strong> button at the top right of the page.<\/li><li>Select <strong>Existing Nodes<\/strong><\/li><li><strong>Name <\/strong>the cluster something useful. I&#8217;m going to call mine <strong>lab<\/strong>.<\/li><li>At the time of writing this, <strong>Canal <\/strong>is the default network provider for Rancher, so I will be using that. However, there are other options that you may wish to choose. Take some time to do research and compare the different options available.<\/li><li>All of the other options can be left at their <strong>defaults<\/strong>, but feel free to research the different options on your own.<\/li><li>Once you create the cluster, you&#8217;ll be brought to a screen with <strong>3 checkboxes<\/strong> and a big preset command at the bottom of it. Ensure that all 3 boxes are checked, and then <strong>run that whole command<\/strong> on each of the rancher-work servers.<\/li><li>A minute or so after the command runs, you should see &#8220;3 new nodes have registered&#8221; at the bottom of the Rancher web page. When the nodes are registered, you can just click <strong>Done<\/strong> at the bottom of the page.<\/li><\/ul>\n\n\n\n<p>Now, you can create some <strong>DNS <\/strong>entries to make a nice load balanced cluster:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>rancher-work.network.local &#8211; 10.0.0.54 (IP of rancher-work01)<\/li><li>rancher-work.network.local &#8211; 10.0.0.55 (IP of rancher-work02)<\/li><li>rancher-work.network.local &#8211; 10.0.0.56 (IP of rancher-work03)<\/li><\/ul>\n\n\n\n<p>Once that&#8217;s done, you have your kubernetes cluster. Now we just need to run a workload.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>In the Rancher UI, click on <strong>Cluster Explorer<\/strong> at the top right of the page.<\/li><li>On the left, click <strong>Deployments<\/strong><\/li><li>At the top right, click <strong>Create<\/strong><\/li><li>For a <strong>name<\/strong>, just enter something descriptive. I&#8217;m going to use <strong>nginx-test<\/strong><\/li><li>For a container image, I&#8217;m going to use <strong>nginxdemos\/hello<\/strong>. It&#8217;s a nice test container.<\/li><li>Click <strong>Add Port<\/strong><\/li><li>For Service Type, you have a few options<ul><li>Do not create a service &#8211; Gives you the option to create the service definition later<\/li><li>Cluster IP &#8211; Puts the service on an IP only accessible from within the kubernetes cluster. Useful if you want containers to be able to communicate with each other.<\/li><li>Node Port &#8211; Publishes the service on each of the nodes in your cluster.<\/li><li>Load Balancer &#8211; Exposes the service using a cloud provider&#8217;s load balancer. Not useful if you&#8217;re not using AWS, Google, etc.<\/li><\/ul><\/li><li>We&#8217;re actually going to be creating something called an &#8220;ingress&#8221; to point at this nginx container, which is essentially a load balancer that runs on top of Kubernetes. So for now, we&#8217;re going to choose <strong>Cluster IP<\/strong>.<\/li><li>For name, you can just set a friendly name for the service. I&#8217;m using <strong>http<\/strong> in this instance.<\/li><li>That container exposes nginx on port <strong>80<\/strong>, so set that as the &#8220;Private Container Port&#8221;, and keep <strong>TCP <\/strong>selected.<\/li><li>With that set, go to <strong>Health Check<\/strong> on the left. For the readiness check, set the type to <strong>HTTP<\/strong>, and the check port to <strong>80<\/strong>. Set the path to<strong> \/<\/strong>. Do the same for the Liveness and Startup checks.<\/li><li>You can check out the rest of the options, but that&#8217;s all we need for now. Click <strong>Create<\/strong> at the bottom right of the page. You container will start up in a few seconds.<\/li><li>Now, go to <strong>Ingresses<\/strong> on the left. Click <strong>Create<\/strong> at the top right of that page.<\/li><li>Set the name to <strong>nginx-test<\/strong><\/li><li>The rest of the values here are useful for routing to different services running on the same cluster, but for this example, we&#8217;re just going to set the path to <strong>\/<\/strong>, the target service to <strong>nginx-test<\/strong>, and select port <strong>80<\/strong>.<\/li><\/ul>\n\n\n\n<p>Now, if you visit <strong>http:\/\/rancher-work.network.local<\/strong> you should see a nice nginx screen showing the IP address and hostname of the container you&#8217;re hitting.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Expanding The Deployment<\/h2>\n\n\n\n<p>You may notice, if you refresh your browser, you&#8217;ll just see one IP and hostname, even though we have 3 work servers supporting our container deployment. That&#8217;s because the &#8220;Replicas&#8221; value for our deployment is only set to 1, so we only have 1 container deployed. Let&#8217;s change that.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Go to <strong>Deployments <\/strong>on the left, and click the 3 dots on the right side next to <strong>nginx-test<\/strong>, select <strong>Edit Yaml<\/strong><\/li><li>Around line 137, change <strong>replicas <\/strong>to <strong>3<\/strong>. <\/li><li>Then click <strong>Save <\/strong>at the bottom. In a few seconds, the new containers should be deployed.<\/li><li>If you go back to your browser and refresh http:\/\/rancher-work.network.local a few times, you should see 3 different server names and IPs shown.<\/li><li>Wanna take it further? Change replicas to 100 and see what happens. You can see the individual containers being created under &#8220;Pods&#8221; on the left.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>And that&#8217;s the extent of my tutorial. There&#8217;s a ton more to Rancher and Kubernetes, so it&#8217;s certainly not exhaustive, but hopefully this guide helps you get things started.<\/p>\n\n\n\n<p><strong>Like this article? Have questions? Want another post about something else?<\/strong><\/p>\n\n\n\n<p><strong>Put a comment below and let me know.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Getting into Kubernetes can be a little intimidating. In this guide, I\u2019ll show you how to set up a 6 node cluster using Rancher and some Ubuntu VMs.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[6],"tags":[21,13,22],"class_list":["post-76","post","type-post","status-publish","format-standard","hentry","category-how-to","tag-kubernetes","tag-linux","tag-rancher"],"_links":{"self":[{"href":"https:\/\/stupidtechblog.com\/index.php?rest_route=\/wp\/v2\/posts\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stupidtechblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stupidtechblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stupidtechblog.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stupidtechblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=76"}],"version-history":[{"count":12,"href":"https:\/\/stupidtechblog.com\/index.php?rest_route=\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":119,"href":"https:\/\/stupidtechblog.com\/index.php?rest_route=\/wp\/v2\/posts\/76\/revisions\/119"}],"wp:attachment":[{"href":"https:\/\/stupidtechblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stupidtechblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stupidtechblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}