写点什么

不到 1 分钟,从零完成 k3s Kubeconfig 配置!

  • 2020-05-16
  • 本文字数:5190 字

    阅读完需:约 17 分钟

不到1分钟,从零完成k3s Kubeconfig配置!

K3s 是由 Rancher Labs 于今年年初发布的一款开源、极轻量的Kubernetes发行版,完美适用于在资源有限的环境中运行 Kubernetes。K3s 一经发布就得到了业界的高度关注和认可,发布半年以来已在 GitHub 上拥有近 8500 颗 Star


最初 k3s 只是一个用于本地 Kubernetes 开发的 POC 项目,而如今已有很多企业用户在生产环境中使用它。


官方 GitRepo:


https://github.com/rancher/k3s


官方定义:k3s 是完全符合标准的生产级 Kubernetes 发行版,具有以下四个方面的主要变化:


  • 删除旧的、非必须的代码: K3s 不包括任何默认禁用的 Alpha 功能或者过时的功能,原有的 API 组件目前仍运行于标准部署当中。除此之外,Rancher 还删除了所有非默认许可控制器,in- tree 云提供商和存储驱动程序,但允许用户添加任何他们需要的驱动程序。

  • 整合正在运行的打包进程:为了节省 RAM,Rancher 将通常在 Kubernetes 管理服务器上运行的多流程合并为单个流程。Rancher 还将在工作节点上运行的 kubelet、kubeproxy 和 flannel 代理进程组合成一个进程。

  • 使用 containerd 代替 Docker 作为运行时的容器引擎: 通过用 containderd 替换 Docker,Rancher 能够显著减少运行时占用空间,删除 libnetwork、swarm、Docker 存储驱动程序和其他插件等功能。

  • 除了 etcd 之外,引入 SQLite 作为可选的数据存储: Rancher 在 k3s 中添加了 SQLite 作为可选的数据存储,从而为 etcd 提供了一个轻量级的替代方案。该方案不仅占用了较少的内存,而且大幅简化了操作。


鉴于 k3s 受到极高的关注度以及广泛的应用,大量的开源社区用户开始搭建有关 k3s 的项目,本文将分享如何使用其中一个项目——k3sup,来实现一分钟内从零到完全配置 k3s Kubeconfig。

K3sup:1 分钟之内,从零到完全配置 Kubeconfig

K3sup:https://github.com/alexellis/k3sup


K3sup 是由 Alex Ellis 创建的一个开源项目,这使得 k3s 的安装和 kubeconfig 文件的生成变得快速和方便。这一工具能以极快的速度自动化安装 k3s、将 SAN 地址更新为公共 IP、下载 k3s 配置文件然后使用 VM 的公共 IP 地址将其更新,以便可以使用 kubectl 连接它。


以下步骤使用 Amazon EC2 实例说明了此过程:


步骤 1: 启动 Amazon EC2 实例,让安全组打开端口并通过 ssh 登录。


  • 登录亚马逊控制台并单击 EC2

  • 在 EC2 Dashboard 上,点击启动实例

  • 选择 Amazon Linux 2 AMI 镜像

  • 选择实例类型(我们将使用 t2.medium)

  • 选择 VPC/Subnet/IAM 等并且启用自动分配公共 IP(一切都保持默认)

  • 添加存储(使用默认配置)

  • 添加标签(如果需要的话)

  • 配置安全组以允许不同端口上的流量(本 demo 中启用所有流量)

  • 选择一个密钥对并且启动实例











EC2 与 Amazon AMI 一起运行


步骤 2: 下载 k3sup 的二进制文件


curl -sLS https://raw.githubusercontent.com/alexellis/k3sup/master/get.sh | sh[root@ip-172-31-33-136 ec2-user]# curl -sLS https://raw.githubusercontent.com/alexellis/k3sup/master/get.sh | shwhich: no k3sup in (/sbin:/bin:/usr/sbin:/usr/bin)x86_64Downloading package https://github.com/alexellis/k3sup/releases/download/0.2.0/k3sup as /tmp/k3supDownload complete.
Running as root - Attempting to move k3sup to /usr/local/binNew version of k3sup installed to /usr/local/bin
复制代码


步骤 3: 将 ssh 密钥复制到 VM 中的’vi /root/.ssh/id_rsa’位置,导出 IP 并使用 k3sup 安装 k3s


