在mac上搭建Kubernetes集群(单机版)

在mac上搭建Kubernetes集群(单机版)

安装步骤

  • 安装VirtualBox.app
  • 安装minikube
  • 安装kubectl

其中VirtualBox去官网下载就好了,而下面2个命令可以通过brew来安装

    brew cask install minikube
    brew install kubectl

至此,你的Kubernetes需要的东西就完全装齐了

启动集群

使用minikube start启动集群
使用minikube version查看版本信息

    ~ » minikube version
    minikube version: v0.24.1

可以通过minikube start启动kubernetes集群

    ~ » minikube start
    Starting local Kubernetes cluster...
    Starting VM...
    SSH-ing files into VM...
    Setting up certs...
    Starting cluster components...
    Connecting to cluster...
    Setting up kubeconfig...
    Kubectl is now configured to use the cluster.

我们会使用kubectl命令来和kubernetes集群进行交互,可以使用kubectl version来检查kubectl是否安装

    ~ » kubectl version
    Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.0", GitCommit:"925c127ec6b946659ad0fd596fa959be43f0cc05", GitTreeState:"clean", BuildDate:"2017-12-16T03:16:50Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"darwin/amd64"}
    Server Version: version.Info{Major:"1", Minor:"8", GitVersion:"v1.8.0", GitCommit:"0b9efaeb34a2fc51ff8e4d34ad9bc6375459c4a4", GitTreeState:"clean", BuildDate:"2017-11-29T22:43:34Z", GoVersion:"go1.9.1", Compiler:"gc", Platform:"linux/amd64"}

使用kubectl cluster-info查看集群相关的信息

    ~ » kubectl cluster-info
    Kubernetes master is running at https://192.168.99.100:8443
    KubeDNS is running at https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
    kubernetes-dashboard is running at https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kubernetes-dashboard/proxy
    To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

也可以使用minikube dashboard来启动UI页面

    ~ » minikube dashboard
    Opening kubernetes dashboard in default browser...

如果一切顺利的话,你会看到Kubernetes的ui页面

停止集群和删除集群

停止集群使用命令:minikube stop, 删除集群使用minikube delete

解决dashboard启动的问题

如果一切顺利的话,按照上面的步骤,你会看到Kubernetes的ui页面。如果你在中国,很可能(基本上一定可能)会出现
endpoints kubernetes-dashboard not found' launching docker cluster差不多这样的错误吧,本小节就来分析和解决一下这个问题
使用kubectl get pods --all-namespaces命令可以查看当前集群的pods,当然了,因为是新集群么,全部是系统使用的一些东西,
如下所示(我这个结果可能会和你有一些区别,因为我已经把前3个pod解决了),以我这个输出为例子哈

    ~ » kubectl get pods --all-namespaces
    NAMESPACE     NAME                          READY     STATUS             RESTARTS   AGE
    kube-system   kube-addon-manager-minikube   1/1       Running            0          50m
    kube-system   kube-dns-86f6f55dd5-b99wf     3/3       Running            0          50m
    kube-system   kubernetes-dashboard-kgtvd    1/1       Running            0          50m
    kube-system   storage-provisioner           0/1       ImagePullBackOff   0          50m

