关于kubernetes:Kubernetes学习笔记使用Minikube快速部署K8S单机学习环境

44次阅读

共计 5446 个字符,预计需要花费 14 分钟才能阅读完成。

介绍

  • https://minikube.sigs.k8s.io/…

Minikube 用于疾速在本地搭建 Kubernetes 单节点集群环境,它对硬件资源没有太高的要求,不便开发人员学习试用,或者进行日常的开发。

其反对大部分 kubernetes 的性能,列表如下

  • DNS
  • NodePorts
  • ConfigMaps and Secrets
  • Dashboards
  • Container Runtime: Docker, and rkt
  • Enabling CNI (Container Network Interface)
  • Ingress

Minikube 反对 Windows、macOS、Linux 三种 OS,会依据平台不同,下载对应的虚拟机镜像,并在镜像内装置 k8s。

目前的虚拟机技术都是基于 Hypervisor 来实现的,Hypervisor 规定了对立的虚构层接口,由此 Minikube 就能够无缝切换不同的虚拟机实现,如 macOS 能够切换 hyperkit 或 VirtualBox,Windows 下能够切换 Hyper-V 或 VirtualBox 等。

虚拟机的切换能够通过 –vm-driver 实现,如 minikube start --vm-driver hyperkit/ minikube start --vm-driver hyperv

如果 Minikube 装置在内核原生就反对 LXC 的 OS 内,如 Ubuntu 等,再装置一次虚拟机显然就是对资源的节约了,Minikube 提供了间接对接 OS 底层的形式

  • driver!=none mode

    `In this case minikube provisions a new docker-machine (Docker daemon/Docker host) using any supported providers.
    For instance:
    a) local provider = your Windows/Mac local host: it frequently uses VirtualBox as a hypervisor, and creates inside it a VM based on boot2docker image (configurable). In this case k8s bootstraper (kubeadm) creates all Kubernetes components inside this isolated VM. In this setup you have usually two docker daemons, your local one for development (if you installed it prior), and one running inside minikube VM.
    b) cloud hosts – not supported by minikube`

  • driver=none mode

    `In this mode, your local docker host is re-used.
    In case no.1 there will be a performance penalty, because each VM generates some overhead, by running several system processes required by VM itself, in addition to those required by k8s components running inside VM. I think driver-mode=none is similar to ” HYPERLINK “https://blog.alexellis.io/be-kind-to-yourself/”kind” version of k8s boostraper, meant for doing CI/integration tests.`

Minikube 装置

下载 Minikube

  • https://kubernetes.io/docs/ta…

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

下载 Kubectl

  • https://kubernetes.io/docs/ta…

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/

启动 Minikube

启动参数

启动命令:minikube start "参数"

- --image-mirror-country cn 将缺省利用 registry.cn-hangzhou.aliyuncs.com/google_containers 作为装置 Kubernetes 的容器镜像仓库,- --iso-url=*** 利用阿里云的镜像地址下载相应的 .iso 文件
- --cpus=2: 为 minikube 虚拟机调配 CPU 核数
- --memory=2000mb: 为 minikube 虚拟机分配内存数
- --kubernetes-version=***: minikube 虚拟机将应用的 kubernetes 版本 ,e.g. --kubernetes-version v 1.17.3
- --docker-env http_proxy 传递代理地址

默认启动应用的是 VirtualBox 驱动,应用 --vm-driver 参数能够指定其它驱动
# https://minikube.sigs.k8s.io/docs/drivers/
- --vm-driver=none 示意用容器;- --vm-driver=virtualbox 示意用虚拟机;

留神: To use kubectl or minikube commands as your own user, you may need to relocate them. For example, to overwrite your own settings, run:

    sudo mv /root/.kube /root/.minikube $HOME
    sudo chown -R $USER $HOME/.kube $HOME/.minikube

示例

–vm-driver=kvm2

参考: https://minikube.sigs.k8s.io/…

minikube start --image-mirror-country cn --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --registry-mirror=https://ovfftd6p.mirror.aliyuncs.com --driver=kvm2

