iT邦幫忙

2021 iThome 鐵人賽

DAY 7
0

k0s

k0s是Mirantis推出的輕量化Kubernetes發行版,Mirantis有一個k8s套件相當有名lens,這個後面會再簡單介紹一下。

k0s主打三項特點

  • Zero Friction 零磨擦:主要是降低安裝Kubernetes的難度,降低Kubernetes的入門門檻,不會光是安裝就先死一片/images/emoticon/emoticon01.gif
  • Zero Deps 這個翻不出來QQ /images/emoticon/emoticon04.gif
    binary檔安裝,除了作業系統核心外,k0s對系統依賴性最少,而己適用於任何作業系統
  • Zero Cost 零成本:免費仔歡呼吧,它是完全免費,並且開源在github上面 k0s source

系統需求

硬體基本上1c2t的cpu就能支援,現在應該低標都是2c4t或是4c8t了吧,穩穩der,唯一缺點就是目前只支援

  • Linux : kernel v3.10 以上
  • windows server 2019 以上
    macos居然不支援.../images/emoticon/emoticon04.gif

手把手建立k0s single node Kubernetes cluster

下載k0s

curl -sSLf https://get.k0s.sh | sudo sh

執行結果

Downloading k0s from URL: https://github.com/k0sproject/k0s/releases/download/v1.21.4+k0s.0/k0s-v1.21.4+k0s.0-amd64
k0s is now executable in /usr/local/bin

安裝k0s

安裝參數可參考 configuration

sudo k0s install controller --single
INFO[2021-09-13 06:06:16] no config file given, using defaults
INFO[2021-09-13 06:06:16] creating user: etcd
INFO[2021-09-13 06:06:16] creating user: kube-apiserver
INFO[2021-09-13 06:06:16] creating user: konnectivity-server
INFO[2021-09-13 06:06:16] creating user: kube-scheduler
INFO[2021-09-13 06:06:16] Installing k0s service

測試是否安裝成功

k0s version
v1.22.1+k0s.1

sudo k0s status
Version: v1.22.1+k0s.1
Process ID: 1275
Role: controller
Workloads: true

移除k0s

1.先停止k0s service
移除k0s需要sudo權限喔,不然會噴錯誤

k0s stop #會失敗
Error: this command must be run as root
sudo k0s stop #成功

2.移除k0s相關資源,會移除k0s安裝的system service, data directories, containers, mounts and network namespaces

sudo k0s reset
INFO[2021-09-13 06:11:07] no config file given, using defaults
INFO[2021-09-13 06:11:07] * remove k0s users step:
INFO[2021-09-13 06:11:07] no config file given, using defaults
INFO[2021-09-13 06:11:07] * uninstall service step
INFO[2021-09-13 06:11:07] Uninstalling the k0s service
INFO[2021-09-13 06:11:07] * remove directories step
INFO k0s cleanup operations done. To ensure a full reset, a node reboot is recommended.

3.最後重開機就完全移除掉k0s囉

kubectl

在k0s底下要執行kubectl的話,需要前面掛上sudo k0s

sudo k0s kubectl get pods -A
NAMESPACE     NAME                              READY   STATUS    RESTARTS   AGE
kube-system   kube-proxy-2kqmf                  1/1     Running   0          13m
kube-system   kube-router-kfvxm                 1/1     Running   0          13m
kube-system   coredns-5ccbdcc4c4-45s4w          1/1     Running   0          13m
kube-system   metrics-server-6bd95db5f4-pdprq   1/1     Running   0          13m

sudo k0s kubectl get nodes -A
NAME       STATUS   ROLES    AGE   VERSION
k0s-test   Ready    <none>   19m   v1.22.1+k0s

實作

安裝好k0s之後,我們來測試看看是否能部署pod到k0s上面。
這邊使用nginx container來測試

sudo k0s kubectl apply -f - <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: k0s-nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1 
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
EOF
sudo k0s kubectl get pods -A
NAMESPACE     NAME                               READY   STATUS    RESTARTS   AGE
kube-system   kube-proxy-2kqmf                   1/1     Running   0          23m
kube-system   kube-router-kfvxm                  1/1     Running   0          23m
kube-system   coredns-5ccbdcc4c4-45s4w           1/1     Running   0          23m
kube-system   metrics-server-6bd95db5f4-pdprq    1/1     Running   0          23m
default       k0s-nginx-585449566-4cgzk          1/1     Running   0          9s

把k0s-nginx expose出去

sudo k0s kubectl expose deployment k0s-nginx --type=NodePort --port=80
service/k0s-nginx exposed

查看service,可以看到本機31180 port轉到nginx 80 port

sudo k0s  kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        27m
k0s-nginx    NodePort    10.106.142.96   <none>        80:31180/TCP   44s

測試是否成功

curl 127.0.0.1:31180
<!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>

k0s done /images/emoticon/emoticon34.gif


上一篇
[DAY6]從0開始裝k8s(1)-kind
下一篇
[DAY8]k8s必學的設定檔-yaml (上)
系列文
k8s新手船長船難記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
高魁良
iT邦新手 1 級 ‧ 2021-09-15 08:02:20

請問能夠用 k0s 架設一個 cluster 而不只是單機嗎?
有 HA 嗎?

k0s有提供ha,但是不確認是否合您的需求
https://docs.k0sproject.io/main/high-availability/

我要留言

立即登入留言