The best way to control Claude Code, Codex and more from any computer or phone

main
mdx 172 lines 4.64 KB
Raw
1 ---
2 title: "Part 2: Namespaces & Configuration"
3 description: Create a Namespace with Kubernetes.Manifest, move the Deployment into it, and configure the app with environment variables and resource limits.
4 sidebar:
5 order: 2
6 ---
7
8 import Terminal from "../../../../components/Terminal.astro";
9 import PackageManagers from "../../../../components/PackageManagers.astro";
10
11 In [Part 1](/kubernetes/tutorial/part-1) you deployed podinfo into
12 the `default` namespace. Now you'll give the app a namespace of its
13 own, declared as a raw Kubernetes object. Then you'll configure the
14 app from your program.
15
16 ## Declare a Namespace
17
18 Anything that isn't a Deployment or a Job is a
19 [`Kubernetes.Manifest`](/kubernetes/objects/manifests). A Manifest is
20 a literal Kubernetes object, written exactly as you'd write it in
21 YAML. Add a Namespace:
22
23 ```diff lang="typescript"
24 Effect.gen(function* () {
25 + const namespace = yield* Kubernetes.Manifest("Namespace", {
26 + cluster,
27 + manifest: {
28 + apiVersion: "v1",
29 + kind: "Namespace",
30 + metadata: { name: "my-app" },
31 + },
32 + });
33
34 const web = yield* Kubernetes.Deployment("Web", {
35 ```
36
37 Alchemy applies the object with server-side apply and tracks it like
38 any other resource. Any kind works, such as `ConfigMap`, `Secret`,
39 `StatefulSet`, `Ingress`, or a custom resource from a CRD.
40
41 ## Move the Deployment into it
42
43 Point the Deployment at the new namespace:
44
45 ```diff lang="typescript"
46 const web = yield* Kubernetes.Deployment("Web", {
47 cluster,
48 name: "web",
49 + namespace: namespace.name,
50 image: "ghcr.io/stefanprodan/podinfo:6.15.0",
51 port: 9898,
52 replicas: 2,
53 serviceType: "ClusterIP",
54 });
55 ```
56
57 `namespace.name` is an **Output**. It's a reference to the Manifest's
58 attribute that resolves at deploy time. Passing it instead of the
59 string `"my-app"` makes the Deployment depend on the Namespace, so
60 Alchemy creates the Namespace first. Workload resources expect their
61 namespace to exist already.
62
63 ## Configure the app
64
65 podinfo shows the value of `PODINFO_UI_MESSAGE` in its response. Set
66 it with `env`:
67
68 ```diff lang="typescript"
69 const web = yield* Kubernetes.Deployment("Web", {
70 cluster,
71 name: "web",
72 namespace: namespace.name,
73 image: "ghcr.io/stefanprodan/podinfo:6.15.0",
74 port: 9898,
75 replicas: 2,
76 serviceType: "ClusterIP",
77 + env: {
78 + PODINFO_UI_MESSAGE: "hello from alchemy",
79 + },
80 });
81 ```
82
83 Every key in `env` becomes a container environment variable. Values
84 can also be Outputs from other resources, such as a database URL or a
85 bucket name. Alchemy resolves them before applying.
86
87 ## Set resource requests and limits
88
89 Tell the scheduler what each pod needs:
90
91 ```diff lang="typescript"
92 env: {
93 PODINFO_UI_MESSAGE: "hello from alchemy",
94 },
95 + resources: {
96 + requests: { cpu: "10m", memory: "32Mi" },
97 + limits: { memory: "64Mi" },
98 + },
99 });
100 ```
101
102 `resources` maps directly onto the container's Kubernetes
103 `resources` field and takes the usual Kubernetes quantities.
104
105 ## Deploy
106
107 <PackageManagers run="alchemy deploy" />
108
109 <Terminal content={`[u]Plan[/u]: [g]1 to create[/g], [y]1 to update[/y]
110
111 [g]+[/g] [b]Namespace[/b] [d](Kubernetes.Manifest)[/d]
112 [y]~[/y] [b]Web[/b] [d](Kubernetes.Deployment)[/d]
113
114 Proceed?
115 ◉ Yes ○ No
116 [g]✓[/g] [b]Namespace[/b] [d](Kubernetes.Manifest)[/d] [g]created[/g]
117 [g]✓[/g] [b]Web[/b] [d](Kubernetes.Deployment)[/d] [y]updated[/y]
118 {
119 [s2]context: "kind-alchemy",
120 [s2]namespace: "my-app",
121 [s2]service: "web",
122 }`} />
123
124 The Namespace is created first. The Deployment update applies its
125 objects into `my-app` and deletes the old ones from `default`.
126 Alchemy records every object it applies, and anything that drops out
127 of the desired set is pruned.
128
129 ## Verify it worked
130
131 The `default` namespace is empty again, and the app runs in `my-app`:
132
133 ```sh
134 kubectl --context kind-alchemy get deployments --all-namespaces | grep web
135 kubectl --context kind-alchemy -n my-app rollout status deployment/web
136 kubectl --context kind-alchemy -n my-app port-forward service/web 9898:9898
137 ```
138
139 ```sh
140 curl -s localhost:9898 | grep message
141 ```
142
143 ```json
144 "message": "hello from alchemy",
145 ```
146
147 ## Change it again
148
149 Edit the message and deploy:
150
151 ```diff lang="typescript"
152 env: {
153 - PODINFO_UI_MESSAGE: "hello from alchemy",
154 + PODINFO_UI_MESSAGE: "configured in TypeScript",
155 },
156 ```
157
158 The plan shows one update. Alchemy re-applies the Deployment and
159 Kubernetes rolls the pods over to the new environment, one replica
160 at a time.
161
162 ## Recap
163
164 You now have:
165
166 - A `my-app` Namespace declared as a raw manifest
167 - The Deployment moved into it, ordered after the Namespace by an
168 Output reference
169 - Environment variables and resource limits set from your program
170
171 In [Part 3](/kubernetes/tutorial/part-3), you'll run one-off and
172 scheduled work with `Kubernetes.Job`.