Just 5分钟!使用k3s部署轻量Kubernetes集群快速教程

大小仅有40MB的k3s为想要节省开销进行开发和测试的企业提供了一个很好的选择。本文将用一种极为简洁的方式,教你在5分钟之内使用k3s部署轻量Kubernetes集群。

本文来自:Rancher Labs
 
Kubernetes已经改变了如何大规模部署和管理容器化工作负载。现在开发人员面临的挑战主要在于设置过程的复杂性和资源需求量巨大。如果你深受内存不足的困扰,想要部署轻量级Kubernetes集群来减少内存占用,那么你一定要考虑由Rancher Labs发布的轻量级Kubernetes发行版——k3s。它把安装Kubernetes所需的一切文件都打包进一个40MB大小的二进制文件中,仅需512MB的RAM即可运行。非常适用于资源有限的环境,如边缘计算场景、IoT等。

在实际场景中,为了获得开发和测试的动力,节省开销,用户希望能够以最少的资源利用率和较低的硬件规格来部署Kubernetes。而k3s正好满足了这一需求,它能够在任何512MB RAM以上的设备上运行集群,如IoT设备或ARM驱动的设备。

既然k3s仅需少量资源即可运行,那么这意味着一些Kubernetes的特性被移除了:

  • 旧的、非默认的、alpha功能
  • 大部分in-tree插件(云提供商和存储插件),将其用附加组件进行替换
  • 用sqlite来代替etcd作为默认存储机制
     

    5分钟之内使用k3s部署轻量K8s集群

在本文中,我将使用运行在Debian 10上的3个server,每个server有1GB的RAM和1vcpu。其中一个server作为master,其他两个作为worker节点。


$ openstack server list
+--------------------------------------+-------------------+---------+-----------------------------------+-----------+-----------+
| ID                                   | Name              | Status  | Networks                          | Image     | Flavor    |
+--------------------------------------+-------------------+---------+-----------------------------------+-----------+-----------+
| 4df6a6dc-26e8-4ae0-8b6e-2f97daec0ef3 | k3s-master        | ACTIVE  | private=10.10.1.159               | Debian-10 | m1.tiny   |
| 5ca13239-b745-4f62-ab11-0a27949c9b35 | k3s-node02        | ACTIVE  | private=10.10.1.142               | Debian-10 | m1.tiny   |
| a54997f2-4d94-4718-86ab-73609b328761 | k3s-node01        | ACTIVE  | private=10.10.1.126               | Debian-10 | m1.tiny   |
+--------------------------------------+-------------------+---------+-----------------------------------+-----------+-----------+

我将在每个服务器的/ etc / hosts文件中为服务器添加A record。

sudo tee -a /etc/hosts<<EOF
10.10.1.159 k3s-master
10.10.1.126 k3s-node01
10.10.1.142 k3s-node02
EOF

在Master节点上安装k3s

运行k3s的方式有很多,最快的方式是通过提供的bash脚本进行安装,同时该脚本提供了一个便捷的方式来安装到systemd或openrc。

curl -sfL https://get.k3s.io | sh -

安装输出:

[INFO]  Finding latest release
[INFO]  Using v0.8.1 as release
[INFO]  Downloading hash https://github.com/rancher/k3s/releases/download/v0.8.1/sha256sum-amd64.txt
[INFO]  Downloading binary https://github.com/rancher/k3s/releases/download/v0.8.1/k3s
[INFO]  Verifying binary download
[INFO]  Installing k3s to /usr/local/bin/k3s
[INFO]  Creating /usr/local/bin/kubectl symlink to k3s
[INFO]  Creating /usr/local/bin/crictl symlink to k3s
[INFO]  Creating /usr/local/bin/ctr symlink to k3s
[INFO]  Creating killall script /usr/local/bin/k3s-killall.sh
[INFO]  Creating uninstall script /usr/local/bin/k3s-uninstall.sh
[INFO]  env: Creating environment file /etc/systemd/system/k3s.service.env
[INFO]  systemd: Creating service file /etc/systemd/system/k3s.service
[INFO]  systemd: Enabling k3s unit
Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.
[INFO]  systemd: Starting k3s

安装完成之后,服务会自动启动。

