🎈「k8s 探索」系列:
操作系统安装完成后,我们可以说是「万事俱备」了!
1 安装 docker 第一步,我们先来安装 docker
,docker 是最常用的容器,k8s 是容器的编排工具。打个比方,如果说应用是货物,而docker就是集装箱,k8s则是就是码头。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/nullsudo apt update sudo apt install docker-ce docker-ce-cli containerd.io sudo groupadd docker sudo gpasswd -a $USER docker newgrp docker docker -v sudo docker run hello-world cat <<EOF | sudo tee /etc/docker/daemon.json { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2" } EOF sudo vi /boot/firmware/cmdline.txt cgroup_enable=cpuset cgroup_enable=memory cgroup_memory=1 swapaccount=1 net.ifnames=0 dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=LABEL=writable rootfstype=ext4 elevator=deadline rootwait fixrtc sudo systemctl enable docker sudo systemctl daemon-reload sudo systemctl restart docker sudo apt-mark hold docker-ce docker-ce-cli containerd.io docker-ce-rootless-extras
2 安装 kubeadm 都说 k8s 配置复杂,但 kubeadm
改变了这一切!它把 k8s 操作封装起来,变得极其易用,像我这样的小白也可以轻松运行起来。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf br_netfilter EOF cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sudo sysctl --system curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add - echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.listsudo apt update sudo apt install -y kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl kubeadm version
3 使用 kubeadm 创建集群 这一步会遇到很多网络问题,出现问题也比较多,所以需要耐心一点。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 sudo kubeadm config images list vi get_k8s_config_images.sh bash get_k8s_config_images.sh sudo kubeadm init mkdir -p $HOME /.kubesudo cp -i /etc/kubernetes/admin.conf $HOME /.kube/config sudo chown $(id -u):$(id -g) $HOME /.kube/config kubectl get nodes kubectl describe node server1 Ready False Mon, 20 Sep 2021 01:22:22 +0800 Mon, 20 Sep 2021 01:01:25 +0800 KubeletNotReady container runtime network not ready: NetworkReady=false reason:NetworkPluginNotReady message:docker: network plugin is not ready: cni config uninitialized kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n') " NAME STATUS ROLES AGE VERSION server1 Ready control-plane,master 22m v1.22.2
现在我们已经完成了第一个主节点的部署,接下来只要把剩余节点加入进来,整个集群就搭建完成了!
4 添加其他节点 另外两个节点也是一样的步骤,先安装 docker 和 kubeadm。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 sudo kubeadm join 192.168.2.101:6443 --token xxxxx \ --discovery-token-ca-cert-hash sha256:xxxxxx sudo kubeadm reset sudo kubeadm join 192.168.2.101:6443 --token xxxxx \ --discovery-token-ca-cert-hash sha256:xxxxxx kubectl get nodes
现在,一个运行在树莓派上的 k8s 集群,就搭建完成了!🎉
除了 k8s
以外,你还可以选择 microk8s
和 k3s
这样的 k8s 替代品,它们针对资源受限的设备进行了优化。
参考
🎈「k8s 探索」系列: