on minikube with WSL2, loadbalancer or nodeport not working, but kubectl expose is work well

Viewed 42

I'm using minikube on WSL2. I deployed a simple flask app image and write a LoadBalancer to expose the service.

My question is, how do I modify service manifest to get the same result as expose?

Below are more details.

flask app deployment yaml.

rss.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: esg
spec:
  selector:
    matchLabels:
      app: rss
  replicas: 3
  template:
    metadata:
      labels:
        app: rss
    spec: 
      containers:
      - name: rss
        image: "idioluck/00esg_rss:v01"
        ports:
        - containerPort: 5000

service yaml (I tried nodeport and loadbalanacer either.)

rss_lb.yaml

apiVersion: v1
kind: Service
metadata:
  name: esg-lb
spec:
  type: NodePort # LoadBalancer
  selector:
    app: rss  
  ports:
  - protocol: TCP
    port: 8080 
    targetPort: 5000

kubectl command is

sjw@DESKTOP-MFPNHRC:~/esg_kube/kubesvc/rss$ kubectl apply -f rss.yaml
deployment.apps/esg created
sjw@DESKTOP-MFPNHRC:~/esg_kube/kubesvc/rss$ kubectl apply -f rss_lb.yaml
service/esg-lb created
sjw@DESKTOP-MFPNHRC:~/esg_kube/kubesvc/rss$ kubectl get pods
NAME                  READY   STATUS    RESTARTS   AGE
esg-757f659b4-4vndc   1/1     Running   0          13s
esg-757f659b4-4wd2w   1/1     Running   0          13s
esg-757f659b4-sf5q6   1/1     Running   0          13s
sjw@DESKTOP-MFPNHRC:~/esg_kube/kubesvc/rss$ kubectl get all
NAME                      READY   STATUS    RESTARTS   AGE
pod/esg-757f659b4-4vndc   1/1     Running   0          16s
pod/esg-757f659b4-4wd2w   1/1     Running   0          16s
pod/esg-757f659b4-sf5q6   1/1     Running   0          16s

NAME                 TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
service/esg-lb       LoadBalancer   10.101.221.26   <pending>     8080:31308/TCP   8s
service/kubernetes   ClusterIP      10.96.0.1       <none>        443/TCP          23h

NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/esg   3/3     3            3           16s

NAME                            DESIRED   CURRENT   READY   AGE
replicaset.apps/esg-757f659b4   3         3         3       16s

Exteranl ip is pending. so i delete loabdbalancer and use expose

sjw@DESKTOP-MFPNHRC:~/esg_kube/kubesvc/rss$ kubectl expose deployment esg --type=LoadBalancer --port=8080
service/esg exposed
sjw@DESKTOP-MFPNHRC:~/esg_kube/kubesvc/rss$ kubectl get svc
NAME         TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
esg          LoadBalancer   10.99.208.98   127.0.0.1     8080:30929/TCP   46s
kubernetes   ClusterIP      10.96.0.1      <none>        443/TCP          23h

The service has been successfully exposed. And the service is a load balancer.

1 Answers

Your LoadBalancer type service is showing Pending as status because it is waiting for you to provision an external Load Balancer like AWS's Elastic Load Balancer or GCP's Load Balancer. LoadBalancer type services are usually used together with managed Kubernetes service e.g EKS, GKE etc.

On the other hand, you're able to expose your service because it already has clusterIP assigned to it.

If you want to use LB in Minikube, this official doc may help you. Otherwise, you can use NodePort type service directly to expose your flask app.

Related