Macos-minikube

作草分茶About 1 min

Macos 搭建 minikube 玩转 k8s

介绍

官网 minikube startopen in new window

搭建

  1. 安装
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-arm64
$ sudo install minikube-darwin-arm64 /usr/local/bin/minikube
  1. 启动
# 一直无法拉取镜像
$ minikube start

需要将镜像中心改为中国

# 先删除,加参数重新启动
$ minikube delete
$ minikube start --image-mirror-country='cn' --kubernetes-version=v1.23.3

结果还是不行,找资料,需要加驱动

# 设置全局驱动
$ minikube config set driver docker
❗  These changes will take effect upon a minikube delete and then a minikube start
# 或者指定驱动启动
$ minikube start --image-mirror-country='cn' --dirver=docker --kubernetes-version=v1.23.3 --network="host"

这次成功了。

设置一下自己的 shell

alias kubectl="minikube kubectl --"

部署应用

  1. 创建pod
$ kubectl create deployment game2048 --image=daocloud.io/daocloud/dao-2048:latest
deployment.apps/game2048 created
$ kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
hello-minikube-56964d98c6-tckdm   1/1     Running   0          13s
  1. 创建 service
$ kubectl expose deployment game2048 --type=NodePort --port=80
service/game2048 exposed

如何访问服务呢?

$ minikube service game2048 
|-----------|----------------|-------------|---------------------------|
| NAMESPACE |      NAME      | TARGET PORT |            URL            |
|-----------|----------------|-------------|---------------------------|
| default   | game2048 |          80 | http://192.168.49.2:30148 |
|-----------|----------------|-------------|---------------------------|
🏃  Starting tunnel for service hello-minikube.
|-----------|----------------|-------------|------------------------|
| NAMESPACE |      NAME      | TARGET PORT |          URL           |
|-----------|----------------|-------------|------------------------|
| default   | game2048 |             | http://127.0.0.1:63167 |
|-----------|----------------|-------------|------------------------|
🎉  Opening service default/game2048 in default browser...
❗  Because you are using a Docker driver on darwin, the terminal needs to be open to run it.

访问 http://127.0.0.1:63167open in new window

或者通过端口转发方式

$ kubectl port-forward service/game2048 4433:80  
Forwarding from 127.0.0.1:4433 -> 80
Forwarding from [::1]:4433 -> 80

访问 http://127.0.0.1:4433open in new window

控制台

$ minikube dashboard