<p>I've been following <a href="https://www.youtube.com/watch?v=DwlIn9zOcfc" rel="nofollow noreferrer">tutorial videos</a> and trying to understand to build a small minimalistic application. The videos I followed are pulling containers from the registries while I'm trying to test, build and deploy everything locally at the moment if possible. Here's my setup.</p>

<ol>
<li><p>I've the latest docker installed with Kubernetes enabled on mac OS.</p></li>
<li><p>A helloworld NodeJS application running with Docker and Docker Compose</p></li>
</ol>

<p><strong>TODO:</strong> I'd like to be able to start my instances, let's say 3 in the kubernetes cluster</p>

<hr>

<p><strong>Dockerfile</strong></p>

<pre><code>FROM node:alpine

COPY package.json package.json
RUN npm install

COPY . .
CMD ["npm", "start"]
</code></pre>

<hr>

<p><strong>docker-compose.yml</strong></p>

<pre><code>version: '3'

services:
    user:
      container_name: users
      build:
        context: ./user
        dockerfile: Dockerfile
</code></pre>

<hr>

<p>Creating a deployment file with the help of this <a href="https://www.mirantis.com/blog/introduction-to-yaml-creating-a-kubernetes-deployment/" rel="nofollow noreferrer">tutorial</a> and it may have problems since I'm merging information both from youtube as well as the web link.</p>

<p>Creating a miminalistic yml file for to be able to get up and running, will study other aspects like readiness and liveness later.</p>

<hr>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: user
spec:
  selector:
    app: user
  ports:
    - port: 8080
  type: NodePort
</code></pre>

<p>Please review the above yml file for correctness, so the question is what do I do next?</p>

<p>The snippets you provide are regrettably insufficient but you have the basics.</p>
<p>I had a Google for you for a tutorial and -- unfortunately -- nothing obvious jumped out. That doesn't mean that there isn't one, just that I didn't find it.</p>
<p>You've got the right idea and there are quite a few levels of technology to understand but, I commend your approach and think we can get you there.</p>
<ol>
<li>Let's start with a helloworld Node.JS tutorial</li>
</ol>
<p><a href="https://nodejs.org/en/docs/guides/getting-started-guide/" rel="nofollow noreferrer">https://nodejs.org/en/docs/guides/getting-started-guide/</a></p>
<ol start="2">
<li>Then you want to containerize this</li>
</ol>
<p><a href="https://nodejs.org/de/docs/guides/nodejs-docker-webapp/" rel="nofollow noreferrer">https://nodejs.org/de/docs/guides/nodejs-docker-webapp/</a></p>
<p>For #3 below, the last step here is:</p>
<pre class="lang-sh prettyprint-override"><code>docker build --tag=&lt;your username&gt;/node-web-app .
</code></pre>
<p>But, because you're using Kubernetes, you'll want to push this image to a public repo. This is so that, regardless of where your cluster runs, it will be able to access the container image.</p>
<p>Since the example uses DockerHub, let's continue using that:</p>
<pre class="lang-sh prettyprint-override"><code>docker push &lt;your username&gt;/node-web-app
</code></pre>
<p><strong>NB</strong> There's an implicit <code>https://docker.io/&lt;your username&gt;/node-web-app:latest</code> here</p>
<ol start="3">
<li>Then you'll need a Kubernetes cluster into which you can deploy your app</li>
</ol>
<ul>
<li>I think <a href="https://microk8s.io/" rel="nofollow noreferrer">microk8s</a> is excellent</li>
<li>I'm a former Googler but <a href="https://cloud.google.com/kubernetes-engine/" rel="nofollow noreferrer">Kubernetes Engine</a> is the benchmark (requires $$$)</li>
<li>Big fan of DigitalOcean too and it has <a href="https://www.digitalocean.com/products/kubernetes/" rel="nofollow noreferrer">Kubernetes</a> (also $$$)</li>
</ul>
<p>My advice is (except microk8s and minikube) don't ever run your own Kubernetes clusters; leave it to a cloud provider.</p>
<ol start="4">
<li>Now that you have all the pieces, I recommend you just:</li>
</ol>
<pre class="lang-sh prettyprint-override"><code>kubectl run yourapp \
--image=&lt;your username&gt;/node-web-app:latest \
--port=8080 \
--replicas=1
</code></pre>
<p>I believe <code>kubectl run</code> is deprecated but use it anyway. It will create a Kubernetes Deployment (!) for you with 1 Pod (==replica). Feel free to adjust that value (perhaps <code>--replicas=2</code>) if you wish.</p>
<p>Once you've created a Deployment, you'll want to create a Service to make your app accessible (top of my head) this command is:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl expose deployment/yourapp --type=NodePort
</code></pre>
<p>Now you can query the service:</p>
<pre><code>kubectl get services/yourapp

NAME    TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
yourapp NodePort   10.152.183.27   &lt;none&gt;        80:32261/TCP   7s
</code></pre>
<p><strong>NB</strong> The NodePort that's been assigned (in this case!) is <code>:32261</code> and so I can then interact with the app using <code>curl http://localhost:32261</code> (localhost because I'm using microk8s).</p>
<p><code>kubectl</code> is powerful. Another way to determine the NodePort is:</p>
<pre><code>kubectl get service/yourapp \
--output=jsonpath=&quot;{.spec.ports[0].nodePort}&quot;
</code></pre>
<p>The advantage of the approach of starting from <code>kubectl run</code> is you can then easily determine the Kubernetes configuration that is needed to recreate this Deployment|Service by:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl get deployment/yourapp \
--format=yaml \
&gt; ./yourapp.deployment.yaml

kubectl get service/yourapp \
--format=yaml \
&gt; ./yourapp.service.yaml

</code></pre>
<p>These commands will interrogate the cluster, retrieve the configuration for you and pump it into the files. It will include some instance data too but the gist of it shows you what you would need to recreate the deployment. You will need to edit this file.</p>
<p>But, you can test this by first deleting the deployment and the service and then recreating it from the configuration:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl delete deployment/yourapp
kubectl delete service/yourapp

kubectl apply --filename=./yourapp.deployment.yaml
kubectl apply --filename=./yourapp.service.yaml
</code></pre>
<p><strong>NB</strong> You'll often see multiple resource configurations merged into a single YAML file. This is perfectly valid YAML but you only ever see it used by Kubernetes. The format is:</p>
<pre><code>...
some: yaml
---
...
some: yaml
---
</code></pre>
<p>Using this you could merge the <code>yourapp.deployment.yaml</code> and <code>yourapp.service.yaml</code> into a single Kubernetes configuration.</p>
