基于Ubuntu 22.04
除非特别标注,其余步骤master和worker节点都需要操作

在这里插入图片描述

准备工作

修改Ubuntu镜像

https://developer.aliyun.com/mirror/ubuntu?spm=a2c6h.13651102.0.0.2e611b11BvTeZL

备份默认 /etc/apt/sources.list
修改 /etc/apt/sources.list

修改后更新

sudo apt update && sudo apt upgrade

主机名解析

主机名成解析 编辑三台服务器的/etc/hosts文件,添加下面内容
根据自己情况配置,这里我有一台master节点对应 192.168.90.100,node1节点对应 192.168.90.106,node2节点对应 192.168.90.107

192.168.90.100 master
192.168.90.106 node1
192.168.90.107 node2
角色IP地址组件
master192.168.90.100containerd,kubectl,kubeadm,kubelet
node1192.168.90.106containerd,kubectl,kubeadm,kubelet
node2192.168.90.107containerd,kubectl,kubeadm,kubelet

同步时间

启动chronyd服务

[root@master ~]# systemctl start chronyd
[root@master ~]# systemctl enable chronyd
[root@master ~]# date

配置主机名

hostnamectl # 查询主机名
hostnamectl --static set-hostname node1 # 查询主机名为对应节点名

禁用selinux

selinux是linux系统下的一个安全服务,如果不关闭它,在安装集群中会产生各种各样的奇葩问题

Ubuntu 下默认没有,不用改

编辑 /etc/selinux/config 文件,修改SELINUX的值为disable
注意修改完毕之后需要重启linux服务

SELINUX=disabled

修改linux的内核参数

修改linux的内核采纳数,添加网桥过滤和地址转发功能

  • 创建/etc/sysctl.d/kubernetes.conf文件,添加如下配置:
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
  • 重新加载配置
[root@master ~]# sysctl -p
  • 加载网桥过滤模块
[root@master ~]# modprobe br_netfilter
  • 查看网桥过滤模块是否加载成功
[root@master ~]# lsmod | grep br_netfilter
  • 如果未生效查询结果为0,需重启服务器生效(查询结果为0)
cat /proc/sys/net/ipv4/ip_forward
0

禁用iptable和firewalld服务

kubernetes 在运行的中会产生大量的iptables规则,为了不让系统规则跟它们混淆,直接关闭系统的规则

  • 关闭firewalld服务
[root@master ~]# systemctl stop firewalld
[root@master ~]# systemctl disable firewalld
  • 关闭iptables服务
[root@master ~]# systemctl stop iptables
[root@master ~]# systemctl disable iptables

关闭swap

swap分区指的是虚拟内存分区,它的作用是物理内存使用完,之后将磁盘空间虚拟成内存来使用,启用swap设备会对系统的性能产生非常负面的影响,因此kubernetes要求每个节点都要禁用swap设备,但是如果因为某些原因确实不能关闭swap分区,就需要在集群安装过程中通过明确的参数进行配置说明

swapoff -a # 临时禁用
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久禁用

或者忽略

  • 创建文件 /etc/systemd/system/kubelet.service.d/20-allow-swap.conf
[Service]
Environment="KUBELET_EXTRA_ARGS=--fail-swap-on=false"
  • 执行
sudo systemctl daemon-reload
  • 查看关闭结果

free -h 查看内存

blkid命令 ,查找TYPE=”swap”行以确定交换分区

lsblk命令来搜索和识别[SWAP]分区

配置ipvs功能

在Kubernetes中Service有两种带来模型,一种是基于iptables的,一种是基于ipvs的两者比较的话,ipvs的性能明显要高一些,但是如果要使用它,需要手动载入ipvs模块


# 1.安装ipset和ipvsadm
[root@master ~]# apt install ipvsadm ipset sysstat conntrack -y
# 2.添加需要加载的模块写入脚本文件
[root@master ~]# cat >> /etc/modules-load.d/ipvs.conf <<EOF 
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack
ip_tables
ip_set
xt_set
ipt_set
ipt_rpfilter
ipt_REJECT
ipip
EOF

