Home > Software design >  Problem deploying golang app to kubernetes
Problem deploying golang app to kubernetes

Time:07-25

So I followed this tutorial that explains how to building containerized microservices in Golang, Dockerize and Deploy to Kubernetes.

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

CodePudding user response:

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.

CodePudding user response:

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

  • Related