Skip to content

Commit 917b2b5

Browse files
authored
[feat/#27] 엘라스틱 서치 환경설정 구성 (#28)
* chore: redis의 host와 port는 민감한 정보가 아니므로 환경변수 제거 * feat: Elastic Search 초기 셋팅 구현 * deploy: user_data에 Docker와 Docker Compose 스크립트 추가 * chore: tech-blog를 tech-fork로 네이밍 변경 * deploy: user_data 변경 시 EC2 강제 재생성
1 parent 2a2485c commit 917b2b5

7 files changed

Lines changed: 84 additions & 23 deletions

File tree

.github/workflows/cd.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
branches: [ develop, main ]
88

99
env:
10-
DOCKER_IMAGE: ${{ secrets.DOCKERHUB_USERNAME }}/tech-blog
10+
DOCKER_IMAGE: ${{ secrets.DOCKERHUB_USERNAME }}/tech-fork
1111

1212
jobs:
1313
docker-build:
@@ -21,7 +21,7 @@ jobs:
2121
- name: Download JAR from CI
2222
uses: actions/download-artifact@v4
2323
with:
24-
name: tech-blog-jar
24+
name: tech-fork-jar
2525
path: build/libs/
2626
github-token: ${{ secrets.GITHUB_TOKEN }}
2727
run-id: ${{ github.event.workflow_run.id }}
@@ -51,8 +51,6 @@ jobs:
5151
DB_URL: ${{ secrets.DB_URL }}
5252
DB_USERNAME: ${{ secrets.DB_USERNAME }}
5353
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
54-
REDIS_HOST: redis
55-
REDIS_PORT: 6379
5654
REDIS_PASSWORD: ${{ secrets.REDIS_PASSWORD }}
5755

5856
steps:
@@ -74,7 +72,7 @@ jobs:
7472
host: ${{ secrets.EC2_HOST }}
7573
username: ${{ secrets.EC2_USERNAME }}
7674
key: ${{ secrets.EC2_SSH_KEY }}
77-
envs: DOCKER_IMAGE,BRANCH,SPRING_PROFILES_ACTIVE,DB_URL,DB_USERNAME,DB_PASSWORD,REDIS_HOST,REDIS_PORT,REDIS_PASSWORD
75+
envs: DOCKER_IMAGE,BRANCH,SPRING_PROFILES_ACTIVE,DB_URL,DB_USERNAME,DB_PASSWORD,REDIS_PASSWORD
7876
script: |
7977
cd ~/deploy
8078
chmod +x deploy.sh

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ jobs:
3030
- name: Upload build artifact
3131
uses: actions/upload-artifact@v4
3232
with:
33-
name: tech-blog-jar
33+
name: tech-fork-jar
3434
path: build/libs/*.jar
3535
retention-days: 3

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ dependencies {
5353
// implementation 'org.springframework.boot:spring-boot-starter-security'
5454
// testImplementation 'org.springframework.security:spring-security-test'
5555

56-
// implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
56+
implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
5757
// implementation 'org.springframework.ai:spring-ai-advisors-vector-store'
5858
// implementation 'org.springframework.ai:spring-ai-starter-vector-store-elasticsearch'
5959

docker-compose.yml

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: '3.8'
33
services:
44
app:
55
image: ${DOCKER_IMAGE}:${BRANCH}
6-
container_name: tech-blog-app
6+
container_name: tech-fork-app
77
restart: always
88
ports:
99
- "8080:8080"
@@ -12,17 +12,16 @@ services:
1212
- DB_URL=${DB_URL}
1313
- DB_USERNAME=${DB_USERNAME}
1414
- DB_PASSWORD=${DB_PASSWORD}
15-
- REDIS_HOST=${REDIS_HOST}
16-
- REDIS_PORT=${REDIS_PORT}
1715
- REDIS_PASSWORD=${REDIS_PASSWORD}
1816
networks:
1917
- app-network
2018
depends_on:
2119
- redis
20+
- elasticsearch
2221

2322
redis:
2423
image: redis:7-alpine
25-
container_name: tech-blog-redis
24+
container_name: tech-fork-redis
2625
restart: always
2726
ports:
2827
- "6379:6379"
@@ -32,9 +31,33 @@ services:
3231
volumes:
3332
- redis-data:/data
3433

34+
elasticsearch:
35+
image: docker.elastic.co/elasticsearch/elasticsearch:8.11.0
36+
container_name: tech-fork-elasticsearch
37+
restart: always
38+
ports:
39+
- "9200:9200"
40+
- "9300:9300"
41+
environment:
42+
- discovery.type=single-node
43+
- xpack.security.enabled=false
44+
- "ES_JAVA_OPTS=-Xms1g -Xmx1g"
45+
networks:
46+
- app-network
47+
volumes:
48+
- elasticsearch-data:/usr/share/elasticsearch/data
49+
ulimits:
50+
memlock:
51+
soft: -1
52+
hard: -1
53+
nofile:
54+
soft: 65536
55+
hard: 65536
56+
3557
networks:
3658
app-network:
3759
driver: bridge
3860

3961
volumes:
40-
redis-data:
62+
redis-data:
63+
elasticsearch-data:

infra/main.tf

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,29 @@ resource "aws_instance" "app" {
245245
amazon-linux-extras install -y nginx1
246246
247247
echo "===== Create Application Directories ====="
248-
mkdir -p /opt/tech-blog
249-
mkdir -p /var/log/tech-blog
248+
mkdir -p /opt/tech-fork
249+
mkdir -p /var/log/tech-fork
250250
251-
useradd -r -s /bin/false tech-blog || true
252-
chown -R tech-blog:tech-blog /opt/tech-blog
253-
chown -R tech-blog:tech-blog /var/log/tech-blog
251+
useradd -r -s /bin/false tech-fork || true
252+
chown -R tech-fork:tech-fork /opt/tech-fork
253+
chown -R tech-fork:tech-fork /var/log/tech-fork
254254
255255
echo "===== Install Git ====="
256256
yum install -y git
257+
258+
echo "===== Install Docker ====="
259+
yum install -y docker
260+
systemctl start docker
261+
systemctl enable docker
262+
usermod -aG docker ec2-user
263+
264+
echo "===== Install Docker Compose ====="
265+
curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
266+
chmod +x /usr/local/bin/docker-compose
267+
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
257268
258269
echo "===== Nginx Configuration ====="
259-
cat > /etc/nginx/conf.d/tech-blog.conf <<'NGINX'
270+
cat > /etc/nginx/conf.d/tech-fork.conf <<'NGINX'
260271
upstream springapp {
261272
server 127.0.0.1:8080 fail_timeout=0;
262273
}
@@ -267,8 +278,8 @@ resource "aws_instance" "app" {
267278
268279
client_max_body_size 10M;
269280
270-
access_log /var/log/nginx/tech-blog-access.log;
271-
error_log /var/log/nginx/tech-blog-error.log;
281+
access_log /var/log/nginx/tech-fork-access.log;
282+
error_log /var/log/nginx/tech-fork-error.log;
272283
273284
# Cloudflare Real IP
274285
set_real_ip_from 173.245.48.0/20;
@@ -365,7 +376,7 @@ resource "aws_instance" "app" {
365376
"files": {
366377
"collect_list": [
367378
{
368-
"file_path": "/var/log/tech-blog/*.log",
379+
"file_path": "/var/log/tech-fork/*.log",
369380
"log_group_name": "/aws/ec2/${var.project_name}-${var.environment}/application",
370381
"log_stream_name": "{instance_id}",
371382
"timezone": "Asia/Seoul"
@@ -377,7 +388,7 @@ resource "aws_instance" "app" {
377388
"timezone": "Asia/Seoul"
378389
},
379390
{
380-
"file_path": "/var/log/nginx/tech-blog-*.log",
391+
"file_path": "/var/log/nginx/tech-fork-*.log",
381392
"log_group_name": "/aws/ec2/${var.project_name}-${var.environment}/nginx",
382393
"log_stream_name": "{instance_id}",
383394
"timezone": "Asia/Seoul"
@@ -402,6 +413,8 @@ resource "aws_instance" "app" {
402413
touch /var/log/user-data-complete.txt
403414
EOF
404415

416+
user_data_replace_on_change = true
417+
405418
tags = {
406419
Name = "${var.project_name}-${var.environment}-app-server"
407420
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.techfork.global.config;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.data.elasticsearch.client.ClientConfiguration;
6+
import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
7+
8+
@Configuration
9+
public class ElasticsearchConfig extends ElasticsearchConfiguration {
10+
11+
@Value("${elasticsearch.host:localhost}")
12+
private String elasticsearchHost;
13+
14+
@Value("${elasticsearch.port:9200}")
15+
private int elasticsearchPort;
16+
17+
@Override
18+
public ClientConfiguration clientConfiguration() {
19+
return ClientConfiguration.builder()
20+
.connectedTo(elasticsearchHost + ":" + elasticsearchPort)
21+
.build();
22+
}
23+
}

src/main/resources/application-dev.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@ spring:
3636
max-active: 16
3737
max-idle: 8
3838
min-idle: 0
39-
max-wait: 5s
39+
max-wait: 5s
40+
41+
elasticsearch:
42+
host: elasticsearch
43+
port: 9200

0 commit comments

Comments
 (0)