记得之前应用 Minikube 装置 K8S 的时候,给分 3G 内存都嫌小!最近发现一个 K8S 的经量级实现 K3S,最低 0.5G 内存就能运行起来,装置不便,和 K8S 用起来区别不大。举荐给大家,心愿更多没高端机器的敌人也可能把 K8S 玩起来!
SpringBoot 实战电商我的项目 mall(40k+star)地址:https://github.com/macrozheng/mall
K3S 简介
K3S 是一个完全符合 Kubernetes 的发行版。能够应用繁多二进制包装置(不到 100MB),安装简单,内存只有一半,最低 0.5G 内存就能运行。
为什么叫 K3S?开发者心愿 K3S 在内存占用方面只有 K8S 的一半,Kubernetes 是一个 10 个字母的单词,简写为 K8S。那么一半大小就是 5 个字母的单词,简写为 K3S。
装置
应用官网提供的脚本装置非常不便,一个命令即可实现装置!
- 应用脚本装置 K3S,同时会装置其余实用程序,包含
kubectl
、crictl
、ctr
、k3s-killall.sh
和k3s-uninstall.sh
;
curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -
- 装置实现后提醒如下信息,并且会将 K3S 注册为 Linux 中的服务;
Complete!
[INFO] Creating /usr/local/bin/kubectl symlink to k3s
[INFO] Creating /usr/local/bin/crictl symlink to k3s
[INFO] Skipping /usr/local/bin/ctr symlink to k3s, command exists in PATH at /usr/bin/ctr
[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 k3s
- 能够查看下服务的运行状态,此时显示状态为
active
。
[root@linux-local k3s]# systemctl status k3s
● k3s.service - Lightweight Kubernetes
Loaded: loaded (/etc/systemd/system/k3s.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-01-28 10:18:39 CST; 2min 0s ago
Docs: https://k3s.io
Process: 14983 ExecStartPre=/sbin/modprobe overlay (code=exited, status=0/SUCCESS)
Process: 14981 ExecStartPre=/sbin/modprobe br_netfilter (code=exited, status=0/SUCCESS)
Main PID: 14986 (k3s-server)
Tasks: 71
Memory: 776.3M
应用
咱们应用 kubectl 命令操作 K3S 与之前操作 Minikube 中的 K8S 并没有什么区别,这次还是创立一个 Nginx 的 Deployment,而后通过创立 Service 将其裸露到内部拜访。
创立集群
- 因为 K3S 默认装置了 kubectl 工具,咱们能够间接应用它,比方查看 kubectl 的版本号;
kubectl version
Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2+k3s1", GitCommit:"1d4adb0301b9a63ceec8cabb11b309e061f43d5f", GitTreeState:"clean", BuildDate:"2021-01-14T23:52:37Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2+k3s1", GitCommit:"1d4adb0301b9a63ceec8cabb11b309e061f43d5f", GitTreeState:"clean", BuildDate:"2021-01-14T23:52:37Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}
- 还能够查看集群详细信息;
kubectl cluster-info
Kubernetes control plane is running at https://127.0.0.1:6443
CoreDNS is running at https://127.0.0.1:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://127.0.0.1:6443/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy
- 查看集群中的所有 Node,能够发现 K3S 和之前的 Minikube 一样创立了一个单节点的简略集群。
kubectl get nodes
NAME STATUS ROLES AGE VERSION
linux-local Ready control-plane,master 11m v1.20.2+k3s1
部署利用
- 指定好利用镜像并创立一个 Deployment,这里创立一个 Nginx 利用;
kubectl create deployment nginx-deployment --image=nginx:1.10
- 查看所有 Deployment;
kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 1/1 1 1 6s
查看利用
- 查看 Pod 的具体状态,包含 IP 地址、占用端口、应用镜像等信息;
kubectl describe pods
Name: nginx-deployment-597c48c9dd-j49bc
Namespace: default
Priority: 0
Node: linux-local/192.168.5.15
Start Time: Thu, 28 Jan 2021 10:53:14 +0800
Labels: app=nginx-deployment
pod-template-hash=597c48c9dd
Annotations: <none>
Status: Running
IP: 10.42.0.7
IPs:
IP: 10.42.0.7
Controlled By: ReplicaSet/nginx-deployment-597c48c9dd
Containers:
nginx:
Container ID: containerd://560bbeefc9c5714b92ae9d0a1305c2b8746082f4aa11791a2b6e1f4288254ef0
Image: nginx:1.10
Image ID: docker.io/library/nginx@sha256:6202beb06ea61f44179e02ca965e8e13b961d12640101fca213efbfd145d7575
Port: <none>
Host Port: <none>
State: Running
Started: Thu, 28 Jan 2021 10:53:16 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-fnrf7 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-fnrf7:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-fnrf7
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 38s default-scheduler Successfully assigned default/nginx-deployment-597c48c9dd-j49bc to linux-local
Normal Pulled 38s kubelet Container image "nginx:1.10" already present on machine
Normal Created 38s kubelet Created container nginx
Normal Started 37s kubelet Started container nginx
- 进入容器外部并执行
bash
命令,如果想退出容器能够应用exit
命令。
kubectl exec -it nginx-deployment-597c48c9dd-j49bc -- bash
内部拜访利用
- 创立一个 Service 来裸露
nginx-deployment
这个 Deployment:
kubectl expose deployment/nginx-deployment --name="nginx-service" --type="NodePort" --port=80
- 查看所有 Service 的状态;
kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 77m
nginx-service NodePort 10.43.29.39 <none> 80:31494/TCP 10s
- 在 Linux 服务器上通过 CURL 命令即可拜访 Nginx 服务,此时将打印 Nginx 主页信息;
curl localhost:31494
- 相比 Minikube 在虚拟机中装置容器化利用,K3S 间接在本机上安装,间接关上防火墙端口即可在内部拜访;
# 开启端口
firewall-cmd --zone=public --add-port=31494/tcp --permanent
# 重启防火墙
firewall-cmd --reload
- 在内部即可拜访 Nginx 主页,拜访地址:http://192.168.5.15:31494
总结
K3S 的确是一个很好用的 K8S 发行版本,不仅装置不便,而且内存占用也升高了。因为间接在本机上安装容器化利用,内部拜访也不便了!
本文 GitHub https://github.com/macrozheng/mall-learning 曾经收录,欢送大家 Star!