kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具。

1、准备环境

规划部署:

ip角色系统主机名cpumem
192.168.132.128mastercentos7.9k8s12c4g
192.168.132.129nodecentos7.9k8s22c4g
192.168.132.130nodecentos7.9k8s32c4g

###系统环境优化配置

CentOS 7.x系统自带的3.10.x内核存在一些bug,导致运行的docker,kubernetes不稳定
#获取源

rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm

#安装内核,装完成后检查 /boot/grub2/grub.cfg中对应内核menuentry中是否包含 initrd16 配置,如果没有,再安装一次!耗时可能会有点久

yum --enablerepo=elrepo-kernel install -y kernel-lt 
#查看系统的全部内核
rpm -qa | grep kernel
#kernel-3.10.0-1160.el7.x86_64
#kernel-lt-5.4.271-1.el7.elrepo.x86_64
#kernel-tools-libs-3.10.0-1160.el7.x86_64
#kernel-tools-3.10.0-1160.el7.x86_64
#设置开机从新内核启动
grub2-set-default 'CentoS Linux(5.4.271-1.el7.elrepo.x86_64) 7 (Core)'
reboot
#查看正在使用的内核
uname -r

2、系统配置&&优化参数
 

###重要:首先将各主机ip配置为静态、主机名修改、时间同步 等

#修改仓库源、安装必要工具
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git
#关闭firewalld配置iptabes
systemctl stop firewalld && systemctl disable firewalld
yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && iptables-save > /etc/sysconfig/iptables
#关闭swap:
swapoff -a && sed -ri 's/.*swap.*/#&/' /etc/fstab
#关闭selinux:
setenforce 0 && sed -i 's/enforcing/disabled/' /etc/selinux/config

#添加主机名与IP对应关系: ####根据自身环境修改

cat >> /etc/hosts <<EOF
192.168.132.128 k8s1
192.168.132.129 k8s2
192.168.132.130 k8s3
EOF

#开启一些k8s集群中必须的内核参数,所有节点配置k8s内核:

cat > kubernetes.conf <<EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
vm.swappiness=0 #禁止使用swap空间,只有当系统OOM时才允许使用它
vm.overcommit_memory=1 #不检查物理内存是否够用
vm.panic_on_oom=0 #开启OOM
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
net.ipv6.conf.all.disable_ipv6=1
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
cp kubernetes.conf /etc/sysctl.d/kubernetes.conf
sysctl -p /etc/sysctl.d/kubernetes.conf

#limit优化

ulimit -SHn 65535

cat <<EOF >> /etc/security/limits.conf
soft nofile 655360
hard nofile 131072
soft nproc 655350
hard nproc 655350
soft memlock unlimited
hard memlock unlimited
EOF

#设置系统时区为 中国/上海

timedatectl set-timezone Asia/Shanghai
#将当前的 UTC 时间写入硬件时钟
timedatectl set-local-rtc 0
#重启依赖于系统时间的服务
systemctl restart rsyslog
systemctl restart crond
#关闭postfix
systemctl stop postfix && systemctl disable postfix

#持久化保存日志的目录

mkdir /var/log/journal
mkdir /etc/systemd/journald.conf.d
cat > /etc/systemd/journald.conf.d/99-prophet.conf <<EOF
[Journal]
#持久化保存到磁盘
Storage=persistent

#压缩历史日志
Compress=yes

SyncIntervalSec=5m
RateLimitInterval=30s
RateLimitBurst=1000

#最大占用空间10G
SystemMaxUse=10G

#单日志文件最大 200M
SystemMaxFileSize=200M

#日志保存时间2周
MaxRetentionsSec=2week

#不将日志转发到syslog
ForwardToSyslog=no
EOF

systemctl restart systemd-journald

###kube-prox开启ipvs的前置条件

modprobe br_netfilter
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack

3、安装docker

#.安装yum-utils device-mapper-persistent-data lvm2软件包

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

#添加阿里源docker-ce镜像仓库

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

#查看可用docker版本

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

注意:docker和k8s版本有指定要求  可前往github查看对应版本  https://github.com/kubernetes/kubernetes/tree/master/CHANGELOG

###安装指定版本,如:docker-ce-20.10.24-3.el7

yum install docker-ce-20.10.24-3.el7 -y

#启动

systemctl start docker
systemctl enable docker
docker info

#创建 /etc/docker目录

mkdir /etc/docker

#配置daemon
cat > /etc/docker/daemon.json <<EOF
{
    "exec-opts":["native.cgroupdriver=systemd"],
    "log-driver":"json-file",
    "log-opts":{
        "max-size":"100m"
    }
}
EOF