[root@ip-172-31-33-136 ec2-user]# **cd /usr/local/bin**
[root@ip-172-31-33-136 bin]# **export IP=54.159.112.255**
[root@ip-172-31-33-136 bin]# **./k3sup install --ip $IP --user ec2-user**
Public IP: 54.159.112.255ssh -i /root/.ssh/id_rsa ec2-user@54.159.112.255
ssh: curl -sLS https://get.k3s.io | INSTALL_K3S_EXEC='server --tls-san 54.159.112.255' sh -[INFO] Finding latest release[INFO] Using v0.8.0 as release[](https://github.com/rancher/k3s/releases/download/v0.8.0/sha256sum-amd64.txt)[](https://github.com/rancher/k3s/releases/download/v0.8.0/k3s)[INFO] Verifying binary download[INFO] Installing k3s to /usr/local/bin/k3swhich: no kubectl in (/usr/local/bin:/usr/bin)[INFO] Creating /usr/local/bin/kubectl symlink to k3swhich: no crictl in (/usr/local/bin:/usr/bin)[INFO] Creating /usr/local/bin/crictl symlink to k3swhich: no ctr in (/usr/local/bin:/usr/bin)[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 from /etc/systemd/system/multi-user.target.wants/k3s.service to /etc/systemd/system/k3s.service.[INFO] systemd: Starting k3sResult: [INFO] Finding latest release[INFO] Using v0.8.0 as release[](https://github.com/rancher/k3s/releases/download/v0.8.0/sha256sum-amd64.txt)[](https://github.com/rancher/k3s/releases/download/v0.8.0/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[INFO] systemd: Starting k3swhich: no kubectl in (/usr/local/bin:/usr/bin)which: no crictl in (/usr/local/bin:/usr/bin)which: no ctr in (/usr/local/bin:/usr/bin)Created symlink from /etc/systemd/system/multi-user.target.wants/k3s.service to /etc/systemd/system/k3s.service.
复制代码


它将创建一个 kubeconfig 文件并且保存在你运行命令的目录中。


步骤 4: 导出 KUBECONFIG


[root@ip-172-31-33-136 bin]#** export KUBECONFIG=`pwd`/kubeconfig**
复制代码


步骤 5: 开始玩转 k3s


[root@ip-172-31-33-136 bin]# **./kubectl get nodes**
NAME STATUS ROLES AGE VERSION
ip-172-31-33-136.ec2.internal Ready master 24s v1.14.5-k3s.1

[root@ip-172-31-33-136 bin]# **./kubectl get pods -n kube-system**
NAME READY STATUS RESTARTS AGE
coredns-b7464766c-ngf46 1/1 Running 0 4m3s
helm-install-traefik-x6wfj 0/1 Completed 0 4m3s
svclb-traefik-65gw9 2/2 Running 0 3m49s
traefik-56688c4464-5rdss 1/1 Running 0 3m49s
复制代码


运行一个简单的服务:


# Create a Deployment**./kubectl run nginx --image=nginx**
# Create Service[root@ip-172-31-33-136 bin]# **./kubectl expose deployment nginx --port=80 --target-port=80**
service/nginx exposed
[root@ip-172-31-33-136 bin]# **./kubectl get svc**
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 45m
nginx ClusterIP 10.43.99.179 <none> 80/TCP 5s
[root@ip-172-31-33-136 bin]# **curl 10.43.99.179**
<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>body {width: 35em;margin: 0 auto;font-family: Tahoma, Verdana, Arial, sans-serif;}</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p><p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p></body></html>
复制代码


加入两个节点:是否可以在不登录节点的情况下将节点与主节点连接?是的,k3sup 可以做到这一点。


在 VM 上获得 k3s 设置后,你可以轻松地将节点加入到集群:


