y0ngb1n

Aben Blog

欢迎来到我的技术小黑屋ヾ(◍°∇°◍)ノ゙
github

Kubernetes Introduction to Practice: Initial Experience of Setting Up a K3s Cluster

Practice Environment#

This article will use the Ubuntu system. If you choose the CentOS system, it can also be used as a reference, as the steps and commands are quite similar, following the same principles.

This article was first published at: https://github.com/y0ngb1n/y0ngb1n.github.io

Install Docker#

K3s includes and defaults to containerd, an industry-standard container runtime. This article will use Docker as the container runtime, so you need to install the Docker environment in advance.

# Update software sources
sudo apt update

# Switch to root user
sudo su -

# Install docker
apt install docker.io

# Start docker daemon
systemctl enable --now docker
systemctl status docker

# Verify docker environment
docker version && docker info
docker ps

Subsequently, use the --docker option when starting K3s. Refer to Using Docker as a Container Runtime documentation.

Install K3s#

Lightweight Kubernetes. Production ready, easy to install, half the memory, all in a binary less than 100 MB.

K3s is a highly available, CNCF-certified lightweight Kubernetes distribution designed for IoT and edge computing.

image

k3s packages everything needed to install Kubernetes into a binary file of only 60MB in size and fully implements the Kubernetes API. To reduce the memory required to run Kubernetes, k3s removes many unnecessary drivers and replaces them with add-ons. Because it can run with extremely low resources, it can operate a cluster on any device with more than 512MB of memory.

We will start with the simple K3s to experience half of K8s. Once we have successfully mastered K3s, we will use kubeadm to deploy a highly available Kubernetes cluster. The deployment architecture is almost the same, with only a few core components changing, following the same principles.

Option 1: One-Click Installation (Official Source, Requires Internet Access)#

Since the core component images of K3s need to be pulled from gcr.io (which is not accessible from domestic networks), an environment with internet access is required. This option is suitable for environments where all servers are located abroad, allowing for a simple and straightforward one-click installation.

# One-click default installation
curl -sfL https://get.k3s.io | sh -

Domestic users can use the following method to speed up the installation, which includes Alibaba Cloud mirror sources:

# Specify K3s version
export INSTALL_K3S_VERSION=v1.23.13+k3s1

# Custom startup execution command
export INSTALL_K3S_EXEC="--docker"

# Only install, do not start
export INSTALL_K3S_SKIP_START=true

# https://docs.rancher.cn/docs/k3s/installation/installation-requirements/_index#先决条件
# Design a unique name for each node you add to the cluster
export K3S_NODE_NAME=k3s-node-01

###
# Agent needs to be added, others remain the same
###
# Setting K3S_URL will default to "agent". If K3S_URL is not set, it will default to "server"
export K3S_URL=
# Shared secret for joining the server or agent to the cluster
export K3S_TOKEN=

# Install using Alibaba Cloud mirror source
curl -sfL https://rancher-mirror.oss-cn-beijing.aliyuncs.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -

# Start K3s service
systemctl enable --now k3s

# Check K3s service status
systemctl status k3s

Option 3: Offline Installation#

Prerequisites#

  • Before installing K3s, complete the above Deploy Private Image Repository or Manually Deploy Images to import the images required for installing K3s. This section will demonstrate the method of manually deploying images.
  • Download the K3s v1.23.13+k3s1 binary file from the K3s GitHub Release page. The K3s binary file needs to match the version of the offline images. Place the binary file in /usr/local/bin on each offline node and ensure that this binary file is executable.
  • Download the K3s installation script, place the installation script anywhere on each offline node, and name it install.sh.

When running the K3s script with the INSTALL_K3S_SKIP_DOWNLOAD environment variable, K3s will use the local script and binary.

Manually Deploy Images#

