local 컴퓨터 위에 airflow 와 postgreSQL 을 설치하고 이 둘을 연동하는 작업 진행

os 는 centos 7

python3 사용

 

 

< postgreSQL 설정 >

 

아래 명령어로 postgres 설치

출처 : https://www.postgresql.org/download/linux/redhat/


sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql13-server
sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
sudo systemctl enable postgresql-13
sudo systemctl start postgresql-13
sudo systemctl status postgresql-13

출처에 가보면, centos 외에 ubuntu 등 다른 os 에 postgreSQL 설치하는 방법이 나와있으니 참고

 

"Peer's certificate issuer has been marked as not trusted by the user." 혹은

"Curl error (60) SSL certificate problem" 등의 SSL certificate 이슈가 나타난다면,

vi /etc/yum.conf 를 열고 "sslverify=false"를 추가

참고 https://eyeballs.tistory.com/544

 

postgreSQL 을 설치하면, postgres 라는 계정이 추가됨 (확인 : cat /etc/passwd | grep postgres)

postgres 계정명으로 psql 에 접근한 후,

airflow 가 사용할 database 및 user role 생성


su - postgres
psql


\list
CREATE DATABASE airflow;
\list
CREATE USER airflow WITH ENCRYPTED PASSWORD 'airflow';
\du
GRANT ALL PRIVILEGES ON DATABASE airflow TO airflow;


airflow 가 사용할 db 의 이름은 airflow 이고,

postgresql 내에서 사용되는 계정명과 비밀번호 모두 airflow 로 설정함

자주 사용되는 postgresql 명령어는 가장 아랫쪽에 적어둠

 

 

< Airflow 설정 >

 

bashrc 를 열고 안에 AIRFLOW_HOME 추가


vi ~/.bashrc 

export AIRFLOW_HOME=/opt/airflow

source ~/.bashrc

 

아래 명령어로 apache airflow 설치

 


pip3 install apache-airflow

 

airflow db init 명령어 실행하면, sqlite 를 이용한 메타데이터 데이터베이스가 설정됨

airflow.cfg 를 아래와 같이 수정함


cd $AIRFLOW_HOME
vi airflow.cfg


sql_alchemy_conn = postgresql+psycopg2://airflow:airflow@localhost:5432/airflow
base_url = http://localhost:8080
web_server_port = 8080
executor = LocalExecutor
load_examples = False


 

sql_alchemy_conn 에 들어가는 airflow 의미는 다음과 같음

sql_alchemy_conn = postgresql+psycopg2://[계정명]:[비밀번호]@localhost:5432/[데이터베이스명]

위에서 계정명, 비번, DB명 모두 airflow 로 설정했기 때문에 모두 airflow 라고 적음

localhost 인 이유는, postgreSQL 데몬이 동일한 서버(localhost:127.0.0.1) 에 떠있기 때문

 

executor 는 병렬 처리가 불가능한 SequentialExecutor 가 기본값인데,

병렬 처리가 가능한 LocalExecutor 로 변경

 

airflow WebUI 포트를 변경하려면 base_url 과 web_server_port 의 기본 8080 포트 번호를 다른 값으로 변경하면 됨

 

load_examples 는 기본값이 True 인데, airflow 처음 실행시 생성되는 예제 dags 를 띄우지 않으려면 False 로 변경

 

 

이후 아래 명령어 실행


airflow db init
airflow users create --username admin --password admin --role Admin --email eyeballs@example.com --firstname admin --lastname admin
airflow scheduler -D > /dev/null
airflow webserver -D


airflow WebUI 에 설정되는 관리자 계정명과 비밀번호는 모두 admin 으로 설정함

email 에는 자신의 이메일을 적어줌

 

airflow db init 진행시 아래 에러가 난다면


ModuleNotFoundError: No module named 'psycopg2'

 psycopg2 설치


pip3 install psycopg2

설치시 모종의 이유로 에러가 난다면, psycopg2-binary 를 대신 설치


pip3 install psycopg2-binary

 

 

아래 url 로 airflow WebUI 접근


localhost:8080

 

로그인에 사용되는 계정명과 비밀번호는 모두 admin

 



 

< 자주 사용되는 postgresql 명령어 >

 

\list # 데이터베이스 확인

\c airflow # 데이터베이스 사용

\dt # 테이블 확인

\ds # Sequence 목록

\df # Function 목록

\dv # View 목록

\du # User 목록

show data_directory; # 데이터 디렉터리 확인

 

< postgresql conf 위치 >

 

/var/lib/pgsql/13/data

중간에 13은 postgres 버전을 의미함

 

 

 

 

참고

airflow 공식 docker-compose https://airflow.apache.org/docs/apache-airflow/stable/docker-compose.yaml

https://progressivecoder.com/how-to-setup-an-airflow-postgres-connection/

 

+ Recent posts