네트워크 캠퍼스/3차 프로젝트

인스턴스 설정하기

gayeon_ 2024. 8. 6. 15:26

인스턴스 설정하기

 

인스턴스는 위와 같이 생성했다.

 

 

a-public-monitoring 인스턴스 설정

a 가용영역의 애플리케이션들의 정상 작동 여부와 자원 사용량을 모니터링 하기 위해 그라파나와 프로메테우스를 설치한다.

 

1) 그라파나 설치

sudo add-apt-repository "deb https://packages.grafana.com/oss/deb stable main"
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
sudo apt-get update
sudo apt-get install grafana
sudo systemctl start grafana-server
sudo systemctl enable grafana-server

 

 

status로 active(running)을 확인하면 된다.

 

 

2) 프로메테우스 설치

wget https://github.com/prometheus/prometheus/releases/download/v2.31.1/prometheus-2.31.1.linux-amd64.tar.gz
tar xvfz prometheus-2.31.1.linux-amd64.tar.gz
cd prometheus-2.31.1.linux-amd64
nano prometheus.yml
  - scrape_configs:
    - job_name: 'prometheus'
      static_configs:
        - targets: ['localhost:9090']
    - job_name: 'node'
      static_configs:
        - targets: ['localhost:9100']

 

프로메테우스 설정 파일에서 위와 같이 9100번 포트를 열어준다.

 

nohup ./prometheus --config.file=prometheus.yml > prometheus.log 2>&1 &

 

프로메테우스 백그라운드 실행 명령어

 

 

3) node-exporter 설치

wget https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz
tar xvfz node_exporter-1.2.2.linux-amd64.tar.gz
cd node_exporter-1.2.2.linux-amd64
nohup ./node_exporter > node_exporter.log 2>&1 &

 

 

ingress-nginx

가용영역 a의 public subnet에 있는 nginx와

가용영역 b의 public subnet에 있는 nginx을 로드밸런싱할 ingress-nginx이다.

ingress-nginx는 플로팅 아이피가 필요하다.

 

1) nginx 설치

nano install_nginx.sh
#!/bin/bash

# 시스템 업데이트
sudo apt-get update

# 필요한 패키지 설치
sudo apt-get install -y curl gnupg2 ca-certificates lsb-release
sudo apt-get install -y ubuntu-keyring

# Nginx 서명 키 다운로드 및 추가
curl -s https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

# 서명 키 확인
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

# Nginx 저장소 추가
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

# 시스템 업데이트
sudo apt-get update

# Nginx 설치
sudo apt-get install -y nginx

# Nginx 버전 확인
nginx -v

# Nginx 시작
sudo systemctl start nginx

 

설치 shell 스크립트

 

chmod +x install_nginx.sh
./install_nginx.sh

 

실행 권한 부여 및 실행

 

 

 

nginx 설치 완료

 

 

2) nginx 설정파일 수정

sudo nano /etc/nginx/nginx.conf
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                      'backend_server=$upstream_addr';

        upstream nginx {
          server ip:포트번호 weight=100 max_fails=3 fail_timeout=3s;
          server ip:포트번호 weight=100 max_fails=3 fail_timeout=3s;
        }

        server {
                location / {
                  proxy_pass http://nginx;
                  proxy_http_version 1.1;
                  proxy_set_header Upgrade $http_upgrade;
                  proxy_set_header Connection 'upgrade';
                  proxy_set_header Host $host;
                  proxy_cache_bypass $http_upgrade;
                }
        }



    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
#tcp_nopush     on;

    keepalive_timeout  65;

#gzip  on;
}

 

upstream에 두 가용영역의 nginx 서버 주소를 넣는다.

 

 

sudo systemctl reload nginx

 

설정파일 수정 후 재가동

 

 

a-public-instance

a 가용영역에 있는 애플리케이션을 로드밸런싱하고 젠킨스로 관리하는 인스턴스

 

 

1) jenkins, java, docker 설치

nano install_docker_jenkins.sh
#!/bin/bash

# 도커 설치
sudo apt-get update

sudo apt-get install -y ca-certificates curl gnupg

sudo install -m 0755 -d /etc/apt/keyrings

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

sudo chmod a+r /etc/apt/keyrings/docker.gpg

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# 자바 설치
sudo apt update
sudo apt install -y openjdk-17-jdk

# 젠킨스 설치
sudo apt update

# Jenkins 공식 키 가져오기
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null

# Jenkins 레포지토리 추가
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null

sudo apt update
sudo apt install -y jenkins

# Jenkins 서비스를 시작하고 부팅 시 자동으로 시작하도록 설정
sudo systemctl start jenkins
sudo systemctl enable jenkins

