Prerequisites and Environment Setup
Begin by verifying network connectivity between your build environment and the target database. Install the required Java Development Kit (JDK) and Maven compiler toolchain using your system's package manager.
sudo yum install java-11-openjdk-devel maven -y
Application Configuration and Compilation
Update the application configurasion file to reflect the correct data base connection parameters and server settings. Compile the project into a deployable archive, explicitly skipping unit tests to optimize build time.
vim src/main/resources/application.yml
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://db-cluster.internal:3306/app_db?useSSL=false&allowPublicKeyRetrieval=true
username: deploy_user
password: SecureP@ssw0rd
driver-class-name: com.mysql.cj.jdbc.Driver
mvn clean package -DskipTests
Container Image Construction and Registry Push
Define a lightweight Docker image based on a preconfigured Tomcat runtime. Copy the compiled WAR artifact into the webapps directory, build the image, and authenticate before pushing to your preferred container registry.
vim Dockerfile
FROM tomcat:9.0-jdk17-slim
LABEL maintainer="devops-team"
RUN rm -rf /usr/local/tomcat/webapps/*
COPY target/app-service-1.0.war /usr/local/tomcat/webapps/ROOT.war
EXPOSE 8080
docker build -t registry.example.com/platform/web-app:v2.1 .
docker login registry.example.com
docker push registry.example.com/platform/web-app:v2.1
Kubernetes Deployment Manifest Generation
Generate a baseline Deployment specification using a dry-run strategy. Modify the manifest to set the desired replica count, verify label selectors, and apply the configuration to the cluster.
kubectl create deployment web-platform \
--image=registry.example.com/platform/web-app:v2.1 \
--dry-run=client -o yaml > deployment.yaml
vim deployment.yaml
###########################
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-platform
labels:
app: web-platform
spec:
replicas: 3
selector:
matchLabels:
app: web-platform
template:
metadata:
labels:
app: web-platform
spec:
containers:
- name: app-container
image: registry.example.com/platform/web-app:v2.1
ports:
- containerPort: 8080
#############################
kubectl apply -f deployment.yaml
kubectl get pods -l app=web-platform
Service Exposure and Traffic Routing
Create a Service resource to route external traffic to the running pods. Utilize a NodePort type to map an ephemeral host port to the internal application port for external accessibility.
kubectl expose deployment/web-platform \
--port=80 \
--target-port=8080 \
--type=NodePort \
--dry-run=client -o yaml > service.yaml
vim service.yaml
#################################
apiVersion: v1
kind: Service
metadata:
name: web-platform-svc
labels:
app: web-platform
spec:
type: NodePort
ports:
- port: 80
protocol: TCP
targetPort: 8080
nodePort: 32088
selector:
app: web-platform
####################################
kubectl apply -f service.yaml
Once applied, the application can be accessed externally via any worker node IP address combined with the mapped port (e.g., http://<node-ip>:32088).
Operational Management: Scaling and Rollbacks
Track deployment revisions to facilitate quick recovery in case of deployment failures. Adjust the replica count dynamically to handle varying load requirements.
kubectl rollout history deployment/web-platform
kubectl rollout undo deployment/web-platform
kubectl scale deployment/web-platform --replicas=5