Minikube搭建Kubernetes集群
Minikube
minikube是一个工具, 能让你在本地运行一个单节点的Kubernetes集群,以便你来尝试Kubernetes或者开展每天的开发工作
简言之:minikube可以在单机环境下快速搭建可用的k8s集群,非常适合测试和本地开发
官网: https://minikube.sigs.k8s.io/docs/start/
kubectl
kubectl 是用来与Kubernetes集群通讯的命令行工具。
通过Kubectl可以在Kubernetes集群上完成如下操作:
部署和管理应用
查看资源信息
删除和更新组件
kubectl可以不用手动安装,Minikube第一次使用会直接安装kubectl
官网:https://kubernetes.io/zh-cn/docs/tasks/tools/install-kubectl-linux/
下载最新发行版
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
下载某个指定的版本
curl -LO https://dl.k8s.io/release/v1.24.0/bin/linux/amd64/kubectl
安装 kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
查看版本信息
kubectl version --client
kubectl version --client --output=yaml
下载Minikube
下载Minikube的二进制安装包并安装
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
启动Minikube
启动异常:
[root@administrator program]# minikube start
* minikube v1.25.2 on Centos 7.9.2009 (amd64)
* Automatically selected the docker driver. Other choices: ssh, none
* 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.
注意: 需要具有管理员访问权限的终端(但未以 root 身份登录)运行
创建了一个属于docker用户组的k8s用户,并切换到该用户
# 创建用户
useradd -g docker k8s
# 设置用户密码
passwd k8s
# 切换用户
su k8s
再次启动Minikube
[k8s@administrator program]$ minikube start
* minikube v1.25.2 on Centos 7.9.2009 (amd64)
* Automatically selected the docker driver
* Starting control plane node minikube in cluster minikube
* Pulling base image ...
* Downloading Kubernetes v1.23.3 preload ...
> preloaded-images-k8s-v17-v1...: 505.68 MiB / 505.68 MiB 100.00% 11.89 Mi
> index.docker.io/kicbase/sta...: 379.06 MiB / 379.06 MiB 100.00% 2.56 MiB
! minikube was unable to download gcr.io/k8s-minikube/kicbase:v0.0.30, but successfully downloaded docker.io/kicbase/stable:v0.0.30 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.23.3 on Docker 20.10.12 ...
- kubelet.housekeeping-interval=5m
- 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
* 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 start --vm-driver=docker --image-mirror-country='cn'
--vm-driver=docker
指定使用 docker作为虚拟化驱动
--image-mirror-country='cn'
配置使用中文dockcer镜像仓库,加速镜像下载
Minikube命令
输入 minikube
直接回车
一组基础命令,用来开始、暂停、恢复、停止、删除 kubernetes 集群:
Basic Commands:
start Starts a local Kubernetes cluster
status Gets the status of a local Kubernetes cluster
stop Stops a running local Kubernetes cluster
delete Deletes a local Kubernetes cluster
dashboard 访问在 minikube 集群中运行的 kubernetes dashboard
pause pause Kubernetes
unpause 恢复 Kubernetes
一组镜像管理命令
Images Commands:
docker-env 配置环境以使用 minikube's Docker daemon
podman-env 配置环境以使用 minikube's Podman service
cache Add, delete, or push a local image into minikube
image Manage images
一组配置和插件管理命令
Configuration and Management Commands:
addons Enable or disable a minikube addon
config Modify persistent configuration values
profile Get or list the current profiles (clusters)
update-context Update kubeconfig in case of an IP or port change
验证Minikube
查看Minikube的版本号
[k8s@administrator program]$ minikube version
minikube version: v1.25.2
commit: 362d5fdc0a3dbee389b3d3f1034e8023e72bd3a7
查看kubectl版本号,第一次使用会直接安装kubectl
[k8s@administrator program]$ minikube kubectl version
> kubectl.sha256: 64 B / 64 B [--------------------------] 100.00% ? p/s 0s
> kubectl: 44.43 MiB / 44.43 MiB [-------------] 100.00% 14.91 MiB p/s 3.2s
Client Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.3", GitCommit:"816c97ab8cff8a1c72eccca1026f7820e93e0d25", GitTreeState:"clean", BuildDate:"2022-01-25T21:25:17Z", GoVersion:"go1.17.6", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.3", GitCommit:"816c97ab8cff8a1c72eccca1026f7820e93e0d25", GitTreeState:"clean", BuildDate:"2022-01-25T21:19:12Z", GoVersion:"go1.17.6", Compiler:"gc", Platform:"linux/amd64"}
minikube是以: minikube kubectl cluster-info
语法形式操作; 如果想直接使用kubectl命令,需要将其复制到/bin目录下
注意: 需要以root权限查询kubectl命令位置
# 查找kubectl命令的位置
[root@administrator program]# find / -name kubectl
/home/k8s/.minikube/cache/linux/amd64/v1.23.3/kubectl
/www/server/docker/volumes/minikube/_data/lib/minikube/binaries/v1.23.3/kubectl
# 复制到/bin目录下
[root@administrator program]# cp /www/server/docker/volumes/minikube/_data/lib/minikube/binaries/v1.23.3/kubectl /bin/
[root@administrator program]# su k8s
[k8s@administrator program]$ kubectl version
Client Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.3", GitCommit:"816c97ab8cff8a1c72eccca1026f7820e93e0d25", GitTreeState:"clean", BuildDate:"2022-01-25T21:25:17Z", GoVersion:"go1.17.6", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.3", GitCommit:"816c97ab8cff8a1c72eccca1026f7820e93e0d25", GitTreeState:"clean", BuildDate:"2022-01-25T21:19:12Z", GoVersion:"go1.17.6", Compiler:"gc", Platform:"linux/amd64"}
查看集群信息
[k8s@administrator root]$ kubectl cluster-info
Kubernetes control plane is running at https://192.168.49.2:8443
CoreDNS is running at https://192.168.49.2:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
查看集群中Node(Minikube创建了一个单节点的简单集群)
[k8s@administrator root]$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane,master 4h v1.23.3
插件的使用
Dashboard是基于网页的K8S用户界面。可以使用Dashboard将容器应用部署到K8S集群中,也可以对容器应用排错,还能管理集群资源。
minikube addons list # 插件列表
minikube addons enable ADDON_NAME # 插件启用
minikube addons disable ADDON_NAME # 插件禁用
查看Minikube内置插件,默认情况下Dashboard插件未启用:minikube addons list
|-----------------------------|----------|--------------|--------------------------------|
| ADDON NAME | PROFILE | STATUS | MAINTAINER |
|-----------------------------|----------|--------------|--------------------------------|
| ambassador | minikube | disabled | third-party (ambassador) |
| auto-pause | minikube | disabled | google |
| csi-hostpath-driver | minikube | disabled | kubernetes |
| dashboard | minikube | disabled | kubernetes |
| default-storageclass | minikube | enabled ✅ | kubernetes |
| efk | minikube | disabled | third-party (elastic) |
| freshpod | minikube | disabled | google |
| gcp-auth | minikube | disabled | google |
| gvisor | minikube | disabled | google |
| helm-tiller | minikube | disabled | third-party (helm) |
| ingress | minikube | disabled | unknown (third-party) |
| ingress-dns | minikube | disabled | google |
| istio | minikube | disabled | third-party (istio) |
| istio-provisioner | minikube | disabled | third-party (istio) |
| kong | minikube | disabled | third-party (Kong HQ) |
| kubevirt | minikube | disabled | third-party (kubevirt) |
| logviewer | minikube | disabled | unknown (third-party) |
| metallb | minikube | disabled | third-party (metallb) |
| metrics-server | minikube | disabled | kubernetes |
| nvidia-driver-installer | minikube | disabled | google |
| nvidia-gpu-device-plugin | minikube | disabled | third-party (nvidia) |
| olm | minikube | disabled | third-party (operator |
| | | | framework) |
| pod-security-policy | minikube | disabled | unknown (third-party) |
| portainer | minikube | disabled | portainer.io |
| registry | minikube | disabled | google |
| registry-aliases | minikube | disabled | unknown (third-party) |
| registry-creds | minikube | disabled | third-party (upmc enterprises) |
| storage-provisioner | minikube | enabled ✅ | google |
| storage-provisioner-gluster | minikube | disabled | unknown (third-party) |
| volumesnapshots | minikube | disabled | kubernetes |
|-----------------------------|----------|--------------|--------------------------------|
启用Dashboard插件:minikube addons enable dashboard
- Using image kubernetesui/dashboard:v2.3.1
- Using image kubernetesui/metrics-scraper:v1.0.7
* Some dashboard features require the metrics-server addon. To enable all features please run:
minikube addons enable metrics-server
* The 'dashboard' addon is enabled
通过–url参数在控制台获得访问路径,此方式不会打开管理页面:minikube dashboard --url
* Verifying dashboard health ...
* Launching proxy ...
* Verifying proxy health ...
http://127.0.0.1:33211/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
使用kubectl设置代理,–address设置为服务器地址(内外IP,外网IP不行),然后才能从外部访问Dashboard
kubectl proxy --port=[需要暴露的端口号] --address='[服务器IP]' --accept-hosts='^[外部访问服务器的IP]$' >/dev/null 2>&1&
kubectl proxy --port=8100 --address=172.22.4.21 --accept-hosts='^.*' &
修改访问路径中的IP及端口后访问
http://ip:8100/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
通过yaml脚本创建资源
管理集群
停止集群 minikube stop
启动集群 minikube start
删除集群 minikube delete
不影响已部署应用情况下暂停 Kubernetes:minikube pause
取消暂停的实例:minikube unpause
增加默认内存限制(需重启):minikube config set memory 16384
Minikube内置插件:minikube addons list
节点操作: minikube node [add|start|stop|delete|list]
额外启动参数
--image-mirror-country=cn # 镜像所在国家
--image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers # 镜像仓库地址
--cpus=2 # 设置minikube虚拟机分配CPU核数
--memory=2000mb # 设置minikube虚拟机分配内存
--kubernetes-version=version # 使用的kubernetes版本
--docker-env http_proxy=http://IP:7890 http_proxy=https://IP:7890 # minikube虚拟机内部docker使用代理地址
# 指定驱动
--vm-driver=none 在主机上运行Kubernetes组件,而不是在VM中,该方式驱动依赖Docker
--vm-driver=virtualbox 表示用虚拟机,默认
注意:不通过--vm-driver=none参数启动,则以虚拟机方式运行Kubernetes集群,那么创建的Pod、Service均不能通过外网访问,只能minikube ssh 进入集群访问操作
[root@administrator ~]# minikube start --vm-driver=none --image-mirror-country=cn --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers
* minikube v1.25.2 on Centos 7.9.2009 (amd64)
* Using the none driver based on user configuration
* Using image repository registry.cn-hangzhou.aliyuncs.com/google_containers
* Starting control plane node minikube in cluster minikube
* Running on localhost (CPUs=2, Memory=3602MB, Disk=80503MB) ...
* OS release is CentOS Linux 7 (Core)
* Preparing Kubernetes v1.23.3 on Docker 20.10.14 ...
- kubelet.housekeeping-interval=5m
> kubeadm.sha256: 64 B / 64 B [--------------------------] 100.00% ? p/s 0s
> kubelet.sha256: 64 B / 64 B [--------------------------] 100.00% ? p/s 0s
> kubectl.sha256: 64 B / 64 B [--------------------------] 100.00% ? p/s 0s
> kubeadm: 43.12 MiB / 43.12 MiB [--------------] 100.00% 4.43 MiB p/s 9.9s
> kubectl: 44.43 MiB / 44.43 MiB [---------------] 100.00% 3.86 MiB p/s 12s
> kubelet: 118.75 MiB / 118.75 MiB [-------------] 100.00% 6.44 MiB p/s 19s
- Generating certificates and keys ...
- Booting up control plane ...
- Configuring RBAC rules ...
* Configuring local host environment ...
*
! 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 and minikube configuration will be stored in /root
! To use kubectl or minikube commands as your own user, you may need to relocate them. For example, to overwrite your own settings, run:
*
- sudo mv /root/.kube /root/.minikube $HOME
- sudo chown -R $USER $HOME/.kube $HOME/.minikube
*
* This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
* Verifying Kubernetes components...
- Using image registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner:v5
* Enabled addons: default-storageclass, storage-provisioner
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
测试
创建Pod
[root@administrator ~]# kubectl create deploy nginx-pod --image=nginx --port=80
deployment.apps/nginx-pod created
查看Pod
[root@administrator ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-pod-6d99999569-jzqpl 1/1 Running 0 23s 172.17.0.5 administrator <none> <none>
访问Pod
[root@administrator ~]# curl 172.17.0.5
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
暴露端口
[root@administrator ~]# kubectl expose deploy nginx-pod --type=NodePort
service/nginx-pod exposed
查看Service
[root@administrator ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4m44s
nginx-pod NodePort 10.108.14.242 <none> 80:32235/TCP 4s
访问Service
[root@administrator ~]# curl 10.108.14.242
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
通过端口外网访问Pod
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/136986.html