mkdir -p /etc/systemd/system/docker.service.d

#重启docker服务
systemctl daemon-reload && systemctl restart docker && systemctl enable docker

4、配置cri-docker

###配置cri-docker使kubernetes以docker作为运行时
自 1.24 版起,Dockershim 已从 Kubernetes 项目中移除。因为历史问题docker却不支持kubernetes主推的CRI(容器运行时接口)标准,所以docker不能再作为kubernetes的容器运行时了,即从kubernetesv1.24开始不再使用docker了。
但是如果想继续使用docker的话,可以在kubelet和docker之间加上一个中间层cri-docker。cri-docker是一个支持CRI标准的shim(垫片)。一头通过CRI跟kubelet交互,另一头跟docker api交互,从而间接的实现了kubernetes以docker作为容器运行时。但是这种架构缺点也很明显,调用链更长,效率更低。

#到下面的链接下载最新版cri-docker: https://github.com/Mirantis/cri-dockerd/tags

#先在master上解压出cri-docker,然后拷贝到其他节点
tar -zxf cri-dockerd-0.3.11.amd64.tgz
cp cri-dockerd/cri-dockerd /usr/bin/
scp /usr/bin/cri-dockerd root@k8s2:/usr/bin/
scp /usr/bin/cri-dockerd root@k8s3:/usr/bin/


#创建cri-docker启动文件,然后拷贝到其他节点

vim /usr/lib/systemd/system/cri-docker.service
[Unit]
Description=CRI Interface for Docker Application Container Engine
Documentation=https://docs.mirantis.com
After=network-online.target firewalld.service docker.service
Wants=network-online.target
Requires=cri-docker.socket

[Service]
Type=notify
ExecStart=/usr/bin/cri-dockerd --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.9
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

StartLimitBurst=3

StartLimitInterval=60s

LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

TasksMax=infinity
Delegate=yes
KillMode=process

[Install]
WantedBy=multi-user.target
vim /usr/lib/systemd/system/cri-docker.socket
[Unit]
Description=CRI Docker Socket for the API
PartOf=cri-docker.service

[Socket]
ListenStream=%t/cri-dockerd.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker

[Install]
WantedBy=sockets.target
scp /usr/lib/systemd/system/cri-docker.service /usr/lib/systemd/system/cri-docker.socket root@k8s2:/usr/lib/systemd/system/

scp /usr/lib/systemd/system/cri-docker.service /usr/lib/systemd/system/cri-docker.socket root@k8s3:/usr/lib/systemd/system/

#启动cri-docker并设置开机自动启动

systemctl daemon-reload ; systemctl enable cri-docker --now
#查看状态
systemctl is-active cri-docker

5、安装kubeadm

#配置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=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF


#安装kubeadm kubelet kubectl指定版本

yum list kubeadm --showduplicates | sort -r

yum install -y kubelet-1.28.2-0 kubeadm-1.28.2-0 kubectl-1.28.2-0


#设置kubelet开机自启

systemctl enable --now kubelet.service

6、初始化master&&安装网络插件&&加入集群

#在master节点执行

kubeadm init --image-repository registry.aliyuncs.com/google_containers --apiserver-advertise-address=192.168.132.128 --kubernetes-version=v1.28.2 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16 --upload-certs --cri-socket /var/run/cri-dockerd.sock


#按照提示创建kubeconfig文件

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


然后即可根据提示加入其他node,在node节点上执行下面语句,注意后面加上 --cri-socket /var/run/cri-dockerd.sock 参数

#初始化之后查看node节点状态为 NotReady,安装flannel插件后与node节点的网络才会正常

kubectl get node

#下载flannel的yml文件并创建,这里flannel.yml文件里镜像拉取地址为外网地址,构建pod时会拉取超时,这里单独下载了镜像上传到了阿里云ACR上,需要修改yml文件里的image地址。

wget https://github.com/flannel-io/flannel/releases/download/v0.24.3/kube-flannel.yml
sed -i '/ image:/s#docker.io/flannel#registry.cn-hangzhou.aliyuncs.com/luoyuxin#' kube-flannel.yml

#构建flannel

kubectl create -f kube-flannel.yml
kubectl get pod -n kube-flannel

# 再次查看节点状态全部变为 Ready

kubectl get nodes

###重新初始化

kubeadm reset --cri-socket /var/run/cri-dockerd.sock

rm -rf .kube/
sudo mkdir ~/.kube
sudo cp /etc/kubernetes/admin.conf ~/.kube/
cd ~/.kube
mv admin.conf config
service kubelet restart


 

Logo

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

更多推荐