“This is the first day of my participation in the Gwen Challenge in November. See details of the event: The last Gwen Challenge in 2021”.


Author: SRE operations blog

Blog: www.cnsre.cn/

Article address: www.cnsre.cn/posts/21110…

Related topics: www.cnsre.cn/tags/k3s/


On Double Eleven, major cloud manufacturers have sown seeds and planted leek (to grab users). The Conscience cloud is also the conscience as always. New users can buy three years 2C4G8M lightweight application server through a bao 148. So I sold the wool and got one. However, the various components of K8S are a bit laborious to run, so I decided to deploy a lightweight Kubernetes: K3S for this example

k8s VS k3s

The K3S is a lightweight K8S from Rancher. K3s itself contains the source code of K8S, while the binary package is only 60M but is essentially the same as K8S. However, in order to reduce resource occupation, THERE are some differences between K3S and K8S, mainly as follows:

  • The container runtime uses a lighter containerd than Docker (Docker is not the only container option).
  • Removed Legacy, Alpha, non-default features of k8S.
  • Use SQlite3 as the default storage instead of ETCD.
  • With some other optimizations, k3s is ultimately just a binary file, which is very easy to deploy. ,

Therefore, K3S is suitable for edge computing, IoT and other resource-constrained scenarios. K3s is also very easy to deploy, with one-click deployment scripts available on the website.

The advantages of k3s

  • K3s packages everything you need to install Kubernetes into a 60MB binary and fully implements the Kubernetes API. To reduce the memory required to run Kubernetes, Rancher removed many unnecessary drivers and replaced them with add-ons.
  • K3s is a fully CNCF-certified Kubernetes distribution, which means you can write YAML to operate on the full version of Kubernetes, and they will also work with THE K3S cluster.
  • Since it requires minimal resources to run, it can run clusters on any device with more than 512MB of RAM. In other words, we can have pod run on master and nodes.

The disadvantage of k3s

  • First, the current version of K3S (K3S V0.8.1) can only run a single master, which means that if your master goes down, you can’t manage your cluster, even if the existing cluster continues to run. However, in k3S V0.10, multi-master mode is already an experimental feature, and GA may be available in the next version.
  • Second, in k3S with a single master, the default data store is SQLite, which is nice for small databases, but SQLite becomes a major pain point if it gets hammered. However, the changes that occur in the Kubernetes control plane are more related to frequently updated deployments, scheduling pods, etc., so the database is not too much of a load for a small development/test cluster.

conclusion

K8s and K3S have their own advantages and disadvantages, and their application scenarios are different. If you want to do a large cluster deployment, I recommend using K8s.

If, like me, you just want to develop or test, k3S is a more cost-effective option.

Install k3s

Make sure you are a clean CentOS7 server. Change the source to the domestic YUM source before updating.

#Change the domestic yum source
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#update
yum update -y
Copy the code

Changing the host Name

hostnamectl  set-hostname  k3s-master
Copy the code

After modification, disconnect and reconnect. {{< alert theme=”warning” dir=” LTR “>}} ⚠️ Note that K3s will use containerd as the container environment by default, please select Docker or containerd installation below. {{< /alert >}} {{< tabs install using docker install using Containerd >}} {{< TAB >}}

Install using Docker

