underpost 2.8.78 → 2.8.82

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/.github/workflows/ghpkg.yml +23 -21
  2. package/.github/workflows/npmpkg.yml +16 -11
  3. package/.github/workflows/pwa-microservices-template.page.yml +12 -3
  4. package/.github/workflows/pwa-microservices-template.test.yml +20 -17
  5. package/.vscode/extensions.json +1 -2
  6. package/.vscode/settings.json +3 -0
  7. package/Dockerfile +14 -33
  8. package/README.md +25 -24
  9. package/bin/db.js +1 -0
  10. package/bin/deploy.js +91 -796
  11. package/bin/vs.js +10 -3
  12. package/cli.md +340 -191
  13. package/conf.js +4 -0
  14. package/docker-compose.yml +1 -1
  15. package/manifests/deployment/dd-template-development/deployment.yaml +167 -0
  16. package/manifests/deployment/dd-template-development/proxy.yaml +46 -0
  17. package/manifests/lxd/lxd-admin-profile.yaml +17 -0
  18. package/manifests/lxd/lxd-preseed.yaml +30 -0
  19. package/manifests/lxd/underpost-setup.sh +163 -0
  20. package/manifests/maas/device-scan.sh +43 -0
  21. package/manifests/maas/lxd-preseed.yaml +32 -0
  22. package/manifests/maas/maas-setup.sh +120 -0
  23. package/manifests/maas/nat-iptables.sh +26 -0
  24. package/manifests/mariadb/statefulset.yaml +2 -1
  25. package/manifests/mariadb/storage-class.yaml +10 -0
  26. package/manifests/mongodb-4.4/service-deployment.yaml +2 -2
  27. package/manifests/valkey/service.yaml +3 -9
  28. package/manifests/valkey/statefulset.yaml +10 -12
  29. package/package.json +1 -1
  30. package/src/cli/baremetal.js +1248 -0
  31. package/src/cli/cloud-init.js +528 -0
  32. package/src/cli/cluster.js +459 -232
  33. package/src/cli/deploy.js +34 -10
  34. package/src/cli/env.js +2 -2
  35. package/src/cli/image.js +57 -9
  36. package/src/cli/index.js +256 -218
  37. package/src/cli/lxd.js +380 -4
  38. package/src/index.js +40 -14
  39. package/src/runtime/lampp/Dockerfile +41 -47
  40. package/src/server/conf.js +58 -0
  41. package/src/server/logger.js +3 -3
  42. package/src/server/runtime.js +1 -6
  43. package/src/server/ssl.js +1 -12
  44. package/src/server/valkey.js +3 -3
  45. package/supervisord-openssh-server.conf +0 -5
@@ -9,6 +9,38 @@ const logger = loggerFactory(import.meta);
9
9
 
