如何使用Minikube 搭建单节点Kubernetes环境

minikube提供了一种非常简单的方法来安装单节点的K8S环境,minikube支持多种环境,比如linux,macos,windows都可以,安装的介质也支持多种,默认会使用virtualbox创建虚拟机并安装k8s环境,也可以在启动的时候通过–vm-driver 使用虚拟化技术,目前版本支持的列表,详见:https://github.com/kubernetes/minikube/blob/master/docs/drivers.md#vmware-unified-driver

minikue的工作原理

如何使用Minikube 搭建单节点Kubernetes环境

如何在MacOS中安装minikube

  • 安装virtualbox https://www.virtualbox.org/wiki/Downloads

  • 安装kubectl https://kubernetes.io/docs/tasks/tools/install-kubectl/

curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/amd64/kubectlchmod +x ./kubectlsudo mv ./kubectl /usr/local/bin/kubectlkubectl version
  • 安装minikube 最新版 https://kubernetes.io/docs/tasks/tools/install-minikube/

curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 chmod +x minikubesudo mv ./minikube /usr/local/bin/minikubeminikube version
  • 使用minikube开始部署kubernetes

    由于minikube start 开始部署的时候,需要从k8s.gcr.io下载进行,很多情况下会被墙掉,大致报错

E0507 11:28:19.218002  1347 start.go:307] Error caching images:  Caching images for kubeadm: caching images: caching image /Users/yangjunjun/.minikube/cache/images/k8s.gcr.io/k8s-dns-kube-dns-amd64_1.14.13: fetching remote image: Get https://k8s.gcr.io/v2/: dial tcp 108.177.125.82:443: i/o timeout✨  Preparing Kubernetes environment ...❌  Unable to load cached images: loading cached images: loading image /Users/yangjunjun/.minikube/cache/images/k8s.gcr.io/kube-proxy_v1.14.1: stat /Users/yangjunjun/.minikube/cache/images/k8s.gcr.io/kube-proxy_v1.14.1: no such file or directory

解决这个报错有两个办法:

方法一:通过科学上网解决,下面加一下代理配置

export http_proxy="socks5://127.0.0.1:1080"export https_proxy="socks5://127.0.0.1:1080"

完成配置之后使用以下命令部署kubenetes,(需要注意,代理需要你提前配置的,不然这个方法是不行的)

minikube start

方法二:把原版的minikube删除,使用阿里云提供的minikube工具来部署

以下版本minikube 可以安装Kubernetes 1.14+ release:

curl -Lo minikube https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.0.0/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

以下版本minikube 可以安装Kubernetes 1.9 release

curl -Lo minikube https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v0.25.2/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

下载特定minikube版本之后,使用以下命令部署kubenetes

minikube start --registry-mirror=https://registry.docker-cn.com😄  minikube v1.0.0 on darwin (amd64)🤹  Downloading Kubernetes v1.14.0 images in the background ...🔥  Creating virtualbox VM (CPUs=2, Memory=2048MB, Disk=20000MB) ...📶 "minikube" IP address is 192.168.99.108🐳  Configuring Docker as the container runtime ...🐳  Version of container runtime is 18.06.2-ce⌛  Waiting for image downloads to complete ...✨  Preparing Kubernetes environment ...💾  Downloading kubelet v1.14.0 from https://kubernetes.oss-cn-hangzhou.aliyuncs.com/kubernetes-release/release/v1.14.0/bin/linux/amd64/kubelet🚜  Pulling images required by Kubernetes v1.14.0 ...🚀  Launching Kubernetes v1.14.0 using kubeadm ...⌛  Waiting for pods: apiserver proxy etcd scheduler controller dns🔑  Configuring cluster permissions ...🤔  Verifying component health .....💗  kubectl is now configured to use "minikube"🏄  Done! Thank you for using minikube!

部署完成之后,启动dashboard,此时会自动打开默认浏览器

minikube dashboard

如何在CentOS7 中安装minikube

在Centos7 下安装minikube的时候,会使用–vm-driver=none,添加这个参数之后,minikube会直接在当前系统里安装kubenetes,以下介绍主要的步骤

