使用KVM在CentOS 8/CentOS 7上安装Minikube Kubernetes

时间:2020-02-23 14:30:47  来源:igfitidea点击:

本教程旨在在带有KVM Hypervisor的CentOS 8/CentOS 7上安装Minikube。 Minikube是一个开放源代码工具,旨在使开发人员和系统管理员可以在几分钟内在其本地计算机Laptops,Desktop工作站中引导单个节点Kubernetes集群。这是用于开发和POC的理想选择,但不适用于运行生产工作负载。

简而言之,Minikube打包并配置了Linux VM,然后将Docker和所有Kubernetes组件安装到其中。我们可以通过主机系统中运行的kubectl管理和部署应用程序。

Kubernetes支持的功能

我们可以从在Minikube中运行的Kubernetes运行的一些功能包括:

DNSNodePortsConfigMaps和SecretsDashboards容器运行时:Docker,CRI-O和容器化启用CNI(容器网络接口)IngressPersistenthostPath类型的卷

Minikube支持VirtualBox和KVM虚拟机管理程序。但是,本指南适用于在CentOS 8/CentOS 7 Linux机器上运行带有KVM的Minikube。

更新系统

运行以下命令以将所有系统软件包更新到最新版本:

sudo yum -y update

安装KVM虚拟机监控程序

如前所述,最好将KVM用作Minikube VM的首选Hypervisor。这是我们在CentOS/RHEL 8上安装KVM的完整指南。

确认libvirtd服务正在运行。

$systemctl status libvirtd
● libvirtd.service - Virtualization daemon
   Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2017-01-20 14:33:07 EAT; 1s ago
     Docs: man:libvirtd(8)
           https://libvirt.org
 Main PID: 20569 (libvirtd)
    Tasks: 20 (limit: 32768)
   Memory: 70.4M
   CGroup: /system.slice/libvirtd.service
           ├─ 2653 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
           ├─ 2654 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
           └─20569 /usr/sbin/libvirtd

Jan 20 14:33:07 cent8.localdomain systemd[1]: Starting Virtualization daemon...
Jan 20 14:33:07 cent8.localdomain systemd[1]: Started Virtualization daemon.
Jan 20 14:33:08 cent8.localdomain dnsmasq[2653]: read /etc/hosts - 2 addresses
Jan 20 14:33:08 cent8.localdomain dnsmasq[2653]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addresses
Jan 20 14:33:08 cent8.localdomain dnsmasq-dhcp[2653]: read /var/lib/libvirt/dnsmasq/default.hostsfile

如果安装后未运行,请启动并将其设置为在启动时启动。

sudo systemctl enable --now libvirtd

用户应属于libvirt组。

sudo usermod -a -G libvirt $(whoami)
newgrp libvirt

打开文件/etc/libvirt/libvirtd.conf进行编辑。

sudo vi /etc/libvirt/libvirtd.conf

将UNIX域套接字组所有权设置为libvirt(在line85附近)

unix_sock_group = "libvirt"

设置R/W套接字的UNIX套接字权限(在第102行附近)

unix_sock_rw_perms = "0770"

进行更改后,重新启动libvirt守护程序。

sudo systemctl restart libvirtd.service

下载minikube

我们需要下载minikube二进制文件。我将把二进制文件放在/usr/local/bin目录下,因为它在$PATH内部。

sudo yum -y install wget
wget https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x minikube-linux-amd64
sudo mv minikube-linux-amd64 /usr/local/bin/minikube

确认在系统上安装了Minikube。

$minikube version
minikube version: v1.6.2
commit: 54f28ac5d3a815d1196cd5d57d707439ee4bb392

安装kubectl

我们需要kubectl,这是用于在Kubernetes上部署和管理应用程序的命令行工具。

curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl

给该文件可执行文件位,然后移动到PATH中的某个位置。

chmod +x kubectl
sudo mv kubectl  /usr/local/bin/

确认安装的kubectl版本。

$kubectl version --client -o json
 {
   "clientVersion": {
     "major": "1",
     "minor": "17",
     "gitVersion": "v1.17.1",
     "gitCommit": "d224476cd0730baca2b6e357d144171ed74192d6",
     "gitTreeState": "clean",
     "buildDate": "2017-01-14T21:04:32Z",
     "goVersion": "go1.13.5",
     "compiler": "gc",
     "platform": "linux/amd64"
   }
 }

启动minikube

现在已经安装了组件,我们可以启动minikube。将为Kubernetes单节点群集下载并配置VM镜像。

有关选项列表,请运行:

$minikube start --help

要使用默认选项创建一个minikube VM,请运行:

$minikube start

要使用的默认容器运行时是docker,但我们也可以使用crio或者containerd:

$minikube start --container-runtime=cri
$minikube start --container-runtime=containerd

安装程序将自动检测KVM并下载KVM驱动程序。

