CentOS8 安装kubernetes(K8S)

时间:2024-03-12 08:07:24

一、环境准备

1. 关闭交换区

#临时关闭
sudo swapoff -a

#永久关闭
#把/etc/fstab中的swap注释掉
sudo sed -i \'s/.*swap.*/#&/\' /etc/fstab

2. 禁用selinux

#临时关闭
setenforce 0


#永久关闭
sudo sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

3. 关闭防火墙

sudo systemctl stop firewalld.service
sudo systemctl disable firewalld.service

二、安装K8S

1. 配置系统基本安装源

sudo curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo

2. 添加K8S安装源

将如下内容保存到:/etc/yum.repos.d/kubernetes.repo

[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el8-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

3. 安装docker

sudo yum install -y yum-utils device-mapper-persistent-data lvm2 net-tools
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce

为了docker加速pull,可以设置阿里云加速:

sudo mkdir -p /etc/docker
sudo vi /etc/docker/daemon.json

设置为如下内容:

{
"registry-mirrors" : ["https://mj9kvemk.mirror.aliyuncs.com"]
}

4. 安装kubectl、kubelet、kubeadm

安装kubectl、kubelet、kubeadm,设置kubelet开机启动,启动kubelet。

sudo yum install -y kubectl kubelet kubeadm
sudo systemctl enable kubelet
sudo systemctl start kubelet

查看K8S版本

kubeadm version
kubectl version --client
kubelet --version

5. 初始化kubernetes集群

kubeadm init --apiserver-advertise-address=0.0.0.0 --apiserver-cert-extra-sans=127.0.0.1 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.21.1 --pod-network-cidr=10.18.0.0/16

运行后出现问题:

[root@k8s-master01 ~]# kubeadm init --apiserver-advertise-address=0.0.0.0 \
--apiserver-cert-extra-sans=127.0.0.1 \
--image-repository=registry.aliyuncs.com/google_containers \
--ignore-preflight-errors=all \
--kubernetes-version=v1.21.1\
--service-cidr=10.10.0.0/16 \
--pod-network-cidr=10.18.0.0/16
W0702 16:23:11.951553 16395 configset.go:202] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[init] Using Kubernetes version: v1.18.5
[preflight] Running pre-flight checks
[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using \'kubeadm config images pull\'
[WARNING IsDockerSystemdCheck]: detected “cgroupfs” as the Docker cgroup driver. The recommended driver is “systemd”.

出现[WARNING IsDockerSystemdCheck],是由于docker的Cgroup Driver和kubelet的Cgroup Driver不一致导致的,此处选择修改docker的和kubelet一致

查看docker信息:

docker info | grep Cgroup

[root@k8s-master01 ~]# docker info | grep Cgroup
Cgroup Driver: cgroupfs
Cgroup Version: 1

可以看到驱动为Cgroup,需要改为systemd。编辑文件/usr/lib/systemd/system/docker.service

vi /usr/lib/systemd/system/docker.service

在ExecStart命令中添加

--exec-opt native.cgroupdriver=systemd

 然后重启docker,再查看信息,可以看到已经变为systemd了

systemctl daemon-reload
systemctl restart docker
docker info | grep Cgroup

 此时再执行下面的命令进行初始化:

kubeadm init --apiserver-advertise-address=0.0.0.0 \
--apiserver-cert-extra-sans=127.0.0.1 \
--image-repository=registry.aliyuncs.com/google_containers \
--ignore-preflight-errors=all \
--kubernetes-version=v1.21.1 \
--service-cidr=10.10.0.0/16 \
--pod-network-cidr=10.18.0.0/16

 

kubeadm init --apiserver-advertise-address=0.0.0.0 --apiserver-cert-extra-sans=127.0.0.1 --image-repository=registry.aliyuncs.com/google_containers --ignore-preflight-errors=all --kubernetes-version=v1.21.1 --service-cidr=10.10.0.0/16 --pod-network-cidr=10.18.0.0/16

执行提示中的命令:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

再执行:

kubectl get node
kubectl get pod --all-namespaces

 如果node节点为NotReady,因为coredns pod没有启动,缺少网络pod。

6. 安装calico网络

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

运行之后查看信息,节点处于Ready状态。