☕ ☁️ Cloud-Native Development with TuskLang Java
☁️ Cloud-Native Development with TuskLang Java
"We don't bow to any king" - Cloud-Native Java Edition
TuskLang Java enables building cloud-native applications with Kubernetes, Docker, and cloud deployment patterns that scale automatically and run anywhere.
🎯 Cloud-Native Architecture Overview
Kubernetes Configuration
// cloud-native-app.tsk
[cloud_native]
platform: "kubernetes"
orchestrator: "k8s"
container_runtime: "containerd"
service_mesh: "istio"[application]
name: "cloud-native-tusklang-app"
version: "2.0.0"
namespace: @env("K8S_NAMESPACE", "default")
replicas: {
min: 3
max: 10
target_cpu_utilization: 70
target_memory_utilization: 80
}
[containers]
app: {
image: @env("APP_IMAGE", "tusklang/java-app:latest")
port: 8080
resources: {
requests: {
cpu: "500m"
memory: "512Mi"
}
limits: {
cpu: "1000m"
memory: "1Gi"
}
}
health_check: {
liveness: {
path: "/health/live"
initial_delay: 30
period: 10
timeout: 5
failure_threshold: 3
}
readiness: {
path: "/health/ready"
initial_delay: 5
period: 5
timeout: 3
failure_threshold: 3
}
startup: {
path: "/health/startup"
initial_delay: 0
period: 10
timeout: 5
failure_threshold: 30
}
}
env: [
{
name: "APP_ENV"
value: @env("APP_ENV", "production")
},
{
name: "DB_HOST"
value_from: {
secret_key_ref: {
name: "db-secret"
key: "host"
}
}
},
{
name: "DB_PASSWORD"
value_from: {
secret_key_ref: {
name: "db-secret"
key: "password"
}
}
},
{
name: "JWT_SECRET"
value_from: {
secret_key_ref: {
name: "jwt-secret"
key: "secret"
}
}
}
]
volume_mounts: [
{
name: "config-volume"
mount_path: "/app/config"
read_only: true
},
{
name: "logs-volume"
mount_path: "/app/logs"
}
]
}
sidecar: {
image: @env("SIDECAR_IMAGE", "tusklang/sidecar:latest")
port: 9090
resources: {
requests: {
cpu: "100m"
memory: "128Mi"
}
limits: {
cpu: "200m"
memory: "256Mi"
}
}
env: [
{
name: "SIDECAR_MODE"
value: "monitoring"
}
]
}
[volumes]
config_volume: {
type: "config_map"
name: "app-config"
items: [
{
key: "app.tsk"
path: "app.tsk"
},
{
key: "database.tsk"
path: "database.tsk"
}
]
}
logs_volume: {
type: "empty_dir"
medium: "memory"
size_limit: "100Mi"
}
persistent_volume: {
type: "persistent_volume_claim"
name: "app-data"
access_mode: "ReadWriteOnce"
storage_class: "fast-ssd"
size: "10Gi"
}
[networking]
service: {
type: "ClusterIP"
port: 8080
target_port: 8080
session_affinity: "ClientIP"
session_affinity_config: {
client_ip: {
timeout_seconds: 3600
}
}
}
ingress: {
enabled: true
class: "nginx"
annotations: {
"kubernetes.io/ingress.class": "nginx"
"cert-manager.io/cluster-issuer": "letsencrypt-prod"
"nginx.ingress.kubernetes.io/ssl-redirect": "true"
"nginx.ingress.kubernetes.io/force-ssl-redirect": "true"
}
hosts: [
{
host: @env("INGRESS_HOST", "app.example.com")
paths: [
{
path: "/"
path_type: "Prefix"
backend: {
service: {
name: "app-service"
port: {
number: 8080
}
}
}
}
]
}
]
tls: [
{
secret_name: "app-tls"
hosts: [@env("INGRESS_HOST", "app.example.com")]
}
]
}
[monitoring]
prometheus: {
enabled: true
scrape_interval: "15s"
metrics_path: "/metrics"
annotations: {
"prometheus.io/scrape": "true"
"prometheus.io/port": "8080"
"prometheus.io/path": "/metrics"
}
}
grafana: {
enabled: true
dashboard: "app-dashboard"
datasource: "prometheus"
}
[security]
rbac: {
enabled: true
service_account: {
name: "app-service-account"
namespace: @env("K8S_NAMESPACE", "default")
}
role: {
name: "app-role"
rules: [
{
api_groups: [""]
resources: ["pods", "services", "endpoints"]
verbs: ["get", "list", "watch"]
},
{
api_groups: ["apps"]
resources: ["deployments", "replicasets"]
verbs: ["get", "list", "watch"]
}
]
}
role_binding: {
name: "app-role-binding"
role_ref: {
kind: "Role"
name: "app-role"
api_group: "rbac.authorization.k8s.io"
}
subjects: [
{
kind: "ServiceAccount"
name: "app-service-account"
namespace: @env("K8S_NAMESPACE", "default")
}
]
}
}
network_policy: {
enabled: true
ingress: [
{
from: [
{
namespace_selector: {
match_labels: {
name: "frontend"
}
}
}
]
ports: [
{
protocol: "TCP"
port: 8080
}
]
}
]
egress: [
{
to: [
{
namespace_selector: {
match_labels: {
name: "database"
}
}
}
]
ports: [
{
protocol: "TCP"
port: 5432
}
]
}
]
}
🐳 Docker Configuration
Multi-Stage Dockerfile
Dockerfile
Build stage
FROM maven:3.8.6-openjdk-17 AS builderWORKDIR /app
Copy pom.xml and download dependencies
COPY pom.xml .
RUN mvn dependency:go-offline -BCopy source code and build
COPY src ./src
COPY config ./config
RUN mvn clean package -DskipTestsRuntime stage
FROM openjdk:17-jdk-slimInstall necessary packages
RUN apt-get update && apt-get install -y \
curl \
&& rm -rf /var/lib/apt/lists/*Create app user
RUN groupadd -r appuser && useradd -r -g appuser appuserWORKDIR /app
Copy application JAR
COPY --from=builder /app/target/cloud-native-tusklang-app.jar app.jarCopy TuskLang configuration
COPY --from=builder /app/config/ config/Create necessary directories
RUN mkdir -p /app/logs /app/temp && \
chown -R appuser:appuser /appSwitch to non-root user
USER appuserHealth check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8080/health/live || exit 1Expose port
EXPOSE 8080JVM options for cloud-native environment
ENV JAVA_OPTS="-XX:+UseContainerSupport \
-XX:MaxRAMPercentage=75.0 \
-XX:+UseG1GC \
-XX:+UseStringDeduplication \
-Djava.security.egd=file:/dev/./urandom"Run application
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]
Docker Compose for Development
docker-compose.yml
version: '3.8'services:
app:
build: .
ports:
- "8080:8080"
environment:
- APP_ENV=development
- DB_HOST=postgres
- DB_PORT=5432
- DB_NAME=tusklang_app
- DB_USER=app_user
- DB_PASSWORD=app_password
- REDIS_HOST=redis
- REDIS_PORT=6379
volumes:
- ./config:/app/config:ro
- app-logs:/app/logs
depends_on:
- postgres
- redis
networks:
- app-network
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health/live"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
postgres:
image: postgres:15-alpine
environment:
- POSTGRES_DB=tusklang_app
- POSTGRES_USER=app_user
- POSTGRES_PASSWORD=app_password
volumes:
- postgres-data:/var/lib/postgresql/data
ports:
- "5432:5432"
networks:
- app-network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app_user -d tusklang_app"]
interval: 10s
timeout: 5s
retries: 5
redis:
image: redis:7-alpine
command: redis-server --appendonly yes
volumes:
- redis-data:/data
ports:
- "6379:6379"
networks:
- app-network
healthcheck:
test: ["CMD", "redis-cli", "ping"]
interval: 10s
timeout: 3s
retries: 5
prometheus:
image: prom/prometheus:latest
ports:
- "9090:9090"
volumes:
- ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml:ro
- prometheus-data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
- '--web.console.libraries=/etc/prometheus/console_libraries'
- '--web.console.templates=/etc/prometheus/consoles'
- '--storage.tsdb.retention.time=200h'
- '--web.enable-lifecycle'
networks:
- app-network
grafana:
image: grafana/grafana:latest
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
volumes:
- grafana-data:/var/lib/grafana
- ./monitoring/grafana/dashboards:/etc/grafana/provisioning/dashboards:ro
- ./monitoring/grafana/datasources:/etc/grafana/provisioning/datasources:ro
networks:
- app-network
depends_on:
- prometheus
volumes:
postgres-data:
redis-data:
prometheus-data:
grafana-data:
app-logs:
networks:
app-network:
driver: bridge
☸️ Kubernetes Deployment
Deployment YAML
k8s/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: tusklang-app
labels:
app: tusklang-app
version: v2.0.0
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: tusklang-app
template:
metadata:
labels:
app: tusklang-app
version: v2.0.0
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "8080"
prometheus.io/path: "/metrics"
spec:
serviceAccountName: app-service-account
containers:
- name: app
image: tusklang/java-app:2.0.0
ports:
- containerPort: 8080
name: http
env:
- name: APP_ENV
value: "production"
- name: DB_HOST
valueFrom:
secretKeyRef:
name: db-secret
key: host
- name: DB_PORT
valueFrom:
secretKeyRef:
name: db-secret
key: port
- name: DB_NAME
valueFrom:
secretKeyRef:
name: db-secret
key: name
- name: DB_USER
valueFrom:
secretKeyRef:
name: db-secret
key: user
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-secret
key: password
- name: JWT_SECRET
valueFrom:
secretKeyRef:
name: jwt-secret
key: secret
- name: REDIS_HOST
valueFrom:
secretKeyRef:
name: redis-secret
key: host
- name: REDIS_PASSWORD
valueFrom:
secretKeyRef:
name: redis-secret
key: password
resources:
requests:
cpu: 500m
memory: 512Mi
limits:
cpu: 1000m
memory: 1Gi
volumeMounts:
- name: config-volume
mountPath: /app/config
readOnly: true
- name: logs-volume
mountPath: /app/logs
livenessProbe:
httpGet:
path: /health/live
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 3
readinessProbe:
httpGet:
path: /health/ready
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
timeoutSeconds: 3
failureThreshold: 3
startupProbe:
httpGet:
path: /health/startup
port: 8080
initialDelaySeconds: 0
periodSeconds: 10
timeoutSeconds: 5
failureThreshold: 30
- name: sidecar
image: tusklang/sidecar:latest
ports:
- containerPort: 9090
name: metrics
env:
- name: SIDECAR_MODE
value: "monitoring"
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 200m
memory: 256Mi
volumes:
- name: config-volume
configMap:
name: app-config
- name: logs-volume
emptyDir:
medium: Memory
sizeLimit: 100Mi
---
apiVersion: v1
kind: Service
metadata:
name: app-service
labels:
app: tusklang-app
spec:
type: ClusterIP
ports:
- port: 8080
targetPort: 8080
protocol: TCP
name: http
- port: 9090
targetPort: 9090
protocol: TCP
name: metrics
selector:
app: tusklang-app
sessionAffinity: ClientIP
sessionAffinityConfig:
clientIP:
timeoutSeconds: 3600
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: tusklang-app
minReplicas: 3
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
behavior:
scaleUp:
stabilizationWindowSeconds: 60
policies:
- type: Percent
value: 100
periodSeconds: 15
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 10
periodSeconds: 60
ConfigMap and Secrets
k8s/config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
app.tsk: |
[application]
name: "Cloud Native TuskLang App"
version: "2.0.0"
environment: "production"
[server]
port: 8080
host: "0.0.0.0"
[logging]
level: "INFO"
format: "json"
output: "stdout"
[monitoring]
metrics: true
health_checks: true
distributed_tracing: true
database.tsk: |
[database]
type: "postgresql"
host: "${DB_HOST}"
port: "${DB_PORT}"
name: "${DB_NAME}"
user: "${DB_USER}"
password: "${DB_PASSWORD}"
pool_size: 20
connection_timeout: "30s"
[cache]
type: "redis"
host: "${REDIS_HOST}"
port: 6379
password: "${REDIS_PASSWORD}"
ttl: "1h"
---
apiVersion: v1
kind: Secret
metadata:
name: db-secret
type: Opaque
data:
host: cG9zdGdyZXMtc2VydmljZQ== # postgres-service
port: NTQzMg== # 5432
name: dHVza2xhbmdfYXBw # tusklang_app
user: YXBwX3VzZXI= # app_user
password: YXBwX3Bhc3N3b3Jk # app_password
---
apiVersion: v1
kind: Secret
metadata:
name: jwt-secret
type: Opaque
data:
secret: c3VwZXJfc2VjcmV0X2p3dF9rZXk= # super_secret_jwt_key
---
apiVersion: v1
kind: Secret
metadata:
name: redis-secret
type: Opaque
data:
host: cmVkaXMtc2VydmljZQ== # redis-service
password: cmVkaXNfcGFzc3dvcmQ= # redis_password
🚀 Cloud-Native Application Implementation
Spring Boot Cloud-Native Configuration
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.tusklang.java.annotations.TuskConfig;
import org.tusklang.java.TuskLang;@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@TuskConfig
public class CloudNativeApplication {
public static void main(String[] args) {
SpringApplication.run(CloudNativeApplication.class, args);
}
@Bean
public TuskLang tuskLang() {
return new TuskLang();
}
}
@Configuration
@TuskConfig
public class CloudNativeConfig {
private final TuskLang tuskLang;
public CloudNativeConfig(TuskLang tuskLang) {
this.tuskLang = tuskLang;
}
@Bean
public ApplicationConfig applicationConfig() {
return tuskLang.parseFile("config/app.tsk", ApplicationConfig.class);
}
@Bean
public DatabaseConfig databaseConfig() {
return tuskLang.parseFile("config/database.tsk", DatabaseConfig.class);
}
}
@TuskConfig
public class ApplicationConfig {
private String name;
private String version;
private String environment;
private ServerConfig server;
private LoggingConfig logging;
private MonitoringConfig monitoring;
// Getters and setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getVersion() { return version; }
public void setVersion(String version) { this.version = version; }
public String getEnvironment() { return environment; }
public void setEnvironment(String environment) { this.environment = environment; }
public ServerConfig getServer() { return server; }
public void setServer(ServerConfig server) { this.server = server; }
public LoggingConfig getLogging() { return logging; }
public void setLogging(LoggingConfig logging) { this.logging = logging; }
public MonitoringConfig getMonitoring() { return monitoring; }
public void setMonitoring(MonitoringConfig monitoring) { this.monitoring = monitoring; }
}
@TuskConfig
public class ServerConfig {
private int port;
private String host;
// Getters and setters
public int getPort() { return port; }
public void setPort(int port) { this.port = port; }
public String getHost() { return host; }
public void setHost(String host) { this.host = host; }
}
@TuskConfig
public class LoggingConfig {
private String level;
private String format;
private String output;
// Getters and setters
public String getLevel() { return level; }
public void setLevel(String level) { this.level = level; }
public String getFormat() { return format; }
public void setFormat(String format) { this.format = format; }
public String getOutput() { return output; }
public void setOutput(String output) { this.output = output; }
}
@TuskConfig
public class MonitoringConfig {
private boolean metrics;
private boolean healthChecks;
private boolean distributedTracing;
// Getters and setters
public boolean isMetrics() { return metrics; }
public void setMetrics(boolean metrics) { this.metrics = metrics; }
public boolean isHealthChecks() { return healthChecks; }
public void setHealthChecks(boolean healthChecks) { this.healthChecks = healthChecks; }
public boolean isDistributedTracing() { return distributedTracing; }
public void setDistributedTracing(boolean distributedTracing) { this.distributedTracing = distributedTracing; }
}
Health Checks Implementation
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Component
public class ApplicationHealthIndicator implements HealthIndicator {
private final DatabaseHealthChecker databaseHealthChecker;
private final CacheHealthChecker cacheHealthChecker;
private final ExternalServiceHealthChecker externalServiceHealthChecker;
public ApplicationHealthIndicator(DatabaseHealthChecker databaseHealthChecker,
CacheHealthChecker cacheHealthChecker,
ExternalServiceHealthChecker externalServiceHealthChecker) {
this.databaseHealthChecker = databaseHealthChecker;
this.cacheHealthChecker = cacheHealthChecker;
this.externalServiceHealthChecker = externalServiceHealthChecker;
}
@Override
public Health health() {
try {
Health.Builder builder = new Health.Builder();
// Check database health
boolean dbHealthy = databaseHealthChecker.isHealthy();
builder.withDetail("database", dbHealthy ? "UP" : "DOWN");
// Check cache health
boolean cacheHealthy = cacheHealthChecker.isHealthy();
builder.withDetail("cache", cacheHealthy ? "UP" : "DOWN");
// Check external services health
boolean externalHealthy = externalServiceHealthChecker.areHealthy();
builder.withDetail("external_services", externalHealthy ? "UP" : "DOWN");
// Determine overall health
if (dbHealthy && cacheHealthy && externalHealthy) {
return builder.up().build();
} else {
return builder.down().build();
}
} catch (Exception e) {
return Health.down()
.withException(e)
.build();
}
}
}
@Component
public class DatabaseHealthChecker {
private final DataSource dataSource;
public DatabaseHealthChecker(DataSource dataSource) {
this.dataSource = dataSource;
}
public boolean isHealthy() {
try (Connection connection = dataSource.getConnection()) {
try (PreparedStatement stmt = connection.prepareStatement("SELECT 1")) {
try (ResultSet rs = stmt.executeQuery()) {
return rs.next() && rs.getInt(1) == 1;
}
}
} catch (Exception e) {
log.error("Database health check failed", e);
return false;
}
}
}
@Component
public class CacheHealthChecker {
private final RedisTemplate<String, Object> redisTemplate;
public CacheHealthChecker(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
public boolean isHealthy() {
try {
String result = redisTemplate.execute((RedisCallback<String>) connection -> {
return connection.ping();
});
return "PONG".equals(result);
} catch (Exception e) {
log.error("Cache health check failed", e);
return false;
}
}
}
@Component
public class ExternalServiceHealthChecker {
private final RestTemplate restTemplate;
private final List<String> externalServiceUrls;
public ExternalServiceHealthChecker(RestTemplate restTemplate,
@Value("${external.services.urls}") List<String> externalServiceUrls) {
this.restTemplate = restTemplate;
this.externalServiceUrls = externalServiceUrls;
}
public boolean areHealthy() {
return externalServiceUrls.stream()
.allMatch(this::isServiceHealthy);
}
private boolean isServiceHealthy(String url) {
try {
ResponseEntity<String> response = restTemplate.getForEntity(url + "/health", String.class);
return response.getStatusCode().is2xxSuccessful();
} catch (Exception e) {
log.error("External service health check failed for: {}", url, e);
return false;
}
}
}
🔧 Best Practices
1. Containerization
- Use multi-stage builds for smaller images - Run as non-root user for security - Use health checks for container orchestration - Optimize JVM settings for containers2. Kubernetes Deployment
- Use rolling updates for zero-downtime deployments - Implement proper resource limits and requests - Use horizontal pod autoscaling - Configure network policies for security3. Configuration Management
- Use ConfigMaps for non-sensitive configuration - Use Secrets for sensitive data - Implement configuration hot-reloading - Use environment-specific configurations4. Monitoring and Observability
- Expose metrics endpoints for Prometheus - Implement comprehensive health checks - Use distributed tracing for request tracking - Set up proper logging with structured format5. Security
- Use RBAC for access control - Implement network policies - Use secrets for sensitive data - Run containers as non-root users🎯 Summary
TuskLang Java cloud-native development provides:
- Kubernetes Integration: Complete deployment and orchestration support - Docker Optimization: Multi-stage builds and container best practices - Cloud-Native Patterns: Health checks, metrics, and observability - Security: RBAC, network policies, and secrets management - Scalability: Horizontal pod autoscaling and load balancing
The combination of TuskLang's executable configuration with Java's cloud-native capabilities creates a powerful platform for building scalable, resilient, and maintainable cloud applications.
"We don't bow to any king" - Deploy cloud-native applications with confidence!