I deployed a web app in minikube.
Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 3
selector:
matchLabels:
app: maypp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp
imagePullPolicy: Never
ports:
- containerPort: 8080
restartPolicy: Always
Service:
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
type: NodePort
ports:
- port: 8080
After kubectl apply -f .
to deploy, all of them are running.
kubectl get po
NAMESPACE NAME READY STATUS RESTARTS AGE
default myapp-5d042b65c8-h7kag 1/1 Running 0 10m
kubectl get svc
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default myservice NodePort 10.102.128.35 <none> 8080:32212/TCP 10m
Run minikube service
to open service in browser:
minikube service myservice
|----------------|--------------------|-------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|----------------|--------------------|-------------|---------------------------|
| default | myservice | 8080 | http://192.168.49.2:32212 |
|----------------|--------------------|-------------|---------------------------|
🏃 Starting tunnel for service myservice.
|----------------|--------------------|-------------|------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|----------------|--------------------|-------------|------------------------|
| default | myservice | | http://127.0.0.1:55401 |
|----------------|--------------------|-------------|------------------------|
🎉 Opening service default/myservice in default browser...
In browser, I can't access my app with both http://192.168.49.2:32212
and http://127.0.0.1:55401
. Even http://127.0.0.1:8080
.
From the pod's log, the web server in the container is running:
kubectl logs -f myapp-5d042b65c8-h7kag
...
____________________________________O/_______
O\
⇨ http server started on [::]:8080
But didn't get the way to access the endpoint correctly. So there isn't any traffic happened.
If I deploy the container with docker, it works with http://127.0.0.1:8080
.
The problem is the missing selector
field in the Service
.
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
type: NodePort
selector:
app: myapp
ports:
- port: 8080
Services route the traffic to the pods based on the selector defined in the service spec that match to the labels defined on the pods.
From the defining-a-service section of the docs:
The controller for the Service selector continuously scans for Pods that match its selector, and then POSTs any updates to an Endpoint object also named "my-service".