Auycro

Hello Skaffold

03 May 2022

THis is just a memo about how I created a skaffold yaml from an existing Node.js app.

Prerequisites

  1. minikube
  2. skaffold
  3. kubectl
  4. Docker
  5. Node.js with yarn

Create Node.js app (Vite)

$ yarn create vite
✔ Project name: … webapp
...
...
$ yarn install
$ yarn dev

You will get webapp folder here

Prepare Dockerfile

webapp/Dockerfile

#for dev purpose
FROM node:16
EXPOSE 3000

WORKDIR /workspace/app

COPY package.json package.json
RUN yarn install

COPY . .

CMD ["yarn", "dev","--host"]

Test run docker

$ docker build -t nodejs-webapp .
$ docker run -it --rm -p 3000:3000 --name sample nodejs-webapp

Optional: .dockerignore

node_modules
kubernetes

Setting up minikube and skaffold

quickstart for minikube and skaffold

Start minikube

$ minikube start --profile custom
$ skaffold config set --global local-cluster true
$ eval $(minikube -p custom docker-env)

Prepare k8s yaml

web/kubernetes/k8s-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: webapp
spec:
  containers:
  - name: webapp
    image: nodejs-webapp

web/kubernetes/k8s-pod-local.yaml

apiVersion: v1
kind: Pod
metadata:
  name: webapp
spec:
  containers:
  - name: webapp
    image: nodejs-webapp
    imagePullPolicy: Never # prevent from pulling image
  restartPolicy: Never

Test pod in minikube

$ kubectl apply -f web/kubernetes/k8s-pod-local.yaml
$ kubectl get all
$ kubectl port-forward webapp 3000:3000

Check app with curl

$ curl localhost:3000

Test application with skaffold

$ skaffold init

web/skaffold.yaml

apiVersion: skaffold/v2beta28
kind: Config
metadata:
  name: sample-skaffold
build:
  artifacts:
  - image: nodejs-webapp
    context: web
    docker:
      dockerfile: Dockerfile
deploy:
  kubectl:
    manifests:
    - web/kubernetes/k8s-pod.yaml

Test run

$ skaffold dev

Create proxy to test webapp

$ kubectl port-forward webapp 3000:3000

Project Structure

hello-skaffold
├── skaffold.yaml
└── web
    ├── Dockerfile
    ├── README.md
    ├── index.html
    ├── kubernetes
    │   └── k8s-pod.yaml
    │   └── k8s-pod-local.yaml
    ├── package.json
    ├── public
    │   └── favicon.ico
    ├── src
    │   ├── App.vue
    │   ├── assets
    │   ├── components
    │   ├── env.d.ts
    │   └── main.ts
    ├── tsconfig.json
    ├── tsconfig.node.json
    ├── vite.config.ts
    └── yarn.lock

:)