$ systemctl status k3s
● k3s.service - Lightweight Kubernetes
   Loaded: loaded (/etc/systemd/system/k3s.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-09-17 19:20:00 UTC; 2min 24s ago
     Docs: https://k3s.io
  Process: 833 ExecStartPre=/sbin/modprobe br_netfilter (code=exited, status=0/SUCCESS)
  Process: 836 ExecStartPre=/sbin/modprobe overlay (code=exited, status=0/SUCCESS)
 Main PID: 837 (k3s-server)
    Tasks: 98
   Memory: 571.1M
   CGroup: /system.slice/k3s.service
           ├─ 837 /usr/local/bin/k3s server KillMode=process
           ├─ 851 containerd -c /var/lib/rancher/k3s/agent/etc/containerd/config.toml -a /run/k3s/containerd/containerd.sock --state /run/k3s/conta
           ├─1110 containerd-shim -namespace k8s.io -workdir /var/lib/rancher/k3s/agent/containerd/io.containerd.runtime.v1.linux/k8s.io/f6eeb59978
           ├─1127 /pause
           ├─1207 containerd-shim -namespace k8s.io -workdir /var/lib/rancher/k3s/agent/containerd/io.containerd.runtime.v1.linux/k8s.io/0baf0ca181
           ├─1225 /coredns -conf /etc/coredns/Corefile
           ├─1576 containerd-shim -namespace k8s.io -workdir /var/lib/rancher/k3s/agent/containerd/io.containerd.runtime.v1.linux/k8s.io/dcce4b7e17
           ├─1594 /pause
           ├─1599 containerd-shim -namespace k8s.io -workdir /var/lib/rancher/k3s/agent/containerd/io.containerd.runtime.v1.linux/k8s.io/50816ffba8
           ├─1617 /pause
           ├─1824 containerd-shim -namespace k8s.io -workdir /var/lib/rancher/k3s/agent/containerd/io.containerd.runtime.v1.linux/k8s.io/d0ff393609
           ├─1842 /bin/sh /usr/bin/entry
           ├─1882 containerd-shim -namespace k8s.io -workdir /var/lib/rancher/k3s/agent/containerd/io.containerd.runtime.v1.linux/k8s.io/046779175f
           ├─1899 /bin/sh /usr/bin/entry
           ├─1904 containerd-shim -namespace k8s.io -workdir /var/lib/rancher/k3s/agent/containerd/io.containerd.runtime.v1.linux/k8s.io/93f0fe2361
           └─1921 /traefik --configfile=/config/traefik.toml

Sep 17 19:20:34 deb10 k3s[837]: E0917 19:20:34.714229     837 daemon_controller.go:302] kube-system/svclb-traefik failed with : error storing statu
Sep 17 19:20:34 deb10 k3s[837]: E0917 19:20:34.719452     837 daemon_controller.go:302] kube-system/svclb-traefik failed with : error storing statu
Sep 17 19:20:34 deb10 k3s[837]: I0917 19:20:34.726816     837 reconciler.go:207] operationExecutor.VerifyControllerAttachedVolume started for volum
Sep 17 19:20:34 deb10 k3s[837]: I0917 19:20:34.726836     837 reconciler.go:207] operationExecutor.VerifyControllerAttachedVolume started for volum
Sep 17 19:20:34 deb10 k3s[837]: I0917 19:20:34.726857     837 reconciler.go:207] operationExecutor.VerifyControllerAttachedVolume started for volum
Sep 17 19:20:34 deb10 k3s[837]: I0917 19:20:34.726869     837 reconciler.go:207] operationExecutor.VerifyControllerAttachedVolume started for volum
Sep 17 19:20:35 deb10 k3s[837]: I0917 19:20:35.529102     837 reconciler.go:181] operationExecutor.UnmountVolume started for volume "helm-traefik-t
Sep 17 19:20:35 deb10 k3s[837]: I0917 19:20:35.542858     837 operation_generator.go:799] UnmountVolume.TearDown succeeded for volume "kubernetes.i
Sep 17 19:20:35 deb10 k3s[837]: I0917 19:20:35.629277     837 reconciler.go:285] Volume detached for volume "helm-traefik-token-kjwrl" (UniqueName:
Sep 17 19:20:36 deb10 k3s[837]: W0917 19:20:36.355273     837 pod_container_deletor.go:75] Container "2f0c4a787b13c029d65aa865c1b473f5a7497cb6f9b92

