湘乡彭于晏学习前端
Mac使用UTM虚拟机搭建k8s集群
2024-05-23k8s录2277

1.安装前准备

  • 电脑:mac, m1
  • 虚拟机:下载UTM
  • 安装三个节点(linux镜像版本:ubuntu-20.04.5-live-server-arm64) 下载地址
  • 安装教程:点这

集群规划:

  • 共3个节点: node1 node2 node3
  • node1 master
  • node2 worker
  • node3 worker

1.1 基础配置

a. 配置 root 账号

复制
sudo passwd root

b.设置 ssh 连接

复制
vim /etc/ssh/sshd_config

然后设置 PermitRootLogin yes 重启sshd服务

复制
sudo service ssh restart

启用 ssh & 设置免密登录

复制
https://zhuanlan.zhihu.com/p/145763789 https://blog.csdn.net/chenxuecheng1984/article/details/115870404

c.关闭防火墙

复制
sudo ufw status sudo ufw disable

d.关闭 swap

k8s init的时候不允许开启swap

临时关闭

复制
swapoff -a

永久关闭

复制
vi /etc/fstab ## 注释掉最后一行 /swap.img

e. 在 master(node1) 添加 hosts

注意替换自己节点对应的 ip

查询 ip 可以用 ip -a

复制
cat >> /etc/hosts << EOF 192.168.64.5 node1 192.168.64.6 node2 192.168.64.7 node3 EOF

f.时间同步

安装 ntpdate 并同步时间

复制
apt update && apt install ntpdate ntpdate cn.ntp.org.cn

g.将桥接的IPv4流量传递到iptables的链

复制
cat > /etc/sysctl.d/k8s.conf << EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 vm.swappiness = 0 EOF

加载br_netfilter模块

复制
modprobe br_netfilter

查看是否加载

复制
lsmod | grep br_netfilter
复制
sysctl --system # 生效

h.确保各节点MAC 地址和 product_uuid 的唯一性

复制
# 确保各节点mac地址唯一性 ifconfig -a|grep ether # 确保各节点produce_uuid唯一性 sudo cat /sys/class/dmi/id/product_uuid #

1.2 开启ipvs(可选)

1.3 安装 docker

k8s支持的容器运行时有很多如docker、containerd、cri-o等等,由于现在主流还是docker,所以这里选择安装docker

复制
apt-get update apt install -y docker.io

2.k8s 安装

2.1 添加阿里云的YUM软件源

复制
apt update && apt install apt-transport-https curl curl -fsSL https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - add-apt-repository "deb [arch=arm64] https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main"

2.2 安装kubeadm、kubelet和kubectl(所有节点)

a.选择版本,可以 apt search 一下

复制
apt search kubeadm

这里用 1.28.2-00 版本

b.安装命令

复制
apt-get update apt-cache madison kubelet kubectl kubeadm |grep '1.28.2-00' apt install -y kubelet=1.28.2-00 kubectl=1.28.2-00 kubeadm=1.28.2-00

c.再次禁用 swap

复制
vim /etc/default/kubelet ## 输入以下内容至 kubelet 文件 KUBELET_EXTRA_ARGS="--fail-swap-on=false" systemctl daemon-reload && systemctl restart kubelet
复制
sudo swapoff -a 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

d.修改cgroup管理器

复制
vim /etc/docker/daemon.json

然后将下面设置加入daemon.json文件

复制
{ "exec-opts": [ "native.cgroupdriver=systemd" ] }
复制
systemctl restart docker systemctl enable kubelet

2.3 初始化&加入节点

a.部署k8s的master节点(node1)

这里的 192.168.64.2 填当前节点的ip

复制
kubeadm init \ --apiserver-advertise-address=192.168.64.2 \ --image-repository registry.aliyuncs.com/google_containers \ --control-plane-endpoint=192.168.64.2:6443 \ --kubernetes-version v1.28.2 \ --service-cidr=10.96.0.0/12 \ --pod-network-cidr=10.244.0.0/16 \ --upload-certs

