您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何完成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: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中啟用所有流量)
選擇一個密鑰對并且啟動實例
步驟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 | sh which: no k3sup in (/sbin:/bin:/usr/sbin:/usr/bin) x86_64 Downloading package https://github.com/alexellis/k3sup/releases/download/0.2.0/k3sup as /tmp/k3sup Download complete. Running as root - Attempting to move k3sup to /usr/local/bin New 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.255 ssh -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 [INFO] Downloading hash [https://github.com/rancher/k3s/releases/download/v0.8.0/sha256sum-amd64.txt](https://github.com/rancher/k3s/releases/download/v0.8.0/sha256sum-amd64.txt) [INFO] Downloading binary [https://github.com/rancher/k3s/releases/download/v0.8.0/k3s](https://github.com/rancher/k3s/releases/download/v0.8.0/k3s) [INFO] Verifying binary download [INFO] Installing k3s to /usr/local/bin/k3s which: no kubectl in (/usr/local/bin:/usr/bin) [INFO] Creating /usr/local/bin/kubectl symlink to k3s which: no crictl in (/usr/local/bin:/usr/bin) [INFO] Creating /usr/local/bin/crictl symlink to k3s which: 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 unit Created symlink from /etc/systemd/system/multi-user.target.wants/k3s.service to /etc/systemd/system/k3s.service. [INFO] systemd: Starting k3s Result: [INFO] Finding latest release [INFO] Using v0.8.0 as release [INFO] Downloading hash [https://github.com/rancher/k3s/releases/download/v0.8.0/sha256sum-amd64.txt](https://github.com/rancher/k3s/releases/download/v0.8.0/sha256sum-amd64.txt) [INFO] Downloading binary [https://github.com/rancher/k3s/releases/download/v0.8.0/k3s](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 k3s 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.service to /etc/systemd/system/k3s.service. ssh: sudo cat /etc/rancher/k3s/k3s.yaml Saving file to: /usr/local/bin/kubeconfig
它將創建一個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> <h2>Welcome to nginx!</h2> <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>
加入兩個節點:是否可以在不登錄節點的情況下將節點與主節點連接?是的,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.255 ssh -i /root/.ssh/id_rsa ec2-user@54.159.112.255 ssh: 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 [INFO] Downloading hash [https://github.com/rancher/k3s/releases/download/v0.8.0/sha256sum-amd64.txt](https://github.com/rancher/k3s/releases/download/v0.8.0/sha256sum-amd64.txt) [INFO] Downloading binary [https://github.com/rancher/k3s/releases/download/v0.8.0/k3s](https://github.com/rancher/k3s/releases/download/v0.8.0/k3s) [INFO] Verifying binary download [INFO] Installing k3s to /usr/local/bin/k3s which: no kubectl in (/usr/local/bin:/usr/bin) [INFO] Creating /usr/local/bin/kubectl symlink to k3s which: no crictl in (/usr/local/bin:/usr/bin) [INFO] Creating /usr/local/bin/crictl symlink to k3s which: 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 unit Created symlink from /etc/systemd/system/multi-user.target.wants/k3s-agent.service to /etc/systemd/system/k3s-agent.service. [INFO] systemd: Starting k3s-agent Logs: 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 [INFO] Downloading hash [https://github.com/rancher/k3s/releases/download/v0.8.0/sha256sum-amd64.txt](https://github.com/rancher/k3s/releases/download/v0.8.0/sha256sum-amd64.txt) [INFO] Downloading binary [https://github.com/rancher/k3s/releases/download/v0.8.0/k3s](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
感謝你能夠認真閱讀完這篇文章,希望小編分享的“如何完成k3s Kubeconfig配置”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。