【Kubernetes部署篇】Kubeadm方式搭建K8s集群 1.26.0版本

2023-10-30

一、集群规划及架构

官方文档:

二进制下载地址

环境规划:

  • pod网段:10.244.0.0/16
  • service网段:10.10.0.0/16
  • 注意: pod和service网段不可冲突,如果冲突会导致K8S集群安装失败。
  • 容器运行时本次使用containerd。
主机名 IP地址 操作系统
master-1 16.32.15.200 CentOS7.8
node-1 16.32.15.201 CentOS7.8
node-2 16.32.15.202 CentOS7.8

二、系统初始化准备(所有节点同步操作)

1、关闭防火墙

systemctl disable firewalld --now
setenforce 0
sed  -i -r 's/SELINUX=[ep].*/SELINUX=disabled/g' /etc/selinux/config

2、配置域名解析

cat  >> /etc/hosts << EOF
16.32.15.200 master-1
16.32.15.201 node-1
16.32.15.202 node-2
EOF

在指定主机上面修改主机名

hostnamectl set-hostname master-1 && bash
hostnamectl set-hostname node-1 && bash
hostnamectl set-hostname node-2 && bash

3、配置服务器时间保持一致

yum -y install ntpdate
ntpdate ntp1.aliyun.com

添加定时同步 每天凌晨1点自动同步时间

echo "0 1 * * * ntpdate ntp1.aliyun.com" >> /var/spool/cron/root
crontab -l

4、禁用swap交换分区(kubernetes强制要求禁用)

swapoff --all

禁止开机自启动swap交换分区

sed -i -r '/swap/ s/^/#/' /etc/fstab

5、修改Linux内核参数,添加网桥过滤器和地址转发功能

cat >> /etc/sysctl.d/kubernetes.conf <<EOF
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/kubernetes.conf

加载网桥过滤器模块

modprobe br_netfilter
lsmod | grep br_netfilter # 验证是否生效

6、配置ipvs功能

在kubernetes中Service有两种代理模型,一种是基于iptables的,一种是基于ipvs,两者对比ipvs的性能要高,如果想要使用ipvs模型,需要手动载入ipvs模块

yum -y install ipset ipvsadm

cat > /etc/sysconfig/modules/ipvs.modules <<EOF
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4  
EOF

chmod +x /etc/sysconfig/modules/ipvs.modules 
# 执行脚本
/etc/sysconfig/modules/ipvs.modules

# 验证ipvs模块
lsmod | grep -e ip_vs -e nf_conntrack_ipv4

7、安装Docker容器组件

注意:Docker用来下载镜像、Dockerfile构建镜像等操作,和Containerd不冲突。

curl -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
yum makecache

# yum-utils软件用于提供yum-config-manager程序
yum install -y yum-utils

# 使用yum-config-manager创建docker阿里存储库
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

yum install docker-ce-20.10.6 docker-ce-cli-20.10.6 -y

Docker配置加速源:

mkdir /etc/docker
cat <<EOF > /etc/docker/daemon.json
{
  "registry-mirrors": ["https://aoewjvel.mirror.aliyuncs.com"]
}
EOF

# 启动docker并设置开机自启
systemctl enable docker --now
systemctl status docker

8、重启服务器 可略过

reboot

三、安装并配置Containerd容器运行时

三台服务器同时操作

1、安装containerd

yum -y install containerd.io-1.6.6

2、生成containerd配置文件,并修改配置文件

mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml

修改配置文件,主要修改以下配置:

vim /etc/containerd/config.toml

SystemdCgroup = true
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.7"

3、启动containerd

systemctl enable containerd  --now

4、添加 crictl 工具的配置文件

crictl 是一个命令行工具,用于与 CRI(Container Runtime Interface)兼容的容器运行时进行交互。在 Kubernetes 中,Kubelet 使用 CRI 接口与容器运行时进行通信,而 crictl 工具则可以用于直接与容器运行时进行交互。

cat > /etc/crictl.yaml <<EOF
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
EOF
  • runtime-endpoint:指定容器运行时的地址
  • image-endpoint:指定镜像仓库的地址,即containerd所使用的镜像仓库的地址。

5、配置containerd镜像加速器

指定加速器目录信息:

vim /etc/containerd/config.toml

config_path = "/etc/containerd/certs.d"

配置加速信息:

