安装 minikube
可以去官网 https://minikube.sigs.k8s.io/docs/start/ 下载对应的版本。
比如通过可以选择二进制的安装方式:
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
安装 kubectl
minikube 只能够搭建 Kubernetes 环境,要操作 Kubernetes,还需要另一个专门的客户端工具 kubectl。可以通过以下方法进行安装:
- 使用 minikube安装 kubectl
- 通过 curl 进行下载二进制文件
- 通过包管理方式安装
使用minikube安装kubectl
kubectl 是一个与 Kubernetes、minikube 彼此独立的项目,不包含在 minikube 里,但 minikube 提供了安装它的简化方式,只需执行minikube kubectl
命令即可:
# minikube kubectl
> kubectl.sha256: 64 B / 64 B [-------------------------] 100.00% ? p/s 0s
> kubectl: 47.56 MiB / 47.56 MiB [--------------] 100.00% 2.59 MiB p/s 19s
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/
Basic Commands (Beginner):
create Create a resource from a file or from stdin
expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
run Run a particular image on the cluster
set Set specific features on objects
以上这条命令会把与当前 Kubernetes 版本匹配的 kubectl 下载下来,存放在内部目录.minikube/cache/linux/amd64/
中,然后就可以使用它来操作 Kubernetes 了。
# ls .minikube/cache/linux/amd64/v1.28.3/
kubectl
通过curl进行下载二进制文件
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)
部分即可。例如下载 v1.26.0 版本:
curl -LO "https://dl.k8s.io/release/v1.26.0/bin/linux/amd64/kubectl"
增加权限:
chmod +x ./kubectl
mv ./kubectl /usr/local/bin
通过包管理方式安装
Debian/Ubuntu
sudo apt-get update
sudo apt-get install -y ca-certificates curl apt-transport-https
sudo curl -fsSLo /etc/apt/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubectl
Red Hat/Centos
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
enabled=1
gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
sudo yum install -y kubectl
使用 root 用户启动 minikube
安装了 minikube 和 kubectl 就可以在本机上运行 minikube,使用 root 创建 Kubernetes 实验环境:
minikube start \
--service-cluster-ip-range='172.16.0.0/16' \
--image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers \
--iso-url=https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/iso/minikube-v1.32.1-amd64.iso \
--registry-mirror=https://ole0c9mw.mirror.aliyuncs.com \
--kubernetes-version='v1.28.3' \
--image-mirror-country='cn'
如果出现类似以下的问题:
* minikube v1.32.0 on Rocky 8.7
* Automatically selected the docker driver. Other choices: none, ssh
* The "docker" driver should not be used with root privileges. If you wish to continue as root, use --force.
* 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.
提示“docker”驱动程序不应该以 root 权限使用。可以选择更换普通用户进行启动minikube,也可以使用加上 --force
强制 root 用户启动:
minikube start \
--service-cluster-ip-range='172.16.0.0/16' \
--image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers \
--iso-url=https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/iso/minikube-v1.32.1-amd64.iso \
--registry-mirror=https://ole0c9mw.mirror.aliyuncs.com \
--kubernetes-version='v1.28.3' \
--image-mirror-country='cn' \
--force
安装的时候出来类似提示:
* minikube v1.32.0 on Rocky 8.7
! minikube skips various validations when --force is supplied; this may lead to unexpected behavior
* Using the docker driver based on existing profile
* The "docker" driver should not be used with root privileges. If you wish to continue as root, use --force.
* If you are running minikube within a VM, consider using --driver=none:
* https://minikube.sigs.k8s.io/docs/reference/drivers/none/
* Tip: To remove this root owned cluster, run: sudo minikube delete
* Starting control plane node minikube in cluster minikube
* Pulling base image ...
* Updating the running docker "minikube" container ...
> kubelet: 105.65 MiB / 105.65 MiB [-------] 100.00% 123.46 KiB p/s 14m36s
- Generating certificates and keys ...
- Booting up control plane ...
- Configuring RBAC rules ...
* Configuring bridge CNI (Container Networking Interface) ...
* Verifying Kubernetes components...
- Using image registry.cn-hangzhou.aliyuncs.com/google_containers/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
则安装成功!
简单使用
如果使用 minikube 自带的 kubectl 有一点限制,必须要在前面加上 minikube 的前缀,后面再加上--
才能使用,像这样:
# minikube kubectl -- get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane 9m44s v1.28.3
可以通过使用alias
方式,简化命令:
alias kubectl=" minikube kubectl --"
这样就可以直接使用kubectl
来进行操作。
运行一个 nginx 的 pod:
# kubectl run nginx --image=nginx
pod/nginx created
查看 nginx 的状态:
# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 59s
关闭minikube
minikube stop