1、什么是minikube ?
Minikube是由Kubernetes社区维护的单机版的Kubernetes集群,支持macOS, Linux, and Windows等多种操作系统平台,使用最新的官方stable版本,并支持Kubernetes的大部分功能,从基础的容器编排管理,到高级特性如负载均衡、Ingress,权限控制等。非常适合作为Kubernetes入门,或开发测试环境使用。
安装k8s的麻烦就不用多说了,而且特别容易出错,烦的很,minikube部署简单可以用来测试学习用。
2、minikube安装
1、准备环境
一台装centos7.6的服务器
docker
安装最新 docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum list docker-ce --showduplicates | sort -r
sudo yum -y install docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo systemctl start docker && systemctl enable docker
2 配置镜像源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
3、安装kubectl
curl -LO 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
mv ./kubectl /usr/local/bin/kubectl
4、安装minukube
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x minikube
mv minikube /usr/local/bin/
5、启动集群
新建一个普通用户,否则会提示The “docker” driver should not be used with root privileges.
useradd ops -G docker
echo "123456" | passwd --stdin ops
#Changing password for user ops.
su ops
启动本地k8s集群
[ops@VM-0-8-centos ~]$ minikube start
* minikube v1.23.2 on Centos 7.6.1908 (amd64)
* Automatically selected the docker driver
* Starting control plane node minikube in cluster minikube
* Pulling base image ...
* Downloading Kubernetes v1.22.2 preload ...
> preloaded-images-k8s-v13-v1...: 511.69 MiB / 511.69 MiB 100.00% 10.70 Mi
> index.docker.io/kicbase/sta...: 355.39 MiB / 355.40 MiB 100.00% 5.06 MiB
! minikube was unable to download gcr.io/k8s-minikube/kicbase:v0.0.27, but successfully downloaded docker.io/kicbase/stable:v0.0.27 as a fallback image
* 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.2 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: default-storageclass, storage-provisioner
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
6、查看minikube状态
[ops@VM-0-8-centos ~]$ minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
3、配置可视化面板
minikube dashboard --url
* Enabling dashboard ...
* Verifying dashboard health ...
* Launching proxy ...
* Verifying proxy health ...
http://127.0.0.1:33457/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
开启kube-proxy端口映射,使其可以远程访问
kubectl proxy --port=30030 --address='0.0.0.0' --accept-hosts='^.*' &
http://127.0.0.1:30030/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
启动minikube start的坑点
问题1:无法使用root启动
[root@VM-16-14-centos bin]# minikube start
* Centos 7.8.2003 上的 minikube v1.13.0
* Automatically selected the docker driver
* The "docker" driver should not be used with root privileges.
* If you are running minikube within a VM, consider using --driver=none:
* https://minikube.sigs.k8s.io/docs/reference/drivers/none/
X Exiting due to DRV_AS_ROOT: The "docker" driver should not be used with root privileges.
问题1:解决方法
因为我是用root账号登录操作的。所以提示不能用root账号启动,得用别的账号。所以要创建一个新的账号进行操作,创建一个test账号进行启动
adduser test
passwd test
问题2:minikube需要docker组启动
[test@VM-16-14-centos ~]$ minikube start --driver=docker
* Centos 7.8.2003 上的 minikube v1.13.0
* 根据用户配置使用 docker 驱动程序
X Exiting due to PROVIDER_DOCKER_ERROR: "docker version --format -" exit status 1: 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.40/version: dial unix /var/run/docker.sock: connect: permission denied
* 建议:Add your user to the 'docker' group: 'sudo usermod -aG docker $USER && newgrp docker'
* 文档:https://docs.docker.com/engine/install/linux-postinstall/
问题2:解决方法
#创建docker组
sudo groupadd docker
#将您的用户添加到该docker组
sudo usermod -aG docker $USER
#在Linux上,运行以下命令来激活对组的更改
newgrp docker
问题3:root账号启动docker导致无法启动
还是问题2的显示
问题3:解决办法:
先用root用户关闭docker,然后用test用户启动docker即可
docker启动和关闭命令
systemctl start docker
systemctl stop docker
启动minikube,举例启动一个nginx
先准备一个nginx的yaml文件,名字为nginx-deployment.yaml
内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
启动minikube并且启动nginx
minikube start --driver=docker
kubectl create -f nginx-deployment.yaml
[test@VM-16-14-centos ~]$ kubectl get pods -l app=nginx
NAME READY STATUS RESTARTS AGE
nginx-deployment-5d59d67564-k6q76 1/1 Running 0 84s
nginx-deployment-5d59d67564-sgzjw 1/1 Running 0 84s