从0开始安装一个本地K8s环境

文章目录

本地K8s安装流程:Vmware → CentOS7.9 → Docker → Golang → kubectl → kind → K8s

本地KubeBlocks安装流程:本地K8s安装流程 → kbcli/helm → KubeBlocks

一、安装CentOS

1、下载安装VMware

VMware官网:https://www.vmware.com/

2、下载安装CentOS

强烈推荐阿里云镜像官网,一如既往的好用。

阿里云镜像官网:https://developer.aliyun.com/mirror/

Centos下载地址: https://mirrors.aliyun.com/centos/

具体下载地址:https://mirrors.aliyun.com/centos/7.9.2009/isos/x86_64/CentOS-7-x86_64-DVD-2207-02.iso?spm=a2c6h.25603864.0.0.65a56aeaHmvwUh

安装完成之后进行Linux环境初始化配置:https://blog.csdn.net/qq_41822345/article/details/118096213

3、安装Golang、Kubectl、Kind等命令工具

Golang安装参考Golang官网:https://go.dev/doc/install

Kubctl命令安装参考k8s官网:https://kubernetes.io/docs/tasks/tools/install-kubectl-linux

Kind命令安装参考Kind官网:https://kind.sigs.k8s.io/docs/user/quick-start#installing-from-release-binaries

Helm命令安装参考helm官网:https://helm.sh/docs/intro/install

4、通过Kind工具安装k8s集群

在本地 Linux 虚拟机里,可以使用 Kind[1] 来搭建一个多节点的 K8S 集群。也可以用其他的本地 K8S 方案,比如 Minikube[8] 或者K3D[9] 。

这里以 Kind 为例子,说明如何搭建一套3个数据节点的本地 K8S 集群。

首先安装 Kind 工具:可以参考官方安装文档[2] 来安装 Kind 工具。确保把它放在PATH 环境变量能够搜索到的目录中。

然后构造一个 Kind 集群的配置文件:

shell 复制代码
## 参考kind官网
cat << EOF > ~/tmp/kind-cluster-config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  image: kindest/node:v1.27.3
- role: worker
  image: kindest/node:v1.27.3
- role: worker
  image: kindest/node:v1.27.3
- role: worker
  image: kindest/node:v1.27.3
EOF

# kindest/node:v1.27.3 这些镜像的下载可能需要科学上网

在这个文件中,使用了 1.22 的 K8S 镜像。如果不指定这个镜像,默认 Kind 会使用最新的 K8S 版本来部署本地 K8S 集群。

最后安装集群:

shell 复制代码
kind create cluster --config ~/tmp/kind-cluster-config.yaml --name k8s-dev

附注:如果出现如下报错Your kernel does not support cgroup namespaces. Cgroup namespace setting discarded.

,则说明当前内核版本不支持cgroup。需要先升级Linux内核,将内核从3.10升级至5.4。

请参考:https://blog.csdn.net/XiaoWang0777/article/details/140466154

shell 复制代码
## 报错示例
[root@localhost ~]# kind create cluster --config ~/tmp/kind-cluster-config.yaml --name k8s-dev
Creating cluster "k8s-dev" ...
 ✓ Ensuring node image (kindest/node:v1.22.0) 🖼 
 ✗ Preparing nodes 📦 📦 📦 📦  
Deleted nodes: ["k8s-dev-control-plane" "k8s-dev-worker2" "k8s-dev-worker" "k8s-dev-worker3"]
ERROR: failed to create cluster: command "docker run --name k8s-dev-worker3 --hostname k8s-dev-worker3 --label io.x-k8s.kind.role=worker --privileged --security-opt seccomp=unconfined --security-opt apparmor=unconfined --tmpfs /tmp --tmpfs /run --volume /var --volume /lib/modules:/lib/modules:ro -e KIND_EXPERIMENTAL_CONTAINERD_SNAPSHOTTER --detach --tty --label io.x-k8s.kind.cluster=k8s-dev --net kind --restart=on-failure:1 --init=false --cgroupns=private --volume /dev/mapper:/dev/mapper kindest/node:v1.22.0" failed with error: exit status 125
Command Output: WARNING: Your kernel does not support cgroup namespaces.  Cgroup namespace setting discarded.
75cd3d0582e8c5f5ec61e67726ccdb996c36dbc85feffe434659d2f1dec53c0f
docker: Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: cgroup namespaces aren't enabled in the kernel: unknown.


## 内核升级步骤【yum安装】
# step1:查看内核版本
uname -r
3.10.0-1160.71.1.el7.x86_64

# step2:查看关于内核的包
rpm -qa |grep kernel
kernel-3.10.0-1160.71.1.el7.x86_64
kernel-tools-3.10.0-1160.71.1.el7.x86_64
kernel-headers-3.10.0-1160.119.1.el7.x86_64
kernel-tools-libs-3.10.0-1160.71.1.el7.x86_64

