1 - minikube

Minikube是Kubernetes的一个版本,由Kubernetes开源社区开发,专为本地部署而设计,是一种可以让你在本地轻松运行 Kubernetes 的工具。
Minikube使用VM来创建本地单节点Kubernetes安装,因此需先确保安装了类似VirtualBox、Hyper-V或KVM等VM平台。
通过Minikube可以在笔记本电脑上的集群中运行单节点 Kubernetes 集群, 供那些希望尝试 Kubernetes 或进行日常开发的用户使用。
但Minikube只能局限于包含一个节点的本地集群,因此对于生产级别的多节点Kubernetes集群来说,很难在本地进行模拟测试。
Minikube 支持以下 Kubernetes 功能:

  • DNS
  • NodePorts
  • ConfigMaps 和 Secrets
  • Dashboards
  • 容器运行时: Docker、CRI-O 以及 containerd
  • 启用 CNI (容器网络接口)
  • Ingress
  • 安装

2 - 环境准备

已安装docker

  1. [anliven@anliven ~]$ uname -a
  2. Linux anliven 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
  3. [anliven@anliven ~]$
  4. [anliven@anliven ~]$ cat /etc/system-release
  5. CentOS Linux release 7.8.2003 (Core)
  6. [anliven@anliven ~]$
  7. [anliven@anliven ~]$ docker version
  8. Client: Docker Engine - Community
  9. Version: 19.03.11
  10. API version: 1.40
  11. Go version: go1.13.10
  12. Git commit: 42e35e61f3
  13. Built: Mon Jun 1 09:13:48 2020
  14. OS/Arch: linux/amd64
  15. Experimental: false
  16. Server: Docker Engine - Community
  17. Engine:
  18. Version: 19.03.11
  19. API version: 1.40 (minimum version 1.12)
  20. Go version: go1.13.10
  21. Git commit: 42e35e61f3
  22. Built: Mon Jun 1 09:12:26 2020
  23. OS/Arch: linux/amd64
  24. Experimental: false
  25. containerd:
  26. Version: 1.2.13
  27. GitCommit: 7ad184331fa3e55e52b890ea95e65ba581ae3429
  28. runc:
  29. Version: 1.0.0-rc10
  30. GitCommit: dc9208a3303feef5b3839f4323d9beb36df0a9dd
  31. docker-init:
  32. Version: 0.18.0
  33. GitCommit: fec3683
  34. [anliven@anliven ~]$

3 - 用户权限设置

  1. [root@anliven ~]# visudo
  2. [root@anliven ~]#
  3. [root@anliven ~]# cat /etc/sudoers |grep anliven
  4. anliven ALL=(ALL) NOPASSWD: ALL
  5. [root@anliven ~]#
  6. [root@anliven ~]# exit
  7. [anliven@anliven ~]# sudo usermod -aG docker $USER && newgrp docker

4 - 安装kubectl

使用 kubectl 与集群进行交互。
命令

  1. curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
  2. chmod +x kubectl
  3. sudo mv ./kubectl /usr/local/bin/kubectl
  4. kubectl version --client

示例

  1. [anliven@anliven K8s]$ pwd
  2. /home/anliven/K8s
  3. [anliven@anliven K8s]$
  4. [anliven@anliven K8s]$ curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
  5. % Total % Received % Xferd Average Speed Time Time Time Current
  6. Dload Upload Total Spent Left Speed
  7. 100 41.0M 100 41.0M 0 0 2819k 0 0:00:14 0:00:14 --:--:-- 2832k
  8. [anliven@anliven K8s]$
  9. [anliven@anliven K8s]$ chmod +x kubectl
  10. [anliven@anliven K8s]$
  11. [anliven@anliven K8s]$ sudo mv ./kubectl /usr/local/bin/kubectl
  12. [anliven@anliven K8s]$
  13. [anliven@anliven K8s]$ ls -l /usr/local/bin/kubectl
  14. -rwxrwxr-x 1 anliven anliven 43003904 Oct 14 22:43 /usr/local/bin/kubectl
  15. [anliven@anliven K8s]$
  16. [anliven@anliven K8s]$ kubectl version --client
  17. Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.3", GitCommit:"1e11e4a2108024935ecfcb2912226cedeafd99df", GitTreeState:"clean", BuildDate:"2020-10-14T12:50:19Z", GoVersion:"go1.15.2", Compiler:"gc", Platform:"linux/amd64"}
  18. [anliven@anliven K8s]$

