Kubernetes service not exposed on minikube

Viewed 37

I'm new to k8 and i'm trying to figure out how to deploy my first docker image on minikube.

My k8.yaml file is:

apiVersion: apps/v1

kind: Deployment

metadata:
  name: my-deployment
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-docker-image
        image: my-docker-image:1.0
        ports:
         - containerPort: 8080


---
apiVersion: v1

kind: Service

metadata:
  name: my-service
  namespace: default
spec:
  type: LoadBalancer
  selector:
    app: my-service
  ports:
    - port: 8080
      targetPort: 8080
    
     
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: my-ingress
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-service
            port:
              number: 8080

Everything seems fine to me, however, I'm not able to reach my service on cluster. I tried to create a tunnel using the minikube tunnel command and i have this result if i execute kubectl get services

NAME        TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
my-service  LoadBalancer   10.109.154.236   127.0.0.1     8080:30558/TCP   2m53s

However, if i try to call my service at 127.0.0.1:30588 host is unreachable.

Can someone help me?

1 Answers

There is an issue in the service selector as well, so first, we need to fix this service selector and it should match with with deployment label

  replicas: 1
  selector:
    matchLabels:
      app: my-app

and the service should refer to this selector

the selector should be my-app in the service or same as above for deployment

  type: LoadBalancer
  selector:
    app: my-app
  ports:
    - port: 8080
      targetPort: 8080

to access from the host

minikube service my-service

enter image description here

and here you go

kubectl delete -f myapp.yaml
kubectl apply -f myapp.yaml

deployment manifest

apiVersion: apps/v1

kind: Deployment
metadata:
  name: my-deployment
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-docker-image
        image: nginx
        ports:
         - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: my-service
  namespace: default
spec:
  type: LoadBalancer
  selector:
    app:  my-app
  ports:
    - port: 80
      targetPort: 80

Also worth considering the service type for Minikube.

What's the difference between ClusterIP, NodePort and LoadBalancer service types in Kubernetes?

Related