minikube

2020年11月13日 作者 C0isini

minikube 是一个易于在本地运行 Kubernetes 的工具,可在你的笔记本电脑上的虚拟机内轻松创建单机版 Kubernetes 集群。

一、install

Centos

1、下载二进制文件

https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64

2、移动到 bin 目录(/usr/local/bin/),并重命名为 minikube

mv minikube-linux-amd64 /usr/local/bin/minikube

[root@k8s-master ~]# mv minikube-linux-amd64 /usr/local/bin/minikube
[root@k8s-master ~]# ls /usr/local/bin/minikube 
/usr/local/bin/minikube

3、赋予可执行权限

chmod +x /usr/local/bin/minikube

4、启动 minikube

由于是在 VMware 中虚拟机启动的,不允许重复虚拟化,添加参数 –driver=none

minikube start –dirver=none

[root@k8s-master ~]# minikube start --driver=none
* Centos 7.8.2003 上的 minikube v1.11.0
* 根据用户配置使用 none 驱动程序
X Sorry, Kubernetes 1.18.3 requires conntrack to be installed in root's path

提示需要安装 conntrack ,yum 安装指定的包

yum install conntrack -y

minikube start –driver=none –image-mirror-country=’cn’ –image-repository=’registry.cn-hangzhou.aliyuncs.com/google_containers’

[root@k8s-master ~]# minikube start --driver=none --image-mirror-country='cn' --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers' 
* Centos 7.8.2003 上的 minikube v1.11.0
* 根据现有的配置文件使用 none 驱动程序
* Starting control plane node minikube in cluster minikube
* Restarting existing none bare metal machine for "minikube" ...
* OS release is CentOS Linux 7 (Core)
* 正在 Docker 19.03.11 中准备 Kubernetes v1.18.3…
* 开始配置本地主机环境...
* 
! The 'none' driver is designed for experts who need to integrate with an existing VM
* Most users should use the newer 'docker' driver instead, which does not require root!
* For more information, see: https://minikube.sigs.k8s.io/docs/reference/drivers/none/
* 
! kubectl 和 minikube 配置将存储在 /root 中
! 如需以您自己的用户身份使用 kubectl 或 minikube 命令,您可能需要重新定位该命令。例如,如需覆盖您的自定义设置,请运行:
* 
  - sudo mv /root/.kube /root/.minikube $HOME
  - sudo chown -R $USER $HOME/.kube $HOME/.minikube
* 
* 此操作还可通过设置环境变量 CHANGE_MINIKUBE_NONE_USER=true 自动完成
* Verifying Kubernetes components...
* Enabled addons: default-storageclass, storage-provisioner
* 完成!kubectl 已经配置至 "minikube"

如果需要安装指定版本的 kubernetes ,增加 –kubernetes-version=v1.13.0,但是会存在 minikube 和 kubernetes 版本兼容的问题

启动报错及其解决:

1、[ERROR NumCPU]: the number of available CPUs 1 is less than the required 2

启动 minikube 需要 2 个 CPU
解决方案:增加 CPU 数

启动之后的进程:

ps -ef | grep minikube

