인프라

docker Dockerfile 이용한 ubuntu + APM + gitlab + gitlab-runner + git 설정

소행성왕자 2023. 2. 10. 13:18

Dockerfile 이용하여 ubuntu 서버에 아래 패키지를 구축하려고 합니다.

  • ubuntu 20.04 LTS
  • apache2 (내부 port:8081)
  • mysql8.0 (내부 port:3306)
  • php7.4
  • gitlab (내부 port:80)
  • gitlab-runner
  • git

. docker 우분투 image 설치

docker pull ubuntu:20.04

. container 생성 및 실행

# 위 Dockerfile 사용안하고 사용
#docker run -it -d -p 80:80 -p 9181:8081 --name ubuntu_in_apm ubuntu_cofor

# 이걸로 사용
docker run -it -d -p 80:80 -p 9181:8081 --privileged=true --name ubuntu_in_apm ubuntu_cofor /sbin/init

. container ubuntu_in_apm 접속

# docker attach ubuntu_in_apm 
# 아래 명령어 사용
docker exec -it ubuntu_in_apm bash

. container ubuntu_in_apm 에서 빠져나올때

ctrl + p + q

. APM 설치

apt-get update
apt-get -y install sudo
apt-get install -y apache2 
apt-get install -y software-properties-common
add-apt-repository ppa:ondrej/php 
apt-get update
apt-get install -y mysql-client mysql-server
apt-get install -y git openssh-server net-tools php7.4 php7.4-cli php7.4-common php7.4-curl php7.4-zip php7.4-gd php7.4-mysql

. apache2 port 80 -> 8081 로 변경

vi /etc/apache2/ports.conf
Listen 8081

. 서비스 시작

service apache2 start
service mysql start
service mysql start
* Starting MySQL database server mysqld                                                                                                                                                                              
su: warning: cannot change directory to /nonexistent: No such file or directory

오류발생시

service mysql stop
usermod -d /var/lib/mysql/ mysql
service mysql start

. DB 계정생성

create database crawlDB;
create user 'crawl'@'localhost' identified by 'crawlpw';
grant all privileges on *.* to 'crawl'@'localhost' with grant option;
flush privileges;

. document_root 경로 index.php 생성 ( /var/www/html)

vi /var/www/html/index.php

<?php
 
echo '<h1>index.php</h1>';
 
$conn = mysqli_connect(
        '127.0.0.1',
        'crawl',
        'crawlpw',
        'crawlDB',
    );
 
    if(mysqli_connect_error()) {
        echo "Failed to Connect Mysql: ".mysqli_connect_error();
    }
    $sql = "SELECT VERSION()";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_array($result);
    print_r($row["VERSION()"]);
 
 
 
 
phpinfo();
http://220.72.212.xxx:9181/  

 

. gitlab 설치

apt-get install -y curl openssh-server ca-certificates 

# mail 사용하는거라 안해도 될것 같음
apt-get install -y postfix

curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | bash
EXTERNAL_URL="http://220.72.212.xxx/" apt-get install gitlab-ce


# EXTERNAL_URL 접속할 ip 를 적어준다
...
...
  - create symlink at /opt/gitlab/service/logrotate to /opt/gitlab/sv/logrotate
      * ruby_block[wait for logrotate service socket] action run
      
#여기서 먹통
#컨테이너 중지후 다시 시작
#아래 명령어 실행

/opt/gitlab/embedded/bin/runsvdir-start &



# 다시 설치 (오래걸림 5~10분)
EXTERNAL_URL="http://220.72.212.xxx/" apt-get install gitlab-ce


dpkg --configure -a

- 브라우져 접속

. http://220.72.212.xxx/  

. gitlab root 패스워드 찾기

cat /etc/gitlab/initial_root_password

. gitlab 접속후 해야할일

  • root 패스워드 변경
  • 프로젝트 생성 ( ubuntu_in_apm )

 

. gitlab-runner 설치

repo 추가 및 설치

curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | bash

apt install gitlab-runner

- CI/CD 설정

gitlab 로그인후 프로젝트 생성

settings > CI/CD > Runers > Expand 클릭

$gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=58196 revision=4d1ca121 version=15.8.2
Running in system-mode.

Enter the GitLab instance URL (for example, https://gitlab.com/):
http://220.72.212.xxx/
Enter the registration token:
GR1348941kkgKeGcNp3-AdrRp-nT3
Enter a description for the runner:
[bdda4b532ec0]:
Enter tags for the runner (comma-separated):
deploy
Enter optional maintenance note for the runner:

WARNING: Support for registration tokens and runner parameters in the 'register' command has been deprecated in GitLab Runner 15.6 and will be replaced with support for authentication tokens. For more information, see https://gitlab.com/gitlab-org/gitlab/-/issues/380872
Registering runner... succeeded                     runner=GR1348941kkgKeGcN
Enter an executor: ssh, instance, kubernetes, custom, docker-ssh, parallels, shell, docker, virtualbox, docker+machine, docker-ssh+machine:
shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!

Configuration (with the authentication token) was saved in "/etc/gitlab-runner/config.toml"

위와같이 runner 생성된것을 확인할수 있습니다.

- local 에 프로젝트 clone

git clone http://220.72.212.xxx:9180/gitlab-instance-12e44bce/ubuntu_in_apm.git

- .gitlab-ci.yml 생성

deploy-to-server:
   stage: deploy
   only:
     - main
   script:
     - echo 'hello world!'
     - whoami
     - pwd
     - cd /var/www/html
     - git pull

   tags:
     - deploy

- test.php 생성

 <?php

 echo '<h1>test.html</h1>';

 $conn = mysqli_connect(
         '127.0.0.1',
         'crawl',
         'crawlpw',
         'crawlDB',
     );

     if(mysqli_connect_error()) {
         echo "Failed to Connect Mysql: ".mysqli_connect_error();
     }
     $sql = "SELECT VERSION()";
     $result = mysqli_query($conn, $sql);
     $row = mysqli_fetch_array($result);
     print_r($row["VERSION()"]);




 phpinfo();
git add .
git commit -m "first commit"
git push

 

- git pull 암호 물어볼때

git config credential.helper store

 


컨테이너 중지 후 다시 접속

apache2 mysql 시작

service apache2 start
service mysql start