将kubeconfig文件写入/etc/rancher/k3s/k3s.yaml

$ cat /etc/rancher/k3s/k3s.yaml
cat: /etc/rancher/k3s/k3s.yaml: Permission denied
[email protected]:~$ sudo cat /etc/rancher/k3s/k3s.yaml
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJWakNCL3FBREFnRUNBZ0VBTUFvR0NDcUdTTTQ5QkFNQ01DTXhJVEFmQmdOVkJBTU1HR3N6Y3kxelpYSjIKWlhJdFkyRkFNVFUyT0RjME56azVOakFlRncweE9UQTVNVGN4T1RFNU5UWmFGdzB5T1RBNU1UUXhPVEU1TlRaYQpNQ014SVRBZkJnTlZCQU1NR0dzemN5MXpaWEoyWlhJdFkyRkFNVFUyT0RjME56azVOakJaTUJNR0J5cUdTTTQ5CkFnRUdDQ3FHU000OUF3RUhBMElBQkM5aTMyUTdkVnhJaTFCVFNEOTRqYzJaZy9ESHFGc051b0Q4eWhSbjZsUlIKQWp5Q0p3UEZYQ3Y4QUdSMmFaK1lSempTYUJvM2M1LzMwQnZwKzY3OFNYeWpJekFoTUE0R0ExVWREd0VCL3dRRQpBd0lDcERBUEJnTlZIUk1CQWY4RUJUQURBUUgvTUFvR0NDcUdTTTQ5QkFNQ0EwY0FNRVFDSUJwTXdOejAyZzUwCkExdEloU0Y1MFJqSVprVVVuNk8rODdLV25obWRUYkh5QWlBQnJqcDFxWU1HcWE0RmJ2Ym9rTm1kM3VOelVvQm8KeGxqTGlnWnZCN3ZEVGc9PQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
    server: https://localhost:6443
  name: default
contexts:
- context:
    cluster: default
    user: default
  name: default
current-context: default
kind: Config
preferences: {}
users:
- name: default
  user:
    password: 2d99cae31c075743be704bb717ceaae8
    username: admin

其他已经安装的有:

  • kubectl
  • crictl
  • k3s-killall.sh
  • k3s-uninstall.sh
     

    在Worker节点上安装k3s

要在Woker节点上安装k3s,我们应该将K3S_URL以及K3S_TOKEN或K3S_CLUSTER_SECRET环境变量一起传递。

K3S_TOKEN在第一个节点上的/ var / lib / rancher / k3s / server / node-token中创建。

$ sudo cat /var/lib/rancher/k3s/server/node-token
K1042e2f8e353b9409472c1e0cca8457abe184dc7be3f0805109e92c50c193ceb42::node:c83acbf89a7de7026d6f6928dc270028

所以为了在worker节点上安装Kubernetes,我将运行:

k3s_url="https://k3s-master:6443"
k3s_token="K1042e2f8e353b9409472c1e0cca8457abe184dc7be3f0805109e92c50c193ceb42::node:c83acbf89a7de7026d6f6928dc270028"
curl -sfL https://get.k3s.io | K3S_URL=${k3s_url} K3S_TOKEN=${k3s_token} sh -

安装输出:

[INFO]  Finding latest release
[INFO]  Using v0.8.1 as release
[INFO]  Downloading hash https://github.com/rancher/k3s/releases/download/v0.8.1/sha256sum-amd64.txt
[INFO]  Downloading binary https://github.com/rancher/k3s/releases/download/v0.8.1/k3s
[INFO]  Verifying binary download
[INFO]  Installing k3s to /usr/local/bin/k3s
[INFO]  Creating /usr/local/bin/kubectl symlink to k3s
[INFO]  Creating /usr/local/bin/crictl symlink to k3s
[INFO]  Creating /usr/local/bin/ctr symlink to k3s
[INFO]  Creating killall script /usr/local/bin/k3s-killall.sh
[INFO]  Creating uninstall script /usr/local/bin/k3s-agent-uninstall.sh
[INFO]  env: Creating environment file /etc/systemd/system/k3s-agent.service.env
[INFO]  systemd: Creating service file /etc/systemd/system/k3s-agent.service
[INFO]  systemd: Enabling k3s-agent unit
Created symlink /etc/systemd/system/multi-user.target.wants/k3s-agent.service → /etc/systemd/system/k3s-agent.service.
[INFO]  systemd: Starting k3s-agent

