How to do multiple kubernetes Deployments in one file

In Kubernetes, you can define multiple deployments within a single YAML file by separating them with — (three dashes) as a delimiter. Each deployment should have its own kind: Deployment block and a unique metadata.name field to differentiate them. Here’s an example of defining two deployments in a single file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: deployment-1
  template:
    metadata:
      labels:
        app: deployment-1
    spec:
      containers:
        - name: app
          image: your-image-1:latest
          ports:
            - containerPort: 8080

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-2
spec:
  replicas: 2
  selector:
    matchLabels:
      app: deployment-2
  template:
    metadata:
      labels:
        app: deployment-2
    spec:
      containers:
        - name: app
          image: your-image-2:latest
          ports:
            - containerPort: 8080

In the example above, we have two deployments: deployment-1 and deployment-2. Each deployment has its own set of specifications, such as the number of replicas, labels, and container definitions. The deployments are separated by — in the YAML file.

You can save this YAML file, e.g., deployments.yaml, and apply it to your Kubernetes cluster using the kubectl apply command:

kubectl apply -f deployments.yaml

This will create both deployments specified in the file, and Kubernetes will ensure they are running according to the specified specifications.

Note: It’s important to ensure that each deployment has a unique name within the file to avoid conflicts.