mkdir /etc/containerd/certs.d/docker.io/ -p
vim /etc/containerd/certs.d/docker.io/hosts.toml

[host."https://vh3bm52y.mirror.aliyuncs.com",host."https://registry.docker-cn.com"]
  capabilities = ["pull"]

重启containerd

systemctl restart containerd

四、安装kubeadm(所有节点同步操作)

1、配置国内yum源,一键安装 kubeadm、kubelet、kubectl

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
EOF

yum install -y kubelet-1.26.0 kubeadm-1.26.0 kubectl-1.26.0

2、kubeadm将使用kubelet服务以容器方式部署kubernetes的主要服务,所以需要先启动kubelet服务

systemctl enable kubelet.service --now

五、初始化集群

在master-1主机上进行操作

1、配置容器运行时

crictl config runtime-endpoint unix:///run/containerd/containerd.sock

2、生成初始化默认配置文件

kubeadm config print init-defaults > kubeadm.yaml

我们根据自己需求进行修改默认配置文件,我主要更改了一下配置如下:

  • advertiseAddress:更改为master的IP地址
  • criSocket:指定容器运行时
  • imageRepository:配置国内加速源地址
  • podSubnet:pod网段地址
  • serviceSubnet:services网段地址
  • 末尾添加了指定使用ipvs,开启systemd
  • nodeRegistration.name:改为当前主机名称

最终初始化配置文件如下:

apiVersion: kubeadm.k8s.io/v1beta3
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 16.32.15.200
  bindPort: 6443
nodeRegistration:
  criSocket: unix:///run/containerd/containerd.sock
  imagePullPolicy: IfNotPresent
  name: master-1
  taints: null
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta3
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns: {}
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: 1.26.0
networking:
  dnsDomain: cluster.local
  podSubnet: 10.244.0.0/16
  serviceSubnet: 10.96.0.0/12
scheduler: {}
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
cgroupDriver: systemd

3、进行初始化

kubeadm init --config=kubeadm.yaml --ignore-preflight-errors=SystemVerification

初始化成功后输出如下内容:

[init] Using Kubernetes version: v1.26.0
[preflight] Running pre-flight checks
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local node] and IPs [10.96.0.1 16.32.15.200]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [localhost node] and IPs [16.32.15.200 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost node] and IPs [16.32.15.200 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 12.003782 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node node as control-plane by adding the labels: [node-role.kubernetes.io/control-plane node.kubernetes.io/exclude-from-external-load-balancers]
[mark-control-plane] Marking the node node as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]
[bootstrap-token] Using token: abcdef.0123456789abcdef
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate 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 16.32.15.200:6443 --token abcdef.0123456789abcdef \
	--discovery-token-ca-cert-hash sha256:02dc7cc7702f9814d01f9a4d5957da3053e74adcc2f583415e516a4b81fb37bc

4、配置kubectl的配置文件config,相当于对kubectl进行授权,这样kubectl命令可以使用这个证书对k8s集群进行管理

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

验证使用可以使用 kubectl 命令

kubectl get nodes

5、查看镜像,咱们使用containerd拉取的镜像,使用docker images命令是查看不到镜像的。需要使用ctr命令,如下:

ctr -n k8s.io images list
crictl images
  • -n 指明名称空间

六、Node节点添加到集群

在两台node节点进行操

1、赋值初始化输出的token信息,在两台node节点执行

kubeadm join 16.32.15.200:6443 --token abcdef.0123456789abcdef \
	--discovery-token-ca-cert-hash sha256:6839bcc102c7ab089554871dd1a8f3d4261e1482ff13eafdf32fc092ebaf9f7e

如果忘记可以使用以下命令创建并查看token:

kubeadm token create --print-join-command

成功加入到集群如下图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WcReqrzj-1682478646677)(D:\MD归档文档\IMG\image-20230425151533713.png)]

2、给两台node节点打上标签

master-1主机上执行

kubectl label nodes node-1 node-role.kubernetes.io/work=work
kubectl label nodes node-2 node-role.kubernetes.io/work=work

七、安装网络组件Calico

Calico在线文档地址:

Calico.yaml下载地址:

1、上传calico.yaml文件到服务器中,下面提供calico.yaml文件内容:

在master主机执行

kubectl apply -f  calico.yaml

