minikube搭建k8s集群体验
2022-02-18 本文已影响0人
文景大大
一、安装集群
本文演示如何在windows下安装和使用minikube,需要做的工作如下:
-
提前安装好Docker Engine;
-
从minikube官网下载Windows的安装包进行安装;
-
执行
minikube --help
检查是否安装成功; -
本地启动Docker Engine;
-
使用minikube创建k8s集群;
minikube start * Microsoft Windows 10 Pro 10.0.19042 Build 19042 上的 minikube v1.25.1 * 自动选择 docker 驱动 * Starting control plane node minikube in cluster minikube * Pulling base image ... * Downloading Kubernetes v1.23.1 preload ... > preloaded-images-k8s-v16-v1...: 504.42 MiB / 504.42 MiB 100.00% 1.16 MiB > index.docker.io/kicbase/sta...: 378.98 MiB / 378.98 MiB 100.00% 989.99 K ! minikube was unable to download gcr.io/k8s-minikube/kicbase:v0.0.29, but successfully downloaded docker.io/kicbase/stable:v0.0.29 as a fallback image * Creating docker container (CPUs=2, Memory=4000MB) ... ! Unable to create dedicated network, this might result in cluster IP change after restart: failed to create docker network minikube: subnet is taken E0218 09:17:21.697245 1832 kic.go:267] icacls failed applying permissions - err - [%!s(<nil>)], output - [�Ѵ������ļ�: C:\Users\Think\.minikube\machines\minikube\id_rsa �ѳɹ����� 1 ���ļ�; ���� 0 ���ļ�ʱʧ��] ! This container is having trouble accessing https://k8s.gcr.io * To pull new external images, you may need to configure a proxy: https://minikube.sigs.k8s.io/docs/reference/networking/proxy/ * 正在 Docker 20.10.12 中准备 Kubernetes v1.23.1… - kubelet.housekeeping-interval=5m - Generating certificates and keys ... - Booting up control plane ... - Configuring RBAC rules ... * Verifying Kubernetes components... - Using image gcr.io/k8s-minikube/storage-provisioner:v5 * Enabled addons: storage-provisioner, default-storageclass ! C:\Program Files\Docker\Docker\resources\bin\kubectl.exe is version 1.21.5, which may have incompatibilites with Kubernetes 1.23.1. - Want kubectl v1.23.1? Try 'minikube kubectl -- get pods -A' * Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
-
执行
kubectl --help
检查kubectl是否安装成功,后续可以使用它与集群进行交互了。
二、部署应用
接下来,我们使用kubectl部署一个应用,来快速体验k8s集群的能力。
# 使用指定镜像新建一个deployment
kubectl create deployment getting-started --image=docker/getting-started:latest
# 暴露deployment(service)到服务器的端口为8889,表示使用localhost:8889就能访问到service
kubectl expose deployment getting-started --type=NodePort --port=8889
经过如上的命令,k8s会自动创建pod、deployment、service三种资源,我们可以通过如下的命令来查看各种资源的信息:
# 查看所有的pod
kubectl get pods
NAME READY STATUS RESTARTS AGE
getting-started-74c5b869cd-ffcgn 1/1 Running 0 6m46s
# 查看所有的deployment
kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
getting-started 1/1 1 1 6m32s
# 查看所有的service
kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
getting-started NodePort 10.106.209.2 <none> 8889:30813/TCP 4m11s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 24m
可以看到,刚刚部署的服务在本机的8889端口可以访问,这个端口是暴露service时指定的,30813则是service的端口,是随机生成的,我们使用浏览器访问:localhost:8889
,就能看到getting-started这个服务提供的内容了。
三、基本操作
-
集群操作
# 启动集群 minikube start # 停止集群,再次启动后原先的资源都会被清空 minikube stop # 删除集群 minikube delete # 暂停集群,不会删除原有的资源 minikube pause # 恢复集群,原先的资源会恢复运行 minikube unpause # 启动第二个集群 minikube start -p cluster2 # 删除所有集群kube minikube delete --all # 升级集群版本 minikube start --kubernetes-version=latest
-
应用操作
# 创建应用 kubectl create deployment getting-started --image=docker/getting-started:latest # 暴露应用访问端口 kubectl expose deployment getting-started --type=NodePort --port=8889
-
dashboard
# 启动dashboard,并自动使用默认浏览器打开 minikube dashboard
有了dashboard,就不用手动输入kubectl命令来管理集群了,大大简化了步骤,降低了操作门槛。
参考文档: