A Beginner's Guide to Setting Up and Running Pod K3s on Linux

A Beginner's Guide to Setting Up and Running Pod K3s on Linux

In the world of container orchestration, Kubernetes stands tall as the leading platform for automating deployment, scaling, and management of containerized applications. However, Kubernetes can sometimes feel overwhelming, especially for beginners or those looking for a lightweight alternative. Enter K3s – a lightweight Kubernetes distribution designed for resource-constrained environments and ease of use. In this guide, we'll walk through the steps to set up and run Pod K3s on Linux, from installation to deploying your first application.

Important Notes

K3s is not supported on macOS, we have to install Virtual Machine for linux system

Install K3s

First things first, let's install K3s. Open your terminal and run the following command:

curl -sfL https://get.k3s.io | sh -s - --write-kubeconfig-mode 644 

This command will download and install K3s on your system, and set the appropriate permissions for the kubeconfig file.

Set kubeconfig

After the installation, you'll need to set up your kubeconfig file to communicate with the K3s cluster. This can be achieved by exporting the KUBECONFIG environment variable:

export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

Get Nodes

Now that K3s is installed and configured, let's verify that everything is up and running. Run the following command to get the list of nodes in your K3s cluster:

kubectl get nodes

Create a Basic Node.js App and Dockerfile

Next, let's create a simple Node.js application and a Dockerfile to containerize it. You can use your favorite code editor to create the following files:

app.js

const http = require('http');

const hostname = '0.0.0.0';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Dockerfile

FROM node:alpine
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]

Build the Docker image:

docker build -t my-node-app .

Send Docker Image to K3s Docker

To deploy our Docker image to the K3s cluster, we need to import it using the k3s containerd (ctr) command:

docker save my-node-app:latest | sudo k3s ctr images import -

Create YAML File for Deployment and Service

Now, let's create a YAML file for deploying our application and exposing it via a LoadBalancer service. Create a file named app-deployment.yaml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-node-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-node-app
  template:
    metadata:
      labels:
        app: my-node-app
    spec:
      containers:
      - name: my-node-app
        image: my-node-app:latest
        ports:
        - containerPort: 3000

---
apiVersion: v1
kind: Service
metadata:
  name: my-node-app-service
spec:
  selector:
    app: my-node-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
  type: LoadBalancer

Apply the YAML file to create the deployment and service:

kubectl apply -f app-deployment.yaml

Guide to Check Running Pod and Service, Check Logs, and Describe

Now that our application is deployed, let's check its status, logs, and describe its details.

Check running all:

kubectl get all

Check running pods:

kubectl get pods

Check service:

kubectl get svc

View logs:

kubectl logs <pod-name>

Describe pod:

kubectl describe pod <pod-name>

When to use Kubernetes K3

Kubernetes K3s, often referred to simply as K3s, is a lightweight Kubernetes distribution designed for resource-constrained environments and simplified operations. Here are some scenarios where K3s shines:

  • Edge Computing: K3s is well-suited for edge computing environments where resources are limited, and there's a need for lightweight Kubernetes clusters. Its small footprint and simplified installation make it ideal for deploying and managing applications at the edge.

  • Development and Testing: For developers and teams looking to quickly spin up Kubernetes clusters for development and testing purposes, K3s provides a lightweight and fast solution. It allows developers to emulate Kubernetes environments easily without the overhead of a full-scale cluster.

  • IoT and Embedded Systems: In IoT (Internet of Things) and embedded systems applications, where space and processing power are at a premium, K3s offers a Kubernetes solution tailored to these constraints. It enables container orchestration in IoT devices and embedded systems without overwhelming them.

  • Single-Node Clusters: K3s is particularly useful for setting up single-node Kubernetes clusters for small-scale projects or personal use. Whether you're experimenting with Kubernetes concepts or running small applications, K3s simplifies the process of setting up and managing a Kubernetes environment on a single node.

Advantages of Kubernetes K3 in Comparison to Its Alternatives

Kubernetes K3s offers several advantages over its alternatives, especially in scenarios where simplicity, efficiency, and resource optimization are crucial:

  • Lightweight Footprint: One of the primary advantages of K3s is its lightweight footprint. It is optimized to use minimal system resources, making it suitable for deployment on edge devices, IoT platforms, and other resource-constrained environments. This reduced footprint not only conserves resources but also simplifies installation and maintenance.

  • Simplified Installation and Operation: K3s streamlines the installation and operation of Kubernetes clusters by bundling essential components into a single lightweight binary. With a single command installation and minimal configuration, K3s makes it easy for users to get started with Kubernetes without the complexities often associated with traditional distributions.

  • Enhanced Security: K3s incorporates security features such as automatic TLS certificate generation, which simplifies the process of securing communication within the cluster. It also minimizes the attack surface by reducing the number of components and dependencies compared to full-fledged Kubernetes distributions, thereby enhancing security posture.

  • Efficient Resource Utilization: K3s optimizes resource utilization by employing lightweight components and reducing overhead. This efficiency translates to improved performance and responsiveness, especially in environments with limited resources or high demand for scalability.

  • Portability and Flexibility: Despite its lightweight nature, K3s maintains compatibility with standard Kubernetes APIs and tooling, ensuring portability and interoperability with existing Kubernetes ecosystems. This compatibility allows users to leverage a wide range of Kubernetes applications, tools, and resources while benefiting from the simplicity and efficiency of K3s.

In summary, Kubernetes K3s offers a compelling solution for scenarios requiring lightweight Kubernetes clusters, simplified operations, efficient resource utilization, and enhanced security, making it a preferred choice for edge computing, development, testing, IoT, and other resource-constrained environments.

alt text

Install worker node with master node

  • get the master node token
sudo cat /var/lib/rancher/k3s/server/node-token
  • run the following command to install the worker node
curl -sfL https://get.k3s.io | K3S_URL=https://master_ip:6443 K3S_TOKEN=mynodetoken K3S_NODE_NAME="worker" sh -

Conclusion

Congratulations! You've successfully set up and run Pod K3s on Linux. You've learned how to install K3s, deploy a simple Node.js application, and interact with the cluster using kubectl commands. K3s provides a lightweight yet powerful Kubernetes experience, making it ideal for development, testing, and production use cases. Experiment further with K3s and explore its capabilities to unlock the full potential of container orchestration. Happy coding!

PARTNER WITH US TO CREATE A COMPELLING NARRATIVE
FOR YOUR BRAND!

Let's bring your ideas to life, start collaborating with our creative agency and turn your vision into reality.