# Kubernetes部署文档
> 本文档将通过kubeadm+docker部署K8s集群,本次集群使用的容器运行工具为docker,K8s的容器运行工具也可以用除docker之外的、containerd、cio等等,在K8s的1.24版本以后移除了docker-shim,而Docker Engine默认不支持CRI规范,因而二者将无法直接完成整合,因此,Mirantis和Docker联合创建了cri-dockerd项目,用于为Docker Engine提供一个能够支持到CRI规范的垫片,从而能够让Kubernetes基于CRI控制Docker ,所以想在1.24版本及以后的版本中使用docker部署K8s集群,需要安装cri-dockerd
我的博客: https://blog.itwk.cc
## 环境准备
> 准备3台服务器,虚拟机也行,每台主机的CPU要在2核心以上,RAM 2GB以上,否则后续初始化K8s时会不通过。
| 主机名 | IP | CPU | 内存 |
| ------------------ | ------------ | ----- | ---- |
| master.example.com | 172.25.80.10 | 4核心 | 8GB |
| node1.example.com | 172.25.80.11 | 4核心 | 8GB |
| node2.example.com | 172.25.80.12 | 4核心 | 8GB |
## 环境搭建
### 系统环境初始化
**(所有节点同时操作)**
#### 配置主机名
```bash
hostnamectl set-hostname master.example.com && bash
hostnamectl set-hostname node1.example.com && bash
hostnamectl set-hostname node2.example.com && bash
```
#### 配置Hosts文件解析
Edit `/etc/hosts`
```bash
172.25.80.10 master master.example.com
172.25.80.11 node1 node1.example.com
172.25.80.12 node2 node2.example.com
```
#### 关闭`SELINUX`以及swap
```bash
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
```
#### 配置YUM仓库
> 备份原有YUM仓库配置文件并下载docker,K8s的仓库文件
```bash
cd /etc/yum.repos.d/
mkdir back && mv *.repo back
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
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=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
EOF
yum clean all && yum makecache fast
```
#### 调整内核参数
> 将桥接的IPV4流量传递到iptables的链同一节点的不同pod,利用linux bridge进行二层通讯,由于没有原路返回造成pod请求services时的session无法收到返回值而连接超时,所以需要设置让第二层的bridge在转发时也通过第三层的iptables进行通信,并禁止使用swap,只有当系统 OOM 时才允许使用它
```bash
cat <<EOF> /etc/sysctl.d/kubernetes.conf
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
vm.overcommit_memory=1
vm.panic_on_oom=0
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
EOF
modprobe br_netfilter &&
sysctl -p /etc/sysctl.d/kubernetes.conf
```
#### 配置时区,时间同步
**配置时区**
```bash
timedatectl set-timezone Asia/Shanghai
timedatectl set-local-rtc 0
systemctl restart rsyslog
systemctl restart crond
```
**NTP时间同步**(采用chrony)
```bash
yum remove -y ntp ntpdate
yum -y install chrony
```
Edit `/etc/chrony.conf`
注释掉原先配置,并将时间服务器更改为阿里云服务器
https://help.aliyun.com/document_detail/92704.html
```bash
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server ntp.aliyun.com iburst
systemctl enable --now chronyd #启动该服务并设置为开启启动
```
验证 (带* 表示同步成功)
```bash
chronyc sources -v
210 Number of sources = 1
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- xxxx [ yyyy ] +/- zzzz
|| Reachability register (octal) -. | xxxx = adjusted offset,
|| Log2(Polling interval) --. | | yyyy = measured offset,
|| \ | | zzzz = estimated error.
|| | | \
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88 2 6 7 1 -1258us[+28752s] +/- 19ms
```
#### 配置防火墙
> 设置防火墙为 Iptables 并设置空规则
```bash
systemctl stop firewalld && systemctl disable firewalld
yum -y install iptables-services && systemctl start iptables && systemctl enable
iptables -F && iptables-save
```
#### 开启ipvs
> 不开启ipvs将会使用iptables,但是效率低,所以官网推荐需要开通ipvs内核
```bash
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
ipvs_modules="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_nq ip_vs_sed ip_vs_ftp nf_conntrack"
for kernel_module in \${ipvs_modules}; do
/sbin/modinfo -F filename \${kernel_module} > /dev/null 2>&1
if [ $? -eq 0 ]; then
/sbin/modprobe \${kernel_module}
fi
done
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep ip_vs
```
#### 配置持久化
```bash
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 周
MaxRetentionSec=2week
# 不将日志转发到 syslog
ForwardToSyslog=no
EOF
systemctl restart systemd-journald
```
### 安装Docker并配置镜像加速
**(所有节点同时操作)**
> 如需指定版本可以使用 `yum list docker-ce --showduplicates | sort -r` 查看版本信息通过`yum -y install docker-ce-18.0.3`安装
```bash
yum install -y vim yum-utils device-mapper-persistent-data lvm2
yum install -y docker-ce
```
配置Docker镜像加速
```bash
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://vwlrpbcp.mirror.aliyuncs.com"]
}
EOF
```
```bash
systemctl daemon-reload && systemctl restart docker && systemctl enable --now docker
```
安装`cri-docker` 使kubernetes以docker作为运行时 (因为github速度慢,这边把rpm包镜像到自己服务器了)
https://github.com/Mirantis/cri-dockerd/releases/
```bash
yum localinstall -y http://opt.itwk.cc/rpm/el7/cri-dockerd-0.2.6-3.el7.x86_64.rpm
```
修改service文件
Edit `/usr/lib/systemd/system/cri-docker.service`
```bash
ExecStart=/usr/bin/cri-dockerd --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.8 --container-runtime-endpoint
```
```bash
systemctl daemon-reload && systemctl enable --now cri-docker cri-docker.socket
```
### 安装K8S
```bash
yum install -y kubelet-1.25.2 kubeadm-1.25.2 kubectl-1.25.2
systemctl enable kubelet
```
#### 部署Master节点
```bash
kubeadm reset --cri-socket=unix:///var/run/cri-dockerd.sock
```
```bash
kubeadm init --kubernetes-version=v1.25.6 --pod-network-cidr=10.224.0.0/16 --apiserver-advertise-address=172.25.80.10 --cri-socket unix:///var/run/cri-dockerd.sock --image-repository=registry.aliyuncs.com/google_containers
```
部署成功; 按照日志说明进行配置
![](https://cdn.jsdelivr.net/gh/itwk-studio/images/202301240103562.png)
根据日志的提示创建文件夹
```bash
mkdir -p $HOME/.kube
```
根据日志的提示把配置文件复制进刚刚新创建的文件夹里
```bash
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
```
根据日志的提示输入以下命令
```bash
chown $(id -u):$(id -g) $HOME/.kube/config
```
安装 Pod 网络插件(CNI)
```bash
wget https://docs.projectcalico.org/manifests/calico.yaml
kubectl apply -f calico.yaml
```
![](https://cdn.jsdelivr.net/gh/itwk-studio/images/202301240118968.png)
#### Node节点部署
根据提示把node节点加如到master节点中,复制你们各自在日志里的提示,然后分别粘贴在2个node节点上,最后回车即可**(注意要在后面加上`--cri-socket unix:///var/run/cri-dockerd.sock`这一参数,不然会失败)**
如下图所示,最终提示"This node has joined the cluster" 即为成功
![](https://cdn.jsdelivr.net/gh/itwk-studio/images/202301240108859.png)
![](https://cdn.jsdelivr.net/gh/itwk-studio/images/202301240111473.png)