#AGENT_IP is the IP of the VM that you want to add to the cluster that has k3s running**export AGENT_IP=54.196.113.156**
#SERVER_IP is the IP of the server where k3 is installed export SERVER_IP=54.159.112.255**export USER=ec2-user**
[root@ip-172-31-33-136 bin]# **./k3sup join --ip $AGENT_IP --server-ip $SERVER_IP --user ec2-user**Server IP: 54.159.112.255ssh -i /root/.ssh/id_rsa ec2-user@54.159.112.255ssh: sudo cat /var/lib/rancher/k3s/server/node-token
K1077d707d0868b652aca7adfc21b5f34a1f0357e71b216bf8b3c94a559cd4f93a6::node:014c8ed3f6574d27db73b9c8bf67f617
ssh: curl -sfL https://get.k3s.io/ | K3S_URL="https://54.159.112.255:6443" K3S_TOKEN="K1077d707d0868b652aca7adfc21b5f34a1f0357e71b216bf8b3c94a559cd4f93a6::node:014c8ed3f6574d27db73b9c8bf67f617" sh -
[INFO] Finding latest release[INFO] Using v0.8.0 as release[](https://github.com/rancher/k3s/releases/download/v0.8.0/sha256sum-amd64.txt)[](https://github.com/rancher/k3s/releases/download/v0.8.0/k3s)[INFO] Verifying binary download[INFO] Installing k3s to /usr/local/bin/k3swhich: no kubectl in (/usr/local/bin:/usr/bin)[INFO] Creating /usr/local/bin/kubectl symlink to k3swhich: no crictl in (/usr/local/bin:/usr/bin)[INFO] Creating /usr/local/bin/crictl symlink to k3swhich: no ctr in (/usr/local/bin:/usr/bin)[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 from /etc/systemd/system/multi-user.target.wants/k3s-agent.service to /etc/systemd/system/k3s-agent.service.[INFO] systemd: Starting k3s-agentLogs: which: no kubectl in (/usr/local/bin:/usr/bin)which: no crictl in (/usr/local/bin:/usr/bin)which: no ctr in (/usr/local/bin:/usr/bin)Created symlink from /etc/systemd/system/multi-user.target.wants/k3s-agent.service to /etc/systemd/system/k3s-agent.service.Output: [INFO] Finding latest release[INFO] Using v0.8.0 as release[](https://github.com/rancher/k3s/releases/download/v0.8.0/sha256sum-amd64.txt)[](https://github.com/rancher/k3s/releases/download/v0.8.0/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[INFO] systemd: Starting k3s-agent


[root@ip-172-31-33-136 bin]# **./kubectl get nodes**
NAME STATUS ROLES AGE VERSION
ip-172-31-33-136.ec2.internal Ready master 19m v1.14.5-k3s.1
ip-172-31-37-118.ec2.internal Ready worker 11s v1.14.5-k3s.1
复制代码


你可以看到使用 k3sup 设置 k3s 集群并在不到 60 秒的时间内将其作为 Amazon EC2 实例上运行的节点加入 VM 是如此容易。赶紧上手尝试吧!


2020-05-16 17:181592

评论 1 条评论

发布
用户头像
看似快,其实要踩坑蛮多的。
例如说,官方的文档,写的比较粗糙。。
2021-08-11 11:04
回复
没有更多了
发现更多内容

漫画 | 架构设计中的那些事

码农神说

架构设计 架构师 漫画编程

图片处理不用愁,给你十个小帮手

阿宝哥

Java 开源 大前端 工具 图片

还在划水?这个SQL你能写出来吗?

书旅

php MySQL SQL语法 sql查询

性能碾压 POI !利用模板语法快速生成 Excel 报表

葡萄城技术团队

表格控件 GCExcel 服务器端开发

kubernetes集群安装(二进制)

小小文

Kubernetes 容器

工厂方法模式

Leetao

Python 设计模式 工厂方法模式

mac vmware centos7 设置静态IP

愤毛阿青

network vmware Centos 7

Java集合总结,从源码到并发一路狂飙

给你买橘子

Java 编程 算法 集合

Linux 进程必知必会

苹果看辽宁体育

Linux 操作系统

Redis进阶篇三——主从复制

多选参数

redis redis高可用 redis6.0.0 Redis项目

字节跳动面试经验分享,已拿 Offer!

伍陆柒

Java 面试 大厂

Python中的@staticmethod和@classmethod的区别

BigYoung

Python classmethod staticmethod

一些思考

张健

JVM中栈的frames详解

程序那些事

JVM 堆栈 性能调优 JIT GC

Rust是如何保障内存安全的

博文视点Broadview

读书笔记 rust

猿灯塔:spring Boot Starter开发及源码刨析(二)

猿灯塔

Java 猿灯塔 源码刨析

带你解析MySQL binlog

Simon

MySQL Binlog

Node.js与二进制数据流

自然醒

Java node.js 大前端 二进制

一文看懂 OAuth2

Geek_z9ygea

Java 大前端 Web oauth2.0

信创舆情一线--《关键信息基础设施安全保护条例》纳入2020年立法计划

统小信uos

信息安全

30 张图带你分分钟看懂进程和线程基础知识全家桶

爱嘤嘤嘤斯坦

Java 线程 进程 进程线程区别

重学 Java 设计模式:实战访问者模式「模拟家长与校长,对学生和老师的不同视角信息的访问场景」

小傅哥

设计模式 小傅哥 重构 代码优化 访问者模式

​区块链技术的重要性

CECBC

关于计划的思考

zhongzhq

不是完成你学习的 KPI ,而是要形成指导你行动的 OKR

非著名程序员

学习 程序员 提升认知 知识管理

Tomcat8.5源码构建

知春秋

tomcat tomcat构建 tomcat源码解读 tomcat剖析

开发者必备——IDEA配置清单

Noneplus

配置 IDEA

浅析 VO、DTO、DO、PO 的概念、区别和用处!

Java小咖秀

学习 设计模式 模型 经验分享

推荐一款Python开源库,技术人必备的造数据神器!

狂师

Python 开源 自动化 开发工具 开发数据

Python类中的__new__和__init__的区别

BigYoung

Python __init__ __new__

SpringBoot入门:00 - 初始化项目

封不羁

Spring Boot java 14

不到1分钟,从零完成k3s Kubeconfig配置!_文化 & 方法_Rancher_InfoQ精选文章