Centos7.9 安装k8s-1.23.4

箴水技术分享 . 发布于 2023-05-25 20:17:13 . 阅读 703

1.1 实验拓扑图,安装三台虚拟机,配置2c/4g,1台master,2台worker.

1.2安装前准备

安装k8s之前,需要配置yum源,关闭selinux和swap及防火墙等工作,操作如下:

步骤1:操作系统版本是Centos7.9,配置/etc/hosts

[root@localhost ~]# hostnamectl set-hostname master01

[root@localhost ~]# su -

上一次登录:一 37 04:43:47 EST 2022192.168.56.1pts/0

[root@master01 ~]# hostname

master01

[root@master01 ~]# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.56.120 master01

192.168.56.121 worker01

192.168.56.122 worker02

步骤2:在所有节点上配置防火墙和关闭selinux。

[root@master01 ~]# systemctl stop firewalld

[root@master01 ~]# systemctl disable firewalld
临时关闭:
[root@master01 ~]# setenforce 0

[root@master01 ~]# getenforce

Permissive
永久关闭:
vi /etc/selinux/config

修改配置如下:

SELINUX=disabled

步骤3:在所有节点上关闭swap,并注释掉/etc/fstab里swap相关条目。

[root@master01 ~]# swapon -s

Filename Type Size Used Priority

/dev/dm-1 partition 4063228 0 -2

[root@master01 ~]# swapoff /dev/dm-1

[root@master01 ~]# sed -i '/swap/d' /etc/fstab

[root@master01 ~]# cat /etc/fstab

#

# /etc/fstab

# Created by anaconda on Wed Aug 11 20:55:21 2021

#

# Accessible filesystems, by reference, are maintained under '/dev/disk'

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

/dev/mapper/centos-root / xfs defaults 0 0

UUID=fe9751cd-5999-4ed4-ad40-38ffce1565ac /boot