登录到其中一个master节点并检查集群状态:

$ sudo kubectl config get-clusters
NAME
default

$ sudo kubectl cluster-info
Kubernetes master is running at https://localhost:6443
CoreDNS is running at https://localhost:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

$ sudo kubectl get  nodes
NAME         STATUS   ROLES    AGE     VERSION
k3s-master   Ready    master   14m     v1.14.6-k3s.1
k3s-node01   Ready    worker   3m11s   v1.14.6-k3s.1
k3s-node02   Ready    worker   3m58s   v1.14.6-k3s.1

$ sudo kubectl get namespaces
NAME              STATUS   AGE
default           Active   16m
kube-node-lease   Active   16m
kube-public       Active   16m
kube-system       Active   16m

$ sudo  kubectl get endpoints -n kube-system
NAME       ENDPOINTS                                  AGE
kube-dns   10.42.0.2:53,10.42.0.2:53,10.42.0.2:9153   14m
traefik    10.42.0.5:80,10.42.0.5:443                 14m

$ sudo kubectl get pods -n kube-system
NAME                         READY   STATUS      RESTARTS   AGE
coredns-b7464766c-q9frk      1/1     Running     0          15m
helm-install-traefik-8dhpk   0/1     Completed   0          15m
svclb-traefik-9c2j8          2/2     Running     0          4m49s
svclb-traefik-bf9zd          2/2     Running     0          4m2s
svclb-traefik-v2fpx          2/2     Running     0          14m
traefik-5c79b789c5-k589d     1/1     Running     0          14m

使用crictl命令来查看正在运行的容器

# Master
$ sudo crictl ps
CONTAINER ID        IMAGE               CREATED             STATE               NAME                ATTEMPT             POD ID
acfafb50852d3       18471c10e6e4b       16 minutes ago      Running             traefik             0                   bf8534452389f
fee5ac7e88f2e       4a065d8dfa588       16 minutes ago      Running             lb-port-443         0                   e7068ff7ab2f2
bbab5b07e5efb       4a065d8dfa588       16 minutes ago      Running             lb-port-80          0                   e7068ff7ab2f2
65c5d1333ea04       2ee68ed074c6e       16 minutes ago      Running             coredns             0                   435c51f4716fc

# Workers
$ sudo crictl ps
CONTAINER ID        IMAGE               CREATED             STATE               NAME                ATTEMPT             POD ID
7ad5c83d6466f       4a065d8dfa588       6 minutes ago       Running             lb-port-443         0                   bf8d9fe57c3f3
c1380eabc0b33       4a065d8dfa588       6 minutes ago       Running             lb-port-80          0                   bf8d9fe57c3f3

大功告成啦!如果你需要更高级的配置,请参阅k3s文档:

https://rancher.com/docs/k3s/latest/en/

原文地址:https://blog.51cto.com/12462495/2447002

时间: 2024-10-10 13:47:17

Just 5分钟!使用k3s部署轻量Kubernetes集群快速教程的相关文章

通过Rancher部署并扩容Kubernetes集群基础篇二

接上一篇通过Rancher部署并扩容Kubernetes集群基础篇一 7. 使用ConfigMap配置redis https://github.com/kubernetes/kubernetes.github.io/blob/master/docs/user-guide/configmap/redis/redis-config redis-config maxmemory 2mb     maxmemory-policy allkeys-lru # kubectl create configma

在 Linux 部署多节点 Kubernetes 集群与 KubeSphere 容器平台

KubeSphere 是在 Kubernetes 之上构建的以应用为中心的企业级容器平台,所有供为用户提供简单易用的操作界面以及向导式操作方式.同时,KubeSphere Installer 提供了 快速部署生产级别的 Kubernetes 集群和全栈化容器平台的功能,能够快速帮助企业用户快速搭建一套以应用为中心的 DevOps 平台. KubeSphere 支持部署和运行在包括公有云.私有云.VM.BM 和 Kubernetes 等任何基础设施之上,可以部署在公有云托管的 Kubernetes

