K8S环境搭建

标签: Kubernetes Kubernetes | 发表时间:2025-04-25 01:32 | 作者:
出处:https://www.ascotbe.com/

安装环境说明

硬件要求

内存:3GB或更多RAM

CPU: 2核CPU或更多CPU

硬盘: 20GB或更多

本次环境说明:

**操作系统:**CentOS 10

master: 192.168.23.133

node01: 192.168.23.135

node02: 192.168.23.134

环境准备

关闭防火墙和selinux

关闭防火墙

systemctl stop firewalld && systemctl disable firewalld && iptables -F     

关闭selinux

sed -i 's/enforcing/disabled/' /etc/selinux/config && setenforce 0     

关闭swap分区

临时关闭

swapoff -a     

永久关闭swap

永久关闭是需要关机重启生效的,所以还是要把临时关闭命令也执行下

sed -ri 's/.*swap.*/#&/' /etc/fstab     

修改hosts文件

设置主机名

不设置也可以,但是要保证主机名不相同,并且修改的hosts内容要与之对应

master上

hostnamectl set-hostname master.local      

node01

hostnamectl set-hostname node01.local     

node02

hostnamectl set-hostname node02.local     

修改本地hosts文件

vi /etc/hosts添加如下内容

192.168.23.133 master.local     
192.168.23.135 node01.local
192.168.23.134 node02.local

修改内核参数

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
EOF

sysctl --system

加载ip_vs内核模块

如果kube-proxy 模式为ip_vs则必须加载,本文采用iptables

modprobe ip_vs     
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack_ipv4
modprobe br_netfilter
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 > /proc/sys/net/ipv4/ip_forward

设置下次开机自动加载

cat > /etc/modules-load.d/ip_vs.conf << EOF      
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack_ipv4
EOF

安装docker

配置yum源

这里使用阿里云的源

yum install wget -y      
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

安装docker

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

编辑docker配置文件

编辑 /etc/docker/daemon.json

mkdir /etc/docker/      
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://gqs7xcfd.mirror.aliyuncs.com","https://hub-mirror.c.163.com"],
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
EOF

启动docker服务

systemctl daemon-reload && systemctl enable docker && systemctl start docker     

安装Kubernetes

配置yum源

这里使用阿里云的源

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=1
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 kubelet --showduplicates     

指定版本安装

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

正常安装,需要添加 --nogpgcheck来跳过证书验证

yum install -y kubelet kubeadm kubectl --nogpgcheck     

设置开机自启

systemctl enable kubelet     

配置CRI

修复 containerd CRI 问题

确保配置已正确覆盖:

containerd config default > /etc/containerd/config.toml     
vim /etc/containerd/config.toml

确保下面两个内容修改完成

[plugins."io.containerd.grpc.v1.cri"]     
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true

重启 containerd:

systemctl daemon-reexec     
systemctl restart containerd
systemctl enable containerd

验证 CRI 是否启用:

ctr plugins list | grep cri     

输出应包含:

io.containerd.grpc.v1.cri         -          ok     

确保 containerd 启动成功并监听 CRI socket

确认是否有以下文件存在:

ls /run/containerd/containerd.sock     

如果存在,尝试运行:

crictl --runtime-endpoint=unix:///run/containerd/containerd.sock ps -a     

看到类似结果则说明 containerd 已正常运行。

部署Kubernetes Master节点

master节点初始化

如果之前初始化失败了,需要重新初始化需要先运行下面命令

kubeadm reset -f      
rm -rf ~/.kube
rm -rf /etc/cni/net.d /opt/cni

如果第一次运行就运行下面命令