* minikube v1.6.2 on Centos 8.1.1911
* Automatically selected the 'kvm2' driver (alternates: [none])
* Downloading driver docker-machine-driver-kvm2:
    > docker-machine-driver-kvm2.sha256: 65 B/65 B [-------] 100.00% ? p/s 0s
    > docker-machine-driver-kvm2: 13.86 MiB/13.86 MiB  100.00% 791.09 KiB p/s
* Downloading VM boot image ...

如果我们有多个管理程序,请指定它。

$minikube start --vm-driver kvm2

请注意,已安装Kubernetes的最新稳定版本。使用--kubernetes-version标志来指定要安装的版本。例:

--kubernetes-version='1.16.5'

等待下载和设置完成,然后确认一切正常。

$minikube start
* minikube v1.6.2 on Centos 8.1.1911
* Automatically selected the 'kvm2' driver (alternates: [none])
* Downloading driver docker-machine-driver-kvm2:
    > docker-machine-driver-kvm2.sha256: 65 B/65 B [-------] 100.00% ? p/s 0s
    > docker-machine-driver-kvm2: 13.86 MiB/13.86 MiB  100.00% 791.09 KiB p/s
* Downloading VM boot image ...
    > minikube-v1.6.0.iso.sha256: 65 B/65 B [--------------] 100.00% ? p/s 0s
    > minikube-v1.6.0.iso: 150.93 MiB/150.93 MiB [] 100.00% 1.27 MiB p/s 2m0s
* Creating kvm2 VM (CPUs=2, Memory=2000MB, Disk=20000MB) ...
* Preparing Kubernetes v1.17.0 on Docker '19.03.5' ...
* Downloading kubelet v1.17.0
* Downloading kubeadm v1.17.0
* Pulling images ...
* Launching Kubernetes ... 
* Waiting for cluster to come online ...
* Done! kubectl is now configured to use "minikube"

Minikube基本操作

kubectl命令行工具配置为使用minikube。

要检查集群状态,请运行:

$kubectl cluster-info
Kubernetes master is running at https://192.168.39.2:8443
KubeDNS is running at https://192.168.39.2:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

Minikube配置文件位于~/.minikube/machines/minikube/config.json下

要查看配置,请使用:

$kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /home/jkmutai/.minikube/ca.crt
    server: https://192.168.39.2:8443
  name: minikube
contexts:
- context:
    cluster: minikube
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: /home/jkmutai/.minikube/client.crt
    client-key: /home/jkmutai/.minikube/client.key

要检查正在运行的节点:

$kubectl get nodes
NAME       STATUS   ROLES    AGE     VERSION
minikube   Ready    master   2m58s   v1.17.0

使用ssh访问minikube VM:

$minikube ssh

                         _             _            
            _         _ ( )           ( )           
  ___ ___  (_)  ___  (_)| |/')  _   _ | |_      __  
/' _ ` _ `\| |/' _ `\| || , <  ( ) ( )| '_`\  /'__`\
| ( ) ( ) || || ( ) || || |\`\ | (_) || |_) )(  ___/
(_) (_) (_)(_)(_) (_)(_)(_) (_)`___/'(_,__/'`____)

$sudo su 

要停止正在运行的本地kubernetes集群,请运行:

$minikube stop
* Stopping "minikube" in kvm2 ...
* "minikube" stopped.

要启动VM,请运行:

$minikube start
* minikube v1.6.2 on Centos 8.1.1911
* Selecting 'kvm2' driver from existing profile (alternates: [none])
* Tip: Use 'minikube start -p <name>' to create a new cluster, or 'minikube delete' to delete this one.
* Starting existing kvm2 VM for "minikube" ...
* Waiting for the host to be provisioned ...
* Preparing Kubernetes v1.17.0 on Docker '19.03.5' ...
* Launching Kubernetes ... 
* Done! kubectl is now configured to use "minikube"

要删除本地kubernetes集群,请使用:

$minikube delete

启用Kubernetes仪表板

Kubernetes随附有一个Web面板,我们可以在不与命令行交互的情况下管理集群。仪表板插件在minikube上默认安装并启用。

$minikube addons list

- addon-manager: enabled
- coredns: disabled
- dashboard: enabled
- default-storageclass: enabled
- efk: disabled
- freshpod: disabled
- heapster: disabled
- ingress: disabled
- kube-dns: enabled
- metrics-server: disabled
- registry: disabled
- registry-creds: disabled
- storage-provisioner: enabled

要直接在默认浏览器上打开,请使用:

$minikube dashboard

获取仪表盘的URL

$minikube dashboard --url
http://192.168.39.117:30000

通过在我们喜欢的浏览器上打开URL来访问Kubernetes仪表板。要进一步阅读,请查看:

Hello Minikube系列:https://kubernetes.io/docs/tutorials/stateless-application/hello-minikube/

针对新手的Minikube指南:https://kubernetes.io/docs/getting-started-guides/minikube /