10
10
  class UnderpostCluster {
11
11
  static API = {
12
+ /**
13
+ * @method init
14
+ * @description Initializes and configures the Kubernetes cluster based on provided options.
15
+ * This method handles host prerequisites, cluster initialization (Kind, Kubeadm, or K3s),
16
+ * and optional component deployments.
17
+ * @param {string} [podName] - Optional name of a pod for specific operations (e.g., listing).
18
+ * @param {object} [options] - Configuration options for cluster initialization.
19
+ * @param {boolean} [options.mongodb=false] - Deploy MongoDB.
20
+ * @param {boolean} [options.mongodb4=false] - Deploy MongoDB 4.4.
21
+ * @param {boolean} [options.mariadb=false] - Deploy MariaDB.
22
+ * @param {boolean} [options.mysql=false] - Deploy MySQL.
23
+ * @param {boolean} [options.postgresql=false] - Deploy PostgreSQL.
24
+ * @param {boolean} [options.valkey=false] - Deploy Valkey.
25
+ * @param {boolean} [options.full=false] - Deploy a full set of common components.
26
+ * @param {boolean} [options.info=false] - Display extensive Kubernetes cluster information.
27
+ * @param {boolean} [options.certManager=false] - Deploy Cert-Manager for certificate management.
28
+ * @param {boolean} [options.listPods=false] - List Kubernetes pods.
29
+ * @param {boolean} [options.reset=false] - Perform a comprehensive reset of Kubernetes and container environments.
30
+ * @param {boolean} [options.dev=false] - Run in development mode (adjusts paths).
31
+ * @param {string} [options.nsUse=''] - Set the current kubectl namespace.
32
+ * @param {boolean} [options.infoCapacity=false] - Display resource capacity information for the cluster.
33
+ * @param {boolean} [options.infoCapacityPod=false] - Display resource capacity information for pods.
34
+ * @param {boolean} [options.istio=false] - Deploy Istio service mesh.
35
+ * @param {boolean} [options.pullImage=false] - Pull necessary Docker images before deployment.
36
+ * @param {boolean} [options.dedicatedGpu=false] - Configure for dedicated GPU usage (e.g., NVIDIA GPU Operator).
37
+ * @param {boolean} [options.kubeadm=false] - Initialize the cluster using Kubeadm.
38
+ * @param {boolean} [options.k3s=false] - Initialize the cluster using K3s.
39
+ * @param {boolean} [options.initHost=false] - Perform initial host setup (install Docker, Podman, Kind, Kubeadm, Helm).
40
+ * @param {boolean} [options.config=false] - Apply general host configuration (SELinux, containerd, sysctl, firewalld).
41
+ * @param {boolean} [options.worker=false] - Configure as a worker node (for Kubeadm or K3s join).
42
+ * @param {boolean} [options.chown=false] - Set up kubectl configuration for the current user.
43
+ */
12
44
  async init(
13
45
  podName,
14
46
  options = {
@@ -31,38 +63,46 @@ class UnderpostCluster {
31
63
  pullImage: false,
32
64
  dedicatedGpu: false,
33
65
  kubeadm: false,
66
+ k3s: false,
67
+ initHost: false,
68
+ config: false,
69
+ worker: false,
70
+ chown: false,
34
71
  },
35
72
  ) {
36
- // sudo dnf update
37
- // 1) Install kind, kubeadm, docker, podman, helm
38
- // 2) Check kubectl, kubelet, containerd.io
39
- // 3) Install Nvidia drivers from Rocky Linux docs
40
- // 4) Install LXD with MAAS from Rocky Linux docs
41
- // 5) Install MAAS src from snap
73
+ // Handles initial host setup (installing docker, podman, kind, kubeadm, helm)
74
+ if (options.initHost === true) return UnderpostCluster.API.initHost();
75
+
76
+ // Applies general host configuration (SELinux, containerd, sysctl)
77
+ if (options.config === true) return UnderpostCluster.API.config();
78
+
79
+ // Sets up kubectl configuration for the current user
80
+ if (options.chown === true) return UnderpostCluster.API.chown();
81
+
42
82
  const npmRoot = getNpmRootPath();
43
83
  const underpostRoot = options?.dev === true ? '.' : `${npmRoot}/underpost`;
84
+
85
+ // Information gathering options
44
86
  if (options.infoCapacityPod === true) return logger.info('', UnderpostDeploy.API.resourcesFactory());
45
87
  if (options.infoCapacity === true)
46
- return logger.info('', UnderpostCluster.API.getResourcesCapacity(options.kubeadm));
47
- if (options.reset === true) return await UnderpostCluster.API.reset();
88
+ return logger.info('', UnderpostCluster.API.getResourcesCapacity(options.kubeadm || options.k3s)); // Adjust for k3s
48
89
  if (options.listPods === true) return console.table(UnderpostDeploy.API.get(podName ?? undefined));
49
-
50
90
  if (options.nsUse && typeof options.nsUse === 'string') {
51
91
  shellExec(`kubectl config set-context --current --namespace=${options.nsUse}`);
52
92
  return;
53
93
  }
54
94
  if (options.info === true) {
55
- shellExec(`kubectl config get-contexts`); // config env persisente for manage multiple clusters
95
+ shellExec(`kubectl config get-contexts`);
56
96
  shellExec(`kubectl config get-clusters`);
57
- shellExec(`kubectl get nodes -o wide`); // set of nodes of a cluster
97
+ shellExec(`kubectl get nodes -o wide`);
58
98
  shellExec(`kubectl config view | grep namespace`);
59
- shellExec(`kubectl get ns -o wide`); // A namespace can have pods of different nodes
60
- shellExec(`kubectl get pvc --all-namespaces -o wide`); // PersistentVolumeClaim -> request storage service
61
- shellExec(`kubectl get pv --all-namespaces -o wide`); // PersistentVolume -> real storage
99
+ shellExec(`kubectl get ns -o wide`);
100
+ shellExec(`kubectl get pvc --all-namespaces -o wide`);
101
+ shellExec(`kubectl get pv --all-namespaces -o wide`);
62
102
  shellExec(`kubectl get cronjob --all-namespaces -o wide`);
63
- shellExec(`kubectl get svc --all-namespaces -o wide`); // proxy dns gate way -> deployments, statefulsets, pods
64
- shellExec(`kubectl get statefulsets --all-namespaces -o wide`); // set pods with data/volume persistence
65
- shellExec(`kubectl get deployments --all-namespaces -o wide`); // set pods
103
+ shellExec(`kubectl get svc --all-namespaces -o wide`);
104
+ shellExec(`kubectl get statefulsets --all-namespaces -o wide`);
105
+ shellExec(`kubectl get deployments --all-namespaces -o wide`);
66
106
  shellExec(`kubectl get configmap --all-namespaces -o wide`);
67
107
  shellExec(`kubectl get pods --all-namespaces -o wide`);
68
108
  shellExec(
@@ -84,51 +124,103 @@ class UnderpostCluster {
84
124
  shellExec(`sudo kubectl api-resources`);
85
125
  return;
86
126
  }
87
- const alrreadyCluster =
88
- UnderpostDeploy.API.get('kube-apiserver-kind-control-plane')[0] ||
89
- UnderpostDeploy.API.get('calico-kube-controllers')[0];
90
-
91
- if (
92
- !alrreadyCluster &&
93
- ((!options.istio && !UnderpostDeploy.API.get('kube-apiserver-kind-control-plane')[0]) ||
94
- (options.istio === true && !UnderpostDeploy.API.get('calico-kube-controllers')[0]))
95
- ) {
96
- shellExec(`sudo setenforce 0`);
97
- shellExec(`sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config`);
98
- // sudo systemctl disable kubelet
99
- // shellExec(`sudo systemctl enable --now kubelet`);
100
- shellExec(`containerd config default > /etc/containerd/config.toml`);
101
- shellExec(`sed -i -e "s/SystemdCgroup = false/SystemdCgroup = true/g" /etc/containerd/config.toml`);
102
- // shellExec(`cp /etc/kubernetes/admin.conf ~/.kube/config`);
103
- // shellExec(`sudo systemctl restart kubelet`);
104
- shellExec(`sudo service docker restart`);
105
- shellExec(`sudo systemctl enable --now containerd.service`);
106
- shellExec(`sudo swapoff -a; sudo sed -i '/swap/d' /etc/fstab`);
107
- if (options.istio === true) {
108
- shellExec(`sysctl net.bridge.bridge-nf-call-iptables=1`);
127
+
128
+ // Reset Kubernetes cluster components (Kind/Kubeadm/K3s) and container runtimes
129
+ if (options.reset === true) return await UnderpostCluster.API.reset();
130
+
131
+ // Check if a cluster (Kind, Kubeadm, or K3s) is already initialized
132
+ const alreadyKubeadmCluster = UnderpostDeploy.API.get('calico-kube-controllers')[0];
133
+ const alreadyKindCluster = UnderpostDeploy.API.get('kube-apiserver-kind-control-plane')[0];
134
+ // K3s pods often contain 'svclb-traefik' in the kube-system namespace
135
+ const alreadyK3sCluster = UnderpostDeploy.API.get('svclb-traefik')[0];
136
+
137
+ // --- Kubeadm/Kind/K3s Cluster Initialization ---
138
+ // This block handles the initial setup of the Kubernetes cluster (control plane or worker).
139
+ // It prevents re-initialization if a cluster is already detected.
140
+ if (!options.worker && !alreadyKubeadmCluster && !alreadyKindCluster && !alreadyK3sCluster) {
141
+ if (options.k3s === true) {
142
+ logger.info('Initializing K3s control plane...');
143
+ // Install K3s
144
+ console.log('Installing K3s...');
145
+ shellExec(`curl -sfL https://get.k3s.io | sh -`);
146
+ console.log('K3s installation completed.');
147
+
148
+ // Move k3s binary to /bin/k3s and make it executable
149
+ shellExec(`sudo mv /usr/local/bin/k3s /bin/k3s`);
150
+ shellExec(`sudo chmod +x /bin/k3s`);
151
+ console.log('K3s binary moved to /bin/k3s and made executable.');
152
+
153
+ // Configure kubectl for the current user for K3s *before* checking readiness
154
+ // This ensures kubectl can find the K3s kubeconfig immediately after K3s installation.
155
+ UnderpostCluster.API.chown('k3s');
156
+
157
+ // Wait for K3s to be ready
158
+ logger.info('Waiting for K3s to be ready...');
159
+ let k3sReady = false;
160
+ let retries = 0;
161
+ const maxRetries = 20; // Increased retries for K3s startup
162
+ const delayMs = 5000; // 5 seconds
163
+
164
+ while (!k3sReady && retries < maxRetries) {
165
+ try {
166
+ // Explicitly use KUBECONFIG for kubectl commands to ensure it points to K3s config
167
+ const nodes = shellExec(`KUBECONFIG=/etc/rancher/k3s/k3s.yaml kubectl get nodes -o json`, {
168
+ stdout: true,
169
+ silent: true,
170
+ });
171
+ const parsedNodes = JSON.parse(nodes);
172
+ if (
173
+ parsedNodes.items.some((node) =>
174
+ node.status.conditions.some((cond) => cond.type === 'Ready' && cond.status === 'True'),
175
+ )
176
+ ) {
177
+ k3sReady = true;
178
+ logger.info('K3s cluster is ready.');
179
+ } else {
180
+ logger.info(`K3s not yet ready. Retrying in ${delayMs / 1000} seconds...`);
181
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
182
+ }
183
+ } catch (error) {
184
+ logger.info(`Error checking K3s status: ${error.message}. Retrying in ${delayMs / 1000} seconds...`);
185
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
186
+ }
187
+ retries++;
188
+ }
189
+
190
+ if (!k3sReady) {
191
+ logger.error('K3s cluster did not become ready in time. Please check the K3s logs.');
192
+ return;
193
+ }
194
+
195
+ // K3s includes local-path-provisioner by default, so no need to install explicitly.
196
+ logger.info('K3s comes with local-path-provisioner by default. Skipping explicit installation.');
197
+ } else if (options.kubeadm === true) {
198
+ logger.info('Initializing Kubeadm control plane...');
199
+ // Initialize kubeadm control plane
109
200
  shellExec(
110
201
  `sudo kubeadm init --pod-network-cidr=192.168.0.0/16 --control-plane-endpoint="${os.hostname()}:6443"`,
111
202
  );
112
- shellExec(`sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config`);
113
- shellExec(`sudo chown $(id -u):$(id -g) $HOME/.kube/config**`);
114
- // https://docs.tigera.io/calico/latest/getting-started/kubernetes/quickstart
203
+ // Configure kubectl for the current user
204
+ UnderpostCluster.API.chown('kubeadm'); // Pass 'kubeadm' to chown
205
+
206
+ // Install Calico CNI
207
+ logger.info('Installing Calico CNI...');
115
208
  shellExec(
116
209
  `sudo kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.29.3/manifests/tigera-operator.yaml`,
117
210
  );
118
- // shellExec(
119
- // `wget https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/custom-resources.yaml`,
120
- // );
121
211
  shellExec(`sudo kubectl apply -f ${underpostRoot}/manifests/kubeadm-calico-config.yaml`);
122
- shellExec(`sudo systemctl restart containerd`);
212
+ // Untaint control plane node to allow scheduling pods
123
213
  const nodeName = os.hostname();
124
214
  shellExec(`kubectl taint nodes ${nodeName} node-role.kubernetes.io/control-plane:NoSchedule-`);
215
+ // Install local-path-provisioner for dynamic PVCs (optional but recommended)
216
+ logger.info('Installing local-path-provisioner...');
125
217
  shellExec(
126
218
  `kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/deploy/local-path-storage.yaml`,
127
219
  );
128
220
  } else {
129
- shellExec(`sudo systemctl restart containerd`);
221
+ // Kind cluster initialization (if not using kubeadm or k3s)
222
+ logger.info('Initializing Kind cluster...');
130
223
  if (options.full === true || options.dedicatedGpu === true) {
131
- // https://kubernetes.io/docs/tasks/manage-gpus/scheduling-gpus/
132
224
  shellExec(`cd ${underpostRoot}/manifests && kind create cluster --config kind-config-cuda.yaml`);
133
225
  } else {
134
226
  shellExec(
@@ -137,11 +229,20 @@ class UnderpostCluster {
137
229
  }.yaml`,
138
230
  );
139
231
  }
140
- shellExec(`sudo chown $(id -u):$(id -g) $HOME/.kube/config**`);
232
+ UnderpostCluster.API.chown('kind'); // Pass 'kind' to chown
141
233
  }
142
- } else logger.warn('Cluster already initialized');
234
+ } else if (options.worker === true) {
235
+ // Worker node specific configuration (kubeadm join command needs to be executed separately)
236
+ logger.info('Worker node configuration applied. Awaiting join command...');
237
+ // No direct cluster initialization here for workers. The `kubeadm join` or `k3s agent` command
238
+ // needs to be run on the worker after the control plane is up and a token is created.
239
+ // This part of the script is for general worker setup, not the join itself.
240
+ } else {
241
+ logger.warn('Cluster already initialized or worker flag not set for worker node.');
242
+ }
143
243
 
144
- // shellExec(`sudo kubectl apply -f ${underpostRoot}/manifests/kubelet-config.yaml`);
244
+ // --- Optional Component Deployments (Databases, Ingress, Cert-Manager) ---
245
+ // These deployments happen after the base cluster is up.
145
246
 
146
247
  if (options.full === true || options.dedicatedGpu === true) {
147
248
  shellExec(`node ${underpostRoot}/bin/deploy nvidia-gpu-operator`);
@@ -154,26 +255,40 @@ class UnderpostCluster {
154
255
  if (options.pullImage === true) {
155
256
  shellExec(`docker pull valkey/valkey:latest`);
156
257
  shellExec(`sudo podman pull valkey/valkey:latest`);
157
- if (!options.kubeadm)
158
- shellExec(
159
- `sudo ${
160
- options.kubeadm === true ? `ctr -n k8s.io images import` : `kind load docker-image`
161
- } valkey/valkey:latest`,
162
- );
258
+ if (!options.kubeadm && !options.k3s)
259
+ // Only load if not kubeadm/k3s (Kind needs it)
260
+ shellExec(`sudo kind load docker-image valkey/valkey:latest`);
261
+ else if (options.kubeadm || options.k3s)
262
+ // For kubeadm/k3s, ensure it's available for containerd
263
+ shellExec(`sudo crictl pull valkey/valkey:latest`);
163
264
  }
164
- shellExec(`kubectl delete statefulset service-valkey`);
265
+ shellExec(`kubectl delete statefulset valkey-service --ignore-not-found`);
165
266
  shellExec(`kubectl apply -k ${underpostRoot}/manifests/valkey`);
166
267
  }
167
268
  if (options.full === true || options.mariadb === true) {
168
269
  shellExec(
169
- `sudo kubectl create secret generic mariadb-secret --from-file=username=/home/dd/engine/engine-private/mariadb-username --from-file=password=/home/dd/engine/engine-private/mariadb-password`,
270
+ `sudo kubectl create secret generic mariadb-secret --from-file=username=/home/dd/engine/engine-private/mariadb-username --from-file=password=/home/dd/engine/engine-private/mariadb-password --dry-run=client -o yaml | kubectl apply -f -`,
170
271
  );
171
- shellExec(`kubectl delete statefulset mariadb-statefulset`);
272
+ shellExec(`kubectl delete statefulset mariadb-statefulset --ignore-not-found`);
273
+
274
+ if (options.pullImage === true) {
275
+ shellExec(`docker pull mariadb:latest`);
276
+ shellExec(`sudo podman pull mariadb:latest`);
277
+ if (!options.kubeadm && !options.k3s)
278
+ // Only load if not kubeadm/k3s (Kind needs it)
279
+ shellExec(`sudo kind load docker-image mariadb:latest`);
280
+ else if (options.kubeadm || options.k3s)
281
+ // For kubeadm/k3s, ensure it's available for containerd
282
+ shellExec(`sudo crictl pull mariadb:latest`);
283
+ }
284
+ if (options.kubeadm === true)
285
+ // This storage class is specific to kubeadm setup
286
+ shellExec(`kubectl apply -f ${underpostRoot}/manifests/mariadb/storage-class.yaml`);
172
287
  shellExec(`kubectl apply -k ${underpostRoot}/manifests/mariadb`);
173
288
  }
174
289
  if (options.full === true || options.mysql === true) {
175
290
  shellExec(
176
- `sudo kubectl create secret generic mysql-secret --from-file=username=/home/dd/engine/engine-private/mysql-username --from-file=password=/home/dd/engine/engine-private/mysql-password`,
291
+ `sudo kubectl create secret generic mysql-secret --from-file=username=/home/dd/engine/engine-private/mysql-username --from-file=password=/home/dd/engine/engine-private/mysql-password --dry-run=client -o yaml | kubectl apply -f -`,
177
292
  );
178
293
  shellExec(`sudo mkdir -p /mnt/data`);
179
294
  shellExec(`sudo chmod 777 /mnt/data`);
@@ -183,27 +298,27 @@ class UnderpostCluster {
183
298
  if (options.full === true || options.postgresql === true) {
184
299
  if (options.pullImage === true) {
185
300
  shellExec(`docker pull postgres:latest`);
186
- if (!options.kubeadm)
187
- shellExec(
188
- `sudo ${
189
- options.kubeadm === true ? `ctr -n k8s.io images import` : `kind load docker-image`
190
- } docker-image postgres:latest`,
191
- );
301
+ if (!options.kubeadm && !options.k3s)
302
+ // Only load if not kubeadm/k3s (Kind needs it)
303
+ shellExec(`sudo kind load docker-image postgres:latest`);
304
+ else if (options.kubeadm || options.k3s)
305
+ // For kubeadm/k3s, ensure it's available for containerd
306
+ shellExec(`sudo crictl pull postgres:latest`);
192
307
  }
193
308
  shellExec(
194
- `sudo kubectl create secret generic postgres-secret --from-file=password=/home/dd/engine/engine-private/postgresql-password`,
309
+ `sudo kubectl create secret generic postgres-secret --from-file=password=/home/dd/engine/engine-private/postgresql-password --dry-run=client -o yaml | kubectl apply -f -`,
195
310
  );
196
311
  shellExec(`kubectl apply -k ${underpostRoot}/manifests/postgresql`);
197
312
  }
198
313
  if (options.mongodb4 === true) {
199
314
  if (options.pullImage === true) {
200
315
  shellExec(`docker pull mongo:4.4`);
201
- if (!options.kubeadm)
202
- shellExec(
203
- `sudo ${
204
- options.kubeadm === true ? `ctr -n k8s.io images import` : `kind load docker-image`
205
- } docker-image mongo:4.4`,
206
- );
316
+ if (!options.kubeadm && !options.k3s)
317
+ // Only load if not kubeadm/k3s (Kind needs it)
318
+ shellExec(`sudo kind load docker-image mongo:4.4`);
319
+ else if (options.kubeadm || options.k3s)
320
+ // For kubeadm/k3s, ensure it's available for containerd
321
+ shellExec(`sudo crictl pull mongo:4.4`);
207
322
  }
208
323
  shellExec(`kubectl apply -k ${underpostRoot}/manifests/mongodb-4.4`);
209
324
 
@@ -214,7 +329,7 @@ class UnderpostCluster {
214
329
  if (successInstance) {
215
330
  const mongoConfig = {
216
331
  _id: 'rs0',
217
- members: [{ _id: 0, host: '127.0.0.1:27017' }],
332
+ members: [{ _id: 0, host: 'mongodb-service:27017' }],
218
333
  };
219
334
 
220
335
  const [pod] = UnderpostDeploy.API.get(deploymentName);
@@ -224,20 +339,25 @@ class UnderpostCluster {
224
339
  --eval 'rs.initiate(${JSON.stringify(mongoConfig)})'`,
225
340
  );
226
341
  }
227
-
228
- // await UnderpostTest.API.statusMonitor('mongodb-1');
229
342
  } else if (options.full === true || options.mongodb === true) {
230
343
  if (options.pullImage === true) {
231
344
  shellExec(`docker pull mongo:latest`);
345
+ if (!options.kubeadm && !options.k3s)
346
+ // Only load if not kubeadm/k3s (Kind needs it)
347
+ shellExec(`sudo kind load docker-image mongo:latest`);
348
+ else if (options.kubeadm || options.k3s)
349
+ // For kubeadm/k3s, ensure it's available for containerd
350
+ shellExec(`sudo crictl pull mongo:latest`);
232
351
  }
233
352
  shellExec(
234
- `sudo kubectl create secret generic mongodb-keyfile --from-file=/home/dd/engine/engine-private/mongodb-keyfile`,
353
+ `sudo kubectl create secret generic mongodb-keyfile --from-file=/home/dd/engine/engine-private/mongodb-keyfile --dry-run=client -o yaml | kubectl apply -f -`,
235
354
  );
236
355
  shellExec(
237
- `sudo kubectl create secret generic mongodb-secret --from-file=username=/home/dd/engine/engine-private/mongodb-username --from-file=password=/home/dd/engine/engine-private/mongodb-password`,
356
+ `sudo kubectl create secret generic mongodb-secret --from-file=username=/home/dd/engine/engine-private/mongodb-username --from-file=password=/home/dd/engine/engine-private/mongodb-password --dry-run=client -o yaml | kubectl apply -f -`,
238
357
  );
239
- shellExec(`kubectl delete statefulset mongodb`);
358
+ shellExec(`kubectl delete statefulset mongodb --ignore-not-found`);
240
359
  if (options.kubeadm === true)
360
+ // This storage class is specific to kubeadm setup
241
361
  shellExec(`kubectl apply -f ${underpostRoot}/manifests/mongodb/storage-class.yaml`);
242
362
  shellExec(`kubectl apply -k ${underpostRoot}/manifests/mongodb`);
243
363
 
@@ -264,8 +384,11 @@ class UnderpostCluster {
264
384
  if (options.full === true || options.contour === true) {
265
385
  shellExec(`kubectl apply -f https://projectcontour.io/quickstart/contour.yaml`);
266
386
  if (options.kubeadm === true) {
387
+ // Envoy service might need NodePort for kubeadm
267
388
  shellExec(`sudo kubectl apply -f ${underpostRoot}/manifests/envoy-service-nodeport.yaml`);
268
389
  }
390
+ // K3s has a built-in LoadBalancer (Klipper-lb) that can expose services,
391
+ // so a specific NodePort service might not be needed or can be configured differently.
269
392
  }
270
393
 
271
394
  if (options.full === true || options.certManager === true) {
@@ -281,166 +404,204 @@ class UnderpostCluster {
281
404
  }
282
405
 
283
406
  const letsEncName = 'letsencrypt-prod';
284
- shellExec(`sudo kubectl delete ClusterIssuer ${letsEncName}`);
407
+ shellExec(`sudo kubectl delete ClusterIssuer ${letsEncName} --ignore-not-found`);
285
408
  shellExec(`sudo kubectl apply -f ${underpostRoot}/manifests/${letsEncName}.yaml`);
286
409
  }
287
410
  },
288
- // This function performs a comprehensive reset of Kubernetes and container environments
289
- // on the host machine. Its primary goal is to clean up cluster components, temporary files,
290
- // and container data, ensuring a clean state for re-initialization or fresh deployments,
291
- // while also preventing the loss of the host machine's internet connectivity.
292
-
293
- reset() {
294
- // Step 1: Delete all existing Kind (Kubernetes in Docker) clusters.
295
- // 'kind get clusters' lists all Kind clusters.
296
- // 'xargs -t -n1 kind delete cluster --name' then iterates through each cluster name
297
- // and executes 'kind delete cluster --name <cluster_name>' to remove them.
298
- shellExec(`kind get clusters | xargs -t -n1 kind delete cluster --name`);
299
-
300
- // Step 2: Reset the Kubernetes control-plane components installed by kubeadm.
301
- // 'kubeadm reset -f' performs a forceful reset, removing installed Kubernetes components,
302
- // configuration files, and associated network rules (like iptables entries created by kubeadm).
303
- // The '-f' flag bypasses confirmation prompts.
304
- shellExec(`sudo kubeadm reset -f`);
305
-
306
- // Step 3: Remove specific CNI (Container Network Interface) configuration files.
307
- // This command targets and removes the configuration file for Flannel,
308
- // a common CNI plugin, which might be left behind after a reset.
309
- shellExec('sudo rm -f /etc/cni/net.d/10-flannel.conflist');
310
-
311
- // Note: The aggressive 'sudo iptables -F ...' command was intentionally removed from previous versions.
312
- // This command would flush all iptables rules, including those crucial for the host's general
313
- // internet connectivity, leading to network loss. 'kubeadm reset' and container runtime pruning
314
- // adequately handle Kubernetes and container-specific iptables rules without affecting the host's
315
- // default network configuration.
316
-
317
- // Step 4: Remove the kubectl configuration file from the current user's home directory.
318
- // This ensures that after a reset, there's no lingering configuration pointing to the old cluster,
319
- // providing a clean slate for connecting to a new or re-initialized cluster.
320
- shellExec('sudo rm -f $HOME/.kube/config');
321
-
322
- // Step 5: Clear trash files from the root user's trash directory.
323
- // This is a general cleanup step to remove temporary or deleted files.
324
- shellExec('sudo rm -rf /root/.local/share/Trash/files/*');
325
-
326
- // Step 6: Prune all unused Docker data.
327
- // 'docker system prune -a -f' removes:
328
- // - All stopped containers
329
- // - All unused networks
330
- // - All dangling images
331
- // - All build cache
332
- // - All unused volumes
333
- // This aggressively frees up disk space and removes temporary Docker artifacts.
334
- shellExec('sudo docker system prune -a -f');
335
-
336
- // Step 7: Stop the Docker daemon service.
337
- // This step is often necessary to ensure that Docker's files and directories
338
- // can be safely manipulated or moved in subsequent steps without conflicts.
339
- shellExec('sudo service docker stop');
340
-
341
- // Step 8: Aggressively remove container storage data for containerd and Docker.
342
- // These commands target the default storage locations for containerd and Docker,
343
- // as well as any custom paths that might have been used (`/home/containers/storage`, `/home/docker`).
344
- // This ensures a complete wipe of all container images, layers, and volumes.
345
- shellExec(`sudo rm -rf /var/lib/containers/storage/*`);
346
- shellExec(`sudo rm -rf /var/lib/docker/volumes/*`);
347
- shellExec(`sudo rm -rf /var/lib/docker~/*`); // Cleans up a potential backup directory for Docker data
348
- shellExec(`sudo rm -rf /home/containers/storage/*`); // Cleans up custom containerd/Podman storage
349
- shellExec(`sudo rm -rf /home/docker/*`); // Cleans up custom Docker storage
350
-
351
- // Step 9: Re-configure Docker's default storage location (if desired).
352
- // These commands effectively move Docker's data directory from its default `/var/lib/docker`
353
- // to a new location (`/home/docker`) and create a symbolic link.
354
- // This is a specific customization to relocate Docker's storage.
355
- shellExec('sudo mv /var/lib/docker /var/lib/docker~'); // Moves existing /var/lib/docker to /var/lib/docker~ (backup)
356
- shellExec('sudo mkdir /home/docker'); // Creates the new desired directory for Docker data
357
- shellExec('sudo chmod 0711 /home/docker'); // Sets appropriate permissions for the new directory
358
- shellExec('sudo ln -s /home/docker /var/lib/docker'); // Creates a symlink from original path to new path
359
-
360
- // Step 10: Prune all unused Podman data.
361
- // Similar to Docker pruning, these commands remove:
362
- // - All stopped containers
363
- // - All unused networks
364
- // - All unused images
365
- // - All unused volumes ('--volumes')
366
- // - The '--force' flag bypasses confirmation.
367
- // '--external' prunes external content not managed by Podman's default storage backend.
368
- shellExec(`sudo podman system prune -a -f`);
369
- shellExec(`sudo podman system prune --all --volumes --force`);
370
- shellExec(`sudo podman system prune --external --force`);
371
- shellExec(`sudo podman system prune --all --volumes --force`); // Redundant but harmless repetition
372
-
373
- // Step 11: Create and set permissions for Podman's custom storage directory.
374
- // This ensures the custom path `/home/containers/storage` exists and has correct permissions
375
- // before Podman attempts to use it.
376
- shellExec(`sudo mkdir -p /home/containers/storage`);
377
- shellExec('sudo chmod 0711 /home/containers/storage');
378
-
379
- // Step 12: Update Podman's storage configuration file.
380
- // This command uses 'sed' to modify `/etc/containers/storage.conf`,
381
- // changing the default storage path from `/var/lib/containers/storage`
382
- // to the customized `/home/containers/storage`.
383
- shellExec(
384
- `sudo sed -i -e "s@/var/lib/containers/storage@/home/containers/storage@g" /etc/containers/storage.conf`,
385
- );
386
411
 
387
- // Step 13: Reset Podman system settings.
388
- // This command resets Podman's system-wide configuration to its default state.
389
- shellExec(`sudo podman system reset -f`);
390
-
391
- // Note: The 'sysctl net.bridge.bridge-nf-call-iptables=0' and related commands
392
- // were previously removed. These sysctl settings (bridge-nf-call-iptables,
393
- // bridge-nf-call-arptables, bridge-nf-call-ip6tables) are crucial for allowing
394
- // network traffic through Linux bridges to be processed by iptables.
395
- // Kubernetes and CNI plugins generally require them to be enabled (set to '1').
396
- // Re-initializing Kubernetes will typically set these as needed, and leaving them
397
- // at their system default (or '1' if already configured) is safer for host
398
- // connectivity during a reset operation.
399
-
400
- // https://github.com/kubernetes-sigs/kind/issues/2886
401
- // shellExec(`sysctl net.bridge.bridge-nf-call-iptables=0`);
402
- // shellExec(`sysctl net.bridge.bridge-nf-call-arptables=0`);
403
- // shellExec(`sysctl net.bridge.bridge-nf-call-ip6tables=0`);
404
-
405
- // Step 14: Remove the 'kind' Docker network.
406
- // This cleans up any network bridges or configurations specifically created by Kind.
407
- // shellExec(`docker network rm kind`);
408
-
409
- // Reset kubelet
410
- shellExec(`sudo systemctl stop kubelet`);
411
- shellExec(`sudo rm -rf /etc/kubernetes/*`);
412
- shellExec(`sudo rm -rf /var/lib/kubelet/*`);
413
- shellExec(`sudo rm -rf /etc/cni/net.d/*`);
412
+ /**
413
+ * @method config
414
+ * @description Configures host-level settings required for Kubernetes.
415
+ * This method ensures proper SELinux, Docker, Containerd, and Sysctl settings
416
+ * are applied for a healthy Kubernetes environment. It explicitly avoids
417
+ * iptables flushing commands to prevent conflicts with Kubernetes' own network management.
418
+ */
419
+ config() {
420
+ console.log('Applying host configuration: SELinux, Docker, Containerd, and Sysctl settings.');
421
+ // Disable SELinux (permissive mode)
422
+ shellExec(`sudo setenforce 0`);
423
+ shellExec(`sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config`);
424
+
425
+ // Enable and start Docker and Kubelet services
426
+ shellExec(`sudo systemctl enable --now docker || true`); // Docker might not be needed for K3s
427
+ shellExec(`sudo systemctl enable --now kubelet || true`); // Kubelet might not be needed for K3s (K3s uses its own agent)
428
+
429
+ // Configure containerd for SystemdCgroup
430
+ // This is crucial for kubelet/k3s to interact correctly with containerd
431
+ shellExec(`containerd config default | sudo tee /etc/containerd/config.toml > /dev/null`);
432
+ shellExec(`sudo sed -i -e "s/SystemdCgroup = false/SystemdCgroup = true/g" /etc/containerd/config.toml`);
433
+ shellExec(`sudo service docker restart || true`); // Restart docker after containerd config changes
434
+ shellExec(`sudo systemctl enable --now containerd.service`);
435
+ shellExec(`sudo systemctl restart containerd`); // Restart containerd to apply changes
436
+
437
+ // Disable swap (required by Kubernetes)
438
+ shellExec(`sudo swapoff -a; sudo sed -i '/swap/d' /etc/fstab`);
439
+
440
+ // Reload systemd daemon to pick up new unit files/changes
414
441
  shellExec(`sudo systemctl daemon-reload`);
415
- shellExec(`sudo systemctl start kubelet`);
442
+
443
+ // Enable bridge-nf-call-iptables for Kubernetes networking
444
+ // This ensures traffic through Linux bridges is processed by iptables (crucial for CNI)
445
+ for (const iptableConfPath of [
446
+ `/etc/sysctl.d/k8s.conf`,
447
+ `/etc/sysctl.d/99-k8s-ipforward.conf`,
448
+ `/etc/sysctl.d/99-k8s.conf`,
449
+ ])
450
+ shellExec(`echo 'net.bridge.bridge-nf-call-iptables = 1
451
+ net.bridge.bridge-nf-call-ip6tables = 1
452
+ net.bridge.bridge-nf-call-arptables = 1
453
+ net.ipv4.ip_forward = 1' | sudo tee ${iptableConfPath}`);
454
+ shellExec(`sudo sysctl --system`); // Apply sysctl changes immediately
455
+
456
+ // Disable firewalld (common cause of network issues in Kubernetes)
457
+ shellExec(`sudo systemctl stop firewalld || true`); // Stop if running
458
+ shellExec(`sudo systemctl disable firewalld || true`); // Disable from starting on boot
459
+ },
460
+
461
+ /**
462
+ * @method chown
463
+ * @description Sets up kubectl configuration for the current user based on the cluster type.
464
+ * @param {string} clusterType - The type of Kubernetes cluster ('kubeadm', 'k3s', or 'kind').
465
+ */
466
+ chown(clusterType) {
467
+ console.log(`Setting up kubectl configuration for ${clusterType} cluster...`);
468
+ shellExec(`mkdir -p ~/.kube`);
469
+
470
+ let kubeconfigPath;
471
+ if (clusterType === 'k3s') {
472
+ kubeconfigPath = '/etc/rancher/k3s/k3s.yaml';
473
+ } else if (clusterType === 'kubeadm') {
474
+ kubeconfigPath = '/etc/kubernetes/admin.conf';
475
+ } else {
476
+ // Default to kind if not specified or unknown
477
+ kubeconfigPath = ''; // Kind's kubeconfig is usually managed by kind itself, or merged
478
+ }
479
+
480
+ if (kubeconfigPath) {
481
+ shellExec(`sudo -E cp -i ${kubeconfigPath} ~/.kube/config`);
482
+ shellExec(`sudo -E chown $(id -u):$(id -g) ~/.kube/config`);
483
+ } else if (clusterType === 'kind') {
484
+ // For Kind, the kubeconfig is usually merged automatically or can be explicitly exported
485
+ // This command ensures it's merged into the default kubeconfig
486
+ shellExec(`kind get kubeconfig > ~/.kube/config || true`);
487
+ shellExec(`sudo -E chown $(id -u):$(id -g) ~/.kube/config`);
488
+ } else {
489
+ logger.warn('No specific kubeconfig path defined for this cluster type, or it is managed automatically.');
490
+ }
491
+ console.log('kubectl config set up successfully.');
492
+ },
493
+
494
+ /**
495
+ * @method reset
496
+ * @description Performs a comprehensive reset of Kubernetes and container environments.
497
+ * This function is for cleaning up a node, reverting changes made by 'kubeadm init', 'kubeadm join', or 'k3s install'.
498
+ * It includes deleting Kind clusters, resetting kubeadm, removing CNI configs,
499
+ * cleaning Docker and Podman data, persistent volumes, and resetting kubelet components.
500
+ * It avoids aggressive iptables flushing that would break host connectivity, relying on kube-proxy's
501
+ * control loop to eventually clean up rules if the cluster is not re-initialized.
502
+ */
503
+ async reset() {
504
+ logger.info('Starting comprehensive reset of Kubernetes and container environments...');
505
+
506
+ try {
507
+ // Phase 1: Pre-reset Kubernetes Cleanup (while API server is still up)
508
+ logger.info('Phase 1/6: Cleaning up Kubernetes resources (PVCs, PVs) while API server is accessible...');
509
+
510
+ // Get all Persistent Volumes and identify their host paths for data deletion.
511
+ try {
512
+ const pvListJson = shellExec(`kubectl get pv -o json || echo '{"items":[]}'`, { stdout: true, silent: true });
513
+ const pvList = JSON.parse(pvListJson);
514
+
515
+ if (pvList.items && pvList.items.length > 0) {
516
+ for (const pv of pvList.items) {
517
+ // Check if the PV uses hostPath and delete its contents
518
+ if (pv.spec.hostPath && pv.spec.hostPath.path) {
519
+ const hostPath = pv.spec.hostPath.path;
520
+ logger.info(`Removing data from host path for PV '${pv.metadata.name}': ${hostPath}`);
521
+ shellExec(`sudo rm -rf ${hostPath}/* || true`);
522
+ }
523
+ }
524
+ } else {
525
+ logger.info('No Persistent Volumes found with hostPath to clean up.');
526
+ }
527
+ } catch (error) {
528
+ logger.error('Failed to clean up Persistent Volumes:', error);
529
+ }
530
+
531
+ // Phase 2: Stop Kubelet/K3s agent and remove CNI configuration
532
+ logger.info('Phase 2/6: Stopping Kubelet/K3s agent and removing CNI configurations...');
533
+ shellExec(`sudo systemctl stop kubelet || true`); // Stop kubelet if it's running (kubeadm)
534
+ shellExec(`sudo /usr/local/bin/k3s-uninstall.sh || true`); // Run K3s uninstall script if it exists
535
+
536
+ // CNI plugins use /etc/cni/net.d to store their configuration.
537
+ shellExec('sudo rm -rf /etc/cni/net.d/* || true');
538
+
539
+ // Phase 3: Kind Cluster Cleanup
540
+ logger.info('Phase 3/6: Cleaning up Kind clusters...');
541
+ shellExec(`kind get clusters | xargs -r -t -n1 kind delete cluster || true`);
542
+
543
+ // Phase 4: Kubeadm Reset (if applicable)
544
+ logger.info('Phase 4/6: Performing kubeadm reset (if applicable)...');
545
+ shellExec(`sudo kubeadm reset --force || true`); // Use || true to prevent script from failing if kubeadm is not installed
546
+
547
+ // Phase 5: Post-reset File System Cleanup (Local Storage, Kubeconfig)
548
+ logger.info('Phase 5/6: Cleaning up local storage provisioner data and kubeconfig...');
549
+ shellExec('rm -rf $HOME/.kube || true');
550
+ shellExec(`sudo rm -rf /opt/local-path-provisioner/* || true`);
551
+
552
+ // Phase 6: Container Runtime Cleanup (Docker and Podman)
553
+ logger.info('Phase 6/6: Cleaning up Docker and Podman data...');
554
+ shellExec('sudo docker system prune -a -f || true');
555
+ shellExec('sudo service docker stop || true');
556
+ shellExec(`sudo rm -rf /var/lib/containers/storage/* || true`);
557
+ shellExec(`sudo rm -rf /var/lib/docker/volumes/* || true`);
558
+ shellExec(`sudo rm -rf /var/lib/docker~/* || true`);
559
+ shellExec(`sudo rm -rf /home/containers/storage/* || true`);
560
+ shellExec(`sudo rm -rf /home/docker/* || true`);
561
+ shellExec('sudo mkdir -p /home/docker || true');
562
+ shellExec('sudo chmod 777 /home/docker || true');
563
+ shellExec('sudo ln -sf /home/docker /var/lib/docker || true');
564
+
565
+ shellExec(`sudo podman system prune -a -f || true`);
566
+ shellExec(`sudo podman system prune --all --volumes --force || true`);
567
+ shellExec(`sudo podman system prune --external --force || true`);
568
+ shellExec(`sudo mkdir -p /home/containers/storage || true`);
569
+ shellExec('sudo chmod 0711 /home/containers/storage || true');
570
+ shellExec(
571
+ `sudo sed -i -e "s@/var/lib/containers/storage@/home/containers/storage@g" /etc/containers/storage.conf || true`,
572
+ );
573
+ shellExec(`sudo podman system reset -f || true`);
574
+
575
+ // Final Kubelet and System Cleanup (after all other operations)
576
+ logger.info('Finalizing Kubelet and system file cleanup...');
577
+ shellExec(`sudo rm -rf /etc/kubernetes/* || true`);
578
+ shellExec(`sudo rm -rf /var/lib/kubelet/* || true`);
579
+ shellExec(`sudo rm -rf /root/.local/share/Trash/files/* || true`);
580
+ shellExec(`sudo systemctl daemon-reload`);
581
+ shellExec(`sudo systemctl start kubelet || true`); // Attempt to start kubelet; might fail if fully reset
582
+
583
+ logger.info('Comprehensive reset completed successfully.');
584
+ } catch (error) {
585
+ logger.error(`Error during reset: ${error.message}`);
586
+ console.error(error);
587
+ }
416
588
  },
417
589
 
418
- getResourcesCapacity(kubeadm = false) {
590
+ /**
591
+ * @method getResourcesCapacity
592
+ * @description Retrieves and returns the allocatable CPU and memory resources
593
+ * of the Kubernetes node.
594
+ * @param {boolean} [isKubeadmOrK3s=false] - If true, assumes a kubeadm or k3s-managed node;
595
+ * otherwise, assumes a Kind worker node.
596
+ * @returns {object} An object containing CPU and memory resources with values and units.
597
+ */
598
+ getResourcesCapacity(isKubeadmOrK3s = false) {
419
599
  const resources = {};
420
- const info = false
421
- ? `Capacity:
422
- cpu: 8
423
- ephemeral-storage: 153131976Ki
424
- hugepages-1Gi: 0
425
- hugepages-2Mi: 0
426
- memory: 11914720Ki
427
- pods: 110
428
- Allocatable:
429
- cpu: 8
430
- ephemeral-storage: 153131976Ki
431
- hugepages-1Gi: 0
432
- hugepages-2Mi: 0
433
- memory: 11914720Ki
434
- pods: `
435
- : shellExec(
436
- `kubectl describe node ${
437
- kubeadm === true ? os.hostname() : 'kind-worker'
438
- } | grep -E '(Allocatable:|Capacity:)' -A 6`,
439
- {
440
- stdout: true,
441
- silent: true,
442
- },
443
- );
600
+ const nodeName = isKubeadmOrK3s ? os.hostname() : 'kind-worker';
601
+ const info = shellExec(`kubectl describe node ${nodeName} | grep -E '(Allocatable:|Capacity:)' -A 6`, {
602
+ stdout: true,
603
+ silent: true,
604
+ });
444
605
  info
445
606
  .split('Allocatable:')[1]
446
607
  .split('\n')
@@ -460,6 +621,72 @@ Allocatable:
460
621
 
461
622
  return resources;
462
623
  },
624
+ /**
625
+ * @method initHost
626
+ * @description Installs essential host-level prerequisites for Kubernetes,
627
+ * including Docker, Podman, Kind, Kubeadm, and Helm.
628
+ *
629
+ * Quick-Start Guide for K3s Installation:
630
+ * This guide will help you quickly launch a cluster with default options. Make sure your nodes meet the requirements before proceeding.
631
+ * Consult the Installation page for greater detail on installing and configuring K3s.
632
+ * For information on how K3s components work together, refer to the Architecture page.
633
+ * If you are new to Kubernetes, the official Kubernetes docs have great tutorials covering basics that all cluster administrators should be familiar with.
634
+ *
635
+ * Install Script:
636
+ * K3s provides an installation script that is a convenient way to install it as a service on systemd or openrc based systems. This script is available at https://get.k3s.io. To install K3s using this method, just run:
637
+ * curl -sfL https://get.k3s.io | sh -
638
+ *
639
+ * After running this installation:
640
+ * - The K3s service will be configured to automatically restart after node reboots or if the process crashes or is killed
641
+ * - Additional utilities will be installed, including kubectl, crictl, ctr, k3s-killall.sh, and k3s-uninstall.sh
642
+ * - A kubeconfig file will be written to /etc/rancher/k3s/k3s.yaml and the kubectl installed by K3s will automatically use it
643
+ *
644
+ * A single-node server installation is a fully-functional Kubernetes cluster, including all the datastore, control-plane, kubelet, and container runtime components necessary to host workload pods. It is not necessary to add additional server or agents nodes, but you may want to do so to add additional capacity or redundancy to your cluster.
645
+ *
646
+ * To install additional agent nodes and add them to the cluster, run the installation script with the K3S_URL and K3S_TOKEN environment variables. Here is an example showing how to join an agent:
647
+ * curl -sfL https://get.k3s.io | K3S_URL=https://myserver:6443 K3S_TOKEN=mynodetoken sh -
648
+ *
649
+ * Setting the K3S_URL parameter causes the installer to configure K3s as an agent, instead of a server. The K3s agent will register with the K3s server listening at the supplied URL. The value to use for K3S_TOKEN is stored at /var/lib/rancher/k3s/server/node-token on your server node.
650
+ *
651
+ * Note: Each machine must have a unique hostname. If your machines do not have unique hostnames, pass the K3S_NODE_NAME environment variable and provide a value with a valid and unique hostname for each node.
652
+ * If you are interested in having more server nodes, see the High Availability Embedded etcd and High Availability External DB pages for more information.
653
+ */
654
+ initHost() {
655
+ console.log(
656
+ 'Installing essential host-level prerequisites for Kubernetes (Docker, Podman, Kind, Kubeadm, Helm) and providing K3s Quick-Start Guide information...',
657
+ );
658
+ // Install docker
659
+ shellExec(`sudo dnf -y install dnf-plugins-core`);
660
+ shellExec(`sudo dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo`);
661
+ shellExec(`sudo dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin`);
662
+
663
+ // Install podman
664
+ shellExec(`sudo dnf -y install podman`);
665
+
666
+ // Install kind
667
+ shellExec(`[ $(uname -m) = aarch64 ] && curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.29.0/kind-linux-arm64
668
+ chmod +x ./kind
669
+ sudo mv ./kind /bin/kind`);
670
+ // Install kubeadm, kubelet, kubectl (these are also useful for K3s for kubectl command)
671
+ shellExec(`cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
672
+ [kubernetes]
673
+ name=Kubernetes
674
+ baseurl=https://pkgs.k8s.io/core:/stable:/v1.33/rpm/
675
+ enabled=1
676
+ gpgcheck=1
677
+ gpgkey=https://pkgs.k8s.io/core:/stable:/v1.33/rpm/repodata/repomd.xml.key
678
+ exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni
679
+ EOF`);
680
+ shellExec(`sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes`);
681
+
682
+ // Install helm
683
+ shellExec(`curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3`);
684
+ shellExec(`chmod 700 get_helm.sh`);
685
+ shellExec(`./get_helm.sh`);
686
+ shellExec(`chmod +x /usr/local/bin/helm`);
687
+ shellExec(`sudo mv /usr/local/bin/helm /bin/helm`);
688
+ console.log('Host prerequisites installed successfully.');
689
+ },
463
690
  };
464
691
  }
465
692
  export default UnderpostCluster;