kubeadm init \     
--kubernetes-version=1.28.2 \
--apiserver-advertise-address=192.168.23.133 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.244.0.0/16 \
--image-repository=registry.aliyuncs.com/google_containers \
--ignore-preflight-errors=Mem
参数项 类型 含义与作用 示例
--kubernetes-version 字符串 指定 Kubernetes 版本。用于控制初始化的组件版本,必须与镜像源中存在的版本一致。 1.28.2
--apiserver-advertise-address IP 地址 指定 Kubernetes Master 对外通信的 IP 地址。 0.0.0.0 表示监听所有可用 IP(用于单节点或调试场景)。 192.168.23.1330.0.0.0
--service-cidr CIDR 地址段 集群内部 Service 对象所使用的虚拟 IP 地址段,不应与宿主机或 Pod 网络冲突。 10.96.0.0/16(默认)
--pod-network-cidr CIDR 地址段 分配给每个 Pod 的 IP 地址段,需要与网络插件(如 Calico、Flannel)配置一致。 10.244.0.0/16
--image-repository 镜像仓库地址 指定拉取 Kubernetes 镜像的仓库, 国内推荐使用 Aliyun 镜像加速,避免 gcr.io 被墙。 registry.aliyuncs.com/google_containers
--ignore-preflight-errors=Mem 字符串列表 忽略预检中对内存的最低限制要求。Kubeadm 要求 ≥1700MB 内存,若低于此值需要显式忽略。 Mem

接下来就是等待拉取镜像了,最终返回完整结果

