集群规划:
复制
sudo passwd root
复制
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
复制
sudo ufw status
sudo ufw disable
k8s init的时候不允许开启swap
临时关闭
复制
swapoff -a
永久关闭
复制
vi /etc/fstab
## 注释掉最后一行 /swap.img
注意替换自己节点对应的 ip
查询 ip 可以用 ip -a
复制
cat >> /etc/hosts << EOF
192.168.64.5 node1
192.168.64.6 node2
192.168.64.7 node3
EOF
安装 ntpdate 并同步时间
复制
apt update && apt install ntpdate
ntpdate cn.ntp.org.cn
复制
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 # 生效
复制
# 确保各节点mac地址唯一性
ifconfig -a|grep ether
# 确保各节点produce_uuid唯一性
sudo cat /sys/class/dmi/id/product_uuid #
k8s支持的容器运行时有很多如docker、containerd、cri-o等等,由于现在主流还是docker,所以这里选择安装docker
复制
apt-get update
apt install -y docker.io
复制
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"
复制
apt search kubeadm
这里用 1.28.2-00 版本
复制
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
复制
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
复制
vim /etc/docker/daemon.json
然后将下面设置加入daemon.json文件
复制
{
"exec-opts": [
"native.cgroupdriver=systemd"
]
}
复制
systemctl restart docker
systemctl enable kubelet
这里的 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
参数说明:
初始化输出日志内容如下:日志很重要
复制
...
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
这里的命令都是由前面的日志输出所生成,不要无脑填 token
复制
kubeadm join 192.168.64.2:6443 --token jfik3p.hzsojd6lh0ovw6lh \
--discovery-token-ca-cert-hash sha256:95e11cfbaa35beb5ab206c95a848eb955b0d57af0cedddcfc195b879d2552a03
复制
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
如果想重新设置 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
遇到其他问题直接使用搜索引擎