So I followed this tutorial that explains how to building containerized microservices in Golang, Dockerize and Deploy to Kubernetes.
https://www.youtube.com/watch?v=H6pF2Swqrko
I got to the point that I can access my app via the minikube ip (mine is 192.168.59.100).
I set up kubernetes, I currently have 3 working pods but I can not open my golang app through kubernetes with the url that the kubectl shows me: "192.168.59.100:31705…"
^
|
here
I have a lead…
when i search "https://192.168.59.100:8443/" error 403 comes up:
Here is my deployment.yml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
labels:
app: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: go-web-app
image: go-app-ms:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
Here is my service.yml:
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
type: NodePort
selector:
app.kubernetes.io/name: web
ports:
- port: 80
targetPort: 80
2
Answers
Your service’s selector tries to match pods with label:
app.kubernetes.io/name: web
, but pods haveapp: web
label. They do not match. The selector on service must match labels on pods. As you usedeployment
object, this means the same labels as inspec.template.metadata.labels
.@Szczad has correctly described the problem. I wanted to suggest a way of avoiding that problem in the future. Kustomize is a tool for building Kubernetes manifests. It is built into the
kubectl
command. One of its features is the ability to apply a set of common labels to your resources, including correctly filling in selectors in services and deployments.If we simplify your
Deployment
to this (indeployment.yaml
):And your
Service
to this (inservice.yaml
):And we place the following
kustomization.yaml
in the same directory:Then we can deploy this application by running:
And this will result in the following manifests:
As you can see here, the
app: web
label has been applied to the deployment, to the deployment selector, to the pod template, and to the service selector.Applying the labels through Kustomize like this means that you only need to change the label in one place. It makes it easier to avoid problems caused by label mismatches.