Centos7.9使用kubeadm部署K8S 1.27.6集群环境(内网通过代理部署)

本文主要是介绍Centos7.9使用kubeadm部署K8S 1.27.6集群环境(内网通过代理部署),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Centos7.9使用kubeadm部署K8S 1.27.6集群环境(内网通过代理部署)

在内网借助代理服务器,使用kubeadm部署一个k8s集群,单master+2worker节点,K8S版本为1.7.6,使用containerd作为容器运行时。

1. 环境信息

  • 操作系统:CentOS 7.9.2009
  • 内存: 8GB
  • CPU: 4
  • 网络: 节点通过代理进行访问。
hostnameip备注
k8s-master110.210.10.201master
k8s-node110.210.10.202worker
k8s-node210.210.10.203worker

2. 准备工作

2.1 配置网络代理

# 配置全局代理
export proxy=http://10.0.112.18:808
export all_proxy=$proxy
export no_proxy=localhost,10.0.0.0/8,192.168.0.0/16,172.16.0.0/12# 持久化配置可以将上面配置写/etc/profile文件

在k8s初始化的时候,容器拉取镜像需要单独配置代理:

# docker代理
[root@k8s-master1 ~]# mkdir -p /etc/systemd/system/docker.service.d
[root@k8s-master1 ~]# cat /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://10.0.112.18:808"
Environment="HTTPS_PROXY=http://10.0.112.18:808"
Environment="NO_RPOXY=localhost,127.0.0.1"# containerd代理
[root@k8s-master1 ~]# mkdir -p /etc/systemd/system/containerd.service.d
[root@k8s-master1 ~]# cat /etc/systemd/system/containerd.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://10.0.112.18:808"
Environment="HTTPS_PROXY=http://10.0.112.18:808"
Environment="NO_RPOXY=localhost,127.0.0.1"

说明:

K8S 1.24版本后启用了docker-shim容器运行时,本地安装的k8s版本为1.27.6,选用containd作为容器运行,可以只配置containerd的代理。由于每个节点都要运行容器,所以代理需要在每个节点都需要配置。

2.2 linux基础配置

以下操作在所有节点执行:

# 配置yum源
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo# 安装常用软件
yum install wget vim-enhanced net-tools# 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld# 关闭 swap
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab# 关闭 selinux
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

设置hosts:

# 设置主机名
hostnamectl set-hostname k8s-master1  # k8s-node1 / k8s-node2
hostname# 配置 hosts
cat >> /etc/hosts << EOF
10.210.10.201 k8s-master1
10.210.10.202 k8s-node1
10.210.10.203 k8s-node2
EOF

由于环境在内网,没有ntp服务器。这里手动修改时间,也可以配置内部的ntp服务器。

# 设置时区
timedatectl set-timezone Asia/Shanghai# 将系统时间改为utc时间(如果需要)。编辑下面文件,写入ZONE="Etc/UTC"
vi /etc/sysconfig/clock# 建立软连接
ln -sf /usr/share/zoneinfo/UTC /etc/localtime# 设置系统时间为当前时间
date -s "2024-06-20 19:04:00"# 同步硬件时间
hwclock --systohc

配置内核参数:

cat >/etc/modules-load.d/k8s.conf <<EOF
overlay
br_netfilter
EOFmodprobe overlay
modprobe br_netfilter# 将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1
EOFsysctl --system  # 生效# 通过运行以下指令确认 br_netfilter 和 overlay 模块被加载
lsmod | egrep 'overlay|br_netfilter'
# 确认sysctl配置中被设置为 1
sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward

2.3 安装容器运行时