可以看到storage-provisioner正在pull中ImagePullBackOff,然而在中国你等死你可能也不会等待他下载下来,可以使用:
minikube logs查看日志会发现其实是GFW导致的下载不下来Google的一些docker image导致的。
当然也可以通过下面的命令查看具体的原因
kubectl describe --namespace=kube-system po 「name」命令来查看:

    ~ » kubectl describe --namespace=kube-system po storage-provisioner
    Name:         storage-provisioner
    Namespace:    kube-system
    Node:         minikube/192.168.99.100
    Start Time:   Wed, 27 Dec 2017 18:18:15 +0800
    Labels:       addonmanager.kubernetes.io/mode=EnsureExists
                  integration-test=storage-provisioner
    Annotations:  <none>
    Status:       Pending
    IP:           192.168.99.100
    Containers:
      storage-provisioner:
        Container ID:
        Image:          gcr.io/k8s-minikube/storage-provisioner:v1.8.1
        Image ID:
        Port:           <none>
        State:          Waiting
          Reason:       ImagePullBackOff
        Ready:          False
        Restart Count:  0
        Environment:    <none>
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from default-token-w57qz (ro)
    Conditions:
      Type           Status
      Initialized    True
      Ready          False
      PodScheduled   True
    Volumes:
      default-token-w57qz:
        Type:        Secret (a volume populated by a Secret)
        SecretName:  default-token-w57qz
        Optional:    false
    QoS Class:       BestEffort
    Node-Selectors:  <none>
    Tolerations:     <none>
    Events:
      Type     Reason                 Age                  From               Message
      ----     ------                 ----                 ----               -------
      Normal   Scheduled              50m                  default-scheduler  Successfully assigned storage-provisioner to minikube
      Normal   SuccessfulMountVolume  50m                  kubelet, minikube  MountVolume.SetUp succeeded for volume "default-token-w57qz"
      Warning  Failed                 47m (x4 over 50m)    kubelet, minikube  Failed to pull image "gcr.io/k8s-minikube/storage-provisioner:v1.8.1": rpc error: code = Unknown desc = Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
      Warning  FailedSync             10m (x167 over 50m)  kubelet, minikube  Error syncing pod
      Normal   Pulling                5m (x13 over 50m)    kubelet, minikube  pulling image "gcr.io/k8s-minikube/storage-provisioner:v1.8.1"
      Normal   BackOff                20s (x198 over 50m)  kubelet, minikube  Back-off pulling image "gcr.io/k8s-minikube/storage-provisioner:v1.8.1"

其中的Image: gcr.io/k8s-minikube/storage-provisioner:v1.8.1告诉我们image信息是:gcr.io/k8s-minikube/storage-provisioner:v1.8.1
因此我们的解决办法也出来了:

    1. 给docker配置代理,通过代理来下载
    1. 如果不想配置代理也可以,可以去docker hub上搜索其他人的同步的image,下载下来以后在重新打tag就好了。

Kubernetes dashboard会使用下面的镜像

    gcr.io/google_containers/kubernetes-dashboard-amd64:v1.8.0
    gcr.io/k8s-minikube/storage-provisioner:v1.8.1
    gcr.io/google-containers/kube-addon-manager:v6.4-beta.2
    gcr.io/google_containers/pause-amd64:3.0
    gcr.io/google_containers/k8s-dns-kube-dns-amd64:1.14.5
    gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64:1.14.5
    gcr.io/google_containers/k8s-dns-sidecar-amd64:1.14.5

我使用的办法是自己通过我自己在国外的阿里云ecs服务器下载docker镜像,然后push到docker hub上

    docker pull gcr.io/google_containers/kubernetes-dashboard-amd64:v1.8.0
    docker pull gcr.io/k8s-minikube/storage-provisioner:v1.8.1
    docker pull gcr.io/google-containers/kube-addon-manager:v6.4-beta.2
    docker pull gcr.io/google_containers/pause-amd64:3.0
    docker pull gcr.io/google_containers/k8s-dns-kube-dns-amd64:1.14.5
    docker pull gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64:1.14.5
    docker pull gcr.io/google_containers/k8s-dns-sidecar-amd64:1.14.5
    docker tag gcr.io/google_containers/kubernetes-dashboard-amd64:v1.8.0 rollenholt/kubernetes-dashboard-amd64:v1.8.0
    docker push rollenholt/kubernetes-dashboard-amd64:v1.8.0
    docker tag gcr.io/k8s-minikube/storage-provisioner:v1.8.1 rollenholt/storage-provisioner:v1.8.1
    docker push rollenholt/storage-provisioner:v1.8.1
    docker tag gcr.io/google-containers/kube-addon-manager:v6.4-beta.2 rollenholt/kube-addon-manager:v6.4-beta.2
    docker push rollenholt/kube-addon-manager:v6.4-beta.2
    docker tag gcr.io/google_containers/pause-amd64:3.0 rollenholt/pause-amd64:3.0
    docker push rollenholt/pause-amd64:3.0
    docker tag gcr.io/google_containers/k8s-dns-kube-dns-amd64:1.14.5 rollenholt/k8s-dns-kube-dns-amd64:1.14.5
    docker push rollenholt/k8s-dns-kube-dns-amd64:1.14.5
    docker tag gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64:1.14.5 rollenholt/k8s-dns-dnsmasq-nanny-amd64:1.14.5
    docker push rollenholt/k8s-dns-dnsmasq-nanny-amd64:1.14.5
    docker tag gcr.io/google_containers/k8s-dns-sidecar-amd64:1.14.5 rollenholt/k8s-dns-sidecar-amd64:1.14.5
    docker push rollenholt/k8s-dns-sidecar-amd64:1.14.5

