RPI Microk8s NodeJS Tutorial
Hello NodeJS
Docker Image generieren und in die lokale Registry pushen.
Installation über Helm Chart.
docker build -t localhost:32000/hello-node:1.0 . docker push localhost:32000/hello-node:1.0 microk8s helm3 install hello-node ./k8s --set defaultBackend.enabled=false,controller.defaultBackendService=hello-golang-service -n dev --create-namespace microk8s helm3 uninstall hello-node -n dev
server.js
'use strict'; const express = require('express'); // Constants const PORT = 8080; const HOST = '0.0.0.0'; // App const app = express(); app.get('/', (req, res) => { res.send('Hello World'+JSON.stringify(process.env)+''); }); app.listen(PORT, HOST); console.log(`Running on http://${HOST}:${PORT}`);
Dockerfile
FROM node:14 # Create app directory WORKDIR /usr/src/app # Install app dependencies # A wildcard is used to ensure both package.json AND package-lock.json are copied # where available (npm@5+) COPY package*.json ./ RUN npm install # If you are building your code for production # RUN npm ci --only=production # Bundle app source COPY . . EXPOSE 8080 CMD [ "node", "server.js" ]
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: annotations: nginx.ingress.kubernetes.io/rewrite-target: /$2 name: hello-node-ingress spec: tls: - hosts: - kimo2007.dnshome.de secretName: letsencrypt-tls defaultBackend: service: name: custom-default-backend1 port: number: 80 rules: - host: kimo2007.dnshome.de http: paths: - path: /hello-node(/|$)(.*) pathType: Prefix backend: service: name: hello-node-service port: number: 80
Deployment & Service (deployment.yaml)
apiVersion: apps/v1 kind: Deployment metadata: name: hello-node spec: replicas: 1 selector: matchLabels: app: hello-node template: metadata: labels: app: hello-node spec: containers: - name: app image: localhost:32000/hello-node:1.0 ports: - containerPort: 8080 env: - name: MONGO_URL value: mongodb://mongo:27017/dev imagePullPolicy: Always --- apiVersion: v1 kind: Service metadata: labels: app: hello-node-service name: hello-node-service spec: externalTrafficPolicy: Cluster ports: - port: 80 targetPort: 8080 selector: app: hello-node type: NodePort