安装docker

yum remove docker-latest-logrotate  docker-logrotate  docker-selinux dockdocker-engineyum install -y yum-utils  device-mapper-persistent-data  lvm2使用docker官方源yum-config-manager   --add-repo   https://download.docker.com/linux/centos/docker-ce.repoyum makecache fastyum install docker-ce使用阿里云安装源yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.reporpm --import https://mirrors.aliyun.com/docker-ce/linux/centos/gpgyum makecache fastyum -y install docker-ce安装完成启动Dockersystemctl start dockersystemctl status dockersystemctl enable docker

安装kubectl

方法一:yum 方式安装,适合国内环境

cat <<EOF > /etc/yum.repos.d/kubernetes.repo[kubernetes]name=Kubernetesbaseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64enabled=1gpgcheck=0repo_gpgcheck=0gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpghttps://mirrors.aliyun.com/ www.dfgjpt.com kubernetes/yum/doc/rpm-package-key.gpgEOFyum install -y kubectlkubectl version

方法二:直接下载bin文件,适合国外环境

curl -Lo  kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectlchmod +x kubectlmv kubectl /usr/local/binkubectl version

安装minikube

国外源

curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64   && chmod +x minikubemv minikube /usr/local/binminikube version

国内源

curl -Lo minikube https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.0.0/minikube-linux-amd64 chmod +x minikube mv minikube /usr/local/bin/minikube version

使用minikube开始部署kubernetes

国外版

minikube start --vm-driver=none

国内版

minikube start --vm-driver=none --registry-mirror=https://registry.docker-cn.com

启动dashboard

为dashboard添加外部访问代理,此处的IP为本地系统IP:

kubectl proxy --port=8001--address='10.0.0.2'--accept-hosts='^.*'&minikube dashboard

通过url: https://10.0.0.2:8001/api/v1/namespaces/kube-system/services/http:kubernetes-dashboard:/proxy/ 访问dashboard。

如何编译安装minikube

编译安装minikube aliyun-v0.25.0版本,该版本支持安装指定版本的Kubernetes,如何编译往下看

#安装go环境yum install -y go#创建源代码目录mkdir -p $GOPATH/src/k8s.io/ #下载代码并编译cd $GOPATH/src/k8s.io/git clone https://github.com/AliyunContainerService/minikubecd minikubegit checkout aliyun-v0.25.0make#拷贝执行文件到/usr/local/binsudo cp out/minikube /usr/local/bin/

编译完成之后,就可以使用minikubes部署Kubernetes环境了,不指定版本的时候,安装Kubernetes v1.9.0

minikube start --registry-mirror=https://registry.docker-cn.com --vm-driver=none#以下都是输出的提示信息

安装不同的Kubernetes版本

# 安装Kubernetes v1.12.1minikube start --registry-mirror=https://registry.docker-cn.com --kubernetes-version v1.12.1 --vm-driver=none# 安装Kubernetes v1.11.3minikube start --registry-mirror=https://registry.docker-cn.com --kubernetes-version v1.11.3 --vm-driver=none

Minikube 常用操作

#查看状态~ minikube statushost: Runningkubelet: Runningapiserver: Runningkubectl: Correctly Configured: pointing to minikube-vm at192.168.99.108#登录虚拟机~ minikubessh            _       _     _     _ ( )      ( )___ ___ (_) ___ (_)| |/') _  _ | |_   __/' _ ` _ `| |/'_`| || , < ( ) ( )| '_` /'__`| ( ) ( ) || || ( ) || || |` | (_) || |_) )( ___/(_) (_) (_)(_)(_) (_)(_)(_) (_)`___/'(_,__/'`____)$ sudo-i# whoamiroot#启动,停止,删除~ minikubestart--registry-mirror=https://registry.docker-cn.com~ minikubestop~ minikube delete#重置,重置之后会清理所有缓存的镜像,重头开始rm-rf~/.minikube#启动Dashboard~ minikube dashboard

注:截止2019/05/07最新版本是minikubeversion:v1.0.1,Kubernetes版本1.14.1