ubuntu 22.04安装k3s

一、概述

K3s 是一个轻量级的 Kubernetes 发行版,专为在资源受限的环境中运行 Kubernetes 而设计。它是 Rancher Labs 开发的,旨在简化 Kubernetes 的部署和管理,同时保持与上游 Kubernetes 的兼容性。

K3s 的特点

  1. 轻量级
    • K3s 是一个轻量级的 Kubernetes 发行版,设计用于在资源受限的环境中运行,如边缘计算、物联网设备、开发环境等。
    • 它通过移除一些不必要的组件和依赖来减小体积,同时保持了 Kubernetes 的核心功能。
  2. 易于安装
    • K3s 的安装非常简单,只需要一条命令即可完成安装。它支持多种操作系统,包括 Linux、macOS 和 Windows。
    • 安装脚本会自动配置和启动 K3s 服务,使得部署过程变得非常快捷。
  3. 高性能
    • K3s 通过使用 Flannel 作为默认的网络插件,提供了高效的网络性能。
    • 它还支持多种存储插件,如本地存储和云存储,以满足不同的存储需求。
  4. 高可用性
    • K3s 支持高可用性(HA)配置,可以通过多个服务器节点来提高集群的可靠性和容错能力。
    • 它支持在多个节点上运行控制平面组件,确保在单点故障时集群仍然可以正常运行。
  5. 与 Kubernetes 兼容
    • K3s 与上游 Kubernetes 保持高度兼容,支持 Kubernetes 的所有核心功能和 API。
    • 这意味着在 K3s 上运行的应用程序可以无缝迁移到其他 Kubernetes 集群上。
  6. 社区支持
    • K3s 是一个开源项目,拥有活跃的社区支持和丰富的文档。
    • 社区提供了大量的插件和工具,帮助用户更好地管理和扩展 K3s 集群。

K3s 的应用场景

  1. 边缘计算
    • K3s 适合在边缘计算环境中运行,如物联网设备、远程数据中心等。这些环境通常资源有限,K3s 的轻量级特性使其成为理想选择。
  2. 开发和测试环境
    • K3s 是开发和测试 Kubernetes 应用程序的理想选择,因为它安装简单,启动快速,适合在开发人员的本地机器上运行。
  3. 小型生产环境
    • 对于小型生产环境,K3s 提供了足够的功能和性能,同时减少了资源消耗和管理复杂性。
  4. 容器化应用
    • K3s 支持运行容器化应用程序,提供了一个完整的 Kubernetes 环境,使得应用程序的部署和管理更加简单。

二、安装

前置条件

硬件

确保硬件符合以下条件

最低硬件要求
  • CPU:最低 1 核,推荐 2 核
  • 内存:最低 512 MB,推荐 1 GB
  • 磁盘:建议使用 SSD,以确保最佳性能,尤其是在使用内嵌 etcd 时
推荐硬件配置
  • 服务器节点:
    • CPU:2 核或更多
    • 内存:2 GB 或更多

软件

确保已经安装了docker,参考文档:https://www.cnblogs.com/xiao987334176/p/18809253

确保硬件软件,已经符合了条件,就可以看下面的步骤了。

官方给出的安装方式:

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

运行之后,是会出错的,输出:

[INFO]  Finding release for channel stable
[INFO]  Using v1.32.3+k3s1 as release
[INFO]  Downloading hash https://github.com/k3s-io/k3s/releases/downloa
[ERROR]  Download failed

为什么?因为你的网络,无法访问github,受到了限制。

如果你的服务器在国外,是不受影响的。

国内安装

使用国内的更新源进行安装

curl –sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn INSTALL_K3S_EXEC="--docker" sh -s - --system-default-registry registry.cn-hangzhou.aliyuncs.com

参数解释:

INSTALL_K3S_MIRROR=cn,指定国内镜像源

INSTALL_K3S_EXEC="--docker",使用docker作为运行时容器,默认是containerd 

验证安装

安装完成后,K3s会自动启动,并且Kubeconfig文件会默认放在/etc/rancher/k3s/k3s.yaml路径下。
验证K3s是否运行成功:
# k3s kubectl get nodes
NAME       STATUS   ROLES                  AGE   VERSION
ubuntu-1   Ready    control-plane,master   92s   v1.32.3+k3s1
此命令会列出集群中的所有节点,并显示它们的状态。如果你能看到节点处于Ready状态,说明K3s已成功安装并运行。

配置kubectl

为了更方便使用kubectl,可以将配置文件复制到用户的默认配置路径:
mkdir -p $HOME/.kube
sudo cp /etc/rancher/k3s/k3s.yaml $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

现在你可以通过kubectl命令来管理K3s集群了:

