# PVE LXC 建立 K8S Cluster --- ## 建立好 Ubuntu LXC Container ---- ### 更換 Ubuntu apt 映像來源(建議做) 可以運用指令進行更換 Source 來源位置的描述檔案 /etc/apt/source.list ```shell sudo sed -i 's/archive.ubuntu.com/free.nchc.org.tw/g' /etc/apt/sources.list #更換來源成 NCHC 國網中心 apt 來源 sudo apt update sudo apt upgrade ``` [實作文章](https://note.drx.tw/2012/01/mirror.html) ---- #### 環境前置作業 進到 LXC 描述容器文件檔案 位置 /etc/pve/lxc/<容器ID>.conf 原內容如下 ```shell amd64 cores: 1 features: nesting=1 hostname: test2 memory: 512 net0: name=eth0,bridge=vmbr0,firewall=1,hwaddr=BC:24:11:A8:28:3C,ip=dhcp,ip6=auto,type=veth ostype: ubuntu rootfs: local-lvm:vm-106-disk-0,size=8G swap: 512 unprivileged: 1 ``` 添加這幾行設定到容器設定的檔案下 ```shell lxc.apparmor.profile: unconfined # LXC 不受 AppArmor 安全配置限制。 lxc.cgroup.devices.allow: a # 允許容器訪問所有設備 lxc.cap.drop: # 不指定 LXC 刪除的權限預設與 Host 相同 lxc.mount.auto: "proc:rw sys:rw" # 將 /proc 和 /sys 目錄自動掛載到容器中,並授予容器讀寫權限。 ``` ---- ##### 安裝相關軟體 到 Console ```shell sudo apt install docker.io # 安裝 Docker docker ––version # 檢查 Docker 版本 sudo systemctl enable docker sudo systemctl start docker sudo systemctl status docker #檢查 Docker 狀態 ``` --- ## 在 Ubuntu 安裝 K8S 環境 [安裝](https://hackmd.io/@davvvid/H1ylti6Wi) ---- ### Install Kubernetes ```shell! apt install curl apt install gnupg curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add # 新增 Kubernete apt 來源 sudo apt install software-properties-common #無法使用 apt-add-repository 可以透過安裝他來解決 ``` ---- #### Install Kubernetes Tool ```shell curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes.gpg # Add Kubernete GPG Key echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/kubernetes.gpg] http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list # Add repo sudo apt update # update sudo apt install kubeadm kubelet kubectl # Install Kubernetes Tool kubeadm version # Check kubeadm version ``` --- ### Deploy Kubernetes ---- #### 關閉節點相關設定 進到 PVE 節點(不是 LXC) ```shell sysctl -w net.bridge.bridge-nf-call-iptables=1 #解決節點之間通訊問題 改成 1 表示 bridge 設備在 Layer 2 轉發的時候也會去調用 iptables 配置的 Layer 3 問題 sudo swapoff -a #關閉 Server 之間交換記憶體 echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf # 啟用 IP 轉發 sysctl --system # 查看系統參數 ``` ---- #### 建立 LXC 容器 注意請取消勾選無特權容器  選擇你的 LXC 範本 刪除 SWAP 空間 設置成 0  確認配置後建立容器 ---- #### 更改 PVE 節點 LXC 容器配置 添加這幾行設定到容器設定的檔案下 nano /etc/pve/lxc/[容器ID].conf ```shell lxc.apparmor.profile: unconfined lxc.cgroup.devices.allow: a lxc.cap.drop: lxc.mount.auto: "proc:rw sys:rw" ``` ---- #### 創建容器配置(容器內部) 在 etc 創建 rc.local ```shell touch /etc/rc.local ``` 並且寫入內容 ```shell nano /etc/rc.local ``` ```shell #!/bin/sh -e # Kubeadm 1.15 needs /dev/kmsg to be there, but it's not in lxc, but we can just use /dev/console instead # see: https://github.com/kubernetes-sigs/kind/issues/662 if [ ! -e /dev/kmsg ]; then ln -s /dev/console /dev/kmsg fi # https://medium.com/@kvaps/run-kubernetes-in-lxc-container-f04aa94b6c9c mount --make-rshared / ``` 再給予文件可執行權限 ```shell chmod +x /etc/rc.local ``` --- ### 配置容器 SSH 在容器內 ```shell nano /etc/ssh/sshd_config ``` 取消註解 PubkeyAuthentication yes 重啟 ssh ```shell service ssh restart ``` 產生 ssh key ```shell ssh-keygen ``` --- ### 安裝 K3S ```shell curl -sfL https://get.k3s.io | sh – # 下載 K3S k3sup install --ip $CONTAINER_IP --user root # 安裝 k3S systemctl status k3s # 檢查 k3S 狀態 KUBECONFIG=kubeconfig kubectl get pods --all-namespaces #列出所有 Kubernetes 命名空間中的所有 Pod ``` --- ## 終端機卡住解決方法 ### 透過 PVE 實體環境進入 LXC ```shell pct enter <容器 ID> ``` --- ## 參考文章 1. [Rancher K3s: Kubernetes on Proxmox Containers](https://garrettmills.dev/blog/2022/04/18/Rancher-K3s-Kubernetes-on-Proxmox-Container/) 2. [How to Install Kubernetes on Ubuntu 22.04](https://phoenixnap.com/kb/install-kubernetes-on-ubuntu) 3. [DevOps笔记 - 在 pve 中使用 LXC 创建 k3s](https://zhuanlan.zhihu.com/p/655445211) 4. [How to Install k3s Kubernetes Cluster on Ubuntu 22.04](https://www.linuxbuzz.com/install-k3s-kubernetes-cluster-on-ubuntu/) 5. [K3S in LXC](https://gist.github.com/triangletodd/02f595cd4c0dc9aac5f7763ca2264185)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up