步骤4:在所有节点上配置好yum源(请提前安装好wget,再执行下面的操作

由于国外的yum源有的时候访问不了,我们一般采用阿里云镜像源

centos镜像源:

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

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
>docker的镜像源

step 1: 安装必要的一些系统工具

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

step 2: 添加软件源信息

sudo yum-config-manager --add-repo https:*//mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo*

step 3

sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo

step 4: 更新并安装Docker-CE

sudo yum makecache fast

sudo yum -y install docker-ce

step 4: 开启Docker服务

sudo systemctl enable docker

sudo systemctl start docker

k8s镜像源

cat <<EOF > /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=1

gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg

EOF

注意:

 官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,您可以通过以下方式开启。同理可以开启各种测试版本等。

vim /etc/yum.repos.d/docker-ce.repo

将[docker-ce-test]下方的enabled=0修改为enabled=1

安装指定版本的Docker-CE:

- Step 1: 查找Docker-CE的版本:

yum list docker-ce.x86_64 --showduplicates | sort -r

Loading mirror speeds from cached hostfile

Loaded plugins: branch, fastestmirror, langpacks

docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable

docker-ce.x86_64 17.03.1.ce-1.el7.centos @docker-ce-stable

docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable

Available Packages

- Step2: 安装指定版本的Docker-CE: (VERSION例如上面的17.03.0.ce.1-1.el7.centos)

sudo yum -y install docker-ce-[VERSION]

步骤6:在所有节点设置内核参数。

**cat <<EOF >  /etc/sysctl.d/k8s.conf**

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

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

**net.ipv4.ip_forward = 1**

**EOF**

sysctl -p /etc/sysctl.d/k8s.conf

注意若有报错:

sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-ip6tables: No such file or directory

sysctl: cannot stat /proc/sys/net/bridge/bridge-nf-call-iptables: No such file or directory

解决办法:

**执行modprobe br_netfilter **

步骤7:在所有节点上安装软件包。

1.版本查看

[root@master01 yum.repos.d]# yum list kubelet --showduplicates | sort -r

Loading mirror speeds from cached hostfile

Loaded plugins: fastestmirror

kubelet.x86_64 1.9.9-0 kubernetes

kubelet.x86_64 1.9.8-0 kubernetes

kubelet.x86_64 1.9.7-0 kubernetes

kubelet.x86_64 1.9.6-0 kubernetes

kubelet.x86_64 1.9.5-0 kubernetes

kubelet.x86_64 1.9.4-0 kubernetes

……

安装

yum install -y kubelet-1.23.4 kubeadm-1.23.4 kubectl-1.23.4 **--disableexcludes=kubernetes**

#安装包说明

#kubelet 运行在集群所有节点上,用于启动Pod和容器等对象的工具

#kubeadm 用于初始化集群,启动集群的命令工具

#kubectl 用于和集群通信的命令行,通过kubectl可以部署和管理应用,查看各种资源,创建、删除和更新各种组件

若报错: 执行yum clean all 再重新执行即可

[root@master01 ~]# yum install -y kubelet-1.23.4 kubeadm-1.23.4 kubectl-1.23.4 **--disableexcludes=kubernetes**

Error: Package: kubelet-1.23.4-0.x86_64 (kubernetes)

Requires: conntrack

You could try using --skip-broken to work around the problem

You could try running: rpm -Va --nofiles --nodigest

解决方法如下:

yum clean all 再执行安装。
``
步骤8:在**所有节点**上启动kubelet,并设置开机自动启动。

[root@master01 ~]# systemctl restart kubelet

[root@master01 ~]# systemctl enable kubelet

Created symlink from /etc/systemd/system/multi-user.target.wants/kubelet.service to /usr/lib/systemd/system/kubelet.service.

步骤9:安装master

下面的操作,仅在master01主机上运行,由于我是用的虚拟机,双网卡,我需要指定nat网络的ip进行部署,指定api地址参数:apiserver-advertise-address

kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version=v1.23.4 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.56.120

…..输出…..

tificate and key

[addons] Applied essential addon: CoreDNS

[addons] Applied essential addon: kube-proxy

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/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.56.120:6443 --token 13n18n.x04e82eif1ykgvsq \

--discovery-token-ca-cert-hash sha256:d826d6caf8eaef6c6bbdd69bbb7d520682a8562d0045d9acdfe18c7b9042c9ef

…输出….

步骤2:复制kubeconfig文件。

[root@master01 Packages]# mkdir -p $HOME/.kube

[root@master01 Packages]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

[root@master01 Packages]# sudo chown (id -u):(id -g) $HOME/.kube/config

[root@master01 Packages]#

上面提示中用于添加节点的命令如下:

kubeadm join 192.168.56.120:6443 --token 13n18n.x04e82eif1ykgvsq

--discovery-token-ca-cert-hash sha256:d826d6caf8eaef6c6bbdd69bbb7d520682a8562d0045d9acdfe18c7b9042c9ef

>如果忘记了,可以使用以下命令获取:

[root@master01 Packages]# kubeadm token create --print-join-command

kubeadm join 192.168.56.120:6443 --token radhjt.pqr8x2qzzfvn4dog --discovery-token-ca-cert-hash sha256:d826d6caf8eaef6c6bbdd69bbb7d520682a8562d0045d9acdfe18c7b9042c9ef

**步骤3: 配置node加入集群**

在worker01
和 worker02的进行如下操作:

>worker01主机

[root@worker01 ~]# kubeadm join 192.168.56.120:6443 --token radhjt.pqr8x2qzzfvn4dog --discovery-token-ca-cert-hash sha256:d826d6caf8eaef6c6bbdd69bbb7d520682a8562d0045d9acdfe18c7b9042c9ef

[preflight] Running pre-flight checks

[preflight] Reading configuration from the cluster...

[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'

[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"

[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"

[kubelet-start] Starting the kubelet

[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:

  • Certificate signing request was sent to apiserver and a response was received.
  • The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

[root@worker01 ~]#

>woker02 主机

[root@worker02 ~]# kubeadm join 192.168.56.120:6443 --token radhjt.pqr8x2qzzfvn4dog --discovery-token-ca-cert-hash sha256:d826d6caf8eaef6c6bbdd69bbb7d520682a8562d0045d9acdfe18c7b9042c9ef

[preflight] Running pre-flight checks

[preflight] Reading configuration from the cluster...

[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -o yaml'

[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"

[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"

[kubelet-start] Starting the kubelet

[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...

This node has joined the cluster:

Certificate signing request was sent to apiserver and a response was received.

步骤4:查看k8s节点

[root@master01 ~]# kubectl get nodes

NAME STATUS ROLES AGE VERSION

master01 NotReady control-plane,master 4h54m v1.23.4

worker01 NotReady <none> 4m49s v1.23.4

worker02 NotReady <none> 13s v1.23.4

>注:notready 是因为没有装网络插件,读者可自行选择安装flannel或者calico。