이제 Jenkins 인스턴스에 접속하여 ansible 을 설치한다. (DO 에서 제공하는 레퍼런스 문서 링크 참고)
ansible 설치 후 로컬 PC 에 한것과 마찬가지로 /etc/ansible/digital_ocean.ini 와 /etc/ansible/digital_ocean.py 파일을 복사 후, jenkins
사용자 계정으로 Ansible 플레이북 (/home/jenkins/.ansible/deloy.yml) 이 정상적으로 실행되는지 확인한다.
ansible-playbook ~/.ansible/deploy.yml -i /etc/ansible/digital_ocean.py
위의 수동명령으로, api01 서버에 rolling deployment 실행을 할 수 있었다면, 라라벨 앱의 root 폴더에 있는 Jenkinsfile 의 25번째 라인에 deploy 스크립트 실행이 안되도록 comment out 시킨 부분을 찾아 # 문자를 삭제한다. 이렇게 함으로써, 새로운 commit 이 master 브랜치에 푸쉬될 때마다 새로운 도커 이미지가 api01 서버에 deploy 되는 것까지의 CI/CD 과정이 마무리 되는 것이다.
이제 마지막으로 api01 서버에 HTTPS 연결을 위하여, certbot 를 설치하고 nginx 설정을 변경해보자. Docker Swarm 모드로 nginx reverse proxy 를 설치할 때의 설정과 크게 다르지 않다.
Let’s Encrypt and Certbot
비영리 인터넷 보안 연구 그룹(ISRG)이 제공하는 Let’s Encrypt 를 사용하여, 90 일간 유효한 무료 Certificate 을 사용하면, 무료로 HTTPS 접속을 하도록 웹서버 설정이 가능하다. 이를 사용하기 위한 Python 스크립트 Certbot 과 Nginx 설정에 대한 훌륭한 문서들은 다양하게 존재하므로, 그 내용들은 아래의 링크들을 읽어보기 바란다.
- https://www.humankode.com/ssl/how-to-set-up-free-ssl-certificates-from-lets-encrypt-using-docker-and-nginx
- https://medium.com/@dbillinghamuk/migrating-from-google-cloud-platforms-app-engine-to-digitaloceans-docker-droplet-bcb5a95e9a0d
- https://medium.com/@pentacent/nginx-and-lets-encrypt-with-docker-in-less-than-5-minutes-b4b8a60d3a71
일반적으로 Let’sEcrypt 인증방법은 DNS 인증방법이나, Webroot 방식을 선택하는데, 전자의 경우, 아래의 예시에서 처럼 certbot/certbot 도커 이미지를 이용하여 명령을 내리고, 해당 domain 의 DNS 에 TXT 레코드를 추가/수정하는데, 자동화를 위해서는 Terraform 명령을 실행하는 shell script 를 만들고 –manual-auth-hook 을 사용할 수 있다. 아니면, 후자의 방법을 선택하면 되는데, 이에 대한 설명은 위 레퍼런스 리스트 마지막 글에 자세히 설명되어 있어 생략한다.
docker run --rm -it \ -v /opt/cert/key:/etc/letsencrypt \ -v /opt/cert/lib:/var/lib/letsencrypt \ -v /opt/cert/log:/var/log/letsencrypt \ certbot/certbot \ certonly \ --manual \ --preferred-challenges dns \ -d "api.hanlingo.com" \ --email [email protected] \ --agree-tos
HTTPS 통신을 하려면, 위에서 생성한 Let’s Encrypt 인증서 volume 바인딩을 추가하여 재실행해야 하는데, Nginx 의 reverse proxy 설정은 아래처럼 변경한다.
upstream container { server app:80; } server { listen 80; listen [::]:80; server_name api.hanlingo.com; return 301 https://$host$request_uri; } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name api.hanlingo.com; ssl_certificate /etc/letsencrypt/live/api.hanlingo.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/api.hanlingo.com/privkey.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers HIGH:!aNULL:!MD5; location / { proxy_set_header Host $host:$server_port; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_pass http://container; proxy_redirect off; # Handle web socket connection 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; } }
/opt/conf.d/proxy.conf 를 업데이트 했다면, 아래의 일련의 Docker 명령들을 다시 실행한다.
정상적으로 설정이 되었다면 브라우져 주소창에 https://api.example.com 을 입력하거나, 그냥 api.example.com 를 입력하면, nginx 에 의해 자동으로 https 리다이렉트된 https://api.example.com 주소에서, 정상적으로 앱이 실행되고 있음을 확인할 수 있다.
docker network create app-network docker run -d \ --name=app \ --network=app-network \ --restart=unless-stopped \ registry.hanlingo.com/jinseokoh/laravel-app docker run -d \ --name=nginx \ --network=app-network \ --restart=unless-stopped \ -v /opt/cert/key:/etc/letsencrypt \ -v /opt/conf.d:/etc/nginx/conf.d \ -p 80:80 \ -p 443:443 \ nginx:alpine
지금까지 5회에 걸쳐 Jenkins 를 이용한 PHP Laravel 앱의 CI/CD 설정에 대하여 알아보았는데. 다음에는 Docker Swarm 이나, 최근 GA 서비스로 확장된 DO 의 Kubernetes 에 배포하는 그림까지 확장해 보도록 하겠다.
So Stay tuned. Folks!