一、系统设置

1、关闭swap

临时关闭swap

swapoff -a

永久关闭

注释掉 /etc/fstab 中的下面配置

#/dev/mapper/centos-swap swap swap defaults 0 0
2、 关闭SELinux

kubelet不支持SELinux, 这里需要将SELinux设置为permissive模式

setenforce 0 

sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

主节点执行

#主节点 [root@master k8s]# hostnamectl set-hostname k8s-master

从节点执行

#从节点* [root@master k8s]# hostnamectl set-hostname k8s-node1

修改hosts文件,追加域名映射

[root@master k8s]# cat >>/etc/hosts<<EOF

172.16.1.182 k8s-master

172.16.1.183 k8s-node1
3、关闭防火墙
systemctl disable firewalld

systemctl stop firewalld
4、配置sysctl

创建文件/etc/sysctl.d/k8s.conf, 文件内容如下

net.bridge.bridge-nf-call-ip6tables = 1

net.bridge.bridge-nf-call-iptables = 1

net.ipv4.ip_forward = 1

执行

modprobe  br_netfilter       #加载模块

sysctl -p /etc/sysctl.d/k8s.conf
5、配置安装源为阿里
5.1 配置yum安装源

备份

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

下载阿里源

​
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

​
5.2 配置k8s源

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=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
5.3 重建yum缓存
yum clean all

yum makecache fast

yum -y update

二、安装docker

1、安装docker

卸载旧版本

yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine

安装docker

​
yum install -y yum-utils device-mapper-persistent-data lvm2 

# 设置镜像仓库
yum-config-manager  --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 

yum install -y docker-ce docker-ce-cli containerd.io

​
2、docker配置

创建文件/etc/docker/daemon.json,写入配置 mkdir /etc/docker/ vim /etc/docker/daemon.json

{ 
"registry-mirrors":["https://1nj0zren.mirror.aliyuncs.com","https://docker.mirrors.ustc.edu.cn","http://f1361db2.m.daocloud.io","https://registry.docker-cn.com"] 
}
3、重启docker
mkdir -p /etc/systemd/system/docker.service.d

systemctl daemon-reload

systemctl restart docker

三、集群安装

1、安装kubeadm, kubelet和kubectl
yum install -y kubelet-1.28.0 kubeadm-1.28.0 kubectl-1.28.0 --disableexcludes=kubernetes

systemctl enable --now kubelet && systemctl start kubelet

此处我复制了从节点k8s-node1,目的是从节点不想安装k8s相关包了。

2、使用kubeadm创建集群

只在master节点执行,此处注意 172.16.1.182 是你安装k8s master节点的ip地址

kubeadm init --apiserver-advertise-address=172.16.1.182 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.28.0 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16

此处会报错,

W0423 17:12:43.250196 1811 checks.go:835] detected that the sandbox image "registry.k8s.io/pause:3.6" of the container runtime is inconsistent with that used by kubeadm. It is recommended that using "registry.aliyuncs.com/google_containers/pause:3.9" as the CRI sandbox image.

解决方案:

sudo containerd config default | sudo tee /etc/containerd/config.toml

sudo sed -i 's#SystemdCgroup = false#SystemdCgroup = true#g' /etc/containerd/config.toml

sudo sed -i 's#sandbox_image = "registry.k8s.io/pause:3.6"#sandbox_image ="registry.aliyuncs.com/google_containers/pause:3.9"#g' /etc/containerd/config.toml

sudo systemctl restart containerd

安装成功后,有如下打印

Your Kubernetes control-plane has initialized successfully!

3、用户设置权限(root用户也需要执行)

# master节点执行:

mkdir -p $HOME/.kube

cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

chown $(id -u):$(id -g) $HOME/.kube/config
4、应用flannel网络
​
sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

​

查看flannal是否安装成功

sudo kubectl -n kube-system get po -l app=flannel -o wide
5、节点加入

按照在master节点上构建集群后的打印,执行节点加入集群操作

kubeadm join 172.16.1.182:6443 --token g51929.7332yvwdmplcsdam --discovery-token-ca-cert-hash sha256:0af39b256e3c547eada14b72849f1aedbbb1737819d30c20341c225294bbdd81


四、参考资料

1.Centos7安装K8S集群环境-腾讯云开发者社区-腾讯云

2.https://blog.51cto.com/u_1264026/7670666

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