2、查看集群状态 && 查看自带Pod状态

kubectl get nodes

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wScfqriS-1682478646680)(D:\MD归档文档\IMG\image-20230426104418539.png)]

3、查看组件状态 是否为 Running状态 如下图:

kubectl get pods -n kube-system

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ugY0UxIX-1682478646681)(D:\MD归档文档\IMG\image-20230426104559318.png)]

八、测试CoreDNS解析可用性

1、下载busybox:1.28镜像

ctr -n k8s.io images pull docker.io/library/busybox:1.28

2、测试coredns

kubectl run busybox --image busybox:1.28 --restart=Never --rm -it busybox -- sh

If you don't see a command prompt, try pressing enter.

/ # nslookup kubernetes.default.svc.cluster.local
Server:    10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local

Name:      kubernetes.default.svc.cluster.local
Address 1: 10.96.0.1 kubernetes.default.svc.cluster.local
  • 注意:busybox要用指定的1.28版本,不能用最新版本,最新版本,nslookup会解析不到dns和ip

九、拓展

1、ctr和crictl命令具体区别

ctr和crictl都是用于管理容器运行时的命令行工具,但它们的具体区别如下:

  • ctr是由Docker开发的,而crictl是由Kubernetes开发的。

  • ctr支持多种容器运行时,包括Docker、containerd、CRI-O等,而crictl只支持CRI(Container Runtime Interface)兼容的容器运行时,如CRI-O、containerd等。

  • ctr提供了更多的功能,如镜像管理、网络管理、卷管理等,而crictl只提供了基本的容器管理功能,如容器创建、删除、启动、停止等。

  • ctr的命令更加直观和易用,而crictl的命令更加符合Kubernetes的设计理念和规范。

综上所述,ctr和crictl都是优秀的容器管理工具,选择哪一个取决于具体的使用场景和需求。

2、calico多网卡情况配置

可能会有一些情况,一台服务器上面有多个网卡,但是只有其中一个网卡可以通外网,这种情况下需要添加一些配置,来指定网卡,如下命令是指定ens33网卡,当然也可以使用正则表达式来表示。

 - name: IP_AUTODETECTION_METHOD
   value: "interface=ens33"

在这里插入图片描述
重新执行生效:

kubectl apply -f calico.yaml
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【Kubernetes部署篇】Kubeadm方式搭建K8s集群 1.26.0版本 的相关文章