# kubectl get nodes
NAME       STATUS   ROLES                  AGE   VERSION
ubuntu-1   Ready    control-plane,master   35m   v1.32.3+k3s1

设置k3s开机自启动

sudo systemctl enable k3s

查看k3s系统pod运行情况

# kubectl get pods -A
NAMESPACE     NAME                                      READY   STATUS      RESTARTS   AGE
kube-system   coredns-5f754ccb4b-fg84v                  1/1     Running     0          10m
kube-system   helm-install-traefik-crd-8nqzs            0/1     Completed   0          10m
kube-system   helm-install-traefik-k7spn                0/1     Completed   2          10m
kube-system   local-path-provisioner-65c47647b6-bd8br   1/1     Running     0          10m
kube-system   metrics-server-7d5fccf496-7mddc           1/1     Running     0          10m
kube-system   svclb-traefik-63f1fb11-phgk7              2/2     Running     0          9m27s
kube-system   traefik-f8586b6c-dcsx7                    1/1     Running     0          9m27s

确保状态都是正常的,没有异常

部署应用

为了确认 K3s 已经使用 Docker 作为容器运行时,可以部署一个简单的应用并检查其状态。
例如,部署一个 Nginx 应用:
1. 创建一个名为 nginx-deployment.yaml 的文件,内容如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

应用该配置文件:

kubectl apply -f nginx-deployment.yaml

2. 检查 Pod 状态:

# kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
nginx-deployment-96b9d695-l2m49   1/1     Running   0          10m

如果 Pod 正常运行,说明 K3s 已成功使用 Docker 作为容器运行时。

3. 访问nginx

查看pod ip

# kubectl get pods -o wide
NAME                              READY   STATUS    RESTARTS   AGE   IP          NODE       NOMINATED NODE   READINESS GATES
nginx-deployment-96b9d695-l2m49   1/1     Running   0          11m   10.42.0.9   ubuntu-1   <none>           <none>

直接curl pod ip

# curl 10.42.0.9
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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 and
working. 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>

可以看到html页面,就说明应用访问正常

三、CoreDNS 配置

k3s默认使用的网络组件是Fluentd,上面部署的nginx容器,默认情况下,是没法上网的。

所以需要修改CoreDNS的configMap配置才可以,有2种方式:

1. kubectl -n kube-system edit configmap coredns

2. CoreDNS 配置文件,文件位置:/var/lib/rancher/k3s/server/manifests/coredns.yaml

注意:以上2种方式修改后,重启k3s,就会被重置。好吧,白干活了。

永久修改

如果你想修改 K3s 中 CoreDNS 中的配置,并且持久生效的话,可以通过额外的 coredns-custom configmap 安装到 CoreDNS 容器中,并从包含的文件中导入覆盖和额外的 CoreDNS 配置。

ConfigMap 的 name 一定刚要是 coredns-custom 才能够被 coredns 的 deployment 识别并挂载。

# 创建一个配置文件,例如 host.yaml
cat <<EOF > host.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns-custom
  namespace: kube-system
data:
  Corefile: |
    .:53 {
        errors
        health {
            lameduck 5s
        }
        ready
        kubernetes cluster.local in-addr.arpa ip6.arpa {
            pods insecure
            fallthrough in-addr.arpa ip6.arpa
            ttl 30
        }
        prometheus :9153
        forward . 114.114.114.114
        cache 30
        loop
        reload
        loadbalance
    }
EOF

114.114.114.114是全国通用的DNS地址,适用于国内3大运营商网络,移动,电信, 联通。

使用 kubectl 命令应用配置:

kubectl apply -f host.yaml

验证pod上网

删除nginx的pod,重新启动,并访问百度

root@ubuntu-1:/tmp# kubectl get pods
NAME                              READY   STATUS    RESTARTS      AGE
nginx-deployment-96b9d695-stf5j   1/1     Running   1 (12m ago)   14m
root@ubuntu-1:/tmp# kubectl delete po nginx-deployment-96b9d695-stf5j
pod "nginx-deployment-96b9d695-stf5j" deleted
root@ubuntu-1:/tmp# kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
nginx-deployment-96b9d695-jq4hg   1/1     Running   0          6s
root@ubuntu-1:/tmp# kubectl exec -it nginx-deployment-96b9d695-jq4hg -- curl www.baidu.com
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttp%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>

出现百度的html页面,就说明可以上网了。

四、管理K3s

启动K3s服务:

sudo systemctl start k3s

停止K3s服务:

sudo systemctl stop k3s

查看K3s状态:

sudo systemctl status k3s

卸载K3s

如果你需要卸载K3s,可以运行以下命令:

sudo /usr/local/bin/k3s-uninstall.sh

对于工作节点,使用以下命令:

sudo /usr/local/bin/k3s-agent-uninstall.sh
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值