#Install the docker - ce
yum remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
yum install -y yum-utils
yum-config-manager --add-repo  https://download.docker.com/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io
#Resolve the kernel check problem and restart the system
grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"
systemctl enable docker
systemctl start docker
#Modify the docker source
cat << EOF > /etc/docker/daemon.json
{
    "registry-mirrors":["https://3laho3y3.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
#Installing the Docker is complete
#Disable firewalld firewall
systemctl stop firewalld
systemctl disable firewalld
#Install k3s
curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - --docker
Copy the code

{{< /tab >}} {{< tab >}}

Install using Containerd

curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -
Copy the code

{{< /tab >}} {{< /tabs >}}

Check after installation

After the installation is complete, run the following command to check the host.

#Running configuration Check
k3s check-config
#View the node status and K3S version[root@k3s-master ~]# kubectl get node NAME STATUS ROLES AGE VERSION vm-16-8-centos Ready control-plane,master 52m V1.21.5 + k3s2#View all POD information[root@k3s-master ~]# kubectl get pods -A NAMESPACE NAME READY STATUS RESTARTS AGE kube-system local-path-provisioner-5ff76fc89d-bbps4 1/1 Running 0 52m kube-system coredns-7448499f4d-42v9x 1/1 Running 0 52m kube-system metrics-server-86cbb8457f-xqlrg 1/1 Running 0 52m kube-system helm-install-traefik-crd-9wk9v 0/1 Completed 0  52m kube-system helm-install-traefik-d8llf 0/1 Completed 3 52m kube-system svclb-traefik-jqxvf 2/2 Running 0 49m kube-system traefik-97b44b794-wv6zv 1/1 Running 0 49mCopy the code

As of this point k3S has been installed.

NFS

Installing the NFS Service

yum -y install nfs-utils
systemctl start nfs && systemctl enable nfs
Copy the code

Creating an NFS Directory

mkdir -p /home/k8s/nfs
Copy the code

Modify the permissions

chmod -R 755 /home/k8s/nfs
Copy the code

Edit export file

cat >>/etc/exports << EOF
/home/k8s/nfs *(rw,no_root_squash,sync)
EOF
Copy the code

Configuration to take effect

exportfs -r
Copy the code

Start the rpcbind and NFS services

systemctl restart rpcbind && systemctl enable rpcbind
systemctl restart nfs && systemctl enable nfs
Copy the code

Now that K3S and NFS have been installed, you can try it out. If you want to do all of the above, you can do it with the one-click setup script below

One-click installation of the K3S script

#! /bin/bashPATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH LANG=en_US.UTF-8 RD="\033[31m" # Error GR="\033[32m" # success YL="\033[33m" # Alarm BL="\033[36m" # log PL='\033[0m' clear echo -e "${YL} # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ${PL}" echo - e "${YL} # ${PL} ${GR} ${PL} script name: A key installation k3s scripts ${YL} # ${PL} "echo - e" ${YL} # ${PL} ${GR} ${PL} : sre ops blog ${YL} # ${PL} "echo - e" ${YL} # ${PL} $${PL} {GR} web site: HTTPS: www.cnsre.cn # ${PL} ${YL} "echo - e" ${YL} # ${PL} ${GR} article addresses ${PL} : https://cnsre.cn/posts/211109907029/ ${YL}#${PL}" echo -e "${YL}##################################################${PL}" Sleep 0.5 set -e echo echo echo echo -e "${RD} "read-r -p "${PL}" read-r -p" [y/n]" input if [[ $input ! If [' command -v docker '] = "y"]]; then exit 1 else echo -e "$GR ${PL}" else echo -e "${GR}install docker${PL}" curl https://download.daocloud.io/docker/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo yum -y install The curl https://download.daocloud.io/docker/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm -fsSL https://get.daocloud.io/docker | bash -s docker --mirror Aliyun fi sudo mkdir -p /etc/docker tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["http://f1361db2.m.daocloud.io"] } EOF grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)" sudo systemctl daemon-reload sudo systemctl restart docker sudo systemctl enable docker if [ `command  -v k3s` ]; Then echo -e "${YL}k3s ${PL}" exit 1 else export K3S_NODE_NAME=${HOSTNAME//_/-} export INSTALL_K3S_EXEC="--docker --kube-apiserver-arg service-node-port-range=1-65000 --no-deploy traefik --write-kubeconfig ~/.kube/config --write-kubeconfig-mode 666" curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh  - fi echo -e "${GR}export K3S_TOKEN=$(cat /var/lib/rancher/k3s/server/node-token)${PL}" echo -e "${GR}export K3S_URL = https://$(IP addr | grep - E - o '[0-9] {1, 3} \. [0-9] {1, 3} \. [0-9] {1, 3} \. [0-9] {1, 3}' | grep - E - v "^ 127 \ | ^ 255 \ | ^ 0 \." | head - n 1) : 6443 ${PL} "echo - e" ${GR} end of the installation, please restart the server ${PL} "read - r - p" sure please press any key exit is y! Please select: [y/n]" input if [[ $input != "y" ]]; then reboot else exit 1 fiCopy the code

Author: SRE operations blog

Blog: www.cnsre.cn/

Article address: www.cnsre.cn/posts/21110…

Related topics: www.cnsre.cn/tags/k3s/