Skip to content

Commit

Permalink
add single service kluctl example
Browse files Browse the repository at this point in the history
  • Loading branch information
sidpalas committed Jul 27, 2024
1 parent 01180f3 commit 51b0897
Show file tree
Hide file tree
Showing 27 changed files with 230 additions and 12 deletions.
8 changes: 5 additions & 3 deletions 12-deploying-to-multiple-environments/helm/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ version: "3"
tasks:
template-default:
cmds:
- helm template ./api-golang-helm-chart
- helm template ./api-golang-helm-chart | yq
desc: "Render chart yaml with default values"

template-staging:
cmds:
- helm template ./api-golang-helm-chart --values=./api-golang-helm-chart/values.staging.yaml
- helm template ./api-golang-helm-chart --values=./api-golang-helm-chart/values.staging.yaml | yq
desc: "Render chart yaml with staging values file"

template-production:
cmds:
- helm template ./api-golang-helm-chart --values=./api-golang-helm-chart/values.production.yaml
- helm template ./api-golang-helm-chart --values=./api-golang-helm-chart/values.production.yaml | yq
desc: "Render chart yaml with production values file"

package:
Expand All @@ -24,8 +24,10 @@ tasks:
push:
cmds:
- helm push api-golang-helm-chart-0.1.0.tgz oci://registry-1.docker.io/sidpalas
desc: "Push the helm chart to an oci registry"

build-push:
cmds:
- task: build
- task: push
desc: "Build & Push the helm chart to an oci registry"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
targets:
- name: staging
args:
environment: staging
# Adding a context to .kluctl.yaml helps prevent accidentally deploying to the wrong cluster!
context: devops-directive-kubernetes-course

- name: production
args:
environment: production
# Adding a context to .kluctl.yaml helps prevent accidentally deploying to the wrong cluster!
context: gke_kubernetes-course-424917_us-central1-a_devops-directive-kubernetes-course

args:
- name: environment

discriminator: kluctl-{{ target.name }}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
version: "3"

tasks:
helm-pull:
cmds:
- kluctl helm-pull
desc: "Pre-pull helm charts"

render-staging:
cmds:
- kluctl render -t staging --print-all | yq
desc: "Render resource yaml with staging values"

deploy-staging:
cmds:
- kluctl deploy -t staging
desc: "Deploy staging configuration"

delete-staging:
cmds:
- kluctl delete -t staging
desc: "Render resource yaml with staging values"

render-production:
cmds:
- kluctl render -t production --print-all | yq
desc: "Render resource yaml with production values"

deploy-production:
cmds:
- kluctl deploy -t production
desc: "Deploy production configuration"

delete-production:
cmds:
- kluctl delete -t production
desc: "Render resource yaml with production values"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sharedVars:
hostName: "kubernetes-course.devopsdirective.com"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sharedVars:
hostName: "kubernetes-course-staging.devopsdirective.com"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
vars:
- file: config/{{ args.environment }}.yaml

deployments:
- path: namespaces
- barrier: true
- include: services

commonLabels:
devopsdirective.com/course: "kubernetes-course"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: demo-app
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apiGolang:
version: 0.3.0 # PRODUCTION_IMAGE_TAG
replicas: 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apiGolang:
version: 0.3.0-2-g29b7fb5 # STAGING_IMAGE_TAG
replicas: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vars:
- file: config/{{ args.environment }}.yaml

