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".
frontend-deployment.yaml
:yamlapiVersion: 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
:yamlapiVersion: 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
Use kubectl apply
to deploy the applications:
bashkubectl apply -f frontend-deployment.yaml kubectl apply -f backend-deployment.yaml
Check the status of the deployments:
bashkubectl get deployments
Check the status of the pods:
bashkubectl get pods
If your application requires external access, you can expose the services using Kubernetes Services. For example:
frontend-service.yaml
:yamlapiVersion: 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
:yamlapiVersion: 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:
bashkubectl apply -f frontend-service.yaml kubectl apply -f backend-service.yaml
If you have exposed the services as LoadBalancer types, you can get the external IPs:
bashkubectl 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.