skip to Main Content

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…"

enter image description here

                                                                   ^
                                                                   |
                                                                 here

I have a lead…
when i search "https://192.168.59.100:8443/" error 403 comes up:
enter image description here

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


  1. Your service’s selector tries to match pods with label: app.kubernetes.io/name: web, but pods have app: web label. They do not match. The selector on service must match labels on pods. As you use deployment object, this means the same labels as in spec.template.metadata.labels.

    Login or Signup to reply.
  2. @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 (in deployment.yaml):

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: web-deployment
    spec:
      replicas: 3
      template:
        spec:
          containers:
          - name: go-web-app
            image: go-app-ms:latest
            imagePullPolicy: IfNotPresent
            ports:
            - containerPort: 80
    

    And your Service to this (in service.yaml):

    apiVersion: v1
    kind: Service
    metadata:
      name: web-service
    spec:
      type: NodePort
      ports:
        - port: 80
          targetPort: 80
    

    And we place the following kustomization.yaml in the same directory:

    apiVersion: kustomize.config.k8s.io/v1beta1
    kind: Kustomization
    
    commonLabels:
      app: web
    
    resources:
    - deployment.yaml
    - service.yaml
    

    Then we can deploy this application by running:

    kubectl apply -k .
    

    And this will result in the following manifests:

    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: web
      name: web-service
    spec:
      ports:
      - port: 80
        targetPort: 80
      selector:
        app: web
      type: NodePort
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: web
      name: web-deployment
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: web
      template:
        metadata:
          labels:
            app: web
        spec:
          containers:
          - image: go-app-ms:latest
            imagePullPolicy: IfNotPresent
            name: go-web-app
            ports:
            - containerPort: 80
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search