minikube安装

k8s使用过多次,但就是一直没有使用minikube,一时兴起就玩一下,部署过程中出现相关问题进行了一下记录。

从官方网地址的使用手册开始

https://minikube.sigs.k8s.io/docs/start/

运行环境:

ubuntu 18.04lts vmware虚拟机 网络环境使用nat

在这里插入图片描述

运行前准备:

sudo apt  install curl
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd64.deb
sudo dpkg -i minikube_latest_amd64.deb

启动集群:

通过终端来启动集群,但要求不使用root账号。

minikube start

报错:

* minikube v1.24.0 on Ubuntu 18.04
* Unable to pick a default driver. Here is what was considered, in preference order:
  - docker: Not installed: exec: "docker": executable file not found in $PATH
  - kvm2: Not installed: exec: "virsh": executable file not found in $PATH
  - podman: Not installed: exec: "podman": executable file not found in $PATH
  - vmware: Not installed: exec: "docker-machine-driver-vmware": executable file not found in $PATH
  - virtualbox: Not installed: unable to find VBoxManage in $PATH

X Exiting due to DRV_NOT_DETECTED: No possible driver was detected. Try specifying --driver, or see https://minikube.sigs.k8s.io/docs/start/

这里报错原因是没有驱动,我这里选择dokcer作驱动,不同环境根据实际情况进行选择。

sudo apt install docker.io -y 

kuber@ubuntu:~$ docker images
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.24/images/json: dial unix /var/run/docker.sock: connect: permission denied

解决方法:

sudo su - 
usermod -aG docker  kuber   #把docker添加到kuber用户中
newgrp docker  #docker用户组添加生效
root@ubuntu:~# usermod -aG docker kuber
root@ubuntu:~# id kuber
uid=1000(kuber) gid=1000(kuber) groups=1000(kuber),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),116(lpadmin),126(sambashare),127(docker)
root@ubuntu:~# newgrp docker
root@ubuntu:~# su - kuber
kuber@ubuntu:~$ docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
kuber@ubuntu:~$ 

#完成docker驱动的安装,别的驱动待后面研究看看。

驱动配置:

相关参考地址https://minikube.sigs.k8s.io/docs/drivers/

kuber@ubuntu:~$ minikube start --driver=docker
* minikube v1.24.0 on Ubuntu 18.04
* Using the docker driver based on user configuration
* Starting control plane node minikube in cluster minikube
* Pulling base image ...
* Downloading Kubernetes v1.22.3 preload ...
    > index.docker.io/kicbase/sta...: 355.78 MiB / 355.78 MiB  100.00% 345.16 K
    > preloaded-images-k8s-v13-v1...: 447.64 MiB / 501.73 MiB  89.22% 687.86 Ki! minikube was unable to download gcr.io/k8s-minikube/kicbase:v0.0.28, but successfully downloaded docker.io/kicbase/stable:v0.0.28 as a fallback image
    > preloaded-images-k8s-v13-v1...: 501.73 MiB / 501.73 MiB  100.00% 430.35 K
* Creating docker container (CPUs=2, Memory=2200MB) ...
! This container is having trouble accessing https://k8s.gcr.io
* To pull new external images, you may need to configure a proxy: https://minikube.sigs.k8s.io/docs/reference/networking/proxy/
* Preparing Kubernetes v1.22.3 on Docker 20.10.8 ...
  - Generating certificates and keys ...
  - Booting up control plane ...
  - Configuring RBAC rules ...
* Verifying Kubernetes components...
  - Using image gcr.io/k8s-minikube/storage-provisioner:v5
* Enabled addons: storage-provisioner, default-storageclass
* kubectl not found. If you need it, try: 'minikube kubectl -- get pods -A'
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
kuber@ubuntu:~$ docker images
REPOSITORY       TAG       IMAGE ID       CREATED       SIZE
kicbase/stable   v0.0.28   e2a6c047bedd   6 weeks ago   1.08GB
#这里可能会报无法下载镜像的问题,可以使用以下命令来解决:
minikube start --image-mirror-country=cn --iso-url=https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/iso/minikube-v1.8.0.iso --registry-mirror=https://mirror.aliyuncs.com

设置默认驱动:

minikube config set driver docker

删除minikube所有数据,相当于初始化

kuber@ubuntu:~$ minikube delete --all=false --purge=false
* Deleting "minikube" in docker ...
* Deleting container "minikube" ...
* Removing /home/kuber/.minikube/machines/minikube ...
* Removed all traces of the "minikube" cluster.
kuber@ubuntu:~$ 

minikube简单使用

kubectl两种安装方法

方法一:

minikube kubectl   #会自动安装
正式使用命令格式是:
minikube kubectl -- get po -A
为了方便使用可以:
alias kubectl="minikube kubectl --"

方法二:

