6

I am trying to run my first app with Kubernetes locally (or i should say minikube).

I have a pretty basic web server (one local docker image), and the official mongodb (that i would like to pull ideally from dockerhub) image.

I am not trying to deploy a mongodb cluster, just the minimum stuff to get my app running locally would be a great start!

First, i succeed to run my web server alone with kubectl run <MY_APP> --image=<MY_IMAGE> --port 3030 --image-pull-policy=IfNotPresent, then kubectl port-forward <MY_POD> 3030:80 and it works fine, i can hit the app from the 3030 port (the app is listening and the container expose the port 80).

But i would like to translate that into a manifest file to describe all the containers i need to run easily.

My first issue is i can't find how kubectl port-forward is supposed to be translated into a manifest file. I was thinking about targetPortbut i have a validation error when trying that, it looks like we can't use targetPort in pods containers ports description.

My second issue is that i am not really sure about what i am doing by trying to run that stack by describing a pod only. It may need other pieces, service is i think optional for my need, i am not sure about deployment, but i have seen an endpoint kind, and i could ignore other ones...

I am a little bit confuse since kubectl run seems to create a pod, a deployment, and a replica sets, i am not sure if i have to create all of that from my manifest file.

I just want to run my both containers locally to work on the code and refresh it everytime i make a change, and test it.

So my question have some sub questions due to my lack of knowledges about Kubernetes, but basically, i would like to know how to translate my kubectl run <MY_APP>and kubectl port-forward <MY_POD> 3030:80into a manifest file so i can add the mongodb container and start the whole stack with a single kubectl create -f ./local.yaml command line.

Here is a first draft:

apiVersion: v1
kind: Pod
metadata:
  name: my_app
spec:
  containers:
    - name: web-server
      image: my_app
      imagePullPolicy: IfNotPresent
      ports:
        - name: my_app
          containerPort: 3030
          targetPort: 80
          protocol: TCP
    - name: mongodb
      image: mongodb
      ports:
        - name: mongodb
          containerPort: 27017
          protocol: TCP
Ludo
  • 5,060
  • 15
  • 53
  • 85

1 Answers1

7

Yes, you are right. You can expose your app as a service with Type NodePort (though it's not very clear in documentation), and your service's yaml will look like:

apiVersion: v1
kind: Service
metadata:
  name: app-service
spec:
  type: NodePort
  ports:
    - port: 3030
      targetPort: 80
  selector:
    app: app-server

Your deployment's yaml will look like:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-server
  labels:
    app: app-server
spec:
  selector:
    matchLabels:
      app: app-server
  template:
    metadata:
      labels:
        app: app-server
    spec:
      containers:
      - name: web-server
        image: my_app
        ports:
        - containerPort: 80
      - name: mongodb
        image: mongodb
        ports:
        - containerPort: 27017

As you see, I exposed only your web server. Now, to get access to Mongo from outside the Kubernetes, you need to expose it too.

You can deploy your app as a command:

kubctl apply -f ./file_with_service.yaml ./file_with_deployment.yaml

And you can use all that as an example to begin, and read more documentation to understand it clearly.

Nick Rak
  • 2,629
  • 13
  • 19
  • Hi, thank you very much for your answer, yes i really need this first foundation to experiment further on the top of it. I have it all green on the minikube dashboard, but the port forwarding is not working. I have to still run ``kubectl port-forward app-server- 3030:80`` and it works after that, any idea? i ran the ``kubctl apply`` in two times (one per file) since it looks like you can't pass 2 files in argument – Ludo Apr 27 '18 at 10:14
  • [Here is what the service menu looks like](https://ibb.co/fHz3pH), shouldn't it have an "Endpoints" entry? Or is there a way to check if the ``NodePort``component has been well created somewhere in the minikube dashboard? Thanks ! – Ludo Apr 27 '18 at 10:22
  • try to run: `minikube service app-server --url=true` you should have the url as an answer. that's should be the url for you service – Nick Rak Apr 27 '18 at 10:38
  • There is something wrong, i get 20 ``Waiting, endpoint for service is not ready yet...``, then ``Error opening service: Could not find finalized endpoint being pointed to by app-server: Temporary Error: Endpoint for service is not ready yet``, then 20 ``Temporary Error: Endpoint for service is not ready yet`` – Ludo Apr 27 '18 at 12:01
  • When i run ``kubectl get endpoints --all-namespaces`` the app-server line is: ``default app-server 1h`` (did you expect to see "none" on the ENDPOINTS column?) – Ludo Apr 27 '18 at 12:02
  • @Ludo I found my mistake with selectors. I have just updated the answer. With this error your service can't find pod and can't create the endpoint for it. – Nick Rak Apr 30 '18 at 10:13
  • I was diving around the ``selector`` but couldn't really notice the difference between ``name``and ``app``. Anyway, it works fine now, thanks!! If you want to make your example even better, it is required to add a name for each port (in the service yml) and it's good to go ! Thanks again! – Ludo Apr 30 '18 at 16:50