···11+---
22+title: "My first deploys for a new Kubernetes cluster"
33+desc: "This is documentation for myself, but you may enjoy it too"
44+date: 2024-11-03
55+hero:
66+ ai: Photo by Xe Iaso, iPhone 13 Pro
77+ file: cloudfront
88+ prompt: "An airplane window looking out to cloudy skies."
99+---
1010+1111+I'm setting up some cloud Kubernetes clusters for a bit coming up on the blog. As a result, I need some documentation on what a "standard" cluster looks like. This is that documentation.
1212+1313+<Conv name="Mara" mood="hacker">
1414+ Every Kubernetes term is WrittenInGoPublicValueCase. If you aren't sure what
1515+ one of those terms means, google "site:kubernetes.io KubernetesTerm".
1616+</Conv>
1717+1818+I'm assuming that the cluster is named `mechonis`.
1919+2020+For the "core" of a cluster, I need these services set up:
2121+2222+- Secret syncing with the [1Password operator](https://developer.1password.com/docs/k8s/k8s-operator/)
2323+- Certificate management with [cert-manager](https://cert-manager.io/)
2424+- DNS management with [external-dns](https://kubernetes-sigs.github.io/external-dns/v0.15.0/)
2525+- HTTP ingress with [ingress-nginx](https://kubernetes.github.io/ingress-nginx/)
2626+- High-latency high-volume storage with [csi-s3](https://github.com/yandex-cloud/k8s-csi-s3) pointed to [Tigris](https://tigrisdata.com) (technically optional, but including it for consistency)
2727+- The [metrics-server](https://github.com/kubernetes-sigs/metrics-server) so [k9s](https://k9scli.io) can see how much free CPU and RAM the cluster has
2828+2929+These all complete different aspects of the three core features of any cloud deployment: compute, network, and storage. Most of my data will be hosted in the default StorageClass implementation provided by the platform (or in the case of baremetal clusters, something like [Longhorn](https://longhorn.io)), so the csi-s3 StorageClass is more of a "I need lots of data but am cheap" than anything.
3030+3131+Most of this will be managed with [helmfile](https://github.com/helmfile/helmfile), but 1Password can't be.
3232+3333+## 1Password
3434+3535+The most important thing at the core of my k8s setups is the [1Password operator](https://developer.1password.com/docs/k8s/k8s-operator/). This syncs 1password secrets to my Kubernetes clusters, so I don't need to define them in Secrets manually or risk putting the secret values into my OSS repos. This is done separately as I'm not able to use helmfile
3636+3737+After you have [the `op` command set up](https://developer.1password.com/docs/cli/get-started/), create a new server with access to the `Kubernetes` vault:
3838+3939+```
4040+op connect server create mechonis --vaults Kubernetes
4141+```
4242+4343+Then install the 1password connect Helm release with `operator.create` set to `true`:
4444+4545+```
4646+helm repo add \
4747+ 1password https://1password.github.io/connect-helm-charts/
4848+helm install \
4949+ connect \
5050+ 1password/connect \
5151+ --set-file connect.credentials=1password-credentials.json \
5252+ --set operator.create=true \
5353+ --set operator.token.value=$(op connect token create --server mechonis --vault Kubernetes)
5454+```
5555+5656+Now you can deploy OnePasswordItem resources as normal:
5757+5858+```yaml
5959+apiVersion: onepassword.com/v1
6060+kind: OnePasswordItem
6161+metadata:
6262+ name: falin
6363+spec:
6464+ itemPath: vaults/Kubernetes/items/Falin
6565+```
6666+6767+## cert-manager, ingress-nginx, metrics-server, and csi-s3
6868+6969+In the cluster folder, create a file called `helmfile.yaml`. Copy these contents:
7070+7171+<details>
7272+<summary>helmfile.yaml</summary>
7373+7474+```yaml
7575+repositories:
7676+ - name: jetstack
7777+ url: https://charts.jetstack.io
7878+ - name: csi-s3
7979+ url: cr.yandex/yc-marketplace/yandex-cloud/csi-s3
8080+ oci: true
8181+ - name: ingress-nginx
8282+ url: https://kubernetes.github.io/ingress-nginx
8383+ - name: metrics-server
8484+ url: https://kubernetes-sigs.github.io/metrics-server/
8585+8686+releases:
8787+ - name: cert-manager
8888+ kubeContext: mechonis
8989+ chart: jetstack/cert-manager
9090+ createNamespace: true
9191+ namespace: cert-manager
9292+ version: v1.16.1
9393+ set:
9494+ - name: installCRDs
9595+ value: "true"
9696+ - name: prometheus.enabled
9797+ value: "false"
9898+ - name: csi-s3
9999+ kubeContext: mechonis
100100+ chart: csi-s3/csi-s3
101101+ namespace: kube-system
102102+ set:
103103+ - name: "storageClass.name"
104104+ value: "tigris"
105105+ - name: "secret.accessKey"
106106+ value: ""
107107+ - name: "secret.secretKey"
108108+ value: ""
109109+ - name: "secret.endpoint"
110110+ value: "https://fly.storage.tigris.dev"
111111+ - name: "secret.region"
112112+ value: "auto"
113113+ - name: ingress-nginx
114114+ chart: ingress-nginx/ingress-nginx
115115+ kubeContext: mechonis
116116+ namespace: ingress-nginx
117117+ createNamespace: true
118118+ - name: metrics-server
119119+ kubeContext: mechonis
120120+ chart: metrics-server/metrics-server
121121+ namespace: kube-system
122122+```
123123+124124+</details>
125125+126126+Create a new admin access token in the [Tigris console](https://console.tigris.dev) and copy its access key ID and secret access key into `secret.accessKey` and `secret.secretKey` respectively.
127127+128128+Run `helmfile apply`:
129129+130130+```
131131+$ helmfile apply
132132+```
133133+134134+This will take a second to think, and then everything should be set up. The LoadBalancer Service may take a minute or ten to get a public IP depending on which cloud you are setting things up on, but once it's done you can proceed to setting up DNS.
135135+136136+## external-dns
137137+138138+The next kinda annoying part is getting [external-dns](https://kubernetes-sigs.github.io/external-dns/latest/) set up. It's something that looks like it should be packageable with something like Helm, but realistically it's such a generic tool that you're really better off making your own manifests and deploying it by hand. In my setup, I use these features of external-dns:
139139+140140+- The [AWS Route 53](https://aws.amazon.com/route53/) DNS backend
141141+- The [AWS DynamoDB](https://aws.amazon.com/dynamodb/) registry to remember what records should be set in Route 53
142142+143143+You will need two DynamoDB tables:
144144+145145+- `external-dns-mechonis-crd`: for records created with DNSEndpoint resources
146146+- `external-dns-mechonis-ingress`: for records created with Ingress resources
147147+148148+Create a terraform configuration for setting up these DynamoDB configuration values:
149149+150150+<details>
151151+<summary>main.tf</summary>
152152+153153+```hcl
154154+terraform {
155155+ backend "s3" {
156156+ bucket = "within-tf-state"
157157+ key = "k8s/mechonis/external-dns"
158158+ region = "us-east-1"
159159+ }
160160+}
161161+162162+resource "aws_dynamodb_table" "external_dns_crd" {
163163+ name = "external-dns-crd-mechonis"
164164+ billing_mode = "PROVISIONED"
165165+ read_capacity = 1
166166+ write_capacity = 1
167167+ table_class = "STANDARD"
168168+169169+ attribute {
170170+ name = "k"
171171+ type = "S"
172172+ }
173173+174174+ hash_key = "k"
175175+}
176176+177177+resource "aws_dynamodb_table" "external_dns_ingress" {
178178+ name = "external-dns-ingress-mechonis"
179179+ billing_mode = "PROVISIONED"
180180+ read_capacity = 1
181181+ write_capacity = 1
182182+ table_class = "STANDARD"
183183+184184+ attribute {
185185+ name = "k"
186186+ type = "S"
187187+ }
188188+189189+ hash_key = "k"
190190+}
191191+```
192192+193193+</details>
194194+195195+Create the tables with `terraform apply`:
196196+197197+```
198198+terraform init
199199+terraform apply --auto-approve # yolo!
200200+```
201201+202202+While that cooks, head over to `~/Code/Xe/x/kube/rhadamanthus/core/external-dns` and copy the contents to `~/Code/Xe/x/kube/mechonis/core/external-dns`. Then open `deployment-crd.yaml` and replace the DynamoDB table in the `crd` container's args:
203203+204204+```diff
205205+ args:
206206+ - --source=crd
207207+ - --crd-source-apiversion=externaldns.k8s.io/v1alpha1
208208+ - --crd-source-kind=DNSEndpoint
209209+ - --provider=aws
210210+ - --registry=dynamodb
211211+ - --dynamodb-region=ca-central-1
212212+- - --dynamodb-table=external-dns-crd-rhadamanthus
213213++ - --dynamodb-table=external-dns-crd-mechonis
214214+```
215215+216216+And in `deployment-ingress.yaml`:
217217+218218+```diff
219219+ args:
220220+ - --source=ingress
221221+- - --default-targets=rhadamanthus.xeserv.us
222222++ - --default-targets=mechonis.xeserv.us
223223+ - --provider=aws
224224+ - --registry=dynamodb
225225+ - --dynamodb-region=ca-central-1
226226+- - --dynamodb-table=external-dns-ingress-rhadamanthus
227227++ - --dynamodb-table=external-dns-ingress-mechonis
228228+```
229229+230230+Apply these configs with `kubectl apply`:
231231+232232+```
233233+kubectl apply -k .
234234+```
235235+236236+Then write a DNSEndpoint pointing to the created LoadBalancer. You may have to look up the IP addresses in the admin console of the cloud platform in question.
237237+238238+<details>
239239+<summary>load-balancer-dns.yaml</summary>
240240+241241+```yaml
242242+apiVersion: externaldns.k8s.io/v1alpha1
243243+kind: DNSEndpoint
244244+metadata:
245245+ name: load-balancer-dns
246246+spec:
247247+ endpoints:
248248+ - dnsName: mechonis.xeserv.us
249249+ recordTTL: 3600
250250+ recordType: A
251251+ targets:
252252+ - whatever.ipv4.goes.here
253253+ - dnsName: mechonis.xeserv.us
254254+ recordTTL: 3600
255255+ recordType: AAAA
256256+ targets:
257257+ - 2000:something:goes:here:lol
258258+```
259259+260260+</details>
261261+262262+Apply it with `kubectl apply`:
263263+264264+```
265265+kubectl apply -f load-balancer-dns.yaml
266266+```
267267+268268+This will point `mechonis.xeserv.us` to the LoadBalancer, which will point to ingress-nginx based on Ingress configurations, which will route to your Services and Deployments, using Certs from cert-manager.
269269+270270+## cert-manager ACME issuers
271271+272272+Copy the contents of `~/Code/Xe/x/kube/rhadamanthus/core/cert-manager` to `~/Code/Xe/x/kube/mechonis/core/cert-manager`. Apply them as-is, no changes are needed:
273273+274274+```
275275+kubectl apply -k .
276276+```
277277+278278+This will create `letsencrypt-prod` and `letsencrypt-staging` ClusterIssuers, which will allow the creation of Let's Encrypt certificates in their production and staging environments. 9 times out of 10, you won't need the staging environment, but when you are doing high-churn things involving debugging the certificate issuing setup, the staging environment is very useful because it has a [much higher rate limit](https://letsencrypt.org/docs/staging-environment/) than [the production environment](https://letsencrypt.org/docs/rate-limits/) does.
279279+280280+## Deploying a "hello, world" workload
281281+282282+<Conv name="Mara" mood="hacker">
283283+ Nearly every term for "unit of thing to do" is taken by different aspects of
284284+ Kubernetes and its ecosystem. The only one that isn't taken is "workload". A
285285+ workload is a unit of work deployed somewhere, in practice this boils down to
286286+ a Deployment, its Service, any PersistentVolumeClaims, Ingresses, or other
287287+ resources that it needs in order to run.
288288+</Conv>
289289+290290+Now you can put everything into test by making a simple "hello, world" workload. This will include:
291291+292292+- A ConfigMap to store HTML to show to the user
293293+- A Deployment to run nginx pointed at the contents of the ConfigMap
294294+- A Service to give an internal DNS name for that Deployment's Pods
295295+- An Ingress to route traffic to that Service from the public Internet
296296+297297+Make a folder called `hello-world` and put these files in it:
298298+299299+<details>
300300+<summary>configmap.yaml</summary>
301301+302302+```yaml
303303+apiVersion: v1
304304+kind: ConfigMap
305305+metadata:
306306+ name: hello-world
307307+data:
308308+ index.html: |
309309+ <html>
310310+ <head>
311311+ <title>Hello World!</title>
312312+ </head>
313313+ <body>Hello World!</body>
314314+ </html>
315315+```
316316+317317+</details>
318318+<details>
319319+<summary>deployment.yaml</summary>
320320+321321+```yaml
322322+apiVersion: apps/v1
323323+kind: Deployment
324324+metadata:
325325+ name: hello-world
326326+spec:
327327+ selector:
328328+ matchLabels:
329329+ app: hello-world
330330+ replicas: 1
331331+ template:
332332+ metadata:
333333+ labels:
334334+ app: hello-world
335335+ spec:
336336+ containers:
337337+ - name: web
338338+ image: nginx
339339+ ports:
340340+ - containerPort: 80
341341+ volumeMounts:
342342+ - name: html
343343+ mountPath: /usr/share/nginx/html
344344+ volumes:
345345+ - name: html
346346+ configMap:
347347+ name: hello-world
348348+```
349349+350350+</details>
351351+<details>
352352+<summary>service.yaml</summary>
353353+354354+```yaml
355355+apiVersion: v1
356356+kind: Service
357357+metadata:
358358+ name: hello-world
359359+spec:
360360+ ports:
361361+ - port: 80
362362+ protocol: TCP
363363+ selector:
364364+ app: hello-world
365365+```
366366+367367+</details>
368368+<details>
369369+<summary>ingress.yaml</summary>
370370+371371+```yaml
372372+apiVersion: networking.k8s.io/v1
373373+kind: Ingress
374374+metadata:
375375+ name: hello-world
376376+ annotations:
377377+ cert-manager.io/cluster-issuer: "letsencrypt-prod"
378378+ nginx.ingress.kubernetes.io/ssl-redirect: "true"
379379+spec:
380380+ ingressClassName: nginx
381381+ tls:
382382+ - hosts:
383383+ - hello.mechonis.xeserv.us
384384+ secretName: hello-mechonis-xeserv-us-tls
385385+ rules:
386386+ - host: hello.mechonis.xeserv.us
387387+ http:
388388+ paths:
389389+ - path: /
390390+ pathType: Prefix
391391+ backend:
392392+ service:
393393+ name: hello-world
394394+ port:
395395+ number: 80
396396+```
397397+398398+</details>
399399+<details>
400400+<summary>kustomization.yaml</summary>
401401+402402+```yaml
403403+resources:
404404+ - configmap.yaml
405405+ - deployment.yaml
406406+ - service.yaml
407407+ - ingress.yaml
408408+```
409409+410410+</details>
411411+412412+Then apply it with `kubectl apply`:
413413+414414+```
415415+kubectl apply -k .
416416+```
417417+418418+It will take a minute for it to work, but here are the things that will be done in order so you can validate them:
419419+420420+- The Ingress object has the `cert-manager.io/cluster-issuer: "letsencrypt-prod"` annotation, which triggers cert-manager to create a Cert for the Ingress
421421+- The Cert notices that there's no data in the Secret `hello-mechonis-xeserv-us-tls` in the default Namespace, so it creates an Order for a new certificate from the `letsencrypt-prod` ClusterIssuer (set up in the cert-manager apply step earlier)
422422+- The Order creates a new Challenge for that certificate, setting a DNS record in Route 53 and then waiting until it can validate that the Challenge matches what it expects
423423+- cert-manager asks Let's Encrypt to check the Challenge
424424+- The Order succeeds and the certificate data is written to the Secret `hello-mechonis-xeserv-us-tls` in the default Namespace
425425+- ingress-nginx is informed that the Secret has been updated and rehashes its configuration accordingly
426426+- HTTPS routing is set up for the `hello-world` service so every request to `hello.mechonis.xeserv.us` points to the Pods managed by the `hello-world` Deployment
427427+- external-dns checks for the presence of newly created Ingress objects it doesn't know about, and creates Route 53 entries for them
428428+429429+This results in the `hello-world` workload going from nothing to fully working in about 5 minutes tops. Usually this can be less depending on how lucky you get with the response time of the Route 53 API. If it doesn't work, run through resources in this order in [k9s](https://k9scli.io/):
430430+431431+- The `external-dns-ingress` Pod logs
432432+- The `cert-manager` Pod logs
433433+- Look for the Cert, is it marked as Ready?
434434+- Look for that Cert's Order, does it show any errors in its list of events?
435435+- Look for that Order's Challenge, does it show any errors in its list of events?
436436+437437+<Conv name="Mara" mood="hacker">
438438+ By the way: k9s is fantastic. You should have it installed if you deal with
439439+ Kubernetes. It should be baked into kubectl. It's a near perfect tool.
440440+</Conv>
441441+442442+## Conclusion
443443+444444+From here you can deploy anything else you want, as long as the workload configuration kinda looks like the `hello-world` configuration. Namely, you MUST have the following things set:
445445+446446+- Ingress objects MUST have the `cert-manager.io/cluster-issuer: "letsencrypt-prod"` annotation, if they don't, then no TLS certificate will be minted
447447+- Workloads MUST have the `nginx.ingress.kubernetes.io/ssl-redirect: "true"` to ensure that all plain HTTP traffic is upgraded to HTTPS
448448+- Sensitive data MUST be managed in 1Password via OnePasswordItem objects
449449+450450+Happy kubeing all!