用 curl 在 Linux 系统中安装 kubectl

  1. 用以下命令下载最新发行版:

    curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
    

    说明:

    如需下载某个指定的版本,请用指定版本号替换该命令的这一部分: $(curl -L -s https://dl.k8s.io/release/stable.txt)

    例如,要在 Linux 中下载 v1.22.0 版本,请输入:

    curl -LO https://dl.k8s.io/release/v1.22.0/bin/linux/amd64/kubectl
    
  2. 验证该可执行文件(可选步骤)

    下载 kubectl 校验和文件:

    curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
    

    基于校验和文件,验证 kubectl 的可执行文件:

    echo "$(<kubectl.sha256) kubectl" | sha256sum --check
    

    验证通过时,输出为:

    kubectl: OK
    

    验证失败时,sha256 将以非零值退出,并打印如下输出:

    kubectl: FAILED
    sha256sum: WARNING: 1 computed checksum did NOT match
    

    说明:

    下载的 kubectl 与校验和文件版本必须相同。

  3. 安装 kubectl

    sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
    

    说明:

    即使你没有目标系统的 root 权限,仍然可以将 kubectl 安装到目录 ~/.local/bin 中:

    chmod +x kubectl
    mkdir -p ~/.local/bin/kubectl
    mv ./kubectl ~/.local/bin/kubectl
    # 之后将 ~/.local/bin/kubectl 添加到 $PATH
    
  4. 执行测试,以保障你安装的版本是最新的:

    kubectl version --client
    
minikube version #查看版本信息
kubectl version #查看kubectl版本与k8s服务端的版本
kubectl cluster-info  #查看集群信息
kubectl get node #查看各个节点信息
kuber@ubuntu:~$ minikube dashboard
* Enabling dashboard ...
  - Using image kubernetesui/dashboard:v2.3.1
  - Using image kubernetesui/metrics-scraper:v1.0.7
* Verifying dashboard health ...
* Launching proxy ...
* Verifying proxy health ...
* Opening http://127.0.0.1:40985/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: www-browser: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: links2: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: elinks: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: links: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: lynx: not found
/usr/bin/xdg-open: 851: /usr/bin/xdg-open: w3m: not found
xdg-open: no method available for opening 'http://127.0.0.1:40985/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/'

X Exiting due to HOST_BROWSER: failed to open browser: exit status 3

如上的报错是因为需要在桌面窗口上执行,无法在终端执行。

使用例子

kubectl create deployment hello-minikube --image=e2eteam/echoserver:2.2 #这里的镜像是要可以下载开行,也就是网络可达
kubectl get pod -A -o wide 

查看是否成功开放端口

kubectl get services hello-minikube
kuber@ubuntu:~$ kubectl get svc 
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
hello-minikube   NodePort    10.106.122.115   <none>        8080:31456/TCP   46s   #8080是群集端口,31456才是对外端口
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP          3h20m

使用minikube启动一个web页面

minikube service hello-minikube

我这里使用的是虚拟机,发现从主机是无法访问这个页面的

netstat -lnpt |grep 31456  #发现没有开放相关的端口。

然后我登录到minikube集群,发现端口是有开放的,所以说这个31456对外端口是开放在容器里面,而8080端口是开放在容器的窗口集群里面。

生产环境的kubeclt是直接安装在服务器上的,并不像minikube是安装到容器里面,这样子才能把端口真正的暴露出来。

生产环境的k8s是需要在服务器上面安装kubelet kubeadm kubectl.

安装好后我们需要通过kubeadm 来对集群进行初始化,完成后我们会在~/.kube/目录下面得到config配置文件,在里面告诉我们的kubectl去哪里找k8s集群聊天。

当kubectl与k8s聊天产生服务后,kubelet就收到命令开始干活,它会根据相关要求在集群里面找到对应的服务器下载镜像,运行镜像,运行服务。

minikube其实官方也说了只是用来学习的,k8s集群要求多台服务器,而它只要一台就能完成部署要求。

minikube它是要下载iso镜像的,我也叫它镜像,启动minikube其实相当于驱动(前面说了驱动有很多种)运行起一个容器,而这个容器相当于生产环境中简化后的集群(一个容器运行一个集群),通过以下命令就可以登录这个简化集群

minikube ssh
#查看集群运行的容器
#简单查看
docker ps 
#通过kubectl查看
mkdir ~/.kube && cd ~/.kube
sudo cp /etc/kubernetes/admin.conf ./config
sudo chown docker:docker admin.conf 
export PATH=$PATH:/var/lib/minikube/binaries/v1.22.3/   #注意版本号,不同版本不一样。

运行结果:

docker@minikube:~/.kube$ kubectl get pod -A -o wide 
NAMESPACE              NAME                                         READY   STATUS    RESTARTS      AGE     IP             NODE       NOMINATED NODE   READINESS GATES
default                hello-minikube-75f455f687-prjlp              1/1     Running   0             52m     172.17.0.4     minikube   <none>           <none>
kube-system            coredns-78fcd69978-mn5q5                     1/1     Running   1 (85m ago)   4h9m    172.17.0.2     minikube   <none>           <none>
kube-system            etcd-minikube                                1/1     Running   1 (85m ago)   4h9m    192.168.49.2   minikube   <none>           <none>
kube-system            kube-apiserver-minikube                      1/1     Running   1 (85m ago)   4h9m    192.168.49.2   minikube   <none>           <none>
kube-system            kube-controller-manager-minikube             1/1     Running   1 (85m ago)   4h9m    192.168.49.2   minikube   <none>           <none>
kube-system            kube-proxy-kvjwc                             1/1     Running   1 (85m ago)   4h9m    192.168.49.2   minikube   <none>           <none>
kube-system            kube-scheduler-minikube                      1/1     Running   1 (85m ago)   4h9m    192.168.49.2   minikube   <none>           <none>
kube-system            storage-provisioner                          1/1     Running   3 (84m ago)   4h9m    192.168.49.2   minikube   <none>           <none>
kubernetes-dashboard   dashboard-metrics-scraper-5594458c94-nqt8s   1/1     Running   1 (85m ago)   3h57m   172.17.0.5     minikube   <none>           <none>
kubernetes-dashboard   kubernetes-dashboard-654cf69797-998ks        1/1     Running   2 (84m ago)   3h57m   172.17.0.3     minikube   <none>           <none>
docker@minikube:~/.kube$ 

最后还是要强化一个结论,kubectl这个程序在网络可达的情况下是可以任意漂移的,想部署哪里就哪里。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