Assuming you have already created nodes in an offline/domestic environment. This method requires you to manually deploy the necessary images to each node, suitable for edge deployment scenarios where deploying an image repository is not possible. Refer to the Official Manual Image Deployment documentation.

  1. Obtain the image tar file for the required installation of K3s v1.23.13+k3s1 from the K3s GitHub Release page.

  2. Place the tar file in the images directory, for example:

    # Create a storage folder for offline images for each node
    sudo mkdir -p /var/lib/rancher/k3s/agent/images/
    
    # $ARCH is the current server's CPU architecture
    sudo cp ./k3s-airgap-images-$ARCH.tar /var/lib/rancher/k3s/agent/images/
    
  3. You need to manually deploy the images to each node, executing the above operations on each node.

Download K3s v1.23.13+k3s1 Binary File#

Download the K3s v1.23.13+k3s1 binary file from the K3s GitHub Release page. The K3s binary file needs to match the version of the offline images. Place the binary file in /usr/local/bin on each offline node and ensure that this binary file is executable.

Install Using Script#

The installation script can be downloaded from any of the following addresses:

Script AddressDescription
https://get.k3s.io/Official address, requires internet access
https://rancher-mirror.oss-cn-beijing.aliyuncs.com/k3s/k3s-install.shAlibaba source, includes Alibaba Cloud mirror
https://raw.githubusercontent.com/k3s-io/k3s/master/install.shGitHub address, same as official source
# Download script
curl -sfL https://get.k3s.io > install.sh

# Switch to root user
sudo su -

# Grant execution permission
chmod +x install.sh

# Install K3s, skip downloading binary files, only install, do not start
INSTALL_K3S_SKIP_DOWNLOAD=true INSTALL_K3S_SKIP_START=true ./install.sh

# Start K3s
systemctl enable --now k3s

Verify K3s#

Check Cluster#

# Check the running status of the cluster
kubectl get nodes -owide
kubectl get all -A -owide
kubectl describe nodes k3s-node-01
kubectl -n kube-system describe deploy coredns

# Using Docker as the runtime, it will use docker to pull images and run containers
docker images
docker ps

A Quick Test#

Start whoami Service#

# Quickly deploy whoami application, specify replicas as 2, default replicas are 1
kubectl create deploy whoami --image=traefik/whoami --replicas=2
kubectl describe deploy whoami
# Monitor pod status, you can visually see the scheduling of pods through scaling
kubectl get pods --watch
# Try scaling the whoami application
kubectl scale deploy whoami --replicas=5

Let’s try to understand the request link in k8s:

request public-ip -> node-port -> svc-port -> pod-port -> container

Expose to Internal Cluster Access via Service#

By default, it is in NodePort mode, which listens on each node

kubectl expose deploy whoami --port=80
kubectl get svc -owide
kubectl describe svc whoami

# Access the service multiple times locally to see round-robin access to the container
# curl http://<external-ip>:<port>
$ curl `kubectl get -o template service/whoami --template='{{.spec.clusterIP}}'`
Hostname: whoami-84d974bbd6-shqsr
IP: 127.0.0.1
IP: 10.42.0.13
RemoteAddr: 10.42.0.1:42158
GET / HTTP/1.1
Host: 10.43.181.167
User-Agent: curl/7.68.0
Accept: */*

Expose to External Network Access via Service#

Specify LoadBalancer mode and specify public IP

# Replace <PUBLIC_IP> with the current node's public IP
kubectl expose deploy whoami --type=LoadBalancer --port=80 --external-ip <PUBLIC_IP>

$ kubectl get svc whoami -owide
NAME     TYPE           CLUSTER-IP     EXTERNAL-IP       PORT(S)        AGE   SELECTOR
whoami   LoadBalancer   10.43.77.116   123.123.123.123   80:32101/TCP   16s   app=whoami

# The cluster can still be accessed normally
curl 10.43.77.116
curl `kubectl get -o template service/whoami --template='{{.spec.clusterIP}}'`

# Access via external browser, firewall configuration of cloud service provider needs to be enabled
# Refresh multiple times with Ctrl + F5 to see the effect
http://123.123.123.123:32101

Quickly Clean Up Experimental Environment#

kubectl delete all --all

Uninstall K3s#

# One-click uninstall server
k3s-uninstall.sh

# One-click uninstall agent
k3s-agent-uninstall.sh
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.