<?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: Andy Gnias</title>
    <description>The latest articles on DEV Community by Andy Gnias (@agniasstratagem).</description>
    <link>https://dev.to/agniasstratagem</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%2F611489%2Fc9c9fa9e-800c-4057-a0ad-79bef2d21c22.jpeg</url>
      <title>DEV Community: Andy Gnias</title>
      <link>https://dev.to/agniasstratagem</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/agniasstratagem"/>
    <language>en</language>
    <item>
      <title>Setting up a k3s Server and Agent Node</title>
      <dc:creator>Andy Gnias</dc:creator>
      <pubDate>Tue, 30 Nov 2021 14:40:21 +0000</pubDate>
      <link>https://dev.to/agniasstratagem/setting-up-a-k3s-server-and-agent-node-2mp3</link>
      <guid>https://dev.to/agniasstratagem/setting-up-a-k3s-server-and-agent-node-2mp3</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;For as easy as k3s is to use, I had a difficult time finding a simple guide for setting up a Server-Agent configuration on two separate servers. In this post, I hope to provide that guide without requiring you to download any extra dependencies or use any other tools besides the scripts provided by k3s.&lt;/p&gt;

&lt;p&gt;I used AWS EC2 instances as my VMs, so instructions will be slightly AWS specific, but should work on any two Linux servers configured to communicate with each other.&lt;/p&gt;

&lt;h1&gt;
  
  
  VM Setup
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Provision Servers
&lt;/h2&gt;

&lt;p&gt;First, we'll need to provision two Linux servers. I used two t2.small images, which is about as low as you'd want to go even when just running a sample app.&lt;/p&gt;

&lt;p&gt;It's also worth assigning a permanent IP address to these servers via Elastic IP if you plan on using them more than once. Instructions for doing so are provided by AWS &lt;a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Log onto each VM
&lt;/h2&gt;

&lt;p&gt;Once you have your VMs up and running, pick one to be the Server and one to be the Agent. Get the hostname of each by running &lt;code&gt;hostname -i&lt;/code&gt; and store both values in the &lt;code&gt;~/.bashrc&lt;/code&gt; file on both the server and the agent like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;k3sserver&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;value of &lt;span class="nb"&gt;hostname&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; on k3s server&amp;gt;
&lt;span class="nb"&gt;export &lt;/span&gt;&lt;span class="nv"&gt;k3sagent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&amp;lt;value of &lt;span class="nb"&gt;hostname&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; on k3s agent&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will also use these values to configure the AWS Security Group.&lt;/p&gt;

&lt;h2&gt;
  
  
  Update AWS Security Group
&lt;/h2&gt;

&lt;p&gt;Security group rules must be added to allow you to SSH into the VMs you created from your localhost, and also to SSH to and from the k3s server and agent. The rules for doing so are provided in the table below.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Port Range&lt;/th&gt;
&lt;th&gt;Source&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;SSH&lt;/td&gt;
&lt;td&gt;22&lt;/td&gt;
&lt;td&gt;IP from &lt;a href="http://checkip.amazonaws.com/"&gt;http://checkip.amazonaws.com/&lt;/a&gt;
&lt;/td&gt;
&lt;td&gt;SSH from localhost to Server and Agent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;All Traffic&lt;/td&gt;
&lt;td&gt;All&lt;/td&gt;
&lt;td&gt;Value of $k3sserver&lt;/td&gt;
&lt;td&gt;SSH from Server to Agent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;All Traffic&lt;/td&gt;
&lt;td&gt;All&lt;/td&gt;
&lt;td&gt;Value of $k3sagent&lt;/td&gt;
&lt;td&gt;SSH from Agent to Server&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;These rules are added to the Security Group's Inbound Rules (instructions for doing so can be found &lt;a href="https://docs.aws.amazon.com/vpc/latest/userguide/VPC_SecurityGroups.html#adding-security-group-rules"&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure SSH keys
&lt;/h2&gt;