[root@k8s-master ~]# ps -ef | grep minikube
root       2813   2795  5 03:56 ?        00:00:10 kube-controller-manager --authentication-kubeconfig=/etc/kubernetes/controller-manager.conf --authorization-kubeconfig=/etc/kubernetes/controller-manager.conf --bind-address=127.0.0.1 --client-ca-file=/var/lib/minikube/certs/ca.crt --cluster-name=mk --cluster-signing-cert-file=/var/lib/minikube/certs/ca.crt --cluster-signing-key-file=/var/lib/minikube/certs/ca.key --controllers=*,bootstrapsigner,tokencleaner --kubeconfig=/etc/kubernetes/controller-manager.conf --leader-elect=true --requestheader-client-ca-file=/var/lib/minikube/certs/front-proxy-ca.crt --root-ca-file=/var/lib/minikube/certs/ca.crt --service-account-private-key-file=/var/lib/minikube/certs/sa.key --use-service-account-credentials=true
root       2892   2833 13 03:56 ?        00:00:26 kube-apiserver --advertise-address=10.222.28.14 --allow-privileged=true --authorization-mode=Node,RBAC --client-ca-file=/var/lib/minikube/certs/ca.crt --enable-admission-plugins=NamespaceLifecycle,LimitRanger,ServiceAccount,DefaultStorageClass,DefaultTolerationSeconds,NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota --enable-bootstrap-token-auth=true --etcd-cafile=/var/lib/minikube/certs/etcd/ca.crt --etcd-certfile=/var/lib/minikube/certs/apiserver-etcd-client.crt --etcd-keyfile=/var/lib/minikube/certs/apiserver-etcd-client.key --etcd-servers=https://127.0.0.1:2379 --insecure-port=0 --kubelet-client-certificate=/var/lib/minikube/certs/apiserver-kubelet-client.crt --kubelet-client-key=/var/lib/minikube/certs/apiserver-kubelet-client.key --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname --proxy-client-cert-file=/var/lib/minikube/certs/front-proxy-client.crt --proxy-client-key-file=/var/lib/minikube/certs/front-proxy-client.key --requestheader-allowed-names=front-proxy-client --requestheader-client-ca-file=/var/lib/minikube/certs/front-proxy-ca.crt --requestheader-extra-headers-prefix=X-Remote-Extra- --requestheader-group-headers=X-Remote-Group --requestheader-username-headers=X-Remote-User --secure-port=8443 --service-account-key-file=/var/lib/minikube/certs/sa.pub --service-cluster-ip-range=10.96.0.0/12 --tls-cert-file=/var/lib/minikube/certs/apiserver.crt --tls-private-key-file=/var/lib/minikube/certs/apiserver.key
root       2946   2893  4 03:56 ?        00:00:09 etcd --advertise-client-urls=https://10.222.28.14:2379 --cert-file=/var/lib/minikube/certs/etcd/server.crt --client-cert-auth=true --data-dir=/var/lib/minikube/etcd --initial-advertise-peer-urls=https://10.222.28.14:2380 --initial-cluster=k8s-master=https://10.222.28.14:2380 --key-file=/var/lib/minikube/certs/etcd/server.key --listen-client-urls=https://127.0.0.1:2379,https://10.222.28.14:2379 --listen-metrics-urls=http://127.0.0.1:2381 --listen-peer-urls=https://10.222.28.14:2380 --name=k8s-master --peer-cert-file=/var/lib/minikube/certs/etcd/peer.crt --peer-client-cert-auth=true --peer-key-file=/var/lib/minikube/certs/etcd/peer.key --peer-trusted-ca-file=/var/lib/minikube/certs/etcd/ca.crt --snapshot-count=10000 --trusted-ca-file=/var/lib/minikube/certs/etcd/ca.crt
root       3246      1  5 03:57 ?        00:00:08 /var/lib/minikube/binaries/v1.18.3/kubelet --authorization-mode=Webhook --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --cgroup-driver=cgroupfs --client-ca-file=/var/lib/minikube/certs/ca.crt --cluster-domain=cluster.local --config=/var/lib/kubelet/config.yaml --container-runtime=docker --fail-swap-on=false --hostname-override=k8s-master --kubeconfig=/etc/kubernetes/kubelet.conf --node-ip=10.222.28.14 --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 --pod-manifest-path=/etc/kubernetes/manifests

5、停止 minikube

minikube stop

[root@k8s-master ~]# minikube stop
* Stopping "minikube" in none ...
* Node "minikube" stopped.


[root@k8s-master ~]# minikube status
minikube
type: Control Plane
host: Stopped
kubelet: Stopped
apiserver: Stopped
kubeconfig: Stopped