kubectl命令帮助

  1. kubectl -h # 查看子命令列表
  2. kubectl options # 查看全局选项
  3. Use "kubectl <command> --help" for more information about a given command.
  4. kubectl <command> --help # 查看子命令的帮助
  5. Use "kubectl options" for a list of global command-line options (applies to all commands).
  6. kubectl [command] [PARAMS] -o=<format> # 设置输出格式(如 json、yaml、jsonpath 等)
  7. kubectl explain RESOURCE [options] # 查看资源的定义
  8. kubectl explain -h
  9. kubectl get [flags] [options] # 显示一个或多个资源
  10. kubectl get -h

5 - 安装minikube

命令

  1. curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
  2. sudo install minikube-linux-amd64 /usr/local/bin/minikube
  3. minikube version

示例

  1. [anliven@anliven K8s]$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
  2. % Total % Received % Xferd Average Speed Time Time Time Current
  3. Dload Upload Total Spent Left Speed
  4. 100 56.0M 100 56.0M 0 0 2510k 0 0:00:22 0:00:22 --:--:-- 2234k
  5. [anliven@anliven K8s]$
  6. [anliven@anliven K8s]$ sudo install minikube-linux-amd64 /usr/local/bin/minikube
  7. [anliven@anliven K8s]$
  8. [anliven@anliven K8s]$ ls -l /usr/local/bin/minikube
  9. -rwxr-xr-x 1 root root 58733392 Oct 14 22:46 /usr/local/bin/minikube
  10. [anliven@anliven K8s]$
  11. [anliven@anliven K8s]$ minikube version
  12. minikube version: v1.14.0
  13. commit: b09ee50ec047410326a85435f4d99026f9c4f5c4

6 - 从github下载并安装kubectl和minikube

如果无法访问google站点,也可以从github下载并安装kubectl和minikube。

kubectl

  1. 访问 https://github.com/kubernetes/kubernetes/releases
  2. 点击类似CHANGELOG-*.*.md的链接,在“Client Binaries”区域找到匹配的二进制包,
  3. 下载并解压,然后放入/usr/local/bin目录

minikube

找到匹配的二进制包并下载: https://github.com/kubernetes/minikube/releases/

  1. curl -Lo minikube https://github.com/kubernetes/minikube/releases/download/v1.14.0/minikube-linux-arm64
  2. chmod +x minikube
  3. sudo mv minikube /usr/local/bin

也可以使用阿里云的minikube地址: https://github.com/AliyunContainerService/minikube

  1. curl -Lo minikube https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.13.0/minikube-linux-amd64
  2. chmod +x minikube
  3. sudo mv minikube /usr/local/bin/

7 - 启动与运行minikube

示例

  1. [anliven@anliven ~]$ minikube start
  2. * minikube v1.14.0 on Centos 7.8.2003 (vbox/amd64)
  3. * Automatically selected the docker driver
  4. * Starting control plane node minikube in cluster minikube
  5. * Pulling base image ... # 第一次会拉取基础镜像,用时较久
  6. * Creating docker container (CPUs=2, Memory=2200MB) ...
  7. * Preparing Kubernetes v1.19.2 on Docker 19.03.8 ...
  8. > kubelet.sha256: 65 B / 65 B [--------------------------] 100.00% ? p/s 0s
  9. > kubeadm.sha256: 65 B / 65 B [--------------------------] 100.00% ? p/s 0s
  10. > kubectl.sha256: 65 B / 65 B [--------------------------] 100.00% ? p/s 0s
  11. > kubeadm: 37.30 MiB / 37.30 MiB [-------------] 100.00% 822.08 KiB p/s 47s
  12. > kubectl: 41.01 MiB / 41.01 MiB [-------------] 100.00% 881.98 KiB p/s 48s
  13. > kubelet: 104.88 MiB / 104.88 MiB [------------] 100.00% 1.58 MiB p/s 1m6s
  14. * Verifying Kubernetes components...
  15. * Enabled addons: storage-provisioner, default-storageclass
  16. * Done! kubectl is now configured to use "minikube" by default
  17. [anliven@anliven ~]$
  18. [anliven@anliven ~]$ docker images |grep k8s
  19. gcr.io/k8s-minikube/kicbase v0.0.13 90f1294ff9ac 2 weeks ago 800MB
  20. [anliven@anliven ~]$
  21. [anliven@anliven ~]$ docker ps
  22. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  23. 5d520392622e gcr.io/k8s-minikube/kicbase:v0.0.13 "/usr/local/bin/entr…" 4 hours ago Up 4 hours 127.0.0.1:32775->22/tcp, 127.0.0.1:32774->2376/tcp, 127.0.0.1:32773->5000/tcp, 127.0.0.1:32772->8443/tcp minikube
  24. [anliven@anliven ~]$

