minikube start

如果需要访问 Kubernetes 上部署的应用,可以创建类型为 NodePort 或 LoadBalancer 的服务。下面的echoserver服务的类型是NodePort,可以通过 minikube service 命令得到访问该服务的地址。

Deploy applications

Create a sample deployment and expose it on port 8080:

1
2
kubectl create deployment hello-minikube --image=cilium/echoserver:latest
kubectl expose deployment hello-minikube --type=NodePort --port=8080

It may take a moment, but your deployment will soon show up when you run:

1
2
3
4
5
6
7
8
9
$ kubectl get deployment -o wide -n default
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
hello-minikube 1/1 1 1 34m echoserver cilium/echoserver:latest app=hello-minikube
$ kubectl get services hello-minikube
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-minikube NodePort 10.104.103.50 <none> 8080:30565/TCP 34m
$ kubectl get pod -o wide -n default
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hello-minikube-6ddfcc9757-tcd2m 1/1 Running 0 36m 172.17.0.5 minikube <none> <none>

The easiest way to access this service is to let minikube launch a web browser for you,加上–url可以显示服务ip。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ minikube service hello-minikube
|-----------|----------------|-------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|----------------|-------------|---------------------------|
| default | hello-minikube | 8080 | http://192.168.49.2:30565 |
|-----------|----------------|-------------|---------------------------|
🏃 Starting tunnel for service hello-minikube.
|-----------|----------------|-------------|------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|----------------|-------------|------------------------|
| default | hello-minikube | | http://127.0.0.1:64877 |
|-----------|----------------|-------------|------------------------|
🎉 Opening service default/hello-minikube in default browser...
❗ Because you are using a Docker driver on darwin, the terminal needs to be open to run it.

Alternatively, use kubectl to forward the port:

1
kubectl port-forward service/hello-minikube 7080:8080

Tada! Your application is now available at http://localhost:7080/

Delete service, deployment and pod

1
2
3
4
$ kubectl delete service hello-minikube -n default
# service "hello-minikube", "balanced" deleted
$ kubectl delete deployment hello-minikube -n default
# deployment.apps "hello-minikube", "balanced" deleted

LoadBalancer deployments

对于 LoadBalancer 类型的服务,则要稍微复杂一点。 Here is an example deployment:

1
2
kubectl create deployment hello-minikube --image=cilium/echoserver:latest
kubectl expose deployment hello-minikube --type=LoadBalancer --port=8080

当服务创建之后,它的外部地址的状态会是 Pending,表示处理中:

1
2
3
$ kubectl get services hello-minikube
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-minikube LoadBalancer 10.104.16.253 <pending> 8080:31137/TCP 12m

这时候我是无法访问这个应用的,这个时候需要使用 minikube tunnel 命令来打开一个通道。

1
$ minikube tunnel

通道打开之后不要关闭,再查看服务,就可以看到访问的地址了。To find the routable IP, run this command and examine the EXTERNAL-IP column:

1
2
3
$ kubectl get services hello-minikube
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-minikube LoadBalancer 10.103.181.236 127.0.0.1 8080:30322/TCP 2m24s

Your deployment is now available at :8080

Delete service, deployment and pod

1
2
3
4
$ kubectl delete service hello-minikube -n default
# service "hello-minikube", "balanced" deleted
$ kubectl delete deployment hello-minikube -n default
# deployment.apps "hello-minikube", "balanced" deleted

When you delete deployment the pod will be terminated and deleted.

Stop Minikube

1
$ minikube stop

Tips:

如果pod的状态不是RUNNING的话说明pod还没有准备好

kubectl describe pod $podname:可以查看pod详细状态

kubectl logs -f $podname:可以将pod的日志输出到屏幕上

References:

https://minikube.sigs.k8s.io/docs/start/

https://www.anote.dev/hello-minikube-deployment/