diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 5ff0f88b..4ff7cee7 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -7,7 +7,7 @@ on: branches: [ develop, main ] env: - DOCKER_IMAGE: ${{ secrets.DOCKERHUB_USERNAME }}/tech-blog + DOCKER_IMAGE: ${{ secrets.DOCKERHUB_USERNAME }}/tech-fork jobs: docker-build: @@ -21,7 +21,7 @@ jobs: - name: Download JAR from CI uses: actions/download-artifact@v4 with: - name: tech-blog-jar + name: tech-fork-jar path: build/libs/ github-token: ${{ secrets.GITHUB_TOKEN }} run-id: ${{ github.event.workflow_run.id }} @@ -51,8 +51,6 @@ jobs: DB_URL: ${{ secrets.DB_URL }} DB_USERNAME: ${{ secrets.DB_USERNAME }} DB_PASSWORD: ${{ secrets.DB_PASSWORD }} - REDIS_HOST: redis - REDIS_PORT: 6379 REDIS_PASSWORD: ${{ secrets.REDIS_PASSWORD }} steps: @@ -74,7 +72,7 @@ jobs: host: ${{ secrets.EC2_HOST }} username: ${{ secrets.EC2_USERNAME }} key: ${{ secrets.EC2_SSH_KEY }} - envs: DOCKER_IMAGE,BRANCH,SPRING_PROFILES_ACTIVE,DB_URL,DB_USERNAME,DB_PASSWORD,REDIS_HOST,REDIS_PORT,REDIS_PASSWORD + envs: DOCKER_IMAGE,BRANCH,SPRING_PROFILES_ACTIVE,DB_URL,DB_USERNAME,DB_PASSWORD,REDIS_PASSWORD script: | cd ~/deploy chmod +x deploy.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a8a44b4e..5504e897 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,6 @@ jobs: - name: Upload build artifact uses: actions/upload-artifact@v4 with: - name: tech-blog-jar + name: tech-fork-jar path: build/libs/*.jar retention-days: 3 \ No newline at end of file diff --git a/build.gradle b/build.gradle index 58d091f4..91779f4b 100644 --- a/build.gradle +++ b/build.gradle @@ -53,7 +53,7 @@ dependencies { // implementation 'org.springframework.boot:spring-boot-starter-security' // testImplementation 'org.springframework.security:spring-security-test' - // implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch' + implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch' // implementation 'org.springframework.ai:spring-ai-advisors-vector-store' // implementation 'org.springframework.ai:spring-ai-starter-vector-store-elasticsearch' diff --git a/docker-compose.yml b/docker-compose.yml index 89a84e97..7a39c13b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,7 +3,7 @@ version: '3.8' services: app: image: ${DOCKER_IMAGE}:${BRANCH} - container_name: tech-blog-app + container_name: tech-fork-app restart: always ports: - "8080:8080" @@ -12,17 +12,16 @@ services: - DB_URL=${DB_URL} - DB_USERNAME=${DB_USERNAME} - DB_PASSWORD=${DB_PASSWORD} - - REDIS_HOST=${REDIS_HOST} - - REDIS_PORT=${REDIS_PORT} - REDIS_PASSWORD=${REDIS_PASSWORD} networks: - app-network depends_on: - redis + - elasticsearch redis: image: redis:7-alpine - container_name: tech-blog-redis + container_name: tech-fork-redis restart: always ports: - "6379:6379" @@ -32,9 +31,33 @@ services: volumes: - redis-data:/data + elasticsearch: + image: docker.elastic.co/elasticsearch/elasticsearch:8.11.0 + container_name: tech-fork-elasticsearch + restart: always + ports: + - "9200:9200" + - "9300:9300" + environment: + - discovery.type=single-node + - xpack.security.enabled=false + - "ES_JAVA_OPTS=-Xms1g -Xmx1g" + networks: + - app-network + volumes: + - elasticsearch-data:/usr/share/elasticsearch/data + ulimits: + memlock: + soft: -1 + hard: -1 + nofile: + soft: 65536 + hard: 65536 + networks: app-network: driver: bridge volumes: - redis-data: \ No newline at end of file + redis-data: + elasticsearch-data: \ No newline at end of file diff --git a/infra/main.tf b/infra/main.tf index 669facd1..72c5544c 100644 --- a/infra/main.tf +++ b/infra/main.tf @@ -245,18 +245,29 @@ resource "aws_instance" "app" { amazon-linux-extras install -y nginx1 echo "===== Create Application Directories =====" - mkdir -p /opt/tech-blog - mkdir -p /var/log/tech-blog + mkdir -p /opt/tech-fork + mkdir -p /var/log/tech-fork - useradd -r -s /bin/false tech-blog || true - chown -R tech-blog:tech-blog /opt/tech-blog - chown -R tech-blog:tech-blog /var/log/tech-blog + useradd -r -s /bin/false tech-fork || true + chown -R tech-fork:tech-fork /opt/tech-fork + chown -R tech-fork:tech-fork /var/log/tech-fork echo "===== Install Git =====" yum install -y git + + echo "===== Install Docker =====" + yum install -y docker + systemctl start docker + systemctl enable docker + usermod -aG docker ec2-user + + echo "===== Install Docker Compose =====" + curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose + chmod +x /usr/local/bin/docker-compose + ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose echo "===== Nginx Configuration =====" - cat > /etc/nginx/conf.d/tech-blog.conf <<'NGINX' + cat > /etc/nginx/conf.d/tech-fork.conf <<'NGINX' upstream springapp { server 127.0.0.1:8080 fail_timeout=0; } @@ -267,8 +278,8 @@ resource "aws_instance" "app" { client_max_body_size 10M; - access_log /var/log/nginx/tech-blog-access.log; - error_log /var/log/nginx/tech-blog-error.log; + access_log /var/log/nginx/tech-fork-access.log; + error_log /var/log/nginx/tech-fork-error.log; # Cloudflare Real IP set_real_ip_from 173.245.48.0/20; @@ -365,7 +376,7 @@ resource "aws_instance" "app" { "files": { "collect_list": [ { - "file_path": "/var/log/tech-blog/*.log", + "file_path": "/var/log/tech-fork/*.log", "log_group_name": "/aws/ec2/${var.project_name}-${var.environment}/application", "log_stream_name": "{instance_id}", "timezone": "Asia/Seoul" @@ -377,7 +388,7 @@ resource "aws_instance" "app" { "timezone": "Asia/Seoul" }, { - "file_path": "/var/log/nginx/tech-blog-*.log", + "file_path": "/var/log/nginx/tech-fork-*.log", "log_group_name": "/aws/ec2/${var.project_name}-${var.environment}/nginx", "log_stream_name": "{instance_id}", "timezone": "Asia/Seoul" @@ -402,6 +413,8 @@ resource "aws_instance" "app" { touch /var/log/user-data-complete.txt EOF + user_data_replace_on_change = true + tags = { Name = "${var.project_name}-${var.environment}-app-server" } diff --git a/src/main/java/com/techfork/global/config/ElasticsearchConfig.java b/src/main/java/com/techfork/global/config/ElasticsearchConfig.java new file mode 100644 index 00000000..1f7d5e37 --- /dev/null +++ b/src/main/java/com/techfork/global/config/ElasticsearchConfig.java @@ -0,0 +1,23 @@ +package com.techfork.global.config; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.elasticsearch.client.ClientConfiguration; +import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration; + +@Configuration +public class ElasticsearchConfig extends ElasticsearchConfiguration { + + @Value("${elasticsearch.host:localhost}") + private String elasticsearchHost; + + @Value("${elasticsearch.port:9200}") + private int elasticsearchPort; + + @Override + public ClientConfiguration clientConfiguration() { + return ClientConfiguration.builder() + .connectedTo(elasticsearchHost + ":" + elasticsearchPort) + .build(); + } +} \ No newline at end of file diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 9b950ee5..c0bfe246 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -36,4 +36,8 @@ spring: max-active: 16 max-idle: 8 min-idle: 0 - max-wait: 5s \ No newline at end of file + max-wait: 5s + +elasticsearch: + host: elasticsearch + port: 9200 \ No newline at end of file