启动参数说明

  1. --driver 1.5.0版本开始,Minikube缺省使用本地最好的驱动来创建Kubernetes本地环境
  2. --image-mirror-country cn 将缺省利用 registry.cn-hangzhou.aliyuncs.com/google_containers 作为安装Kubernetes的容器镜像仓库
  3. --iso-url 指定镜像地址下载相应的 .iso 文件
  4. --registry-mirror 为了拉取Docker Hub镜像,需要为 Docker daemon 配置镜像加速
  5. --cpus=2 minikube虚拟机分配CPU核数
  6. --memory=2048mb minikube虚拟机分配内存数
  7. --kubernetes-version minikube 虚拟机将使用的 kubernetes 版本

8 - Minikube 命令

命令帮助

  1. Use "minikube <command> --help" for more information about a given command.
  2. Use "minikube options" for a list of global command-line options (applies to all commands).

命令选项

  1. [anliven@anliven ~]$ minikube options
  2. The following options can be passed to any command:
  3. --alsologtostderr=false: log to standard error as well as files
  4. -b, --bootstrapper='kubeadm': The name of the cluster bootstrapper that will set up the Kubernetes cluster.
  5. --log_backtrace_at=:0: when logging hits line file:N, emit a stack trace
  6. --log_dir='': If non-empty, write log files in this directory
  7. --logtostderr=false: log to standard error instead of files
  8. -p, --profile='minikube': The name of the minikube VM being used. This can be set to allow having multiple instances of minikube independently.
  9. --stderrthreshold=2: logs at or above this threshold go to stderr
  10. -v, --v=0: log level for V logs
  11. --vmodule=: comma-separated list of pattern=N settings for file-filtered logging
  12. [anliven@anliven ~]$

一些命令

Minikube 集群

  1. minikube version # 查看版本
  2. minikube status # 查看状态
  3. minikube profile list # 查看属性
  4. minikube addons list # 查看当前支持的插件
  5. minikube service list # 查看服务列表
  6. minikube node list # 参看添加的node
  7. minikube ssh # 登录集群
  8. minikube start # 启动集群
  9. minikube stop # 停止集群
  10. minikube delete # 删除集群
  11. minikube dashboard # 启动Dashboard
  12. rm-rf ~/.minikube # 重置(清理所有缓存的镜像,重头开始)

