说明:
1.利用Kubeadm的方式安装K8s
2.除关闭防护墙外,其它均可以用于生产环境
3.安装最新K8s版本,如果有更新的版本,就会默认安装最新的版本,截止到目前最新版本是1.30
4.cri-dockerd和runc截止到目前是最新的,如果有最新版本的,根据提供的链接顺藤摸瓜去Github上下载。
注意项:
1.里面的IP需要修改为自己的
2.里面的主机名需要修改为自己的
3.能访问Github,且能下载需要的文件(反正我能裸连,需要第三方网络加速的可以用Watt Toolkit)

文章参考:https://cloud.tencent.com/developer/article/2315878

序号操作系统主机名称配置IP
1Centos7k8s-master4C/4G192.168.2.21
2Centos7k8s-node14C/4G192.168.2.22
3Centos7k8s-node24C/4G192.168.2.23

一,修改主机名和和免密交互

修改主机名称
所有节点都要修改主机名称,只在master做演示

[root@localhost ~]# hostnamectl set-hostname k8s-master && bash

修改hosts文件

[root@k8s-master ~]# cat >> /etc/hosts << EOF
192.168.2.21 k8s-master
192.168.2.22 k8s-node1
192.168.2.23 k8s-node2
EOF

master主机上分发hosts文件到所有主机上

[root@k8s-master ~]# for i in {k8s-master,k8s-node1,k8s-node2}; do scp -r /etc/hosts $i:/etc/; done

测试一下

[root@k8s-master ~]# for i in {k8s-master,k8s-node1,k8s-node2}; do echo "########################$i########################"; ping -c 2 $i; done

生成密钥,复制密钥
所有节点都要生成复制密钥,只在master做演示

[root@k8s-master ~]# ssh-keygen -t rsa
[root@k8s-master ~]# for i in {k8s-master,k8s-node1,k8s-node2}; do ssh-copy-id $i; done

二,内核升级

所有主机都要升级内核,只在master做演示

导入该源的秘钥