参数说明:

  • --image-repository:指定要使用的镜像仓库,默认为gcr.io。
  • --kubernetes-version:Kubernetes程序组件的版本号,它必须与安装的kubelet程序包的版本号相同。
  • --control-plane-endpoint:控制平面的固定访问端点,可以是IP地址或DNS名称,作为集群管理员与集群组件的kubeconfig配置文件的API Server的访问地址。单控制平面部署时可以不使用该选项。
  • --pod-network-cidr:Pod网络的地址范围,其值为CIDR格式的网络地址,通常Flannel网络插件的默认值为10.244.0.0/16,Project Calico插件的默认值为192.168.0.0/16。
  • --service-cidr:Service的网络地址范围,其值为CIDR格式的网络地址,默认为10.96.0.0/12。通常,仅Flannel一类的网络插件需要手动指定该地址。
  • --apiserver-advertise-address:API Server通告给其他组件的IP地址,一般为Master节点用于集群内通信的IP地址,0.0.0.0表示节点上所有可用地址。
  • --token-ttl:共享令牌的过期时长,默认为24小时,0表示永不过期。为防止不安全存储等原因导致的令牌泄露危及集群安全,建议为其设定过期时长

初始化输出日志内容如下:日志很重要

复制
... Your Kubernetes control-plane has initialized successfully! # 通过如下配置,即可操作集群 To start using your cluster, you need to run the following as a regular user: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config Alternatively, if you are the root user, you can run: export KUBECONFIG=/etc/kubernetes/admin.conf You should now deploy a pod network to the cluster. Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/ # 通过如下命令添加master节点 You can now join any number of the control-plane node running the following command on each as root: kubeadm join 192.168.64.5:6443 --token qv4vub.h7aov4ae3z182y99 \ --discovery-token-ca-cert-hash sha256:f3b753e4484154f11c9105427ca614a1e07dfc8ddaa167eec86c1cfed8cbfb7e \ --control-plane --certificate-key a6c6f91d8d2360934b884eb6a5f65d8bad3a2be25c3da0e280de7ad2225668af # token过期,可通过如下命令生成新的token Please note that the certificate-key gives access to cluster sensitive data, keep it secret! As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use "kubeadm init phase upload-certs --upload-certs" to reload certs afterward. # 通过如下命令,添加node节点 Then you can join any number of worker nodes by running the following on each as root: kubeadm join 192.168.64.5:6443 --token qv4vub.h7aov4ae3z182y99 \ --discovery-token-ca-cert-hash sha256:f3b753e4484154f11c9105427ca614a1e07dfc8ddaa167eec86c1cfed8cbfb7e

根据输出提示执行对应操作即可

复制
设置 master role kubectl label node node1 node-role.kubernetes.io/master= 执行.kube配置 mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config 配置calico网络 curl https://projectcalico.docs.tigera.io/manifests/calico.yaml -O kubectl apply -f calico.yaml Systemctl restart kubelet systemctl restart containerd

b. 部署k8s的node节点(node2 node3)

这里的命令都是由前面的日志输出所生成,不要无脑填 token

复制
kubeadm join 192.168.64.2:6443 --token jfik3p.hzsojd6lh0ovw6lh \ --discovery-token-ca-cert-hash sha256:95e11cfbaa35beb5ab206c95a848eb955b0d57af0cedddcfc195b879d2552a03

c. 配置命令补全

复制
apt install bash-completion # 加载bash-completion source /etc/profile.d/bash_completion.sh # 加载环境变量 echo "export KUBECONFIG=/root/.kube/config" >> /root/.bash_profile echo "source <(kubectl completion bash)" >> /root/.bash_profile source .bash_profile source /etc/bash_completion

d. 其他

如果想重新设置 node join 可以进行 reset

复制
# 清除已经join的节点 kubeadm reset

展示 token

复制
kubeadm token list

查询 token对应的 hash

复制
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'

重新创建 token

复制
kubeadm token create --print-join-command

重新生成 certificate-key

复制
kubeadm init phase upload-certs --upload-certs

遇到其他问题直接使用搜索引擎