# 3.重启服务
[root@master ~]# systemctl restart systemd-modules-load.service

# 4.查看对应的模块是否加载成功
[root@master ~]# lsmod | grep -e ip_vs -e nf_conntrack
ip_vs_sh               16384  0
ip_vs_wrr              16384  0
ip_vs_rr               16384  0
ip_vs                 155648  6 ip_vs_rr,ip_vs_sh,ip_vs_wrr
nf_conntrack          139264  1 ip_vs
nf_defrag_ipv6         24576  2 nf_conntrack,ip_vs
nf_defrag_ipv4         16384  1 nf_conntrack
libcrc32c              16384  4 nf_conntrack,btrfs,raid456,ip_vs

安装

安装containerd(使用containerd,和下面docker 二选一)

如果已经安装docker 会卸载 containerd.io docker-ce ,谨慎安装

apt install containerd
systemctl start containerd
mkdir -p /etc/containerd/
containerd config default > /etc/containerd/config.toml
sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
systemctl restart containerd

安装cri-docker(使用docker,和上面containerd 二选一)

首先安装docker

curl -sSL https://get.daocloud.io/docker | sh

配置docker

{
  "exec-opts": ["native.cgroupdriver=cgroupfs"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "800m",
    "max-file": "1" 
  }
}

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

[root@vms41 ~]# tar zxf cri-dockerd-0.2.1.amd64.tgz 
[root@vms41 ~]# cp cri-dockerd/cri-dockerd /usr/bin/
[root@vms41 ~]# scp /usr/bin/cri-dockerd 192.168.90.106:/usr/bin/

这里/usr/bin/cri-dockerd一定要加上参数

–pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.8

用来指定所用的pause镜像是哪个,否则默认拉取k8s.gcr.io/pause:3.6,会导致安装失败。
文件内容:
root@master:~# cat /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 --container-runtime-endpoint fd:// --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.8
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always


# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3


# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s


# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity


# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity
Delegate=yes
KillMode=process


[Install]
WantedBy=multi-user.target
  • 创建启动文件
    文件内容
    root@master:~# cat /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.socket /usr/lib/systemd/system/cri-docker.service 192.168.90.106:/usr/lib/systemd/system/
  • 启动cri-docker并设置开机自动启动
systemctl daemon-reload ; systemctl enable cri-docker --now
  • 查看启动状态
systemctl is-active cri-docker

配置Kubernetes镜像

参考https://developer.aliyun.com/mirror/kubernetes?spm=a2c6h.13651102.0.0.2e611b11BvTeZL

apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - 
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl

kubeadm初始化集群(master操作)

以下二选一
忽略 Swap缓存,使用 cri-dockerd

kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version=v1.24.2 --pod-network-cidr=10.244.0.0/16 --cri-socket /var/run/cri-dockerd.sock

忽略 Swap缓存,使用 containerd

kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version=v1.24.2 --pod-network-cidr=10.244.0.0/16

安装 flannel(master操作)

kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml

把worker加入集群,使用 cri-dockerd (worker操作)

kubeadm join 192.168.90.100:6443  --cri-socket /var/run/cri-dockerd.sock --token 1g8rs2.t1ge709xkhfwx2rc \
        --discovery-token-ca-cert-hash sha256:8e1815d422526af9f7d138f80a44a6b9af1c364c61d71f70d9a512847e548850 

worker 节点使用 kubectl

  • 创建目录
mkdir -p $HOME/.kube
  • 同时将 admin.conf 传输给工作节点
 scp /etc/kubernetes/admin.conf root@192.168.90.106:$HOME/.kube/config
  • 赋权
sudo chown $(id -u):$(id -g) $HOME/.kube/config
  • 查看节点
kubectl get nodes
Logo

开源、云原生的融合云平台

更多推荐