From 51b08979eb5e546fe439af5a97000c105659a14a Mon Sep 17 00:00:00 2001 From: sid palas Date: Sat, 27 Jul 2024 15:14:39 -0400 Subject: [PATCH] add single service kluctl example --- .../helm/Taskfile.yaml | 8 ++-- .../kluctl-single-service/.kluctl.yaml | 17 +++++++ .../kluctl-single-service/README.md | 0 .../kluctl-single-service/Taskfile.yaml | 37 +++++++++++++++ .../config/production.yaml | 2 + .../kluctl-single-service/config/staging.yaml | 2 + .../kluctl-single-service/deployment.yaml | 10 +++++ .../namespaces/Namespace.demo-app.yaml | 4 ++ .../api-golang/config/production.yaml | 3 ++ .../services/api-golang/config/staging.yaml | 3 ++ .../services/api-golang/deployment.yaml | 5 +++ .../api-golang/manifests/Deployment.yaml | 45 +++++++++++++++++++ .../api-golang/manifests/IngressRoute.yaml | 18 ++++++++ .../api-golang/manifests/Job.db-migrator.yaml | 22 +++++++++ .../api-golang/manifests/Middleware.yaml | 10 +++++ .../Secret.db-migrator-password.yaml | 14 ++++++ .../services/api-golang/manifests/Secret.yml | 9 ++++ .../api-golang/manifests/Service.yaml | 12 +++++ .../services/deployment.yaml | 2 + .../kluctl/.kluctl.yaml | 4 +- .../kluctl/Taskfile.yaml | 4 +- .../api-golang/config/production.yaml | 1 + .../services/api-golang/config/staging.yaml | 1 + .../api-golang/manifests/Deployment.yaml | 2 +- .../third-party/traefik/kustomization.yaml | 1 - .../kustomize/Taskfile.yaml | 4 +- .../patches/IngressRoute.replace-host.yaml | 2 +- 27 files changed, 230 insertions(+), 12 deletions(-) create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/.kluctl.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/README.md create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/Taskfile.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/config/production.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/config/staging.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/deployment.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/namespaces/Namespace.demo-app.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/config/production.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/config/staging.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/deployment.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Deployment.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/IngressRoute.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Job.db-migrator.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Middleware.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Secret.db-migrator-password.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Secret.yml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Service.yaml create mode 100644 12-deploying-to-multiple-environments/kluctl-single-service/services/deployment.yaml diff --git a/12-deploying-to-multiple-environments/helm/Taskfile.yaml b/12-deploying-to-multiple-environments/helm/Taskfile.yaml index ef4e022..97dfbe8 100644 --- a/12-deploying-to-multiple-environments/helm/Taskfile.yaml +++ b/12-deploying-to-multiple-environments/helm/Taskfile.yaml @@ -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: @@ -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" diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/.kluctl.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/.kluctl.yaml new file mode 100644 index 0000000..6ed063b --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/.kluctl.yaml @@ -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 }} diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/README.md b/12-deploying-to-multiple-environments/kluctl-single-service/README.md new file mode 100644 index 0000000..e69de29 diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/Taskfile.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/Taskfile.yaml new file mode 100644 index 0000000..d5e9f4d --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/Taskfile.yaml @@ -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" diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/config/production.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/config/production.yaml new file mode 100644 index 0000000..36cc3e6 --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/config/production.yaml @@ -0,0 +1,2 @@ +sharedVars: + hostName: "kubernetes-course.devopsdirective.com" diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/config/staging.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/config/staging.yaml new file mode 100644 index 0000000..66b6d18 --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/config/staging.yaml @@ -0,0 +1,2 @@ +sharedVars: + hostName: "kubernetes-course-staging.devopsdirective.com" diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/deployment.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/deployment.yaml new file mode 100644 index 0000000..3a6fb4a --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/deployment.yaml @@ -0,0 +1,10 @@ +vars: + - file: config/{{ args.environment }}.yaml + +deployments: + - path: namespaces + - barrier: true + - include: services + +commonLabels: + devopsdirective.com/course: "kubernetes-course" diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/namespaces/Namespace.demo-app.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/namespaces/Namespace.demo-app.yaml new file mode 100644 index 0000000..679127d --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/namespaces/Namespace.demo-app.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: demo-app diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/config/production.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/config/production.yaml new file mode 100644 index 0000000..d054fe5 --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/config/production.yaml @@ -0,0 +1,3 @@ +apiGolang: + version: 0.3.0 # PRODUCTION_IMAGE_TAG + replicas: 2 diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/config/staging.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/config/staging.yaml new file mode 100644 index 0000000..8de75e2 --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/config/staging.yaml @@ -0,0 +1,3 @@ +apiGolang: + version: 0.3.0-2-g29b7fb5 # STAGING_IMAGE_TAG + replicas: 1 diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/deployment.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/deployment.yaml new file mode 100644 index 0000000..c4363d3 --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/deployment.yaml @@ -0,0 +1,5 @@ +vars: + - file: config/{{ args.environment }}.yaml + +deployments: + - path: manifests diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Deployment.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Deployment.yaml new file mode 100644 index 0000000..e78b975 --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Deployment.yaml @@ -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 diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/IngressRoute.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/IngressRoute.yaml new file mode 100644 index 0000000..879fc4e --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/IngressRoute.yaml @@ -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 diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Job.db-migrator.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Job.db-migrator.yaml new file mode 100644 index 0000000..813e3df --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Job.db-migrator.yaml @@ -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 diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Middleware.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Middleware.yaml new file mode 100644 index 0000000..9dd4988 --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Middleware.yaml @@ -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 diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Secret.db-migrator-password.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Secret.db-migrator-password.yaml new file mode 100644 index 0000000..2633f54 --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Secret.db-migrator-password.yaml @@ -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:foobarbaz@cnpg-minimal-rw.postgres.svc.cluster.local:5432/postgres diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Secret.yml b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Secret.yml new file mode 100644 index 0000000..6d2fa69 --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Secret.yml @@ -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:foobarbaz@cnpg-minimal-rw.postgres.svc.cluster.local:5432/postgres diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Service.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Service.yaml new file mode 100644 index 0000000..fa4ae25 --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/services/api-golang/manifests/Service.yaml @@ -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 diff --git a/12-deploying-to-multiple-environments/kluctl-single-service/services/deployment.yaml b/12-deploying-to-multiple-environments/kluctl-single-service/services/deployment.yaml new file mode 100644 index 0000000..6afebb5 --- /dev/null +++ b/12-deploying-to-multiple-environments/kluctl-single-service/services/deployment.yaml @@ -0,0 +1,2 @@ +deployments: + - include: api-golang diff --git a/12-deploying-to-multiple-environments/kluctl/.kluctl.yaml b/12-deploying-to-multiple-environments/kluctl/.kluctl.yaml index 229b79c..dc44a74 100644 --- a/12-deploying-to-multiple-environments/kluctl/.kluctl.yaml +++ b/12-deploying-to-multiple-environments/kluctl/.kluctl.yaml @@ -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 diff --git a/12-deploying-to-multiple-environments/kluctl/Taskfile.yaml b/12-deploying-to-multiple-environments/kluctl/Taskfile.yaml index f54e77a..d5e9f4d 100644 --- a/12-deploying-to-multiple-environments/kluctl/Taskfile.yaml +++ b/12-deploying-to-multiple-environments/kluctl/Taskfile.yaml @@ -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: @@ -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: diff --git a/12-deploying-to-multiple-environments/kluctl/services/api-golang/config/production.yaml b/12-deploying-to-multiple-environments/kluctl/services/api-golang/config/production.yaml index de60f14..d054fe5 100644 --- a/12-deploying-to-multiple-environments/kluctl/services/api-golang/config/production.yaml +++ b/12-deploying-to-multiple-environments/kluctl/services/api-golang/config/production.yaml @@ -1,2 +1,3 @@ apiGolang: version: 0.3.0 # PRODUCTION_IMAGE_TAG + replicas: 2 diff --git a/12-deploying-to-multiple-environments/kluctl/services/api-golang/config/staging.yaml b/12-deploying-to-multiple-environments/kluctl/services/api-golang/config/staging.yaml index 49e0bee..8de75e2 100644 --- a/12-deploying-to-multiple-environments/kluctl/services/api-golang/config/staging.yaml +++ b/12-deploying-to-multiple-environments/kluctl/services/api-golang/config/staging.yaml @@ -1,2 +1,3 @@ apiGolang: version: 0.3.0-2-g29b7fb5 # STAGING_IMAGE_TAG + replicas: 1 diff --git a/12-deploying-to-multiple-environments/kluctl/services/api-golang/manifests/Deployment.yaml b/12-deploying-to-multiple-environments/kluctl/services/api-golang/manifests/Deployment.yaml index 8bb8494..e78b975 100644 --- a/12-deploying-to-multiple-environments/kluctl/services/api-golang/manifests/Deployment.yaml +++ b/12-deploying-to-multiple-environments/kluctl/services/api-golang/manifests/Deployment.yaml @@ -6,7 +6,7 @@ metadata: labels: app: api-golang spec: - replicas: 1 + replicas: {{apiGolang.replicas}} selector: matchLabels: app: api-golang diff --git a/12-deploying-to-multiple-environments/kluctl/third-party/traefik/kustomization.yaml b/12-deploying-to-multiple-environments/kluctl/third-party/traefik/kustomization.yaml index e3a0321..26031fe 100644 --- a/12-deploying-to-multiple-environments/kluctl/third-party/traefik/kustomization.yaml +++ b/12-deploying-to-multiple-environments/kluctl/third-party/traefik/kustomization.yaml @@ -1,4 +1,3 @@ resources: - Namespace.yaml - helm-rendered.yaml # generated at deploy time - diff --git a/12-deploying-to-multiple-environments/kustomize/Taskfile.yaml b/12-deploying-to-multiple-environments/kustomize/Taskfile.yaml index 3d74068..e656fff 100644 --- a/12-deploying-to-multiple-environments/kustomize/Taskfile.yaml +++ b/12-deploying-to-multiple-environments/kustomize/Taskfile.yaml @@ -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 diff --git a/12-deploying-to-multiple-environments/kustomize/staging/client-react/patches/IngressRoute.replace-host.yaml b/12-deploying-to-multiple-environments/kustomize/staging/client-react/patches/IngressRoute.replace-host.yaml index ff9930d..6709609 100644 --- a/12-deploying-to-multiple-environments/kustomize/staging/client-react/patches/IngressRoute.replace-host.yaml +++ b/12-deploying-to-multiple-environments/kustomize/staging/client-react/patches/IngressRoute.replace-host.yaml @@ -1,3 +1,3 @@ - op: replace path: /spec/routes/0/match - value: "Host(`kubernetes-course.devopsdirective.com`)" + value: "Host(`kubernetes-course-staging.devopsdirective.com`)"