# 添加镜像源
curl https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo# 查看docker-ce的版本列表
yum list docker-ce --showduplicates | sort -r# 删除 docker(如果有的话)
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engin# 安装必备工具
yum install -y yum-utils device-mapper-persistent-data lvm2# 安装 docker和containerd
yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin# 生成并修改配置文件
cp /etc/containerd/config.toml /etc/containerd/config.toml.bak
containerd config default > /etc/containerd/config.toml
#修改
sed -i "s#registry.k8s.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g" /etc/containerd/config.toml
sed -i "s#SystemdCgroup = false#SystemdCgroup = true#g" /etc/containerd/config.toml# 将 containerd 加入开机自启
sudo systemctl enable --now containerd.service
# 启动 docker
sudo systemctl start docker.service
# 将 docker 加入开机自启
sudo systemctl enable docker.service
sudo systemctl enable docker.socket
sudo systemctl list-unit-files | grep docker# 设置Docker加速
mkdir -p /etc/docker
cat > /etc/docker/daemon.json << EOF
{"registry-mirrors": ["https://wnsrsn9i.mirror.aliyuncs.com"],"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF# 重启配置生效
systemctl daemon-reload
systemctl restart docker
docker info
...Registry Mirrors:https://wnsrsn9i.mirror.aliyuncs.com/
...

2.4 安装 kubeadm、kubelet 和 kubectl

# 添加镜像源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[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# 查看支持的版本
yum list kubelet --showduplicates | sort -r# 安装
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0# 配置kubelet服务自启动
systemctl enable kubelet

3. 部署k8s集群

初始化master:

# 提前拉取镜像kubeadm config images pull  --image-repository=registry.aliyuncs.com/google_containers# 运行初始化命令,apiserver地址为master地址。pod网络设置为210.244.0.0/16
kubeadm init --image-repository=registry.aliyuncs.com/google_containers --kubernetes-version=v1.27.6 --apiserver-advertise-address=10.210.10.201 --pod-network-cidr=210.244.0.0/16
...
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxyYour Kubernetes control-plane has initialized successfully!To start using your cluster, you need to run the following as a regular user:mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/configYou 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 10.210.10.201:6443 --token 3ravyv.7g7re8vu4xpuozmr \--discovery-token-ca-cert-hash sha256:cc5e30bae2b696a9d9d4535a31ed7b0dc53abe905b2ca7234336e7090f5f317a
...# 初始化成功后,按照提示执行如下命令
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config# 查看节点列表,此时节点状态为NotReady
kubectl get nodes

初始化worker:

kubeadm join 10.210.10.201:6443 --token 3ravyv.7g7re8vu4xpuozmr \--discovery-token-ca-cert-hash sha256:cc5e30bae2b696a9d9d4535a31ed7b0dc53abe905b2ca7234336e7090f5f317a

master部署网络插件:

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl get pods -n kube-system  # 查看运行状态

如果无法下载,手动创建kube-flannel.yml,内容如下:

---
kind: Namespace
apiVersion: v1
metadata:name: kube-flannellabels:k8s-app: flannelpod-security.kubernetes.io/enforce: privileged
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:labels:k8s-app: flannelname: flannel
rules:
- apiGroups:- ""resources:- podsverbs:- get
- apiGroups:- ""resources:- nodesverbs:- get- list- watch
- apiGroups:- ""resources:- nodes/statusverbs:- patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:labels:k8s-app: flannelname: flannel
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: flannel
subjects:
- kind: ServiceAccountname: flannelnamespace: kube-flannel
---
apiVersion: v1
kind: ServiceAccount
metadata:labels:k8s-app: flannelname: flannelnamespace: kube-flannel
---
kind: ConfigMap
apiVersion: v1
metadata:name: kube-flannel-cfgnamespace: kube-flannellabels:tier: nodek8s-app: flannelapp: flannel
data:cni-conf.json: |{"name": "cbr0","cniVersion": "0.3.1","plugins": [{"type": "flannel","delegate": {"hairpinMode": true,"isDefaultGateway": true}},{"type": "portmap","capabilities": {"portMappings": true}}]}net-conf.json: |{"Network": "10.244.0.0/16","EnableNFTables": false,"Backend": {"Type": "vxlan"}}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:name: kube-flannel-dsnamespace: kube-flannellabels:tier: nodeapp: flannelk8s-app: flannel
spec:selector:matchLabels:app: flanneltemplate:metadata:labels:tier: nodeapp: flannelspec:affinity:nodeAffinity:requiredDuringSchedulingIgnoredDuringExecution:nodeSelectorTerms:- matchExpressions:- key: kubernetes.io/osoperator: Invalues:- linuxhostNetwork: truepriorityClassName: system-node-criticaltolerations:- operator: Existseffect: NoScheduleserviceAccountName: flannelinitContainers:- name: install-cni-pluginimage: docker.io/flannel/flannel-cni-plugin:v1.4.1-flannel1command:- cpargs:- -f- /flannel- /opt/cni/bin/flannelvolumeMounts:- name: cni-pluginmountPath: /opt/cni/bin- name: install-cniimage: docker.io/flannel/flannel:v0.25.4command:- cpargs:- -f- /etc/kube-flannel/cni-conf.json- /etc/cni/net.d/10-flannel.conflistvolumeMounts:- name: cnimountPath: /etc/cni/net.d- name: flannel-cfgmountPath: /etc/kube-flannel/containers:- name: kube-flannelimage: docker.io/flannel/flannel:v0.25.4command:- /opt/bin/flanneldargs:- --ip-masq- --kube-subnet-mgrresources:requests:cpu: "100m"memory: "50Mi"securityContext:privileged: falsecapabilities:add: ["NET_ADMIN", "NET_RAW"]env:- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: POD_NAMESPACEvalueFrom:fieldRef:fieldPath: metadata.namespace- name: EVENT_QUEUE_DEPTHvalue: "5000"volumeMounts:- name: runmountPath: /run/flannel- name: flannel-cfgmountPath: /etc/kube-flannel/- name: xtables-lockmountPath: /run/xtables.lockvolumes:- name: runhostPath:path: /run/flannel- name: cni-pluginhostPath:path: /opt/cni/bin- name: cnihostPath:path: /etc/cni/net.d- name: flannel-cfgconfigMap:name: kube-flannel-cfg- name: xtables-lockhostPath:path: /run/xtables.locktype: FileOrCreate

说明:

flannel.yaml中的"Network"指定的ip地址与kubeadm init初始化时指定的pod网络保持一致。

部署flannel会拉取两个镜像,国内网络环境有时候无法顺利拉取,可以从其他地方获取后离线导入当前环境:

[root@k8s-master1 ~]# docker images
REPOSITORY                              TAG               IMAGE ID       CREATED        SIZE
flannel/flannel                         v0.25.4           e6c43605b714   18 hours ago   81MB
flannel/flannel-cni-plugin              v1.4.1-flannel1   1e3c860c213d   7 weeks ago    10.3MB

说明:

本次安装容器同时安装了docker和containerd,但是k8s底层对接的是containerd,kubeadm初始化时会走containerd拉取镜像,所以网络代理要配置到containerd一侧。另外containerd底层有命名空间的概念,如果容器镜像采用离线导入的方式,需要指定命名空间,参考命令ctr -n=k8s.io images import xxx.tar

这篇关于Centos7.9使用kubeadm部署K8S 1.27.6集群环境(内网通过代理部署)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1082634

相关文章

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

python使用try函数详解

《python使用try函数详解》Pythontry语句用于异常处理,支持捕获特定/多种异常、else/final子句确保资源释放,结合with语句自动清理,可自定义异常及嵌套结构,灵活应对错误场景... 目录try 函数的基本语法捕获特定异常捕获多个异常使用 else 子句使用 finally 子句捕获所

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

C#中lock关键字的使用小结

《C#中lock关键字的使用小结》在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时,其他线程无法访问同一实例的该代码块,下面就来介绍一下lock关键字的使用... 目录使用方式工作原理注意事项示例代码为什么不能lock值类型在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时