使用 Ingress 暴露 minikube 中的应用

In Deploy a Spring Boot Application into minikube with Command ,We deploy an application for Spring Boot app into minikube, and in this article we will expose the application through ingress for user access outside of the minikube (cluster).

Install Ingress

In minikube, Ingress is an addons. So let’s first look at those addons in the current minikube。 Execute:

1
minikube addons list

The system display:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|-----------------------------|----------|--------------|
| ADDON NAME | PROFILE | STATUS |
|-----------------------------|----------|--------------|
| dashboard | minikube | enabled ✅ |
| default-storageclass | minikube | enabled ✅ |
| efk | minikube | disabled |
| freshpod | minikube | disabled |
| gvisor | minikube | disabled |
| helm-tiller | minikube | disabled |
| ingress | minikube | disabled |
| ingress-dns | minikube | disabled |
| istio | minikube | disabled |
| istio-provisioner | minikube | disabled |
| logviewer | minikube | disabled |
| metrics-server | minikube | disabled |
| nvidia-driver-installer | minikube | disabled |
| nvidia-gpu-device-plugin | minikube | disabled |
| registry | minikube | disabled |
| registry-creds | minikube | disabled |
| storage-provisioner | minikube | enabled ✅ |
| storage-provisioner-gluster | minikube | disabled |
|-----------------------------|----------|--------------|

As you can see, the current ingress is in the disabled state. Execute the enable command to activate it。

1
minikube addons enable ingress

You will be prompted immediately:

1
* The 'ingress' addon is enabled

But in fact, ingress can not be used immediately, because ingress is actually built as a system POD, it also needs to download its own image. We can execute the get pods command to see if it is already set up:

1
kubectl get pods -n kube-system

The above command list the all pods in kube-system (system namesapce). The result is similar:

1
2
3
4
5
6
7
8
9
10
NAME                                        READY   STATUS    RESTARTS   AGE
coredns-5c98db65d4-jqpkd 1/1 Running 10 19d
coredns-5c98db65d4-sfrrv 1/1 Running 10 19d
etcd-minikube 1/1 Running 0 27h
kube-apiserver-minikube 1/1 Running 2 27h
kube-controller-manager-minikube 1/1 Running 4 19d
kube-proxy-zvb6k 1/1 Running 3 19d
kube-scheduler-minikube 1/1 Running 5 19d
nginx-ingress-controller-657fd58d97-wct76 1/1 Running 0 48m
storage-provisioner 1/1 Running 5 19d

If nginx-ingress-controller-xxxx is already in the Running state, activation of ingress has been completed.

Execute ‘addons list’ again,

1
minikube addons list

You can see,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|-----------------------------|----------|--------------|
| ADDON NAME | PROFILE | STATUS |
|-----------------------------|----------|--------------|
| dashboard | minikube | enabled ✅ |
| default-storageclass | minikube | enabled ✅ |
| efk | minikube | disabled |
| freshpod | minikube | disabled |
| gvisor | minikube | disabled |
| helm-tiller | minikube | disabled |
| ingress | minikube | enabled ✅ |
| ingress-dns | minikube | disabled |
| istio | minikube | disabled |
| istio-provisioner | minikube | disabled |
| logviewer | minikube | disabled |
| metrics-server | minikube | disabled |
| nvidia-driver-installer | minikube | disabled |
| nvidia-gpu-device-plugin | minikube | disabled |
| registry | minikube | disabled |
| registry-creds | minikube | disabled |
| storage-provisioner | minikube | enabled ✅ |
| storage-provisioner-gluster | minikube | disabled |
|-----------------------------|----------|--------------|

The ingress has been activated。

Add an Ingress rule

The previous operation just activates ingress, to use ingress to expose the service, you also need to customize the rules of ingress。 Create a new file and name it: k8s-basic-ingress.yaml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: k8s-basic-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: minikube.study.me
http:
paths:
- path: /
backend:
serviceName: springboot-k8s-basic
servicePort: 8080

Note: Host must use a DNS name, not an IP address

In this rule definition, we do not define the port of ingress, and the system uses the default port: 80

Deploy this rule, execute:

1
kubectl apply -f .\k8s-basic-ingress.yaml

The system display:

1
ingress.networking.k8s.io/k8s-basic-ingress created

We can view the ingress that have been deployed in the current minikube, execute:

1
kubectl get ingress

The system display:

1
2
NAME                HOSTS               ADDRESS        PORTS   AGE
k8s-basic-ingress minikube.study.me 192.168.1.54 80 45s

As you can see, the ingress rule for k8s-basic-ingress has been successfully deployed.

Because we use a custom minikube.study.me as the domain name, which doesn’t exist in the DNS system, we need to manually add the host record. Edit the native hosts file, which is located in the window system:

1
C:\Windows\System32\drivers\etc

在文件末尾添加一行:

1
192.168.1.54 minikube.study.me

192.168.1.54 is the IP address of the host on which minikube is running。

It can then be accessed in a browser:

1
http://minikube.study.me

本文标题:使用 Ingress 暴露 minikube 中的应用

文章作者:Morning Star

发布时间:2020年10月08日 - 15:10

最后更新:2022年01月11日 - 18:01

原始链接:https://www.mls-tech.info/microservice/k8s/minikube-use-ingress-en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。