一些示例

  1. [anliven@anliven ~]$ minikube status # 查看集群状态
  2. minikube
  3. type: Control Plane
  4. host: Running
  5. kubelet: Running
  6. apiserver: Running
  7. kubeconfig: Configured
  8. [anliven@anliven ~]$
  9. [anliven@anliven ~]$ minikube profile list # 查看集群属性
  10. |----------|-----------|---------|--------------|------|---------|---------|
  11. | Profile | VM Driver | Runtime | IP | Port | Version | Status |
  12. |----------|-----------|---------|--------------|------|---------|---------|
  13. | minikube | docker | docker | 192.168.49.2 | 8443 | v1.19.2 | Running |
  14. |----------|-----------|---------|--------------|------|---------|---------|
  15. [anliven@anliven ~]$
  16. [anliven@anliven ~]$ minikube addons list # 查看集群当前支持的插件
  17. |-----------------------------|----------|--------------|
  18. | ADDON NAME | PROFILE | STATUS |
  19. |-----------------------------|----------|--------------|
  20. | ambassador | minikube | disabled |
  21. | csi-hostpath-driver | minikube | disabled |
  22. | dashboard | minikube | enabled |
  23. | default-storageclass | minikube | enabled |
  24. | efk | minikube | disabled |
  25. | freshpod | minikube | disabled |
  26. | gcp-auth | minikube | disabled |
  27. | gvisor | minikube | disabled |
  28. | helm-tiller | minikube | disabled |
  29. | ingress | minikube | disabled |
  30. | ingress-dns | minikube | disabled |
  31. | istio | minikube | disabled |
  32. | istio-provisioner | minikube | disabled |
  33. | kubevirt | minikube | disabled |
  34. | logviewer | minikube | disabled |
  35. | metallb | minikube | disabled |
  36. | metrics-server | minikube | disabled |
  37. | nvidia-driver-installer | minikube | disabled |
  38. | nvidia-gpu-device-plugin | minikube | disabled |
  39. | olm | minikube | disabled |
  40. | pod-security-policy | minikube | disabled |
  41. | registry | minikube | disabled |
  42. | registry-aliases | minikube | disabled |
  43. | registry-creds | minikube | disabled |
  44. | storage-provisioner | minikube | enabled |
  45. | storage-provisioner-gluster | minikube | disabled |
  46. | volumesnapshots | minikube | disabled |
  47. |-----------------------------|----------|--------------|
  48. [anliven@anliven ~]$
  49. [anliven@anliven ~]$ minikube service list # 查看集群服务列表
  50. |----------------------|---------------------------|--------------|-----|
  51. | NAMESPACE | NAME | TARGET PORT | URL |
  52. |----------------------|---------------------------|--------------|-----|
  53. | default | kubernetes | No node port |
  54. | kube-system | kube-dns | No node port |
  55. | kubernetes-dashboard | dashboard-metrics-scraper | No node port |
  56. | kubernetes-dashboard | kubernetes-dashboard | No node port |
  57. |----------------------|---------------------------|--------------|-----|
  58. [anliven@anliven ~]$
  59. [anliven@anliven ~]$ minikube ssh # 登录集群
  60. docker@minikube:~$ whoami
  61. docker
  62. docker@minikube:~$ exit
  63. logout
  64. [anliven@anliven ~]$
  65. [anliven@anliven ~]$ minikube stop # 停止集群
  66. * Stopping node "minikube" ...
  67. * Powering off "minikube" via SSH ...
  68. * 1 nodes stopped.
  69. [anliven@anliven ~]$
  70. [anliven@anliven ~]$ minikube status # 查看集群状态
  71. minikube
  72. type: Control Plane
  73. host: Stopped
  74. kubelet: Stopped
  75. apiserver: Stopped
  76. kubeconfig: Stopped
  77. [anliven@anliven ~]$
  78. [anliven@anliven ~]$ minikube start # 重新启动,不会“Pulling base image ...”
  79. * minikube v1.14.0 on Centos 7.8.2003 (vbox/amd64)
  80. * Using the docker driver based on existing profile
  81. * Starting control plane node minikube in cluster minikube
  82. * Restarting existing docker container for "minikube" ...
  83. * Preparing Kubernetes v1.19.2 on Docker 19.03.8 ...
  84. * Verifying Kubernetes components...
  85. * Enabled addons: default-storageclass, storage-provisioner, dashboard
  86. * Done! kubectl is now configured to use "minikube" by default
  87. [anliven@anliven ~]$
  88. [anliven@anliven ~]$ minikube status # 查看集群状态
  89. minikube
  90. type: Control Plane
  91. host: Running
  92. kubelet: Running
  93. apiserver: Running
  94. kubeconfig: Configured
  95. [anliven@anliven ~]$

启动Dashboard
K8s - Install Minikube in Linux - 图1

9 - 问题处理

1 - minikube启动失败

  1. [root@anliven K8s]# minikube start
  2. * minikube v1.14.0 on Centos 7.8.2003 (vbox/amd64)
  3. * Automatically selected the docker driver
  4. * The "docker" driver should not be used with root privileges.
  5. * If you are running minikube within a VM, consider using --driver=none:
  6. * https://minikube.sigs.k8s.io/docs/reference/drivers/none/
  7. X Exiting due to DRV_AS_ROOT: The "docker" driver should not be used with root privileges.

处理方法: 使用非root用户,并将此用户添加到docker组: sudo usermod -aG docker $USER && newgrp docker

2 - dashboard启动失败

  1. [anliven@anliven ~]$ minikube dashboard
  2. * Enabling dashboard ...
  3. * Verifying dashboard health ...
  4. * Launching proxy ...
  5. * Verifying proxy health ...
  6. * Opening http://127.0.0.1:37006/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...
  7. START /usr/bin/firefox "http://127.0.0.1:37006/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/"
  8. Failed to open connection to "session" message bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
  9. Running without a11y support!
  10. Error: no DISPLAY environment variable specified
  11. xdg-open: no method available for opening 'http://127.0.0.1:37006/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/'
  12. X Exiting due to HOST_BROWSER: failed to open browser: exit status 3
  13. [anliven@anliven ~]$

处理方法: 需要图形界面下的命令行中执行minikube dashboard,以便启动浏览器

10 - 参考信息