# step3:添加阿里yum源
cat <<EOF > /etc/yum.repos.d/elrepo.repo
> [elrepo]
> name=elrepo
> baseurl=https://mirrors.aliyun.com/elrepo/archive/kernel/el7/x86_64
> gpgcheck=0
> enabled=1
> EOF

# step4:重建yum缓存
yum clean all && yum makecache

# step5:查看yum仓库中的内核包
yum list --showduplicate kernel*

# step6:安装内核【lt 是 long term support的缩写,即内核的长期支持版本】
 yum install -y kernel-lt kernel-lt-devel

# step7:设置启动内核
grub2-set-default 0

# step8:重启生效
[root@localhost ~]# uname -r
3.10.0-1160.71.1.el7.x86_64
[root@localhost ~]# reboot
...
[root@localhost ~]# uname -r
5.4.278-1.el7.elrepo.x86_64

## 内核升级完成之后,重新执行,通过kind工具部署生产一个k8s集群,成功安装示例如下:
[root@localhost ~]# kind create cluster --config ~/tmp/kind-cluster-config.yaml --name k8s-dev
Creating cluster "k8s-dev" ...
 ✓ Ensuring node image (kindest/node:v1.22.0) 🖼
 ✓ Preparing nodes 📦 📦 📦 📦  
 ✓ Writing configuration 📜 
 ✓ Starting control-plane 🕹️ 
 ✓ Installing CNI 🔌 
 ✓ Installing StorageClass 💾 
 ✓ Joining worker nodes 🚜 
Set kubectl context to "kind-k8s-dev"
You can now use your cluster with:

kubectl cluster-info --context kind-k8s-dev

Have a nice day! 👋
5、如何使用k8s集群

从0开始安装k8s1.25: https://blog.csdn.net/qq_41822345/article/details/126679925

  • 一、kind集群操作
shell 复制代码
# 创建k8s集群【使用默认镜像创建】
kind create cluster --name kind-2
# 是用指定的镜像版本创建,比如使用kindest/node:v1.22
kind create cluster --config ~/tmp/kind-cluster-config.yaml --name k8s-dev2
[root@localhost ~]# cat ~/tmp/kind-cluster-config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  image: kindest/node:v1.22
- role: worker
  image: kindest/node:v1.22
- role: worker
  image: kindest/node:v1.22
- role: worker
  image: kindest/node:v1.22

# 获取k8s集群
kind get clusters
k8s-dev
k8s-dev2
kind-2

# 查看指定k8s集群【集群名加上kind前缀】
kubectl cluster-info --context kind-k8s-dev
kubectl cluster-info --context kind-k8s-dev2
kubectl cluster-info --context kind-k8s-kind-2


# 删除k8s集群
kind delete cluster --name k8s-dev2 
  • 二、kind镜像操作
shell 复制代码
[root@localhost ~]# docker pull nginx:1.14.2
## 将镜像load到k8s集群k8s-dev中
[root@localhost ~]# kind -n k8s-dev load docker-image nginx:1.14.2
Image: "nginx:1.14.2" with ID "sha256:84581e99d807a703c9c03bd1a31cd9621815155ac72a7365fd02311264512656" not yet present on node "k8s-dev-worker", loading...
Image: "nginx:1.14.2" with ID "sha256:84581e99d807a703c9c03bd1a31cd9621815155ac72a7365fd02311264512656" not yet present on node "k8s-dev-control-plane", loading...
Image: "nginx:1.14.2" with ID "sha256:84581e99d807a703c9c03bd1a31cd9621815155ac72a7365fd02311264512656" not yet present on node "k8s-dev-worker2", loading...

# 批量导入镜像到k8s-dev集群中
[root@localhost ~]# xargs kind load docker-image -n k8s-dev < image.manifest
[root@k8s-dev kubeblocks-setups]# cat image.manifest
registry/apecloud/kubeblocks-charts:0.8.0
registry/apecloud/kubeblocks-datascript:0.8.0
registry/apecloud/kubeblocks-dataprotection:0.8.0
registry/apecloud/kubeblocks:0.8.0
registry/apecloud/kubeblocks-tools:0.8.0
registry/kubernetes/sig-storage/csi-snapshotter:v6.2.1


[root@localhost ~]# kubectl apply -f deploy-nginx.yaml
[root@localhost ~]# cat deploy-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3 # 告知 Deployment 运行 3 个与该模板匹配的 Pod
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
# 查看        
[root@localhost ~]# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-66b6c48dd5-cb7d7   1/1     Running   0          36s
nginx-deployment-66b6c48dd5-kvzgc   1/1     Running   0          36s
nginx-deployment-66b6c48dd5-qgf6x   1/1     Running   0          36s
6、安装 KubeBlocks

KubeBlocks有两种安装方式,第一种是用kbcli安装,第二种是使用helm安装。

  • 一、kbcli安装

先安装kbcli:https://cn.kubeblocks.io/docs/preview/user-docs/installation/install-with-kbcli/install-kbcli

再安装kubeblocks:https://cn.kubeblocks.io/docs/preview/user-docs/installation/install-with-kbcli/install-kubeblocks-with-kbcli