deployments:
- path: manifests
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: api-golang
namespace: demo-app
labels:
app: api-golang
spec:
replicas: {{apiGolang.replicas}}
selector:
matchLabels:
app: api-golang
template:
metadata:
labels:
app: api-golang
spec:
containers:
- name: api-golang
image: sidpalas/devops-directive-docker-course-api-golang:{{ apiGolang.version }}
env:
- name: PORT
value: "8000"
envFrom:
- secretRef:
name: api-golang-database-url
ports:
- containerPort: 8000
protocol: TCP
readinessProbe:
httpGet:
path: /ping
port: 8000
resources:
limits:
memory: "100Mi"
requests:
memory: "100Mi"
cpu: "50m"
securityContext:
allowPrivilegeEscalation: false
privileged: false
securityContext:
seccompProfile:
type: RuntimeDefault
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: api-golang
namespace: demo-app
spec:
entryPoints:
- web
routes:
- kind: Rule
match: Host(`{{ sharedVars.hostName }}`) && PathPrefix(`/api/golang`)
middlewares:
- name: strip-api-prefixes
services:
- kind: Service
name: api-golang
port: 8080
scheme: http
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: batch/v1
kind: Job
metadata:
name: db-migrator
namespace: demo-app
annotations:
kluctl.io/hook: pre-deploy
kluctl.io/hook-weight: 2
spec:
template:
spec:
containers:
- name: migrate
image: sidpalas/devops-directive-kubernetes-course-db-migrator:{{ apiGolang.version }}
args:
- -path=/app/migrations
- -database=$(DATABASE_URL)?sslmode=disable
- up
envFrom:
- secretRef:
name: db-migrator-password
restartPolicy: OnFailure
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: strip-api-prefixes-golang
namespace: demo-app
spec:
stripPrefix:
forceSlash: false
prefixes:
- /api/golang
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ⛔️ DONT PUT SECRET FILES IN VCS
# Creating this secret here is a bit of a hack to ensure it exists before
# the db-migrator job which runs as a pre-deploy hook for api-golang
apiVersion: v1
kind: Secret
metadata:
name: db-migrator-password
namespace: demo-app
annotations:
kluctl.io/hook: pre-deploy
kluctl.io/hook-weight: 1
type: Opaque
stringData:
DATABASE_URL: postgres://postgres:[email protected]:5432/postgres
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ⛔️ DONT PUT SECRET FILES IN VCS
apiVersion: v1
kind: Secret
type: Opaque
metadata:
name: api-golang-database-url
namespace: demo-app
stringData:
DATABASE_URL: postgres://postgres:[email protected]:5432/postgres
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: api-golang
namespace: demo-app
spec:
selector:
app: api-golang
ports:
- protocol: TCP
port: 8000
targetPort: 8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
deployments:
- include: api-golang
4 changes: 2 additions & 2 deletions 12-deploying-to-multiple-environments/kluctl/.kluctl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ targets:
environment: staging
environmentType: non-prod
# Adding a context to .kluctl.yaml helps prevent accidentally deploying to the wrong cluster!
# context: SOME_KUBECTL_CONTEXT
context: devops-directive-kubernetes-course

- name: production
args:
environment: production
environmentType: prod
# Adding a context to .kluctl.yaml helps prevent accidentally deploying to the wrong cluster!
# context: SOME_KUBECTL_CONTEXT
context: gke_kubernetes-course-424917_us-central1-a_devops-directive-kubernetes-course-2

args:
- name: environment
Expand Down
4 changes: 2 additions & 2 deletions 12-deploying-to-multiple-environments/kluctl/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tasks:

render-staging:
cmds:
- kluctl render -t staging --print-all
- kluctl render -t staging --print-all | yq
desc: "Render resource yaml with staging values"

deploy-staging:
Expand All @@ -23,7 +23,7 @@ tasks:

render-production:
cmds:
- kluctl render -t production --print-all
- kluctl render -t production --print-all | yq
desc: "Render resource yaml with production values"

deploy-production:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
apiGolang:
version: 0.3.0 # PRODUCTION_IMAGE_TAG
replicas: 2
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
apiGolang:
version: 0.3.0-2-g29b7fb5 # STAGING_IMAGE_TAG
replicas: 1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
labels:
app: api-golang
spec:
replicas: 1
replicas: {{apiGolang.replicas}}
selector:
matchLabels:
app: api-golang
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
resources:
- Namespace.yaml
- helm-rendered.yaml # generated at deploy time

4 changes: 2 additions & 2 deletions 12-deploying-to-multiple-environments/kustomize/Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ tasks:
render-staging:
desc: "Render staging configuration"
cmds:
- kubectl kustomize ./staging
- kubectl kustomize ./staging | yq

render-production:
desc: "Render production configuration"
cmds:
- kubectl kustomize ./production
- kubectl kustomize ./production | yq
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
- op: replace
path: /spec/routes/0/match
value: "Host(`kubernetes-course.devopsdirective.com`)"
value: "Host(`kubernetes-course-staging.devopsdirective.com`)"

0 comments on commit 51b0897

Please sign in to comment.