skip to Main Content

I’m freshman on microk8s, and I’m trying out things by deploying a simple apache2 to see things working on my Mac M1:

◼ ~ $ microk8s kubectl run apache --image=ubuntu/apache2:2.4-22.04_beta --port=80
pod/apache created
◼ ~ $ microk8s kubectl get pods
NAME                                   READY   STATUS             RESTARTS       AGE
apache                                 1/1     Running            0              5m37s
◼ ~ $ microk8s kubectl port-forward pod/apache 3000:80
Forwarding from 127.0.0.1:3000 -> 80

but:

◼ ~ $ curl http://localhost:3000
curl: (7) Failed to connect to localhost port 3000 after 5 ms: Connection refused

I’ve also tried to use a service:

◼ ~ $ microk8s kubectl expose pod apache --type=NodePort  --port=4000 --target-port=80
service/apache exposed
◼ ~ $ curl http://localhost:4000
curl: (7) Failed to connect to localhost port 4000 after 3 ms: Connection refused

I guess I’m doing something wrong?

3

Answers


  1. Chosen as BEST ANSWER

    For some reason I haven't figured it out, if I port-forward right within the VM by opening a shell via multipass, it does work. Next, you simply have to point to the VM's IP:

    within a VM's shell:

    ubuntu@microk8s-vm:~$ sudo microk8s kubectl port-forward service/hellopg 8080:8080 --address="0.0.0.0"
    Forwarding from 0.0.0.0:8080 -> 8080
    Handling connection for 8080
    
    ubuntu@microk8s-vm:~$ ifconfig enp0s1
    enp0s1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 192.168.64.2  netmask 255.255.255.0  broadcast 192.168.64.255
            inet6 fde3:1a04:ba31:1209:5054:ff:fea9:9cf4  prefixlen 64  scopeid 0x0<global>
    
    

    from the host:

     curl http://192.168.64.2:8080/hello                                                                                                                                                                                                                   7
    {"status": "how you doing?", "env_var":"¡hola mundo!"}
    

    it works. I guess the command via microk8s is not executed properly within the machine? If anybody can explain this I'll update the question


  2. Microk8’s acts the same as kuberentes. So, it’s better to create a Service with NodePort. This would expose your apache.

    apiVersion: v1
    kind: Service
    metadata:
      name: my-apache
    spec:
      type: NodePort
      selector:
        app: apache
      ports:
        - port: 80
          targetPort: 80
          nodePort: 30004
    

    Change the selector as per your requirement. For more detailed information to create NodePort service refer to this official document
    You can use ingress as well. But in your case only for testing you can go with NodePort

    Login or Signup to reply.
  3. I think the easiest way for you to test it would be adding: externalIPs to you service.

    kind: Service
    metadata:
      name: nginx-service
    spec:
      selector:
        app: nginx
      ports:
        - name: http
          protocol: TCP
          port: 80
          targetPort: 80
      externalIPs:
        - 192.168.56.100 #your cluster IP 
    

    Happy coding!

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