{"id":40590,"date":"2023-09-29T03:49:23","date_gmt":"2023-09-29T03:49:23","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=40590"},"modified":"2025-01-23T12:45:12","modified_gmt":"2025-01-23T12:45:12","slug":"a-basic-k3s-tutorial-for-kubernetes","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/a-basic-k3s-tutorial-for-kubernetes\/","title":{"rendered":"A Basic k3s Tutorial for Kubernetes"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is k3s?<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-devopsschool-com wp-block-embed-devopsschool-com\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"3OWOSKaN1i\"><a href=\"https:\/\/www.devopsschool.com\/blog\/what-is-k3s\/\">What is k3s?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;What is k3s?&#8221; &#8212; DevOpsSchool.com\" src=\"https:\/\/www.devopsschool.com\/blog\/what-is-k3s\/embed\/#?secret=C47EalVEnu#?secret=3OWOSKaN1i\" data-secret=\"3OWOSKaN1i\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>K3s is a lightweight and easy-to-install Kubernetes distribution designed for use in resource-constrained environments, edge computing, and development scenarios. It&#8217;s a simplified version of Kubernetes that retains most of its functionality. Here&#8217;s a step-by-step guide on how to work with K3s:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Set Up a Linux Environment<\/h2>\n\n\n\n<p>K3s is primarily intended for use on Linux-based systems. Ensure you have a Linux environment available for installation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Install K3s<\/h2>\n\n\n\n<p>You can install K3s on your Linux system using a convenient installation script. Open a terminal and run the following command:<\/p>\n\n\n\n<p>$ curl -sfL https:\/\/get.k3s.io | sh &#8211;<\/p>\n\n\n\n<p>This script will download and install K3s, including the Kubernetes control plane components and the container runtime.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Verify Installation<\/h2>\n\n\n\n<p>After installation, K3s should be up and running. You can check the status of K3s and ensure that it&#8217;s working correctly by running:<\/p>\n\n\n\n<p>$ sudo k3s kubectl get nodes<\/p>\n\n\n\n<p>This command should display the status of your K3s node(s).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Interact with K3s<\/h2>\n\n\n\n<p>You can interact with K3s using the kubectl command, just like you would with a standard Kubernetes cluster. By default, K3s creates a kubeconfig file at \/etc\/rancher\/k3s\/k3s.yaml, which allows you to use kubectl to communicate with the cluster. To use this configuration, copy it to your home directory:<\/p>\n\n\n\n<p>$ sudo cp \/etc\/rancher\/k3s\/k3s.yaml ~\/.kube\/config<\/p>\n\n\n\n<p>Now, you can use kubectl to interact with your K3s cluster:<\/p>\n\n\n\n<p>$ kubectl get nodes<br>$ kubectl get pods &#8211;all-namespaces<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Deploy Applications<\/h2>\n\n\n\n<p>You can deploy applications to your K3s cluster using Kubernetes manifests (YAML files) just like in a standard Kubernetes cluster. For example, to deploy an Nginx web server:<\/p>\n\n\n\n<p>$ kubectl apply -f https:\/\/raw.githubusercontent.com\/kubernetes\/website\/master\/content\/en\/examples\/application\/nginx-app.yaml<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 6: Manage K3s Services<\/h2>\n\n\n\n<p>K3s automatically manages essential services like the container runtime and networking. You can use systemctl to manage the K3s service:<\/p>\n\n\n\n<p>To start K3s: $ sudo systemctl start k3s<br>To stop K3s: $ sudo systemctl stop k3s<br>To check the $ status of K3s: sudo systemctl status k3s<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 7: Explore K3s Features<\/h2>\n\n\n\n<p>K3s retains most of the features of Kubernetes, including deployments, services, pods, and more. Explore and practice these features to get familiar with working in a K3s cluster.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 8: Uninstall K3s<\/h2>\n\n\n\n<p>If you need to uninstall K3s from your system, you can run:<\/p>\n\n\n\n<p>$ sudo \/usr\/local\/bin\/k3s-uninstall.sh<\/p>\n\n\n\n<p>This will remove K3s and any associated components from your system.<\/p>\n\n\n\n<p>K3s is an excellent choice for lightweight Kubernetes testing and development environments. It simplifies the installation process while still providing the core capabilities of Kubernetes.<\/p>\n\n\n\n<p>How to join new Nodes in K3s?<br>Join additional nodes to your K3s cluster. To do this, you will need to provide the new nodes with the IP address of your server node. You can do this by running the following command on your server node:<\/p>\n\n\n\n<p>$ cat \/var\/lib\/rancher\/k3s\/server\/node-token<br>Once you have the node token, you can run the following command on the new nodes to join them to the cluster:<\/p>\n\n\n\n<p>$ k3s agent &#8211;server https:\/\/:6443 &#8211;token<\/p>\n\n\n\n<p>Verify that your K3s cluster is running. To do this, you can run the following command on your server node:<\/p>\n\n\n\n<p>$ kubectl get nodes<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">K3s configuration file<\/h2>\n\n\n\n<p>Use a K3s configuration file. A K3s configuration file allows you to customize the behavior of your K3s cluster. You can create a K3s configuration file using the k3s config command<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use a K3s dashboard<\/h2>\n\n\n\n<p>A K3s dashboard provides a graphical user interface for managing your K3s cluster. You can install a K3s dashboard using the k3s dashboard command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">k3s is running all kubernetes component in container or node?<\/h2>\n\n\n\n<p>K3s can run all Kubernetes components in a container or on the node. By default, K3s will run all Kubernetes components in a container. This is the most efficient way to run K3s, as it reduces the resource overhead of running Kubernetes.<\/p>\n\n\n\n<p>However, you can also choose to run some or all of the Kubernetes components on the node. This may be necessary if you need to use a specific version of a Kubernetes component, or if you need to have more control over the configuration of a Kubernetes component.<\/p>\n\n\n\n<p>To run a Kubernetes component on the node, you need to set the <code>--no-containerized<\/code> flag when you start the K3s server or agent.<\/p>\n\n\n\n<p>For example, to run the K3s API server on the node, you would run the following command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">k3s server --no-containerized\n<\/code><\/span><\/pre>\n\n\n<p>To run the K3s controller manager on the node, you would run the following command:<\/p>\n\n\n\n<p>k3s agent &#8211;no-containerized &#8211;server https:\/\/&lt;server-ip&gt;:6443 &#8211;token &lt;node-token&gt; &#8211;no-containerized-components controller-manager<\/p>\n\n\n\n<p>You can also choose to run all of the Kubernetes components on the node by setting the <code>--no-containerized-all<\/code> flag when you start the K3s server or agent.<\/p>\n\n\n\n<p>For example, to run all of the Kubernetes components on the node, you would run the following command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">k3s server --no-containerized-all<\/code><\/span><\/pre>","protected":false},"excerpt":{"rendered":"<p>What is k3s? K3s is a lightweight and easy-to-install Kubernetes distribution designed for use in resource-constrained environments, edge computing, and development scenarios. It&#8217;s a simplified version of Kubernetes that retains&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[4859],"tags":[],"class_list":["post-40590","post","type-post","status-publish","format-standard","hentry","category-kubernetes"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/40590","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=40590"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/40590\/revisions"}],"predecessor-version":[{"id":40592,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/40590\/revisions\/40592"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=40590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=40590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=40590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}