K8s 安装教程 v1.24
第一步前置环境调试
第一步确定自己的内核版本高于3
uname -sr
第二步:使用elrepo源升级内核检查发现当前内核版本是3.10,
使用elrepo源升级内核
配置elrepo源,执行如下命令
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
yum install https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm -y
查看最新版内核
执行如下命令查看最新的内核版本
yum --disablerepo=“*” --enablerepo=“elrepo-kernel” list available
安装最新的内核版本
执行如下命令安装主线版本:
yum --enablerepo=elrepo-kernel install kernel-ml -y
设置系统默认内核
查看系统上的所有内核版本:
grub2-mkconfig -o /boot/grub2/grub.cfg
awk -F’ ‘$1=="menuentry " {print i++ " : " $2}’ /etc/grub2.cfg
设置默认内核为我们刚才升级的内核版本
cp /etc/default/grub /etc/default/grub-bak #备份
grub2-set-default 0 #设置默认内核版本
vi /etc/default/grub
GRUB_DEFAULT=saved修改为GRUB_DEFAULT=0
重新创建内核配置
grub2-mkconfig -o /boot/grub2/grub.cfg
查看默认内核
grubby --default-kernel
grub2-editenv list
更新软件包并重启
yum makecache
reboot
第二步 安装containerd
1.安装依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
2.添加yum源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3.安装containerd
yum install containerd -y
4.生成配置文件
containerd config default > /etc/containerd/config.toml
5.替换 containerd 默认的 sand_box 镜像,编辑 /etc/containerd/config.toml
sandbox_image = “registry.aliyuncs.com/google_containers/pause:3.2”
6.启动服务
systemctl restart containerd && systemctl enable containerd
第三步安装K8S
后面总结部分有快捷安装方式
修改主机名
hostnamectl set-hostname master
#注意每个节点的名字要不一样
总结 :快捷前置环境配置
#!/bin/bash
systemctl stop firewalld &&
systemctl disable firewalld && \
sed -i ‘s/enforcing/disabled/’ /etc/selinux/config && \
setenforce 0 && \
sed -ri ‘s/.swap./#&/’ /etc/fstab && \
swapoff -a && \
cat >> /etc/hosts << EOF
192.168.65.180 k8s-master
192.168.65.38 k8s-node1
192.168.65.195 k8s-node2
EOF
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sysctl --system && \
yum install ntpdate -y &&
ntpdate time.windows.com
上述是所有节点需要做的内容
下面是主节点的内容:
vim /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
安装k8s
yum install kubelet-1.24.3 kubeadm-1.24.3 kubectl-1.24.3 -y
启动k8s
systemctl enable kubelet.service
初始化主节点
kubeadm init --apiserver-advertise-address=192.168.65.180 --kubernetes-version v1.24.3 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16 --image-repository registry.cn-hangzhou.aliyuncs.com/google_containers
kubeadm init成功后出现下面内容
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
(
i
d
?
u
)
:
(id -u):
(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/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 192.168.65.180:6443 --token 98wkpb.mz5qekplhqfbgfo3
–discovery-token-ca-cert-hash sha256:aa426a445ac902c74cf124520571168eb406b19c08ddfdc1bf90fe66918d3fdd
初始化网络
curl -O https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml && kubectl apply -f kube-flannel.yml
sed -i ‘s/quay.io/quay-mirror.qiniu.com/g’ kube-flannel.yml
查看pod情况
kubectl get pod -A
#查看节点情况
kubectl get nodes
常见问题
The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)
在 CentOS 7 上,默认情况下 cgroups 是启用的,但如果进行了某些配置更改,可能会导致其被禁用。
您可以通过编辑 kubelet 的配置文件 /etc/kubernetes/kubelet 来检查 cgroups 的配置:
vi /etc/kubernetes/kubelet
#修改为合适的 cgroup 驱动程序
KUBELET_EXTRA_ARGS=–cgroup-driver=systemd
保存文件后,重新启动 kubelet 服务:
systemctl restart kubelet