写点什么

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

  • 2020-05-18
  • 本文字数:6101 字

    阅读完需:约 20 分钟

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

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<<EOF10.10.1.159 k3s-master10.10.1.126 k3s-node0110.10.1.142 k3s-node02EOF
复制代码


##在 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 unitCreated 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 statuSep 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 statuSep 17 19:20:34 deb10 k3s[837]: I0917 19:20:34.726816 837 reconciler.go:207] operationExecutor.VerifyControllerAttachedVolume started for volumSep 17 19:20:34 deb10 k3s[837]: I0917 19:20:34.726836 837 reconciler.go:207] operationExecutor.VerifyControllerAttachedVolume started for volumSep 17 19:20:34 deb10 k3s[837]: I0917 19:20:34.726857 837 reconciler.go:207] operationExecutor.VerifyControllerAttachedVolume started for volumSep 17 19:20:34 deb10 k3s[837]: I0917 19:20:34.726869 837 reconciler.go:207] operationExecutor.VerifyControllerAttachedVolume started for volumSep 17 19:20:35 deb10 k3s[837]: I0917 19:20:35.529102 837 reconciler.go:181] operationExecutor.UnmountVolume started for volume "helm-traefik-tSep 17 19:20:35 deb10 k3s[837]: I0917 19:20:35.542858 837 operation_generator.go:799] UnmountVolume.TearDown succeeded for volume "kubernetes.iSep 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] C
复制代码


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


$ cat /etc/rancher/k3s/k3s.yamlcat: /etc/rancher/k3s/k3s.yaml: Permission denieddebian@deb10:~$ sudo cat /etc/rancher/k3s/k3s.yamlapiVersion: v1clusters:- cluster:    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJWakNCL3FBREFnRUNBZ0VBTUFvR0NDcUdTTTQ5QkFNQ01DTXhJVEFmQmdOVkJBTU1HR3N6Y3kxelpYSjIKWlhJdFkyRkFNVFUyT0RjME56azVOakFlRncweE9UQTVNVGN4T1RFNU5UWmFGdzB5T1RBNU1UUXhPVEU1TlRaYQpNQ014SVRBZkJnTlZCQU1NR0dzemN5MXpaWEoyWlhJdFkyRkFNVFUyT0RjME56azVOakJaTUJNR0J5cUdTTTQ5CkFnRUdDQ3FHU000OUF3RUhBMElBQkM5aTMyUTdkVnhJaTFCVFNEOTRqYzJaZy9ESHFGc051b0Q4eWhSbjZsUlIKQWp5Q0p3UEZYQ3Y4QUdSMmFaK1lSempTYUJvM2M1LzMwQnZwKzY3OFNYeWpJekFoTUE0R0ExVWREd0VCL3dRRQpBd0lDcERBUEJnTlZIUk1CQWY4RUJUQURBUUgvTUFvR0NDcUdTTTQ5QkFNQ0EwY0FNRVFDSUJwTXdOejAyZzUwCkExdEloU0Y1MFJqSVprVVVuNk8rODdLV25obWRUYkh5QWlBQnJqcDFxWU1HcWE0RmJ2Ym9rTm1kM3VOelVvQm8KeGxqTGlnWnZCN3ZEVGc9PQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==    server: https://localhost:6443  name: defaultcontexts:- context:    cluster: default    user: default  name: defaultcurrent-context: defaultkind: Configpreferences: {}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-tokenK1042e2f8e353b9409472c1e0cca8457abe184dc7be3f0805109e92c50c193ceb42::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 unitCreated 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 NAMEdefault
$ sudo kubectl cluster-info Kubernetes master is running at https://localhost:6443CoreDNS is running at https://localhost:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
$ sudo kubectl get nodesNAME STATUS ROLES AGE VERSIONk3s-master Ready master 14m v1.14.6-k3s.1k3s-node01 Ready worker 3m11s v1.14.6-k3s.1k3s-node02 Ready worker 3m58s v1.14.6-k3s.1
$ sudo kubectl get namespacesNAME STATUS AGEdefault Active 16mkube-node-lease Active 16mkube-public Active 16mkube-system Active 16m
$ sudo kubectl get endpoints -n kube-systemNAME ENDPOINTS AGEkube-dns 10.42.0.2:53,10.42.0.2:53,10.42.0.2:9153 14mtraefik 10.42.0.5:80,10.42.0.5:443 14m
$ sudo kubectl get pods -n kube-systemNAME READY STATUS RESTARTS AGEcoredns-b7464766c-q9frk 1/1 Running 0 15mhelm-install-traefik-8dhpk 0/1 Completed 0 15msvclb-traefik-9c2j8 2/2 Running 0 4m49ssvclb-traefik-bf9zd 2/2 Running 0 4m2ssvclb-traefik-v2fpx 2/2 Running 0 14mtraefik-5c79b789c5-k589d 1/1 Running 0 14m
复制代码


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


