|
| 1 | +#!/bin/bash |
| 2 | +# eks-agent-setup.sh - Script to set up EKS deployment, service, and ingress |
| 3 | + |
| 4 | +set -e |
| 5 | + |
| 6 | +echo "Setting up EKS resources for Spring AI Agent..." |
| 7 | + |
| 8 | +# Check if namespace exists, exit if it doesn't |
| 9 | +if ! kubectl get namespace unicorn-spring-ai-agent &>/dev/null; then |
| 10 | + echo "Error: Namespace 'unicorn-spring-ai-agent' does not exist. Please create it first." |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | + |
| 14 | +# Get MCP Server URL - continue with placeholder if not found |
| 15 | +echo "Getting MCP Server URL..." |
| 16 | +if ! MCP_URL=$(kubectl get ingress unicorn-store-spring -n unicorn-store-spring -o jsonpath='{.status.loadBalancer.ingress[0].hostname}' 2>/dev/null); then |
| 17 | + echo "Warning: Could not get MCP URL. Using placeholder value." |
| 18 | + MCP_URL="http://placeholder-mcp-url.example.com" |
| 19 | +else |
| 20 | + MCP_URL="http://$MCP_URL" |
| 21 | + echo "MCP URL: $MCP_URL" |
| 22 | +fi |
| 23 | + |
| 24 | +# Get ECR URI - exit if not found |
| 25 | +echo "Getting ECR URI..." |
| 26 | +if ! ECR_URI=$(aws ecr describe-repositories --repository-names unicorn-spring-ai-agent | jq --raw-output '.repositories[0].repositoryUri' 2>/dev/null); then |
| 27 | + echo "Error: Could not get ECR URI. Repository 'unicorn-spring-ai-agent' may not exist. Exiting." |
| 28 | + exit 1 |
| 29 | +else |
| 30 | + echo "ECR URI: $ECR_URI" |
| 31 | +fi |
| 32 | + |
| 33 | +# Get database connection string |
| 34 | +echo "Getting database connection string..." |
| 35 | +if ! SPRING_DATASOURCE_URL=$(aws ssm get-parameter --name unicornstore-db-connection-string | jq --raw-output '.Parameter.Value' 2>/dev/null); then |
| 36 | + echo "Warning: Could not get database connection string. Using placeholder value." |
| 37 | + SPRING_DATASOURCE_URL="jdbc:postgresql://placeholder-db-url:5432/unicornstore" |
| 38 | +else |
| 39 | + echo "Database connection string retrieved successfully." |
| 40 | +fi |
| 41 | + |
| 42 | +# Create directory for Kubernetes manifests |
| 43 | +echo "Creating directory for Kubernetes manifests..." |
| 44 | +mkdir -p ~/environment/unicorn-spring-ai-agent/k8s |
| 45 | + |
| 46 | +# Create deployment manifest |
| 47 | +echo "Creating deployment manifest..." |
| 48 | +cat <<EOF > ~/environment/unicorn-spring-ai-agent/k8s/deployment.yaml |
| 49 | +apiVersion: apps/v1 |
| 50 | +kind: Deployment |
| 51 | +metadata: |
| 52 | + name: unicorn-spring-ai-agent |
| 53 | + namespace: unicorn-spring-ai-agent |
| 54 | + labels: |
| 55 | + project: unicorn-store |
| 56 | + app: unicorn-spring-ai-agent |
| 57 | +spec: |
| 58 | + replicas: 1 |
| 59 | + selector: |
| 60 | + matchLabels: |
| 61 | + app: unicorn-spring-ai-agent |
| 62 | + template: |
| 63 | + metadata: |
| 64 | + labels: |
| 65 | + app: unicorn-spring-ai-agent |
| 66 | + spec: |
| 67 | + nodeSelector: |
| 68 | + karpenter.sh/nodepool: dedicated |
| 69 | + serviceAccountName: unicorn-spring-ai-agent |
| 70 | + containers: |
| 71 | + - name: unicorn-spring-ai-agent |
| 72 | + resources: |
| 73 | + requests: |
| 74 | + cpu: "1" |
| 75 | + memory: "2Gi" |
| 76 | + image: ${ECR_URI}:latest |
| 77 | + imagePullPolicy: Always |
| 78 | + env: |
| 79 | + - name: SPRING_DATASOURCE_PASSWORD |
| 80 | + valueFrom: |
| 81 | + secretKeyRef: |
| 82 | + name: "unicornstore-db-secret" |
| 83 | + key: "password" |
| 84 | + optional: false |
| 85 | + - name: SPRING_DATASOURCE_URL |
| 86 | + value: ${SPRING_DATASOURCE_URL} |
| 87 | + - name: SPRING_AI_MCP_CLIENT_SSE_CONNECTIONS_SERVER1_URL |
| 88 | + value: ${MCP_URL} |
| 89 | + ports: |
| 90 | + - containerPort: 8080 |
| 91 | + livenessProbe: |
| 92 | + httpGet: |
| 93 | + path: /actuator/health/liveness |
| 94 | + port: 8080 |
| 95 | + failureThreshold: 6 |
| 96 | + periodSeconds: 5 |
| 97 | + readinessProbe: |
| 98 | + httpGet: |
| 99 | + path: /actuator/health/readiness |
| 100 | + port: 8080 |
| 101 | + failureThreshold: 6 |
| 102 | + periodSeconds: 5 |
| 103 | + initialDelaySeconds: 10 |
| 104 | + startupProbe: |
| 105 | + httpGet: |
| 106 | + path: / |
| 107 | + port: 8080 |
| 108 | + failureThreshold: 6 |
| 109 | + periodSeconds: 5 |
| 110 | + initialDelaySeconds: 10 |
| 111 | + lifecycle: |
| 112 | + preStop: |
| 113 | + exec: |
| 114 | + command: ["sh", "-c", "sleep 10"] |
| 115 | + securityContext: |
| 116 | + runAsNonRoot: true |
| 117 | + allowPrivilegeEscalation: false |
| 118 | +EOF |
| 119 | +kubectl apply -f ~/environment/unicorn-spring-ai-agent/k8s/deployment.yaml |
| 120 | + |
| 121 | +# Create service manifest |
| 122 | +echo "Creating service manifest..." |
| 123 | +cat <<EOF > ~/environment/unicorn-spring-ai-agent/k8s/service.yaml |
| 124 | +apiVersion: v1 |
| 125 | +kind: Service |
| 126 | +metadata: |
| 127 | + name: unicorn-spring-ai-agent |
| 128 | + namespace: unicorn-spring-ai-agent |
| 129 | + labels: |
| 130 | + project: unicorn-store |
| 131 | + app: unicorn-spring-ai-agent |
| 132 | +spec: |
| 133 | + type: ClusterIP |
| 134 | + ports: |
| 135 | + - port: 80 |
| 136 | + targetPort: 8080 |
| 137 | + protocol: TCP |
| 138 | + selector: |
| 139 | + app: unicorn-spring-ai-agent |
| 140 | +EOF |
| 141 | +kubectl apply -f ~/environment/unicorn-spring-ai-agent/k8s/service.yaml |
| 142 | + |
| 143 | +# Create ingress manifest |
| 144 | +echo "Creating ingress manifest..." |
| 145 | +cat <<EOF > ~/environment/unicorn-spring-ai-agent/k8s/ingress.yaml |
| 146 | +apiVersion: networking.k8s.io/v1 |
| 147 | +kind: Ingress |
| 148 | +metadata: |
| 149 | + name: unicorn-spring-ai-agent |
| 150 | + namespace: unicorn-spring-ai-agent |
| 151 | + annotations: |
| 152 | + alb.ingress.kubernetes.io/scheme: internet-facing |
| 153 | + alb.ingress.kubernetes.io/target-type: ip |
| 154 | + alb.ingress.kubernetes.io/load-balancer-attributes: idle_timeout.timeout_seconds=3600 |
| 155 | + labels: |
| 156 | + project: unicorn-store |
| 157 | + app: unicorn-spring-ai-agent |
| 158 | +spec: |
| 159 | + ingressClassName: alb |
| 160 | + rules: |
| 161 | + - http: |
| 162 | + paths: |
| 163 | + - path: / |
| 164 | + pathType: Prefix |
| 165 | + backend: |
| 166 | + service: |
| 167 | + name: unicorn-spring-ai-agent |
| 168 | + port: |
| 169 | + number: 80 |
| 170 | +EOF |
| 171 | +kubectl apply -f ~/environment/unicorn-spring-ai-agent/k8s/ingress.yaml |
| 172 | + |
| 173 | +kubectl wait deployment unicorn-spring-ai-agent -n unicorn-spring-ai-agent --for condition=Available=True --timeout=120s |
| 174 | +kubectl get deployment unicorn-spring-ai-agent -n unicorn-spring-ai-agent |
| 175 | +SVC_URL=http://$(kubectl get ingress unicorn-spring-ai-agent -n unicorn-spring-ai-agent -o jsonpath='{.status.loadBalancer.ingress[0].hostname}') |
| 176 | +while [[ $(curl -s -o /dev/null -w "%{http_code}" $SVC_URL/) != "200" ]]; do echo "Service not yet available ..." && sleep 5; done |
| 177 | +echo $SVC_URL |
| 178 | +echo Service is Ready! |
0 commit comments