二、use

1、查看 minikube 状态

minikube status

[root@k8s-master ~]# minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

2、开启面板并获取面板连接

minikube dashboard –url

[root@k8s-master ~]# minikube dashboard --url
* 正在验证 dashboard 运行情况 ...
* Launching proxy ...
* 正在验证 proxy 运行状况 ...
http://127.0.0.1:34430/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/

但是这是内网本机的地址,外部无法访问

解决方案:
为 dashboard 添加外部代理

kubectl proxy --port=8001 --address='10.222.28.14' --accept-hosts='^.*' &

address 为 dashboard ip

外部访问地址:

http://10.222.28.14:8001/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/#/overview?namespace=default

3、查看已经启动的项目

minikube service list

[root@k8s-master ~]# minikube service list
|----------------------|---------------------------|--------------|-----------------------------|
|      NAMESPACE       |           NAME            | TARGET PORT  |             URL             |
|----------------------|---------------------------|--------------|-----------------------------|
| default              | kubernetes                | No node port |
| default              | tomcat                    |         8080 | http://192.168.14.142:30921 |
| kube-system          | kube-dns                  | No node port |
| kubernetes-dashboard | dashboard-metrics-scraper | No node port |
| kubernetes-dashboard | kubernetes-dashboard      | No node port |
|----------------------|---------------------------|--------------|-----------------------------|

4、删除服务资源

4.1、查看所有 deployment

kubectl get deployment –all-namespaces

[root@k8s-master ~]# kubectl get deployment --all-namespaces
NAMESPACE              NAME                        READY   UP-TO-DATE   AVAILABLE   AGE
kube-system            coredns                     0/2     2            0           4d22h
kubernetes-dashboard   dashboard-metrics-scraper   1/1     1            1           4d22h
kubernetes-dashboard   kubernetes-dashboard        0/1     1            0           4d22h

4.2、删除 deployment

minikube delete deployment tomcat

4.3、删除 minikube

minikube stop && minikube delete

5、查看所有 pods

kubectl get pods –all-namespaces

[root@k8s-master ~]# kubectl get pods --all-namespaces
NAMESPACE              NAME                                         READY   STATUS             RESTARTS   AGE
kube-system            coredns-546565776c-282xk                     0/1     Running            2          4d21h
kube-system            coredns-546565776c-4c4gn                     0/1     Running            2          4d21h
kube-system            etcd-k8s-master                              1/1     Running            2          4d21h
kube-system            kube-apiserver-k8s-master                    1/1     Running            2          4d21h
kube-system            kube-controller-manager-k8s-master           1/1     Running            2          4d21h
kube-system            kube-proxy-5hp7c                             1/1     Running            2          4d21h
kube-system            kube-scheduler-k8s-master                    1/1     Running            2          4d21h
kube-system            storage-provisioner                          1/1     Running            3          4d21h
kubernetes-dashboard   dashboard-metrics-scraper-84bfdf55ff-wpn9g   1/1     Running            2          4d21h
kubernetes-dashboard   kubernetes-dashboard-696dbcc666-pddbf        0/1     CrashLoopBackOff   30         4d21h

6、获取命名空间

kubectl get namespaces
kubectl get ns

[root@k8s-master ~]# kubectl get namespaces
NAME                   STATUS   AGE
default                Active   4d21h
kube-node-lease        Active   4d21h
kube-public            Active   4d21h
kube-system            Active   4d21h
kubernetes-dashboard   Active   4d21h

7、获取集群信息

kubectl cluster-info

[root@k8s-master ~]# kubectl cluster-info
Kubernetes master is running at https://192.168.14.142:8443
KubeDNS is running at https://192.168.14.142:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

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

kubectl get node

[root@k8s-master ~]# kubectl get node
NAME         STATUS   ROLES    AGE     VERSION
k8s-master   Ready    master   4d21h   v1.18.3