Skip to content

Commit

Permalink
setup-kind-cluster in Makefile (#134)
Browse files Browse the repository at this point in the history
* setup-kind-cluster in Makefile

* "curl: (56) Recv failure: Connection reset by peer"

* sleep 5

* kubectl wait deployments/${WORKLOAD_NAME}

* curl: (56) Recv failure: Connection reset by peer
  • Loading branch information
mathieu-benoit authored Jun 5, 2024
1 parent 0c49fe0 commit c21635d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 67 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/close-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ on:
- closed
env:
ENVIRONMENT_ID: pr-${{ github.event.number }}
HUMCTL_VERSION: '0.23.1'
HUMCTL_VERSION: '0.25.0'
jobs:
ob-pr-closed:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- name: checkout humctl bin
uses: actions/checkout@v4
Expand Down
31 changes: 6 additions & 25 deletions .github/workflows/open-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ env:
SCORE_K8S_VERSION: 'latest'
WORKLOAD_NAME: my-sample-workload
CONTAINER_NAME: my-sample-container
HUMCTL_VERSION: '0.23.1'
HUMCTL_VERSION: '0.25.0'
jobs:
build-run-test-push:
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- name: checkout code
uses: actions/checkout@v4
Expand Down Expand Up @@ -51,28 +51,9 @@ jobs:
- name: make compose-test
run: |
make compose-test
- name: create kind cluster
- name: make kind-create-cluster
run: |
kind create cluster
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=ClusterIP
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
make kind-load-image
make kind-create-cluster
- name: make kind-load-image
run: |
make kind-load-image
Expand All @@ -86,7 +67,7 @@ jobs:
id: k8s-up
run: |
make k8s-up
- name: catch helm install errors
- name: catch k8s-up errors
if: ${{ failure() && steps.k8s-up.outcome == 'failure' }}
run: |
kubectl get events
Expand Down Expand Up @@ -120,7 +101,7 @@ jobs:
${{ env.IMAGE_NAME }}:${{ env.ENVIRONMENT_ID }}
deploy-preview-env:
needs: build-run-test-push
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
env:
BASE_ENVIRONMENT: 'development'
ENVIRONMENT_TYPE: 'development'
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/push-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ env:
IMAGE_NAME: ${{ secrets.REGISTRY_LOCATION }}-docker.pkg.dev/${{ secrets.PROJECT_ID }}/${{ secrets.REGISTRY_NAME }}/my-sample-app
ENVIRONMENT_ID: development
WORKLOAD_NAME: my-sample-workload
HUMCTL_VERSION: '0.23.1'
HUMCTL_VERSION: '0.25.0'
jobs:
build-push:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
steps:
- name: checkout code
uses: actions/checkout@v4
Expand Down Expand Up @@ -61,7 +61,7 @@ jobs:
ghcr.io/${{ github.repository_owner }}/${{ env.WORKLOAD_NAME }}:latest
deploy-humanitec:
needs: build-push
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- name: install humctl
Expand Down
26 changes: 16 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ compose.yaml: score/score.yaml .score-compose/state.yaml Makefile
.PHONY: compose-up
compose-up: compose.yaml
docker compose up --build -d --remove-orphans
sleep 5

## Generate a compose.yaml file from the score spec, launch it and test (curl) the exposed container.
.PHONY: compose-test
compose-test: compose-up
sleep 5
curl $$(score-compose resources get-outputs dns.default#${WORKLOAD_NAME}.dns --format '{{ .host }}:8080')

## Delete the containers running via compose down.
Expand All @@ -49,30 +49,36 @@ manifests.yaml: score/score.yaml .score-k8s/state.yaml Makefile
--image ${CONTAINER_IMAGE} \
--override-property containers.${CONTAINER_NAME}.variables.MESSAGE="Hello, Kubernetes!"

## 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 and apply it in Kubernetes.
## 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
$(MAKE) k8s-down || true
$(MAKE) compose-down || true
kubectl apply \
-f manifests.yaml \
-n ${NAMESPACE}

## Expose the container deployed in Kubernetes via port-forward.
.PHONY: k8s-test
k8s-test: k8s-up
kubectl wait deployments/${WORKLOAD_NAME} \
-n ${NAMESPACE} \
--for condition=Available
kubectl wait pods \
-n ${NAMESPACE} \
-l score-workload=${WORKLOAD_NAME} \
-l app.kubernetes.io/name=${WORKLOAD_NAME} \
--for condition=Ready \
--timeout=90s
kubectl -n nginx-gateway port-forward service/ngf-nginx-gateway-fabric 8080:80

## 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
Expand Down
49 changes: 22 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Associated blog posts:
- [Sail Sharp, 8 tips to optimize and secure your .NET containers for Kubernetes](https://medium.com/p/c68ba253844a)
- [Keyless Google Cloud Authentication to push your containers to Artifact Registry from GitHub Actions](https://medium.com/p/3932dce678b8)

## Deploy the pre-built container
## Deploy the pre-built container with `docker` and `kubectl`

Run the pre-built container:
Run the pre-built container with Docker:
```bash
docker run \
-d \
Expand All @@ -29,6 +29,26 @@ kubectl apply \
-f service.yaml
```

## Build and deploy the container with Score with `docker compose`, `kubectl` and `humctl`

Build and run the container with Docker Compose:
```bash
make compose-up

make compose-test
```

Deploy the container in a local Kind cluster:
```bash
make kind-create-cluster

make kind-load-image

make k8s-up

make k8s-test
```

Deploy the pre-built container with Score/Humanitec:
```bash
humctl score deploy \
Expand All @@ -40,28 +60,3 @@ humctl score deploy \
-i ghcr.io/mathieu-benoit/my-sample-app:latest \
--wait
```

## Build and test the application locally

Build the application:
```bash
docker build \
--tag my-sample-app \
app/
```

Run the application:
```bash
docker run \
-d \
-p 8080:8080 \
--read-only \
--cap-drop=ALL \
--user=65532 \
my-sample-app
```

Test the application:
```bash
curl localhost:8080
```
35 changes: 35 additions & 0 deletions 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

0 comments on commit c21635d

Please sign in to comment.