root@master:/home/ascotbe# kubeadm init \     
--kubernetes-version=1.28.2 \
--apiserver-advertise-address=192.168.23.133 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.244.0.0/16 \
--image-repository=registry.aliyuncs.com/google_containers \
--ignore-preflight-errors=Mem
[init] Using Kubernetes version: v1.28.2
[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 master.local] and IPs [10.96.0.1 192.168.23.133]
[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 master.local] and IPs [192.168.23.133 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [localhost master.local] and IPs [192.168.23.133 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
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[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"
[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
[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 3.501959 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 master.local 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 master.local as control-plane by adding the taints [node-role.kubernetes.io/control-plane:NoSchedule]
[bootstrap-token] Using token: cgu3ya.j0j6a5c7iuwlrwo3
[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 192.168.23.133:6443 --token cgu3ya.j0j6a5c7iuwlrwo3 \
--discovery-token-ca-cert-hash sha256:4bee84018e33091640c109ff15b204140c88777a0db943ddbee47ddb93260b3c

配置kubectl

就是执行初始化成功后输出的那三条命令

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

查看节点信息

root@master:/home/ascotbe# kubectl get nodes     
NAME STATUS ROLES AGE VERSION
master.local NotReady control-plane 46s v1.28.2

此时只能看到master节点,等待其他节点加入进来后即可看到。

安装flannel

root@master:/home/ascotbe# kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml     
namespace/kube-flannel created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created

Node节点加入集群

node1、node2

都运行下面命令

kubeadm join 192.168.23.133:6443 --token cgu3ya.j0j6a5c7iuwlrwo3 \     
--discovery-token-ca-cert-hash sha256:4bee84018e33091640c109ff15b204140c88777a0db943ddbee47ddb93260b3c

master节点上查看集群节点

root@master:/home/ascotbe# kubectl get nodes     
NAME STATUS ROLES AGE VERSION
master.local Ready control-plane 26m v1.28.2
node01.local Ready <none> 13m v1.28.2
node02.local Ready <none> 33s v1.28.2

相关 [k8s 环境] 推荐:

K8S环境搭建

- - ascotbe
内存:3GB或更多RAM. CPU: 2核CPU或更多CPU. **操作系统:**CentOS 10. 永久关闭是需要关机重启生效的,所以还是要把临时关闭命令也执行下. 不设置也可以,但是要保证主机名不相同,并且修改的hosts内容要与之对应. vi /etc/hosts添加如下内容. 如果kube-proxy 模式为ip_vs则必须加载,本文采用iptables.

CentOS7 安装 K8S

- - 企业架构 - ITeye博客
前提:VirtualBox CentOS7. 物理机IP   192.168.18.8. 虚拟机1IP:192.168.18.100(VMaster master). 虚拟机2IP:192.168.18.101(VServer1 node1). 虚拟机3IP:192.168.18.102(VServer2 node2).

k8s水平扩容

- - Bboysoul's Blog
k8s 的好处就是可以弹性水平扩容和纵向扩容,平时纵向扩容用的不太多,所以今天说说水平扩容,在创建hpa之前你要确定集群中已经安装了metrics-server,我使用的是k3s,直接自带. 首先创建需要的容器,下面是dockerfile. 原理就是当你访问index.php的时候会进行一个循环计算来提高cpu的使用率.

# [k8s] HPA: Horizontal Pod Autoscaling

- - V2EX - 技术
HPA 是 K8S 的一大利器. 通过 HPA, 我们可以让服务的 pod 数量根据特定指标自动增加或减少, 使得在高峰期有足够的资源服务请求, 在低峰期又可以避免占用过多的资源. 同时, 在 SOA 架构下, 我们也习惯通过 HPA 来避免劳心劳力的为每个微服务计算所需的资源.. minReplicas: 允许的最小 pod 数量.

K8S 1.24.0 安装部署

- - Share
在 v1.2x 版本中, Kubernetes 支持的最大节点数为 5000. 更具体地说,我们支持满足以下所有条件的配置:. 每个节点的 pod 数量不超过. Kubernetes v1.20 开始,默认移除 docker 的依赖,如果宿主机上安装了 docker 和 containerd,将优先使用 docker 作为容器运行引擎,如果宿主机上未安装 docker 只安装了 containerd,将使用 containerd 作为容器运行引擎;.

k8s docker集群搭建 - CSDN博客

- -
一、Kubernetes系列之介绍篇.     - 一次构建,到处运行. 2.什么是kubernetes.   首先,他是一个全新的基于容器技术的分布式架构领先方案. Kubernetes(k8s)是Google开源的容器集群管理系统(谷歌内部:Borg). 在Docker技术的基础上,为容器化的应用提供部署运行、资源调度、服务发现和动态伸缩等一系列完整功能,提高了大规模容器集群管理的便捷性.

深入掌握K8S Pod - Yabea - 博客园

- -
K8S configmap介绍. Pod是k8s中最小的调度单元,包含了一个“根容器”和其它用户业务容器. 如果你使用过k8s的话,当然会了解pod的基本使用,但是为了更好的应用,你需要深入了解pod的配置、调度、升级和扩缩容等. pod包含一个或多个相对紧密耦合的容器,处于同一个pod中的容器共享同样的存储空间、IP地址和Port端口.

浅谈 k8s ingress controller 选型 - 知乎

- -
大家好,先简单自我介绍下,我叫厉辉,来自腾讯云. 业余时间比较喜欢开源,现在是Apache APISIX PPMC. 今天我来简单给大家介绍下 K8S Ingress 控制器的选型经验,今天我讲的这些内容需要大家对 K8S 有一定的了解,下面是我的分享. 阅读本文需要熟悉以下基本概念:. 集群:是指容器运行所需云资源的集合,包含了若干台云服务器、负载均衡器等云资源.

SkyWalking探针在 k8s 中集成

- - 掘金 后端
最近公司需要在 k8s 环境接入 SkyWalking,要让应用无感知接入. 开始打算的是把agent文件放到基础镜像中,这样应用只需要引用包含agent的基础镜像即可. 但是这样会有几个问题,首先不好管理agent,升级需要应用重新打镜像部署,动静太大. 第二,不是所有应用都需要接入,要按需引入不同基础镜像,这样就多个一个步骤,应用会有感知.

记一次K8s排错实战

- - 掘金 后端
这是我参与更文挑战的第3天,活动详情查看:. 收到测试环境集群告警,登陆K8s集群进行排查. 查看kube-system node2节点calico pod异常. 查看详细信息,查看node2节点没有存储空间,cgroup泄露. 登陆node2查看服务器存储信息,目前空间还很充足. 集群使用到的分布式存储为ceph,因此查看ceph集群状态.