echo "Docker와 Jenkins 설치가 완료되었습니다."

 

설치 shell 스크립트

 

 

chmod +x install_docker_jenkins.sh
./install_docker_jenkins.sh

 

실행 권한 부여 및 실행

 

 

 

가용영역 b의 public instance는 a의 public instance를 이미지화 해서 만들었다.

 

 

2) 젠킨스 설정

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

 

인스턴스의 플로팅ip:8080으로 젠킨스에 접속 후 

sudo cat /var/lib/jenkins/secrets/initialAdminPassword에서 비밀번호를 확인해 입력한다.

입력 후 설치까지 모두 마치면 젠킨스 대시보드에 접속할 수 있다.

 

젠킨스 관리 > Plugins에서 Publish Over SSH 플로그인을 설치한다.

 

 

3) 배스천 설정

 

로컬에 있는 키를 a-public-instance에 전송한 뒤 접속한다.

 

 

 

그러고나서 키의 권한을 변경하면 a-public-instance를 통해 private application 인스턴스에 접속할 수 있다.

 

 

4) rsa 키 설정

ssh-keygen -t rsa -f ~/.ssh/id_rsa
cd .ssh
cat id_rsa.pub

 

a-public-instance에서 rsa 키를 발급한 뒤 공개키를 복사한다.

 

 

nano ~/.ssh/authorized_keys

 

복사한 공개키는 application 인스턴스의 authorized_keys 파일에 붙여넣는다.



 

젠킨스와의 연동을 위해 System 탭에서 Key 란에 비밀키를 입력한다.

 

 

 

a-public-instance가 application 인스턴스를 관리할 수 있도록

SSH Server를 추가한다.

 

이때 Name은 젠킨스에서 사용하고 싶은 이름

Hostname은 서버의 ip

Username은 인스턴스의 Username

Remote Directory는 /home/ubuntu

로 작성했다.

 

a-nginx, b-nginx

각 가용영역 내에서 애플리케이션들을 로드밸런싱할 nginx 인스턴스이다.

서비스는 ingress-nginx로 보여질 것이기 때문에 가용영역 내의 nginx는 플로팅 아이피가 필요 없다.

 

nano install_nginx.sh

 

shell 스크립트 작성

 

#!/bin/bash

# 시스템 업데이트
sudo apt-get update

# 필요한 패키지 설치
sudo apt-get install -y curl gnupg2 ca-certificates lsb-release
sudo apt-get install -y ubuntu-keyring

# Nginx 서명 키 다운로드 및 추가
curl -s https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

# 서명 키 확인
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

# Nginx 저장소 추가
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

# 시스템 업데이트
sudo apt-get update

# Nginx 설치
sudo apt-get install -y nginx

# Nginx 버전 확인
nginx -v

# Nginx 시작
sudo systemctl start nginx
chmod +x install_nginx.sh
./install_nginx.sh

 

실행 권한 부여 및 실행

 

worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                      'backend_server=$upstream_addr';

        upstream nginx {
          server ip:포트번호 weight=100 max_fails=3 fail_timeout=3s;
          server ip:포트번호 weight=100 max_fails=3 fail_timeout=3s;
        }

        server {
                location / {
                  proxy_pass http://nginx;
                  proxy_http_version 1.1;
                  proxy_set_header Upgrade $http_upgrade;
                  proxy_set_header Connection 'upgrade';
                  proxy_set_header Host $host;
                  proxy_cache_bypass $http_upgrade;
                }
        }



    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
#tcp_nopush     on;

    keepalive_timeout  65;

#gzip  on;
}
sudo nano /etc/nginx/nginx.conf
sudo systemctl reload nginx

 

설정파일 수정 후 재가동

 

 

Application instance

private subnet에 있는 애플리케이션 인스턴스들의 설정이다.

애플리케이션 인스턴스에는 도커와 자바가 필요하다.

 

nano docker_java_install.sh
#!/bin/bash

# Update package index
sudo apt-get update

# Install prerequisites
sudo apt-get install -y ca-certificates curl gnupg

# Create directory for Docker keyrings
sudo install -m 0755 -d /etc/apt/keyrings

# Download and install Docker GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# Set proper permissions for Docker GPG key
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add Docker repository to sources list
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Update package index again after adding Docker repository
sudo apt-get update

# Install Docker packages
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# Run Docker hello-world to test installation
sudo docker run hello-world

# Install Java (OpenJDK)
sudo apt install openjdk-17-jdk

# Check Docker service status
sudo systemctl status docker

 

설치 shell 스크립트

 

 

chmod +x docker_java_install.sh
./docker_java_install.sh

 

실행 권한 부여 및 실행

 

+) 그리고 프로메테우스랑 node-exporter 도 설치한다.