然后在minikube ssh中pull下来重新打tag
先通过minikube ssh登录

    ~ » minikube ssh
                             _             _
                _         _ ( )           ( )
      ___ ___  (_)  ___  (_)| |/')  _   _ | |_      __
    /' _ ` _ `\| |/' _ `\| || , <  ( ) ( )| '_`\  /'__`\
    | ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )(  ___/
    (_) (_) (_)(_)(_) (_)(_)(_) (_)`\___/'(_,__/'`\____)
    $

然后执行下面的命令:

    docker pull rollenholt/kubernetes-dashboard-amd64:v1.8.0
    docker tag rollenholt/kubernetes-dashboard-amd64:v1.8.0 gcr.io/google_containers/kubernetes-dashboard-amd64:v1.8.0
    docker pull rollenholt/storage-provisioner:v1.8.1
    docker tag rollenholt/storage-provisioner:v1.8.1 gcr.io/k8s-minikube/storage-provisioner:v1.8.1
    docker pull rollenholt/kube-addon-manager:v6.4-beta.2
    docker tag rollenholt/kube-addon-manager:v6.4-beta.2 gcr.io/google-containers/kube-addon-manager:v6.4-beta.2
    docker pull rollenholt/pause-amd64:3.0
    docker tag rollenholt/pause-amd64:3.0 gcr.io/google_containers/pause-amd64:3.0
    docker pull rollenholt/k8s-dns-kube-dns-amd64:1.14.5
    docker tag rollenholt/k8s-dns-kube-dns-amd64:1.14.5 gcr.io/google_containers/k8s-dns-kube-dns-amd64:1.14.5
    docker pull rollenholt/k8s-dns-dnsmasq-nanny-amd64:1.14.5
    docker tag rollenholt/k8s-dns-dnsmasq-nanny-amd64:1.14.5 gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64:1.14.5
    docker pull rollenholt/k8s-dns-sidecar-amd64:1.14.5
    docker tag rollenholt/k8s-dns-sidecar-amd64:1.14.5 gcr.io/google_containers/k8s-dns-sidecar-amd64:1.14.5

这几步骤弄完以后,重新启动集群:minikube start 在检查一下是否ok

    ~ » minikube start
    Starting local Kubernetes v1.8.0 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.
    ------------------------------------------------------------
    ~ »
    ------------------------------------------------------------
    ~ »
    ------------------------------------------------------------
    ~ » kubectl get pods --all-namespaces
    NAMESPACE     NAME                          READY     STATUS    RESTARTS   AGE
    kube-system   kube-addon-manager-minikube   1/1       Running   13         1h
    kube-system   kube-dns-86f6f55dd5-rn8cj     3/3       Running   47         1h
    kube-system   kubernetes-dashboard-6lfgx    1/1       Running   10         1h
    kube-system   storage-provisioner           1/1       Running   10         1h
    ------------------------------------------------------------
    ~ » minikube dashboard
    Opening kubernetes dashboard in default browser...

按照上面的步骤,你就可以在浏览器中看到http://192.168.99.100:30000/#!/overview?namespace=default

本文版权归作者所有,禁止一切形式的转载,复制等操作
赞赏

微信赞赏支付宝赞赏