Skip to content

Commit

Permalink
score-k8s for node-service (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieu-benoit authored Jul 4, 2024
1 parent 5262204 commit 291d5dc
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 9 deletions.
4 changes: 3 additions & 1 deletion templates/node-service/content/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules/
.score-compose/
compose.yaml
compose.yaml
.score-k8s/
manifests.yaml
57 changes: 56 additions & 1 deletion templates/node-service/content/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,59 @@ compose-test: compose-up
## Delete the containers running via compose down.
.PHONY: compose-down
compose-down:
docker compose down -v --remove-orphans || true
docker compose down -v --remove-orphans || true

.score-k8s/state.yaml:
score-k8s init \
--no-sample

manifests.yaml: score.yaml .score-k8s/state.yaml Makefile
score-k8s generate score.yaml \
--image ${CONTAINER_IMAGE}

## Create a local Kind cluster.
.PHONY: kind-create-cluster
kind-create-cluster:
./scripts/setup-kind-cluster.sh

## Load the local container image in the current Kind cluster.
.PHONY: kind-load-image
kind-load-image:
kind load docker-image ${CONTAINER_IMAGE}

NAMESPACE ?= default
## Generate a manifests.yaml file from the score spec, deploy it to Kubernetes and wait for the Pods to be Ready.
.PHONY: k8s-up
k8s-up: manifests.yaml
kubectl apply \
-f manifests.yaml \
-n ${NAMESPACE}
kubectl wait deployments/${WORKLOAD_NAME} \
-n ${NAMESPACE} \
--for condition=Available \
--timeout=90s
kubectl wait pods \
-n ${NAMESPACE} \
-l app.kubernetes.io/name=${WORKLOAD_NAME} \
--for condition=Ready \
--timeout=90s
sleep 5

## Expose the container deployed in Kubernetes via port-forward.
.PHONY: k8s-test
k8s-test: k8s-up
curl $$(score-k8s resources get-outputs dns.default#${WORKLOAD_NAME}.dns --format '{{ .host }}')

## Delete the deployment of the local container in Kubernetes.
.PHONY: k8s-down
k8s-down:
kubectl delete \
-f manifests.yaml \
-n ${NAMESPACE}

## Deploy the workload to Humanitec.
.PHONY: htc-up
htc-up:
humctl score deploy \
-f score.yaml \
--wait
33 changes: 29 additions & 4 deletions templates/node-service/content/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,36 @@
# Score - ${{ values.name | capitalize }}

A simple score project deploying a hello world app.
The workload is a simple containerized NodeJS app talking to a MySQL database.

[Score](https://score.dev/) is used to deploy the workload locally with `score-compose` in Docker, with `score-k8s` in Kubernetes or with `humctl` in Humanitec. See [Makefile](Makefile) for more details.

## Deploying

[Score](https://score.dev/) is used to deploy the workload to humanitec.
Locally:
```bash
make compose-up

make compose-test
```

In Kubernetes (`Kind`):
```bash
make kind-create-cluster

make kind-load-image

make k8s-up

make k8s-test
```

In Humanitec:
```bash
humctl login

## The Workload
humctl config set org FIXME
humctl config set app ${{ values.name }}
humctl config set app development

The workload is a simple nodejs app which is dockerized.
make htc-up
```
35 changes: 35 additions & 0 deletions templates/node-service/content/scripts/setup-kind-cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
set -o errexit

cat <<EOF | kind create cluster --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 31000
hostPort: 80
protocol: TCP
EOF

kubectl apply \
-f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.1.0/standard-install.yaml

helm install ngf oci://ghcr.io/nginxinc/charts/nginx-gateway-fabric \
--create-namespace \
-n nginx-gateway \
--set service.type=NodePort \
--set-json 'service.ports=[{"port":80,"nodePort":31000}]'

kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: default
spec:
gatewayClassName: nginx
listeners:
- name: http
port: 80
protocol: HTTP
EOF
9 changes: 8 additions & 1 deletion templates/podinfo-example/content/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,11 @@ k8s-test: k8s-up
k8s-down:
kubectl delete \
-f manifests.yaml \
-n ${NAMESPACE}
-n ${NAMESPACE}

## Deploy the workload to Humanitec.
.PHONY: htc-up
htc-up:
humctl score deploy \
-f score.yaml \
--wait
33 changes: 31 additions & 2 deletions templates/podinfo-example/content/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
# Podinfo - ${{ values.name | capitalize }}

A simple score project deploying podinfo.
The workload is the `podinfo` container image.

[Score](https://score.dev/) is used to deploy the workload locally with `score-compose` in Docker, with `score-k8s` in Kubernetes or with `humctl` in Humanitec. See [Makefile](Makefile) for more details.

## Deploying

[Score](https://score.dev/) is used to deploy the workload to humanitec.
Locally:
```bash
make compose-up

make compose-test
```

In Kubernetes (`Kind`):
```bash
make kind-create-cluster

make kind-load-image

make k8s-up

make k8s-test
```

In Humanitec:
```bash
humctl login

humctl config set org FIXME
humctl config set app ${{ values.name }}
humctl config set app development

make htc-up
```

0 comments on commit 291d5dc

Please sign in to comment.