&lt;p&gt;On both the k3s Server and Agent, run the following&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd ~/.ssh
ssh-keygen
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Hit enter at each of the prompts. This should result in an &lt;code&gt;id_rsa&lt;/code&gt; private key and an &lt;code&gt;id_rsa.pub&lt;/code&gt; public key being created. Copy the contents of &lt;code&gt;id_rsa.pub&lt;/code&gt; on the server, and paste them on a new line into &lt;code&gt;~/.ssh/authorized_keys&lt;/code&gt; on the agent. Do the same for the agent, copying the contents from &lt;code&gt;id_rsa.pub&lt;/code&gt; on the agent and pasting them on a new line in &lt;code&gt;~/.ssh/authorized_keys&lt;/code&gt; on the server.&lt;/p&gt;

&lt;p&gt;You should now be able to ssh into the Agent from the Server, and vice versa.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure the VMs
&lt;/h2&gt;

&lt;p&gt;On the Server and Agent, run the following script to run and configure Docker.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;yum update &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;amazon-linux-extras &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; docker
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;docker.service
&lt;span class="nb"&gt;sudo &lt;/span&gt;systemctl &lt;span class="nb"&gt;enable &lt;/span&gt;containerd.service
&lt;span class="nb"&gt;sudo &lt;/span&gt;service docker start
&lt;span class="nb"&gt;sudo &lt;/span&gt;usermod &lt;span class="nt"&gt;-a&lt;/span&gt; &lt;span class="nt"&gt;-G&lt;/span&gt; docker ec2-user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running, exit and log back in. Run sudo &lt;code&gt;visudo&lt;/code&gt; and append the following to the secure_path value: &lt;code&gt;:/usr/local/bin&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Defaults  secure_path="/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that your secure path may have more paths before &lt;code&gt;/usr/local/bin&lt;/code&gt;. This is OK. Adding this path allows you to run Docker on each instance without needing to precede every command with &lt;code&gt;sudo&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Server Configuration
&lt;/h3&gt;

&lt;p&gt;On the Server, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-sfL&lt;/span&gt; https://get.k3s.io | sh &lt;span class="nt"&gt;-s&lt;/span&gt; - &lt;span class="nt"&gt;--docker&lt;/span&gt;
&lt;span class="nb"&gt;sudo chmod &lt;/span&gt;755 /etc/rancher/k3s/k3s.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Confirm the Server is ready with the following commands:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;k3s kubectl get node&lt;/code&gt; - Should display 1 running node with roles &lt;code&gt;control-plane,master&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sudo service k3s status&lt;/code&gt; - Should show the service as running&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Agent Configuration
&lt;/h3&gt;

&lt;p&gt;On the Agent, run the following command, setting &lt;code&gt;NODE_TOKEN&lt;/code&gt; to the contents of the file &lt;code&gt;/var/lib/rancher/k3s/server/node-token&lt;/code&gt; on the Server instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;-sfL&lt;/span&gt; https://get.k3s.io | &lt;span class="nv"&gt;K3S_URL&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;https://&lt;span class="nv"&gt;$k3sserver&lt;/span&gt;:6443 &lt;span class="nv"&gt;K3S_TOKEN&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$NODE_TOKEN&lt;/span&gt; sh &lt;span class="nt"&gt;-s&lt;/span&gt; - &lt;span class="nt"&gt;--docker&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Confirm the Agent service is running with the following commands:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo service k3s-agent status
journalctl -f -u k3s-agent.service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then, on the Server VM, run &lt;code&gt;kubectl get nodes&lt;/code&gt;. The Agent node should now appear without a tag. Add a tag with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;kubectl label node &amp;lt;node name from kubectl get nodes&amp;gt; node-role.kubernetes.io/worker&lt;span class="o"&gt;=&lt;/span&gt;worker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Running &lt;code&gt;kubectl get nodes&lt;/code&gt; again should show the Agent node with a tag. At this point, we've established that the Server and Agent are communicating with each other and k3s is ready for use!&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Reading
&lt;/h2&gt;

&lt;p&gt;There's a ton of resources available to test k3s with. If you're looking for a place to start, I recommend Digital Ocean's &lt;a href="https://www.digitalocean.com/community/meetup_kits/getting-started-with-containers-and-kubernetes-a-digitalocean-workshop-kit"&gt;Getting Started with Containers and Kubernetes: A DigitalOcean Workshop Kit&lt;/a&gt; for running a simple Flask app.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>kubernetes</category>
      <category>k3s</category>
    </item>
  </channel>
</rss>