[root@k8s-master ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

启用该源仓库

[root@k8s-master ~]# rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm

查看有哪些内核版本可供安装

[root@k8s-master ~]# yum --disablerepo="*" --enablerepo="elrepo-kernel" list available

升级最新版本

[root@k8s-master ~]# yum --enablerepo=elrepo-kernel install kernel-ml -y

设置默认启动,生成cfg文件

[root@k8s-master ~]# grub2-set-default 0
[root@k8s-master ~]# grub2-mkconfig -o /boot/grub2/grub.cfg

重启查看

[root@k8s-master ~]# reboot
[root@k8s-master ~]# uname -r

下面的可执行可不执行
查看所有内核

[root@k8s-master ~]# rpm -qa | grep kernel

卸载旧内核
不要把刚才安装新版本内核的卸载了

[root@k8s-master ~]# yum -y remove [指定的内核名称]

重启查看

[root@k8s-master ~]# reboot
[root@k8s-master ~]# uname -r

三,修改系统全局配置

所有主机都要修改全局配置,只在master做演示

同步时间

[root@k8s-master ~]# yum -y install ntp
[root@k8s-master ~]# systemctl enable --now ntpd
[root@k8s-master ~]# ntpdate -u cn.pool.ntp.org
[root@k8s-master ~]# hwclock --systohc
[root@k8s-master ~]# timedatectl set-timezone Asia/Shanghai

关闭防火墙

[root@k8s-master ~]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
[root@k8s-master ~]# setenforce 0
[root@k8s-master ~]# systemctl stop firewalld
[root@k8s-master ~]# systemctl disable firewalld

关闭swap分区

[root@k8s-master ~]# swapoff -a
[root@k8s-master ~]# sed -i '/swap/s/^/#/' /etc/fstab

配置资源限制

[root@k8s-master ~]# ulimit -SHn 65535
[root@k8s-master ~]# cat >> /etc/security/limits.conf << EOF
 *           soft   nofile       65536
 *           hard   nofile       131072
 *           soft   nproc        65535
 *           hard   nproc        655350
 *           soft   memlock      unlimited
 *           hard   memlock      unlimited
EOF

主机将桥接的IPv4流量传递到iptables的链

[root@k8s-master ~]# cat << EOF >> /etc/sysctl.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
[root@k8s-master ~]# modprobe br_netfilter
[root@k8s-master ~]# modprobe overlay
[root@k8s-master ~]# sysctl -p

安装模块

[root@k8s-master ~]# tee /etc/modules-load.d/ipvs.conf << 'EOF'
ip_vs
ip_vs_lc
ip_vs_wlc
ip_vs_rr
ip_vs_wrr
ip_vs_lblc
ip_vs_lblcr
ip_vs_dh
ip_vs_sh
ip_vs_fo
ip_vs_nq
ip_vs_sed
ip_vs_ftp
nf_conntrack
ip_tables
ip_set
ipt_rpfilter
ipt_REJECT
ipip
EOF

开启k8s集群必须的内核参数

[root@k8s-master ~]# cat << EOF > /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
user.max_user_namespaces=28633
fs.may_detach_mounts = 1
net.ipv4.conf.all.route_localnet = 1
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
fs.file-max=52706963
fs.nr_open=52706963
net.netfilter.nf_conntrack_max=2310720
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl =15
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_orphans = 327680
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.ip_conntrack_max = 65536
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_timestamps = 0
net.core.somaxconn = 16384
EOF
[root@k8s-master ~]# sysctl --system

重启查看是否生效

[root@k8s-master ~]# reboot
[root@k8s-master ~]# lsmod |grep --color=auto -e ip_vs -e nf_conntrack

四,安装Dokcer

所有节点都要安装Docker,只在master做演示

配置阿里和Docker官方yum源

[root@k8s-master ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@k8s-master ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@k8s-master ~]# yum makecache -y
[root@k8s-master ~]# yum install -y yum-utils
[root@k8s-master ~]# yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo

安装Docker

[root@k8s-master ~]# yum -y install docker-ce docker-ce-cli containerd.io

配置Docker

[root@k8s-master ~]# cat > /etc/docker/daemon.json<< EOF
{
"registry-mirrors": [
"https://nyakyfun.mirror.aliyuncs.com",
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"https://docker.mirrors.ustc.edu.cn"
],
"exec-opts": ["native.cgroupdriver=systemd"],
"max-concurrent-downloads":10,
"max-concurrent-uploads":5,
"log-driver":"json-file",
"log-opts": {
 "max-size": "300m",
 "max-file": "2"
},
"live-restore": true,
"storage-driver":"overlay2"
}
EOF

自启动Docker

[root@k8s-master ~]# systemctl enable --now docker

查看Docker信息

[root@k8s-master ~]# docker info

五,安装cri-dockerd,runc

所有节点都要安装,只在master做演示

自行到Github下载cri-dockerd,上传到自己服务器
链接:https://github.com/Mirantis/cri-dockerd/releases/tag/v0.3.13
image.png自行到Github下载runc,上传到自己服务器
链接:https://github.com/opencontainers/runc/releases/tag/v1.2.0-rc.1
image.png
上传服务器后,查看确认

[root@k8s-master ~]# ll
总用量 21448
-rw-r--r-- 1 root root 11178376 425 13:01 cri-dockerd-0.3.13-3.el7.x86_64.rpm
-rw-r--r-- 1 root root 10779104 425 13:00 runc.amd64

安装cri-dockerd

[root@k8s-master ~]# rpm -ivh cri-dockerd-0.3.13-3.el7.x86_64.rpm

重载系统进程,自启动

[root@k8s-master ~]# systemctl daemon-reload 
[root@k8s-master ~]# systemctl enable cri-docker.socket cri-docker --now

安装runc

[root@k8s-master ~]# install -m 755 runc.amd64 /usr/local/bin/runc
[root@k8s-master ~]# runc -v

修改cri-dockerd配置文件

[root@k8s-master ~]# vim /usr/lib/systemd/system/cri-docker.service
#修改第十行,为下面的配置
ExecStart=/usr/bin/cri-dockerd --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.7

重启Docker组件

[root@k8s-master ~]# systemctl daemon-reload && systemctl restart docker cri-docker.socket cri-docker

检查Docker组件状态

[root@k8s-master ~]# systemctl status docker cir-docker.socket cri-docker

六,安装Kubeadm

所有节点都要安装,只在master做演示

配置yum文件
官方和阿里的,选择一个就行

#官方,速度慢
[root@k8s-master ~]# cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.30/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.30/rpm/repodata/repomd.xml.key
exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni
EOF
#阿里,速度快
[root@k8s-master ~]# cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=1
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
        http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
exclude=kubelet kubeadm kubectl
EOF

执行安装
如果使用官方源,安装过程中会报错,多次尝试或者换阿里源

[root@k8s-master ~]# yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

设置自启动

[root@k8s-master ~]# systemctl enable  kubelet  --now

查看版本信息

[root@k8s-master ~]# kubectl version
Client Version: v1.30.0
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.30.0

七,Master主机初始化

仅master主机执行初始化

初始化

#这里需要把hostname和IP修改为自己的
[root@k8s-master ~]# kubeadm init --node-name=k8s-master --image-repository=registry.aliyuncs.com/google_containers --cri-socket=unix:///var/run/cri-dockerd.sock --apiserver-advertise-address=192.168.2.21 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12

执行成功
image.png
按照提示执行

[root@k8s-master ~]# mkdir -p $HOME/.kube
[root@k8s-master ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@k8s-master ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
[root@k8s-master ~]# export KUBECONFIG=/etc/kubernetes/admin.conf

永久生效

[root@k8s-master ~]# echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >>  ~/.bash_profile
[root@k8s-master ~]# source ~/.bash_profile 
[root@k8s-master ~]# echo $KUBECONFIG

八,安装flannel

仅master主机执行

自行到Github下载flannel.yml,上传到自己服务器
链接:https://github.com/flannel-io/flannel/releases/tag/v0.25.1
image.png
上传服务器后,查看确认

[root@k8s-master ~]# ll
总用量 21456
-rw-r--r-- 1 root root 11178376 425 13:01 cri-dockerd-0.3.13-3.el7.x86_64.rpm
-rw-r--r-- 1 root root     4468 425 13:38 kube-flannel.yml
-rw-r--r-- 1 root root 10779104 425 13:00 runc.amd64

创建配置flannel

[root@k8s-master ~]# kubectl apply -f kube-flannel.yml

查看pod状态,等全部Running
这里需要等会才会全部Running

[root@k8s-master ~]# kubectl get pod -A

将admin.conf拷贝到所有node节点上

[root@k8s-master ~]# scp -r /etc/kubernetes/admin.conf 192.168.2.21:/etc/kubernetes/
[root@k8s-master ~]# scp -r /etc/kubernetes/admin.conf 192.168.2.22:/etc/kubernetes/

九,node节点加入master

所有node节点执行,master不需要,仅在node1节点上做演示

设置环境变量

[root@k8s-node1 ~]# echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >>  ~/.bash_profile
[root@k8s-node1 ~]# source ~/.bash_profile

执行Join指令加入master
每个人的token都不一样 ,自己的token在master主机初始化成功界面
image.png

[root@k8s-node1 ~]# kubeadm join 192.168.2.21:6443 --token mlbgiz.hj74tozue0dmk38h \
>         --discovery-token-ca-cert-hash sha256:2a3b13ed2836fc6b2dc228e42a18176641790bfaf73e9d56ce8f955d41bb618a 

报错就在后面加入
--cri-socket unix:///var/run/cri-dockerd.sock

[root@k8s-node1 ~]# kubeadm join 192.168.2.21:6443 --token mlbgiz.hj74tozue0dmk38h         --discovery-token-ca-cert-hash sha256:2a3b13ed2836fc6b2dc228e42a18176641790bfaf73e9d56ce8f955d41bb618a --cri-socket unix:///var/run/cri-dockerd.sock

十,检查状态

在master上,检查状态

检查状态
会出现Pending和Init的状态,是正常现象
多等待一会 再次查看即可

[root@k8s-master ~]# kubectl get pod -A
NAMESPACE      NAME                                 READY   STATUS              RESTARTS   AGE
kube-flannel   kube-flannel-ds-2gk9r                1/1     Running             0          13m
kube-flannel   kube-flannel-ds-fjbfb                0/1     Init:0/2            0          40s
kube-flannel   kube-flannel-ds-kkd2c                0/1     Init:0/2            0          77s
kube-system    coredns-7b5944fdcf-g8lfj             1/1     Running             0          26m
kube-system    coredns-7b5944fdcf-k4zc4             1/1     Running             0          26m
kube-system    etcd-k8s-master                      1/1     Running             0          26m
kube-system    kube-apiserver-k8s-master            1/1     Running             0          26m
kube-system    kube-controller-manager-k8s-master   1/1     Running             0          26m
kube-system    kube-proxy-6wnsf                     1/1     Running             0          26m
kube-system    kube-proxy-s29lg                     0/1     ContainerCreating   0          40s
kube-system    kube-proxy-vl64f                     1/1     Running             0          77s
kube-system    kube-scheduler-k8s-master            1/1     Running             0          26m

再次查看,全部Running,完成

[root@k8s-master ~]# kubectl get pod -A
NAMESPACE      NAME                                 READY   STATUS    RESTARTS   AGE
kube-flannel   kube-flannel-ds-2gk9r                1/1     Running   0          17m
kube-flannel   kube-flannel-ds-fjbfb                1/1     Running   0          4m11s
kube-flannel   kube-flannel-ds-kkd2c                1/1     Running   0          4m48s
kube-system    coredns-7b5944fdcf-g8lfj             1/1     Running   0          29m
kube-system    coredns-7b5944fdcf-k4zc4             1/1     Running   0          29m
kube-system    etcd-k8s-master                      1/1     Running   0          30m
kube-system    kube-apiserver-k8s-master            1/1     Running   0          30m
kube-system    kube-controller-manager-k8s-master   1/1     Running   0          30m
kube-system    kube-proxy-6wnsf                     1/1     Running   0          29m
kube-system    kube-proxy-s29lg                     1/1     Running   0          4m11s
kube-system    kube-proxy-vl64f                     1/1     Running   0          4m48s
kube-system    kube-scheduler-k8s-master            1/1     Running   0          30m

查看node状态,全部Ready,完成

[root@k8s-master ~]# kubectl get node
NAME         STATUS   ROLES           AGE   VERSION
k8s-master   Ready    control-plane   98m   v1.30.0
k8s-node1    Ready    <none>          73m   v1.30.0
k8s-node2    Ready    <none>          72m   v1.30.0
Logo

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

更多推荐