# Master$ sudo crictl psCONTAINER ID        IMAGE               CREATED             STATE               NAME                ATTEMPT             POD IDacfafb50852d3       18471c10e6e4b       16 minutes ago      Running             traefik             0                   bf8534452389ffee5ac7e88f2e       4a065d8dfa588       16 minutes ago      Running             lb-port-443         0                   e7068ff7ab2f2bbab5b07e5efb       4a065d8dfa588       16 minutes ago      Running             lb-port-80          0                   e7068ff7ab2f265c5d1333ea04       2ee68ed074c6e       16 minutes ago      Running             coredns             0                   435c51f4716fc
# Workers$ sudo crictl psCONTAINER ID IMAGE CREATED STATE NAME ATTEMPT POD ID7ad5c83d6466f 4a065d8dfa588 6 minutes ago Running lb-port-443 0 bf8d9fe57c3f3c1380eabc0b33 4a065d8dfa588 6 minutes ago Running lb-port-80 0 bf8d9fe57c3f3

复制代码


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


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


2020-05-18 18:054434

评论

发布
暂无评论
发现更多内容

Speed, Efficiency, and Security: IPQ5332 vs. IPQ6010 – Which One is Right for You?

wallyslilly

IPQ6010 IPQ5332

拍卖APP软件开发:推荐使用“东莞梦幻网络”的源代码

软件开发-梦幻运营部

MobPush iOS端 SDK API

MobTech袤博科技

开发者 产品设计 产品动态

win版TouchDesigner Pro(可视化原型渲染设计) v2023特别版

iMac小白

win版UniFab All-In-One(视频AI压缩转换处理) v2.0.2.8特别版

iMac小白

前端说你的API接口太慢了,怎么办?

快乐非自愿限量之名

前端 接口 API

【IoTDB 线上小课 05】时序数据文件 TsFile 三问“解密”!

Apache IoTDB

MobPush 个性化参数API

MobTech袤博科技

开发者 产品动态

院感培训ppt课件怎么做?用这3款做ppt的ai软件自动生成!

彭宏豪95

职场 PPT 办公软件 AIGC AI生成PPT

win版Adobe Substance 3D Sampler(sa 3D材质管理软件) v4.5.0.4785直装版

iMac小白

win版StreamFab Downloader(视频下载工具) v6.1.8.9 激活版

iMac小白

项目管理的终极选择:9款系统比较

易成管理学

项目管理 项目管理软件

win版Adobe Substance 3D Modeler (桌面与VR创作3D建模雕刻软件) v1.12.0.45直装版

iMac小白

win版Athentech Perfectly Clear Video (AI视频清晰化处理软件) v4.6.1.2678特别版

iMac小白

EMQX 跨域集群:连接三大洲,横跨 12,600 公里的数据纽带

EMQ映云科技

mqtt 分布式部署 emqx mqtt broker 跨域集群

深度揭秘!观测云产品核心理念

观测云

观测云 可观测性用观测云

小程序引擎助力APP实现小程序运行能力

Geek_2305a8

11K star!开源WAF的NO1,不花钱也能搞定安全

我再BUG界嘎嘎乱杀

GitHub 开源 网络安全 安全

信息安全: MAC(消息认证码)算法,保护数据完整性和真实性的利器

我再BUG界嘎嘎乱杀

网络安全 安全 后端 网安

win版Maxon Cinema 4D 2024(C4D 2024) v2024.5.1 (x64)特别版

iMac小白

智能交互引擎平台有奖体验活动开始啦!

Openlab_cosmoplat

人工智能 测试 大模型 问答助手 有奖体验

大客户销售设法接触客户决策层 怎么操作成功概率最高

客户在哪儿AI

ToB营销 ToB获客 ToB增长 大客户营销

一文揭开JDK21虚拟线程的神秘面纱

不在线第一只蜗牛

Java Python jdk 算法

【论文速读】| LLMCloudHunter:利用大语言模型(LLMs)从基于云的网络威胁情报(CTI)中自动提取检测规则

云起无垠

即构场景化SDK UIKits,互动应用开发周期缩短 75%

ZEGO即构

企业出海 泛娱乐出海 UI组件库 音视频SDK,直播 即构UIkits

工业智能化|人工智能与仿真的共生

Altair RapidMiner

人工智能 算法 汽车 仿真 altair

win版Perfectly Clear Workbench(图片编辑软件) v4.6.1.2678 特别版

iMac小白

win版Danil Pristupov Fork(Git客户端) v1.99.3 激活版

iMac小白

win版 Adobe InDesign 2024(id2024排版和设计软件) v19.5.0.084直装版

iMac小白

win版Adobe InCopy 2024 (Ic文案编辑软件) v19.5直装版

iMac小白

腾讯云大数据TBDS 助力国有大行一表通业务性能翻三倍!

腾讯云大数据

腾讯云 大数据

Just 5分钟!使用k3s部署轻量Kubernetes集群快速教程_文化 & 方法_Rancher_InfoQ精选文章