Setup Kubernetes cluster with K3sup: Difference between revisions

From UNamur InfoSec
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
= Setup Kubernetes cluster with k3sup =


==Pre Requirements for Raspberry PI ==
=== requirement for Raspbian OS ===
=== requirement for Raspberry PI===


==Requirements ==
=== k3sup ===
=== Pre existing requirement for Raspbian OS ===
=== Pre existing requirement for Raspberry PI===


==Installation==
k3sup is an application that simplifies k3s cluster configuration. We can use the “install” command to configure a master node and the “join” command to set up workers node one by one.


===K3sup ===
=== Requirements ===


=== Remove k3s from master and worker nodes. ===
<ol style="list-style-type: decimal;">
* Ansible playbook for removing k3s from hosts
<li><p>We need SSH access with a public-key authentication method from the server running k3sup to the nodes that we want to install k3s to set up a cluster. To use Ansible to copy authorized_keys to nodes [link].</p></li>
<pre>#clean up k3s
<li><p>The user on the nodes can execute the “sudo” command without typing a password.</p>
---
<pre># to configure NOPASSWD sudo
- name: clean up k3s installation
sudo visudo</pre>
  hosts: red
<p>And add the following</p>
  remote_user: pi
<pre>&lt;username&gt; ALL=(ALL) NOPASSWD: ALL</pre></li>
  tasks:
<li></li></ol>
  - name: stop service
 
    command: /usr/local/bin/k3s-killall.sh
=== Installation ===
    ignore_unreachable: yes
 
  - name: uninstall master node
<pre>curl -sLS https://get.k3sup.dev | sh
    command: /usr/local/bin/k3s-uninstall.sh
sudo install k3sup /usr/local/bin/
    ignore_unreachable: yes
 
- name: clean up k3s installation
#k3sup --help</pre>
  hosts: [yellow black green]
=== Create a master node ===
  remote_user: pi
 
  tasks:
<pre># here we can also use --host if we want to provide hostname instead of IP address
  - name: stop service
k3sup install --ip &lt;ip_of_master_node&gt; --user &lt;user_for_ssh&gt;</pre>
    command: /usr/local/bin/k3s-killall.sh
=== Create a worker node ===
    ignore_unreachable: yes
 
  - name: Uninstall worker node
<pre># here we can replace ip with host, ex: --host, --server-host respectively
    command: /usr/local/bin/k3s-agent-uninstall.sh
k3sup join --ip &lt;worker_node_ip&gt; --server-ip &lt;master_node_ip&gt; --user &lt;worker_node_ssh_user&gt;</pre>
    ignore_unreachable: yes</pre>
=== Cleanup cluster ===
 
<ol style="list-style-type: decimal;">
<li><p>For worker nodes</p>
<pre># ssh into nodes and run this command. the command is located in /usr/local/bin
k3s-killall.sh
k3s-agent-uninstall.sh
</pre>
<pre># incase k3s-agent-uninstall fail to remove this directory
sudo rm -rf /var/lib/kubelet</pre></li>
<li><p>For master node</p>
<pre># ssh into nodes and run this command. the command is located in /usr/local/bin
k3s-killall.sh
k3s-uninstall.sh</pre></li></ol>
 
Node: to use Ansible [link]
 
=== Use kubectl to access the cluster ===
 
=== Install kubectl ===
 
<pre>curl -LO &quot;https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl&quot;
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl</pre>
=== Load kubeconfig file ===
 
<pre>export KUBECONFIG=/home/ubuntu/kubeconfig
kubectl config set-context default</pre>
=== Save kubeconfig to user directory ===
 
<pre>kubectl config view --raw &gt; ~/.kube/config</pre>
=== Check cluster ===
 
<pre>kubectl get node -o wide</pre>
=== Shell script ===
 
<pre>#!/bin/sh
k3sup install --host red --user pi
k3sup join --host yellow --server-host red --user pi
k3sup join --host black --server-host red --user pi
k3sup join --host green --server-host red --user pi
 
export KUBECONFIG=/home/ubuntu/kubeconfig
kubectl config set-context default
kubectl config view --raw &gt; ~/.kube/config</pre>
== Helm installation ==

Revision as of 18:55, 17 September 2021

Setup Kubernetes cluster with k3sup

Pre Requirements for Raspberry PI

requirement for Raspbian OS

requirement for Raspberry PI

k3sup

k3sup is an application that simplifies k3s cluster configuration. We can use the “install” command to configure a master node and the “join” command to set up workers node one by one.

Requirements

  1. We need SSH access with a public-key authentication method from the server running k3sup to the nodes that we want to install k3s to set up a cluster. To use Ansible to copy authorized_keys to nodes [link].

  2. The user on the nodes can execute the “sudo” command without typing a password.

    # to configure NOPASSWD sudo 
    sudo visudo

    And add the following

    <username> ALL=(ALL) NOPASSWD: ALL

Installation

curl -sLS https://get.k3sup.dev | sh
sudo install k3sup /usr/local/bin/

#k3sup --help

Create a master node

# here we can also use --host if we want to provide hostname instead of IP address
k3sup install --ip <ip_of_master_node> --user <user_for_ssh>

Create a worker node

# here we can replace ip with host, ex: --host, --server-host respectively
k3sup join --ip <worker_node_ip> --server-ip <master_node_ip> --user <worker_node_ssh_user>

Cleanup cluster

  1. For worker nodes

    # ssh into nodes and run this command. the command is located in /usr/local/bin
    k3s-killall.sh
    k3s-agent-uninstall.sh
    
    # incase k3s-agent-uninstall fail to remove this directory
    sudo rm -rf /var/lib/kubelet
  2. For master node

    # ssh into nodes and run this command. the command is located in /usr/local/bin
    k3s-killall.sh
    k3s-uninstall.sh

Node: to use Ansible [link]

Use kubectl to access the cluster

Install kubectl

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

Load kubeconfig file

export KUBECONFIG=/home/ubuntu/kubeconfig
kubectl config set-context default

Save kubeconfig to user directory

kubectl config view --raw > ~/.kube/config

Check cluster

kubectl get node -o wide

Shell script

#!/bin/sh
k3sup install --host red --user pi
k3sup join --host yellow --server-host red --user pi
k3sup join --host black --server-host red --user pi
k3sup join --host green --server-host red --user pi

export KUBECONFIG=/home/ubuntu/kubeconfig
kubectl config set-context default
kubectl config view --raw > ~/.kube/config

Helm installation