shell 复制代码
# 默认将 KubeBlocks 安装在 kb-system 命名空间中,可以使用 --namespace 指定一个命名空间
kbcli kubeblocks install
kbcli kubeblocks list-versions
kbcli kubeblocks install --version=0.8.0

# 验证 KubeBlocks 安装
kbcli kubeblocks status
  • 二、helm安装

helm安装的前提,需要先在虚拟机上安装 Helm [10]。KubeBlocks 使用 helm chart 来打包所有要创建在 K8S 集群上面的 K8S API对象。helm 可以从官方文档[3]获取具体的安装方式。

先安装helm安装:https://helm.sh/docs/intro/install

再安装kubeblocks:https://cn.kubeblocks.io/docs/preview/user-docs/installation/install-with-helm/install-kubeblocks-with-helm

shell 复制代码
# 这里使用 --validate=false,是因为 K8S 的某些版本在解析 CRD 有一些字段没有,所以 validate 会失败。为了兼容 K8S 而设置为 false。
kubectl create -f https://github.com/apecloud/kubeblocks/releases/download/v0.8.1/kubeblocks_crds.yaml --validate=false

# 添加 Helm 仓库
helm repo add kubeblocks https://apecloud.github.io/helm-charts
helm repo update

# 安装 KubeBlocks
helm install kubeblocks kubeblocks/kubeblocks --namespace kb-system --create-namespace
# 使用 --version 指定版本安装
helm install kubeblocks kubeblocks/kubeblocks \
--namespace kb-system --create-namespace --version="0.8.0"

# 验证 KubeBlocks 安装
kbcli kubeblocks status

# 验证示例如下
[root@k8s-dev ~]# kbcli kubeblocks status
KubeBlocks is deployed in namespace: kubeblocks-system,version: 0.8.0

Kubernetes Cluster:
VERSION   PROVIDER   REGION   AVAILABLE ZONES   
v1.22.0   Kind                                  

KubeBlocks Workloads:
NAMESPACE           KIND         NAME                           READY PODS   CPU(CORES)   MEMORY(BYTES)   CREATED-AT                   
kubeblocks-system   Deployment   kb-addon-snapshot-controller   1/1          N/A          N/A             Jul 23,2024 23:07 UTC+0800   
kubeblocks-system   Deployment   kubeblocks                     1/1          N/A          N/A             Jul 23,2024 23:07 UTC+0800   
kubeblocks-system   Deployment   kubeblocks-dataprotection      1/1          N/A          N/A             Jul 23,2024 23:07 UTC+0800 
......

官网参考链接:

[1] https://kind.sigs.k8s.io :Kind 官网

[2] https://kind.sigs.k8s.io/docs/user/quick-start#installing-from-release-binaries :安装 Kind

[3] https://helm.sh/docs/intro/install :安装 helm

[4] https://kubeblocks.io/docs/preview/user_docs/installation/install-with-kbcli/install-kbcli :安装 kbcli

[5] https://docs.docker.com/engine/install :如何安装 docker

[6] https://go.dev/doc/install :如何安装 Golang

[7] https://kubernetes.io/docs/tasks/tools/install-kubectl-linux :如何安装 kubectl

[8] https://minikube.sigs.k8s.io/docs :Minikube 官网

[9] https://k3d.io/v5.6.0 :K3D 官网

[10] https://helm.sh :Helm 官网

[11] https://kubeblocks.io/docs/preview/user_docs/overview/introduction :KubeBlocks 用户文档首页

[12] https://kubeblocks.io/docs/preview/developer_docs/overview :KubeBlocks 开发文档首页

相关推荐
放手啊1 小时前
sealos部署K8s,安装docker时master节点突然NotReady
docker·容器·kubernetes
颜淡慕潇14 小时前
【K8S问题系列 | 9】如何监控集群CPU使用率并设置告警?
后端·云原生·容器·kubernetes·问题解决
运维&陈同学14 小时前
【模块一】kubernetes容器编排进阶实战之k8s基础概念
运维·docker·云原生·容器·kubernetes·云计算
葡萄皮Apple14 小时前
[CKS] K8S RuntimeClass SetUp
java·容器·kubernetes
斯普信专业组1 天前
Kubernetes网络揭秘:从DNS到核心概念,一站式综述
网络·容器·kubernetes
颜淡慕潇1 天前
【K8S系列 】在K8S集群怎么查看各个pod占用的资源大小与详细解决方案【已解决】
后端·云原生·容器·kubernetes·问题解决
葡萄皮Apple1 天前
[CKS] K8S Dockerfile和yaml文件安全检测
安全·容器·kubernetes
管理大亨1 天前
K8S之Prometheus 部署(二十)
docker·kubernetes·prometheus
颜淡慕潇1 天前
【K8S问题系列 | 10】在K8S集群怎么查看各个pod占用的资源大小?【已解决】
后端·云原生·容器·kubernetes·问题解决
运维小文1 天前
K8S资源介绍之configmap
docker·云原生·容器·kubernetes