Kubernetes(二):Ubuntu上搭建k8s环境

前言

本文主要介绍在阿里云ecs实例上搭建自建Kubernetes环境的步骤,参照的是k8s官网上minikube的安装教程。

安装k8s

查阅网上资料得知,安装k8s集群一般有几种方式:

  • kubeadm(适用于生产环境)
  • Docker Desktop(如果是本地桌面系统推荐这种方式)
  • minikube(本地快速单机部署,体验最简版的k8s,不推荐在生产环境上使用)

本文选择安装minikube的方式搭建最简k8s环境,官方教程:https://minikube.sigs.k8s.io/docs/start/

安装前注意

通过阅读官方教程得知,用minikube搭建k8s环境时,既支持通过虚拟化技术运行,也可以选择不通过虚拟化技术直接运行在服务器上。所以对于一些不支持虚拟化技术的云服务器实例,只能通过后者安装运行k8s,也无须安装KVMVirtualBox等虚拟化工具,不过还是依赖于Docker环境,所以需要先安装Docker,具体可参考Ubuntu上安装Docker

Note: Minikube also supports a --vm-driver=none option that runs the Kubernetes components on the host and not in a VM. Using this driver requires Docker and a Linux environment but not a hypervisor. It is recommended to use the apt installation of docker from Docker, when using the none driver. The snap installation of docker does not work with minikube.

MiniKube安装步骤

https://kubernetes.io/docs/tasks/tools/install-minikube/

1. 安装kubectl

kubectl是k8s的命令行工具,通过kubectl对k8s进行操作:
https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl-on-linux

# 下载kubectl
$ curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.17.0/bin/linux/amd64/kubectl
$ chmod +x ./kubectl
# 安装到/usr/local/bin/目录
$ sudo mv ./kubectl /usr/local/bin/kubectl

2. 安装hypervisor(略)

阿里云ECS不支持虚拟化技术,略过

3. 安装minikube

# 下载最新版本minikube并赋予执行权限
$ curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 \
  && chmod +x minikube
# 安装到/usr/local/bin/目录
$ sudo mkdir -p /usr/local/bin/
$ sudo install minikube /usr/local/bin/
# 确认安装成功
$ kubectl version --client

4. 启动k8s

$ minikube start --vm-driver=none

输入命令minikube start --vm-driver=none启动kubernetes,注意这里--vm-driver参数使用none,前提是要求本机有安装docker,不然会报错:

💣  Unable to start VM. Please investigate and run 'minikube delete' if possible: create: precreate: exec: "docker": executable file not found in $PATH

初次执行大多数情况会报错启动失败,原因是minikube启动k8s环境时需要从k8s.gcr.io拉取kube-apiserverkube-controller-managerkube-scheduler等docker镜像进行部署。而由于墙的问题从阿里云的云端机器上是无法访问k8s.gcr.io的。

$ minikube start --help
...
--image-repository='': Alternative image repository to pull docker images from. This can be used when you have
limited access to gcr.io. Set it to "auto" to let minikube decide one for you. For Chinese mainland users, you may use
local gcr.io mirrors such as registry.cn-hangzhou.aliyuncs.com/google_containers
...

通过minikube的帮助文档能看到,对于天朝的用户可以直接用阿里云的镜像地址registry.cn-hangzhou.aliyuncs.com/google_containers代替k8s.gcr.io

$ minikube start --vm-driver=none --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers
😄  minikube v1.6.2 on Ubuntu 16.04
✨  Selecting 'none' driver from user configuration (alternates: [])
✅  Using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
🤹  Running on localhost (CPUs=2, Memory=3951MB, Disk=40188MB) ...
ℹ️   OS release is Ubuntu 16.04.6 LTS
🐳  Preparing Kubernetes v1.17.0 on Docker '19.03.5' ...
💾  Downloading kubeadm v1.17.0
💾  Downloading kubelet v1.17.0
🚜  Pulling images ...
🚀  Launching Kubernetes ...
🤹  Configuring local host environment ...

⚠️  The 'none' driver provides limited isolation and may reduce system security and reliability.
⚠️  For more information, see:
👉  https://minikube.sigs.k8s.io/docs/reference/drivers/none/

⚠️  kubectl and minikube configuration will be stored in /root
⚠️  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

💡  This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
⌛  Waiting for cluster to come online ...
🏄  Done! kubectl is now configured to use "minikube"

出现上面的日志表示minikube已经部署完毕,随后查看k8s的运行状态

# 可以看到minikube在docker中启动了很多k8s相关的container
$ docker container ls
# 查看minikube的运行状态
$ minikube status

后记

minikube提供了一种简便快捷的方式在单机中完成k8s集群的部署,默认使用虚拟化技术(或者docker)模拟多节点部署,为学习研究k8s提供了巨大的方便。
另外使用docker运行minikube会存在一些安全性或兼容性问题,需要注意的地方:https://minikube.sigs.k8s.io/docs/reference/drivers/none/

推荐阅读更多精彩内容