To deploy an application with two deployments in Kubernetes,

Category : Kubernetes | Sub Category : Kubernetes With Java | By Prasad Bonam Last updated: 2023-11-21 14:13:48 Viewed : 212



To deploy an application with two deployments in Kubernetes, you would typically define two separate YAML files, each describing a Deployment resource. Below is a simple example of deploying two nginx deployments as part of the same application. One deployment is labeled as "web-frontend" and the other as "web-backend".

1. Create YAML files for Deployments:

frontend-deployment.yaml:

yaml
apiVersion: apps/v1 kind: Deployment metadata: name: web-frontend spec: replicas: 3 selector: matchLabels: app: web tier: frontend template: metadata: labels: app: web tier: frontend spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80

backend-deployment.yaml:

yaml
apiVersion: apps/v1 kind: Deployment metadata: name: web-backend spec: replicas: 3 selector: matchLabels: app: web tier: backend template: metadata: labels: app: web tier: backend spec: containers: - name: nginx image: nginx:latest ports: - containerPort: 80

2. Apply Deployments to the Cluster:

Use kubectl apply to deploy the applications:

bash
kubectl apply -f frontend-deployment.yaml kubectl apply -f backend-deployment.yaml

3. Verify Deployments:

Check the status of the deployments:

bash
kubectl get deployments

Check the status of the pods:

bash
kubectl get pods

4. Expose Services (Optional):

If your application requires external access, you can expose the services using Kubernetes Services. For example:

frontend-service.yaml:

yaml
apiVersion: v1 kind: Service metadata: name: web-frontend-service spec: selector: app: web tier: frontend ports: - protocol: TCP port: 80 targetPort: 80 type: LoadBalancer

backend-service.yaml:

yaml
apiVersion: v1 kind: Service metadata: name: web-backend-service spec: selector: app: web tier: backend ports: - protocol: TCP port: 80 targetPort: 80 type: LoadBalancer

Apply the service configurations:

bash
kubectl apply -f frontend-service.yaml kubectl apply -f backend-service.yaml

5. Access the Application:

If you have exposed the services as LoadBalancer types, you can get the external IPs:

bash
kubectl get services

Access the frontend and backend services using their respective external IPs.

This example demonstrates a simple scenario where you have a frontend and backend deployment, each with its own set of replicas. Depending on your applications complexity, you might need to configure additional resources such as ConfigMaps, Secrets, or Persistent Volumes. Adjust the YAML files according to your applications requirements.

Search
Related Articles

Leave a Comment: