ubuntu安装minikube

ubuntu-18.04安装minikube-v1.31.2

介绍

minikube 是适合开发人员的迷你版 k8s,本文在 ubuntu 18.04 上安装 minikube。

安装minikube

To install the latest minikube stable release on x86-64 Linux using binary download:

1
2
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

启动minikube

From a terminal with administrator access (but not logged in as root), run:

1
minikube start

If minikube fails to start, see the drivers page for help setting up a compatible container or virtual-machine manager.

安装kubectl

用以下命令下载最新 kubectl 发行版

1
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

安装 kubectl

1
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

测试

1
kubectl version --client

操作

使用 kubectl 查看所有命名空间的 pod

1
kubectl get po -A

minikube 集成了 kubernetes dashboard,使用如下命令开启访问 dashboard

1
minikube dashboard

查看 minikube 版本

1
minikube version

部署服务

k8s 有三种暴露服务的方式:NodePort, LoadBalancer, Ingress,分别介绍使用minikube 安装暴露服务

Service

Create a sample deployment and expose it on port 8080:

1
2
kubectl create deployment hello-minikube --image=kicbase/echo-server:1.0
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
kubectl get services hello-minikube

The easiest way to access this service is to let minikube launch a web browser for you:

1
minikube service hello-minikube

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/.

You should be able to see the request metadata in the application output. Try changing the path of the request and observe the changes. Similarly, you can do a POST request and observe the body show up in the output.

LoadBalancer

To access a LoadBalancer deployment, use the “minikube tunnel” command. Here is an example deployment:

1
2
kubectl create deployment balanced --image=kicbase/echo-server:1.0
kubectl expose deployment balanced --type=LoadBalancer --port=8080

In another window, start the tunnel to create a routable IP for the ‘balanced’ deployment:

1
minikube tunnel

To find the routable IP, run this command and examine the EXTERNAL-IP column:

1
kubectl get services balanced

Your deployment is now available at <EXTERNAL-IP>:8080

Ingress

Enable ingress addon:

1
minikube addons enable ingress

The following example creates simple echo-server services and an Ingress object to route to these services.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
kind: Pod
apiVersion: v1
metadata:
  name: foo-app
  labels:
    app: foo
spec:
  containers:
    - name: foo-app
      image: 'kicbase/echo-server:1.0'
---
kind: Service
apiVersion: v1
metadata:
  name: foo-service
spec:
  selector:
    app: foo
  ports:
    - port: 8080
---
kind: Pod
apiVersion: v1
metadata:
  name: bar-app
  labels:
    app: bar
spec:
  containers:
    - name: bar-app
      image: 'kicbase/echo-server:1.0'
---
kind: Service
apiVersion: v1
metadata:
  name: bar-service
spec:
  selector:
    app: bar
  ports:
    - port: 8080
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  rules:
    - http:
        paths:
          - pathType: Prefix
            path: /foo
            backend:
              service:
                name: foo-service
                port:
                  number: 8080
          - pathType: Prefix
            path: /bar
            backend:
              service:
                name: bar-service
                port:
                  number: 8080
---

Apply the contents

1
kubectl apply -f https://storage.googleapis.com/minikube-site-examples/ingress-example.yaml

Wait for ingress address

1
2
3
kubectl get ingress
NAME              CLASS   HOSTS   ADDRESS          PORTS   AGE
example-ingress   nginx   *       <your_ip_here>   80      5m45s

Note for Docker Desktop Users:
To get ingress to work you’ll need to open a new terminal window and run minikube tunnel and in the following step use 127.0.0.1 in place of <ip_from_above>.

Now verify that the ingress works

1
2
3
4
5
6
7
$ curl <ip_from_above>/foo
Request served by foo-app
...

$ curl <ip_from_above>/bar
Request served by bar-app
...

管理minikube

Pause Kubernetes without impacting deployed applications:

1
minikube pause

Unpause a paused instance:

1
minikube unpause

Halt the cluster:

1
minikube stop

Change the default memory limit (requires a restart):

1
minikube config set memory 9001

Browse the catalog of easily installed Kubernetes services:

1
minikube addons list

Create a second cluster running an older Kubernetes release:

1
minikube start -p aged --kubernetes-version=v1.16.1

Delete all of the minikube clusters:

1
minikube delete --all

参考:
https://kubernetes.io/zh-cn/docs/tutorials/hello-minikube/
https://minikube.sigs.k8s.io/docs/start/
https://kubernetes.io/zh-cn/docs/tasks/tools/#kubectl
https://kubernetes.io/zh-cn/docs/tasks/tools/install-kubectl-linux/#install-kubectl-binary-with-curl-on-linux

Built with Hugo
主题 StackJimmy 设计