随机推荐

  • keil如何擦除已经下载的程序_Keil系列教程01_Keil介绍、下载、安装与注册

    说明 本文原创作者 strongerHuang 首发于微信公众号 嵌入式专栏 同时也更新在我的个人网站 EmbeddedDevelop 标签 Keil MDK ARM Vision 该教程基于MDK ARM 大部分内容也适用于Keil其它3
  • 函数指针和函数指针数组

    文章目录 一 函数指针 二 函数指针数组 三 应用 两个数的加减乘除运算 感谢阅读 如有错误请批评指正 一 函数指针 数据在内存中都有自己的地址 地址需要指针变量来保存 我们常见的指针有char int double float 函数也有自
  • QWebEngine应用---基于QWebChannel实现网页与qt层交互

    Qt提供了QWebChannel实现和网页的通信 我们直接拿github上一个能直接运行的demo来做说明 demo是基于Widget 且页面是自己实现的页面 接着会介绍基于QML实现且页面是第三方网站如何使用的 QWebChannel用法
  • 模型只要「变大」就能直通AGI?马库斯再次炮轰:三个危机已经显现!

    视学算法报道 编辑 LRS 导读 模型只管变大 剩下的交给天意 今年5月 DeepMind发布了一个多模态人工智能系统Gato 仅靠一套模型参数即可同时执行600多种不同的任务 一时引起行业内对通用人工智能 AGI 的热议 DeepMind
  • 零基础小白入手hadoop学习路线和教程分享

    这里写图片描述Hadoop技术本身并不是新技术 而是互联网时代成就了它 互联网产生了大量的数据 传统的服务器解决方案成本太高 Hadoop分布式处理技术可以解决这个问题 随着BAT等知名公司的成功案例 越来越多互联网公司也都开始使用 Had
  • 数据结构与算法 ---- 图的广度优先搜索(BFS)和深度优先搜索(DFS)

    1 前言 和树的遍历类似 图的遍历也是从图中某点出发 然后按照某种方法对图中所有顶点进行访问 且仅访问一次 但是图的遍历相对树而言要更为复杂 因为图中的任意顶点都可能与其他顶点相邻 所以在图的遍历中必须记录已被访问的顶点 避免重复访问 根据
  • 在瑞芯微SDK源码上构建自己的356x编译固件

    整体配置 路径 sdk源码 device rockchip rk356x 如下图 有个BoardConfig rk3568 evb1 ddr4 v10 mk是rk3568平台下的基础配置文件 这个文件可以配置uboot kernel内核默认
  • 向上转型与向下转型(超详细)

    本文利用代码例子解释向上转型与向下转型 文末有举例整合原代码 首先 我们要知道 转型发生在继承后 也就是父类子类存在的前提下 其次 我们要清楚 向下转型的前提是已经发生了向上转型 向下转型是再次转回来而已 最后 我们要明白 向上转型和向下转
  • layuiAdmin框架iframe版本:如何调用菜单折叠事件

    原文连接 https www codeleading com article 10252080191 转载 有一个功能需要加 菜单折起来之后如何调用折叠事件 menuFirstList on click li function e 如果是合
  • 在线日志的简单分析

    在线日志分析简单介绍 在互联网项目中 经常需要记录和查看日志 日志中包含了程序在遇到异常所记录的各种信息 同时也会记录程序每次请求的pv adpv等信息 包含了用户的IP地址 请求url 请求页面 城市类别以及系统开发者在系统运行过程中想打
  • Extreme AP7632配置文档

    设备类型如下图 就是这款 因为是英文的配置界面 所以弄起来有点麻烦 详细信息如下图 配置方式下载附件 https download csdn net download Poison 1212 12413234 spm 1001 2014 3
  • 深度学习-人工神经网络网络

    神经元 受生物学的启发 人工神经网络是生物神经网络的一种模拟和近似 它从结构 实现机理和功能上模拟生物神经网络 传统的生物神经元模型由树突 细胞核 细胞体 突触和神经末梢组成 如图所示 如图下图所示 神经元的输入xi对应生物神经元的树突 输
  • AndroidUI绘制流程实例2(继承ViewGroup)

    案例仅供参考学习 MainActivity package com example test import android app Activity import android os Bundle import android view
  • python: GUI using tkinter

    StudentUI py 读文件类 date 2023 06 24 edit Geovin Du geovindu 涂聚文 ide PyCharm 2023 1 python 11 import datetime import sys im
  • 自定义view及优化

    参考链接 自定义View 有这一篇就够了 简书 1 2 自定义 View 菜鸟教程 自定义view 当Android系统内置的View无法实现我们的需求时 我们可以根据要求制订自己的View 自定义view大约需要这几步 继承View类 至
  • vue中视频插件vue-video-player使用

    视频插件 vue video player github地址 https github com surmon china vue video player 安装 npm install vue video player save 引入 全局
  • Application进行详解(unity)

    Application进行详解 unity中的 介绍 在Unity引擎中 Application类是一个非常重要的类 它可以提供一些有用的方法 用于管理Unity应用程序的运行时行为 通过使用Application类 您可以获取有关Unit
  • 剑指offer62. 圆圈中最后剩下的数字(Josephuse约瑟夫环问题) P300

    剑指offer62 圆圈中最后剩下的数字 Josephuse约瑟夫环问题 P300 题目 0 1 n 1这n个数字排成一个圆圈 从数字0开始每次从这个圆圈里删除第m个数字 求出这个圆圈里剩下的最后一个数字 方法1 循环链表 C 官网说lis
  • TextMeshProUGUI的基本使用方法(资源创建、材质、表情等)

    TextMeshPro 以下均简称为TMP 是使用在Unity中的一种强大的文本插件 恩 感觉跟PS差不多了 在unity5 6时代好像就说要用这个插件来代替unity中自带的Text和TextMesh组件 但现在都unity2017 3了
  • 【Kubernetes部署篇】Kubeadm方式搭建K8s集群 1.26.0版本

    文章目录 一 集群规划及架构 二 系统初始化准备 所有节点同步操作 三 安装并配置Containerd容器运行时 四 安装kubeadm 所有节点同步操作 五 初始化集群 六 Node节点添加到集群 七 安装网络组件Calico 八 测试C