Docker+Kubernetes搭建服务集群
Kubernetes(k8s)是自动化容器操作的开源平台,基于这个平台,你可以进行容器部署,资源调度和集群扩容等操作。如果你曾经用过Docker部署容器,那么可以将Docker看成Kubernetes底层使用的组件,Kubernetes是Docker的上层封装,通过它可以很方便的进行Docker集群的管理。今天我们使用minikube在单机上进行Kubernetes集群的部署,目的是让我们对k8s
Kubernetes(k8s)是自动化容器操作的开源平台,基于这个平台,你可以进行容器部署,资源调度和集群扩容等操作。如果你曾经用过Docker部署容器,那么可以将Docker看成Kubernetes底层使用的组件,Kubernetes是Docker的上层封装,通过它可以很方便的进行Docker集群的管理。今天我们使用minikube在单机上进行Kubernetes集群的部署,目的是让我们对k8s有个初步的认识。
*严格按照我的步骤,绝对能跑起来
1、安装Docker
首先安装docker环境,不详细说明了,网上资料一大堆,可以参考官方安装文档
Mac: https://docs.docker.com/docker-for-mac/install/
Ubuntu: https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/
CentOS: https://docs.docker.com/engine/installation/linux/docker-ce/centos/
当然,如果上面所有方法你都失败了,也可以尝试直接下载binary可执行文件,然后启动docker即可
https://docs.docker.com/engine/installation/linux/docker-ce/binaries/
2、安装Minikube
Mac
如未安装cask,自行搜索 brew安装cask (也可以用下面linux的方法安装)
brew cask install minikube
minikube -h
Linux
下载v0.24.1版本
curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.24.1/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
也可以下载最新版,但可能和本文执行环境不一致,得注意⚠️
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
minikube -h
3、安装Kubectl
kubectl即kubernetes的客户端,通过他可以进行类似docker run等容器管理操作
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/
kubectl -h
(至此需要安装的都已经ok了,下面开始启动minikube)
4、启动minikube
minikube start
首次启动会下载localkube,下载过程可能会失败,会有如下提示,重试几次即可
Starting local Kubernetes v1.8.0 cluster... Starting VM... Downloading Minikube ISO
64.70 MB / 140.01 MB [====================>-----------------------] 46.21% 14s E0105
14:06:03.884826 10434 start.go:150] Error starting host: Error attempting to cache
minikube ISO from URL: Error downloading Minikube ISO: failed to download: failed to
download to temp file: failed to copy contents: read tcp 10.0.2.15:47048-
172.217.24.16:443: read: connection reset by peer.
================================================================================ An
error has occurred. Would you like to opt in to sending anonymized crash
information to minikube to help prevent future errors? To opt out of these
messages, run the command: minikube config set WantReportErrorPrompt false
================================================================================
Please enter your response [Y/n]:
如果下载成功,但是报了诸如VBoxManage not found这样的错误,如下
Starting local Kubernetes v1.8.0 cluster... Starting VM... Downloading Minikube ISO 140.01 MB / 140.01 MB [============================================] 100.00% 0s E0105 14:10:00.035369 10474 start.go:150] Error starting host: Error creating host: Error executing step: Running precreate checks. : VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path. Retrying. E0105 14:10:00.035780 10474 start.go:156] Error starting host: Error creating host: Error executing step: Running precreate checks. : VBoxManage not found. Make sure VirtualBox is installed and VBoxManage is in the path ================================================================================ An error has occurred. Would you like to opt in to sending anonymized crash information to minikube to help prevent future errors? To opt out of these messages, run the command: minikube config set WantReportErrorPrompt false ================================================================================ Please enter your response [Y/n]:
解决办法是安装 VirtualBox
【对于windows或者mac】 再重新启动;
当然如果你是Linux,也可以执行如下命令启动minikube,此时就不需要安装VirtualBox了。
因为minikube默认需要虚拟机来初始化kunernetes环境,但Linux是个例外,可以追加
–vm-driver=none
参数来使用自己的环境,说明见https://github.com/kubernetes/minikube#quickstart
linux 下独有,不依赖虚拟机启动
sudo minikube start --vm-driver=none
# 如果是Mac or Windows,安装VirtualBox后再重新start即可
sudo minikube start
如果安装了虚拟机,或者使用了–vm-driver=none参数,并且下载完毕,minikube start 后会有如下提示运行成功:
(base) 开始 ▶️ :/usr/local/bin$minikube start
Starting local Kubernetes v1.9.4 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.
Loading cached images from config file.
此时运行:
minikube dashboard
会出现的下面错误:
(base) 开始 ▶️ :~$minikube dashboard
Could not find finalized endpoint being pointed to by kubernetes-dashboard: Error
validating service: Error getting service kubernetes-dashboard: services "kubernetes-
dashboard" not found
我们用:
minikube logs
日志中出现 failed pulling image… 则是因为镜像拉取失败导致服务创建失败,原因?GFW嘛!服务在拉取自身需要的gcr.io/google_containers/pause-amd64:3.0镜像时失败了,如下报错。
查看错误日志会有如下错误:
Sep 04 09:31:40 minikube localkube[3022]: E0904 09:31:40.073355 3022 kuberuntime_sandbox.go:54] CreatePodSandbox for pod "kube-addon-manager-minikube_kube-system(c4c3188325a93a2d7fb1714e1abf1259)" failed: rpc error: code = Unknown desc = failed pulling image "gcr.io/google_containers/pause-amd64:3.0": Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Sep 04 09:31:40 minikube localkube[3022]: E0904 09:31:40.073368 3022 kuberuntime_manager.go:647] createPodSandbox for pod "kube-addon-manager-minikube_kube-system(c4c3188325a93a2d7fb1714e1abf1259)" failed: rpc error: code = Unknown desc = failed pulling image "gcr.io/google_containers/pause-amd64:3.0": Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Sep 04 09:31:40 minikube localkube[3022]: E0904 09:31:40.073417 3022 pod_workers.go:186] Error syncing pod c4c3188325a93a2d7fb1714e1abf1259 ("kube-addon-manager-minikube_kube-system(c4c3188325a93a2d7fb1714e1abf1259)"), skipping: failed to "CreatePodSandbox" for "kube-addon-manager-minikube_kube-system(c4c3188325a93a2d7fb1714e1abf1259)" with CreatePodSandboxError: "CreatePodSandbox for pod \"kube-addon-manager-minikube_kube-system(c4c3188325a93a2d7fb1714e1abf1259)\" failed: rpc error: code = Unknown desc = failed pulling image \"gcr.io/google_containers/pause-amd64:3.0\": Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)"
Sep 04 09:31:40 minikube localkube[3022]: E0904 09:31:40.303530 3022 healthcheck.go:317] Failed to start node healthz on 0: listen tcp: address 0: missing port in address
Sep 04 09:31:44 minikube localkube[3022]: E0904 09:31:44.055950 3022 remote_runtime.go:92] RunPodSandbox from runtime service failed: rpc error: code = Unknown desc = failed pulling image "gcr.io/google_containers/pause-amd64:3.0": Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Sep 04 09:31:44 minikube localkube[3022]: E0904 09:31:44.056021 3022 kuberuntime_sandbox.go:54] CreatePodSandbox for pod "kube-nginx999_default(c8128f81-ee87-11ea-a947-080027f31cd8)" failed: rpc error: code = Unknown desc = failed pulling image "gcr.io/google_containers/pause-amd64:3.0": Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Sep 04 09:31:44 minikube localkube[3022]: E0904 09:31:44.056040 3022 kuberuntime_manager.go:647] createPodSandbox for pod "kube-nginx999_default(c8128f81-ee87-11ea-a947-080027f31cd8)" failed: rpc error: code = Unknown desc = failed pulling image "gcr.io/google_containers/pause-amd64:3.0": Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Sep 04 09:31:44 minikube localkube[3022]: E0904 09:31:44.056098 3022 pod_workers.go:186] Error syncing pod c8128f81-ee87-11ea-a947-080027f31cd8 ("kube-nginx999_default(c8128f81-ee87-11ea-a947-080027f31cd8)"), skipping: failed to "CreatePodSandbox" for "kube-nginx999_default(c8128f81-ee87-11ea-a947-080027f31cd8)" with CreatePodSandboxError: "CreatePodSandbox for pod \"kube-nginx999_default(c8128f81-ee87-11ea-a947-080027f31cd8)\" failed: rpc error: code = Unknown desc = failed pulling image \"gcr.io/google_containers/pause-amd64:3.0\": Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)"
5、解决方法:用本地镜像替代–安装必要镜像
(base) 开始 ▶️ :~/Desktop$minikube ssh
_ _
_ _ ( ) ( )
___ ___ (_) ___ (_)| |/') _ _ | |_ __
/' _ ` _ `\| |/' _ `\| || , < ( ) ( )| '_`\ /'__`\
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )( ___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker images 没有镜像(应该是8个镜像就对了了,如果不不够,执⾏行行下⾯面的命令)
#⚠️ !!!!!minikube ssh 内执行!!!!!!!⚠️
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/k8s-dns-sidecar-amd64:1.14.5
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/k8s-dns-sidecar-amd64:1.14.5 k8s.gcr.io/k8s-dns-sidecar-amd64:1.14.5
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/k8s-dns-kube-dns-amd64:1.14.5
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/k8s-dns-kube-dns-amd64:1.14.5 k8s.gcr.io/k8s-dns-kube-dns-amd64:1.14.5
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/k8s-dns-dnsmasq-nanny-amd64:1.14.5
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/k8s-dns-dnsmasq-nanny-amd64:1.14.5 k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64:1.14.5
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-addon-manager:v6.5
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-addon-manager:v6.5 gcr.io/google-containers/kube-addon-manager:v6.5
docker pull registry.cn-hangzhou.aliyuncs.com/kube_containers/kubernetes-dashboard-amd64:v1.8.1
docker tag registry.cn-hangzhou.aliyuncs.com/kube_containers/kubernetes-dashboard-amd64:v1.8.1 k8s.gcr.io/kubernetes-dashboard-amd64:v1.8.1
docker pull registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0
docker tag registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0 gcr.io/google_containers/pause-amd64:3.0
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner:v1.8.1
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner:v1.8.1 gcr.io/k8s-minikube/storage-provisioner:v1.8.1
docker pull registry.cn-hangzhou.aliyuncs.com/junv/kubernetes-bootcamp:v1
docker tag registry.cn-hangzhou.aliyuncs.com/junv/kubernetes-bootcamp:v1 gcr.io/google-samples/kubernetes-bootcamp:v1
安装中遇到的错误自行搜索
6、完美运行
中间每做一次更改,记得 minikube restart
运行:
(base) 开始 ▶️ :~/Desktop$minikube dashboard
Opening kubernetes dashboard in default browser...
浏览器会弹出:
🎉 恭喜你!你已经可以在本地跑你的服务集群了。
中间有遇到很多错误,没忘上写,只写了正确的操作流程。
有问题的可以交流下。觉得不错的可以点个赞。
更多推荐
所有评论(0)