通过Rancher部署并扩容Kubernetes集群基础篇

一 应用场景描述 持续跟踪研究Kubernetes也有一段时间了,Kubernetes作为谷歌开源的生产级别的容器调度系统从开源初始便获得了众多的关注.一些有研发实力的公司调研过Kubernetes的设计理念以及应用场景后很快就组织研发人员结合Kubernetes开发出符合自身业务发展的容器管理平台,也有一些公司基于Kubernetes开发公有容器云平台对外提供服务.但是对于一些小型团队对Kubernetes还是保持观望态度并适当做些测试.对比与mesos,kubernetes目前缺少一个像DC

在 Kubernetes 集群快速部署 KubeSphere 容器平台

KubeSphere 不仅支持部署在 Linux 之上,还支持在已有 Kubernetes 集群之上部署 KubeSphere,自动纳管 Kubernetes 集群的已有资源与容器. 前提条件 Kubernetes 版本: 1.13.0 ≤ K8s version < 1.16: Helm,版本 >= 2.10.0(不支持 helm 2.16.0 #6894),且已安装了 Tiller,参考 如何安装与配置 Helm: 集群的可用 CPU > 1 C,可用内存 > 2 G: 集群已

在Kubernetes集群上部署和管理JFrog Artifactory

JFrog Artifactory是一个artifacts仓库管理平台,它支持所有的主流打包格式.构建工具和持续集成(CI)服务器.它将所有二进制内容保存在一个单一位置并提供一个接口,这使得用户在整个应用程序开发和交付过程中,能更易于上传.查找和使用二进制文件. 在本文中我们将介绍如何使用Rancher在Kubernetes集群上部署和管理JFrog Artifactory.在看完本文后,你将会系统地了解JFrog Artifactory OSS的安装设置,并且能够按照同样的步骤在任何Kuber

Rancher2.x 一键式部署 Prometheus + Grafana 监控 Kubernetes 集群

目录 1.Prometheus & Grafana 介绍 2.环境.软件准备 3.Rancher 2.x 应用商店 4.一键式部署 Prometheus 5.验证 Prometheus + Grafana 1.Prometheus & Grafana 介绍 Prometheus 是一套开源的系统监控.报警.时间序列数据库的组合,Prometheus 基本原理是通过 Http 协议周期性抓取被监控组件的状态,而通过 Exporter Http 接口输出这些被监控的组件信息,而且已经有很多 E

部署tomcat负载均衡集群,实现节点之间内存中的Session共享。

在实验之前先来了解下tomcat会话管理器(Manager) Manger对象用于实现HTTP会话管理的功能,介绍下几种常见的: 1.StandardManager(标准会话管理器) 用于非集群环境中对单个处于运行状态的Tomcat实例会话进行管理.当Tomcat关闭时,这些会话相关的数据会被写入磁盘上的一个名叫SESSION.ser的文件,并在Tomcat下次启动时读取此文件.如果tomcat正常关闭会话不会丢失,如果是tomcat主机崩溃或者进程崩溃那会话是一定会丢失的. 2.Persist

使用Rancher的RKE快速部署Kubernetes集群

简要说明: 本文共涉及3台Ubuntu机器,1台RKE部署机器(192.168.3.161),2台Kubernetes集群机器(3.162和3.163). 先在Windows机器上,将rke_linux-amd64从github上下载下来,重新命名为rke ,编辑好cluster.yml集群部署文件,使用putty提供的pscp命令,将文件上传到3.161机器上.在3.161机器上,执行rke命令,将集群部署到3.162和3.163机器上. 只要环境配置正确,部署非常快,整个集群5分钟搞定. 准

Kubernetes 集群的两种部署过程(daemon部署和容器化部署)以及glusterfs的应用!

ClusterIp:通过VIP来访问, NodePort: 需要自己搭建负载据衡器 LoadBalancer:仅仅用于特定的云提供商 和 Google Container Engine https://www.nginx.com/blog/load-balancing-kubernetes-services-nginx-plus/ port:相当于服务端口(对及集群内客户访问) targetPort: 相当于pods端口 nodePort: 宿主机端口(也是服务端口,只不过是对集群外客户访问)