–vm-driver=hyperv
# 创立基于 Hyper- V 的 Kubernetes 测试环境
minikube.exe start --image-mirror-country cn \
    --iso-url=https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/iso/minikube-v1.5.0.iso \
    --registry-mirror=https://xxxxxx.mirror.aliyuncs.com \
    --vm-driver="hyperv" \
    --hyperv-virtual-switch="MinikubeSwitch" \
    --memory=4096
–vm-driver=none

sudo minikube start --image-mirror-country cn --vm-driver=none

sudo minikube start --vm-driver=none --docker-env http_proxy=http://$host_IP:8118 --docker-env https_proxy=https:// $host_IP:8118

其中 $host_IP 指的是 host 的 IP,能够通过 ifconfig 查看;比方在我这台机器是 10.0.2.15,用 virtualbox 部署,则用下列命令启动 minikube

sudo minikube start --vm-driver=none --docker-env http_proxy=http://10.0.2.15:8118 --docker-env https_proxy=https://10.0.2.15:8118

Minikube 状态查看

启动结束,将会运行一个单节点的 Kubernetes 集群。Minikube 也曾经把 kubectl 配置好,因而无需做额定的工作就能够治理容器。
Minikube 创立一个 Host-Only(仅主机模式)网络接口,通过这个接口能够路由到节点。如果要与运行的 pods 或 services 进行交互,你应该通过这个地址发送流量。应用 minikube ip 命令能够查看这个地址:

Minikube 应用

用户应用 Minikube CLI 治理虚拟机上的 Kubernetes 环境,比方:启动,进行,删除,获取状态等。一旦 Minikube 虚拟机启动,用户就能够应用相熟的 Kubectl CLI 在 Kubernetes 集群上执行操作

# 查看集群的所有资源
kubectl get all

# 进入节点服务器
minikube ssh

# 执行节点服务器命令,例如查看节点 docker info
minikube ssh -- docker info

# 删除集群, 删除 ~/.minikube 目录缓存的文件
minikube delete

# 敞开集群
minikube stop

## 销毁集群
minikube stop && minikube delete

Minikube 插件

sudo minikube addons list

Minikube 默认集成了 Kubernetes Dashboard。执行 minikube dashboard 命令后,默认会关上浏览器

装置遇到的问题

问题 -1

Failed to save config: failed to acquire lock for /root/.minikube/profiles/minikube/config.json: unable to open /tmp/juju-mk270d1b5db5965f2dc9e9e25770a63417031943: permission denied

解决办法:

sudo rm -rf /tmp/juju-mk*
sudo rm -rf /tmp/minikube.*

问题 -2

unable to read client-cert /root/.minikube/client.crt for minikube due to open /root/.minikube/client.crt: permission denied
unable to read client-key /root/.minikube/client.key for minikube due to open /root/.minikube/client.key: permission denied
unable to read certificate-authority /root/.minikube/ca.crt for minikube due to open /root/.minikube/ca.crt: permission denied

解决办法:

minikube stop
minikube delete
rm -rf ~/.kube
rm -rf ~/.minikube
sudo rm -rf /var/lib/minikube
sudo rm /var/lib/kubeadm.yaml
sudo rm -rf /etc/kubernetes

参考:https://stackoverflow.com/que…

问题 -3

Error restarting cluster: restarting kube-proxy: waiting for kube-proxy to be up for configmap update: timed out waiting for the condition

通过 minikube delete,minikube start 能够解决

部署利用

$ kubectl run hello-minikube --image=k8s.gcr.io/echoserver:1.10 --port=8080
deployment.apps/hello-minikube created

#拜访利用
$ kubectl expose deployment hello-minikube --type=NodePort
service/hello-minikube exposed

#获取服务地址
$ minikube service hello-minikube --url
http://192.168.99.105:30555

参考资料

  1. Minikube – Kubernetes 本地试验环境
  2. Hello Minikube
  3. Running Kubernetes Locally via Minikube
  4. Install Minikube

正文完
 0