# CMS安裝於GCP(使用Cloud SQL) ## 建DB * GCP左邊列表找到 SQL → 建立執行個體 → 選擇PostgreSQL * 輸入密碼、DB版本選10、地區選 <font color="red">asia-east1(台灣)</font>  * 自訂執行個體 (大部分維持預設) * DB:10G * 自動備份:凌晨 * 維護時間:禮拜日  ## 建VM * GCP左邊列表找到 Computer Engine → VM執行個體 → 建立執行個體 * 區域選 <font color="red">asia-east1(台灣)</font>  * 開機磁碟選 <font color="red">ubuntu 18.04</font>  ## DB 連結 VM - 點選「連線至VM」會出現<font color="green">教學課程</font>,依據教學一步步執行  ### 1) 建VM - 剛才已建好  ### 2) 靜態IP - VPC網路 → 外部IP位置 - 類型:臨時 → <font color="blue">靜態</font>  - 記住網路外部IP:<font color="red">34.80.142.72</font>  ### 3) 新增網路 - SQL → 連線設定 - 「已授權網路」輸入剛才的網路外部IP:<font color="red">34.80.142.72</font>  ### 4) 測試連線 - SQL → 總覽 - 記住DB外部IP:<font color="Coral">35.221.191.162</font> (之後會用到)  - Computer Engine → VM執行個體 → SSH向下箭頭 → 在瀏覽器視窗中開啟  ### 5) 安裝 PostgreSQL 用戶端 ```python= # 先切換成root sudo su ``` - PostgreSQL請執行下列指令 - 其實可以不用全裝,因為本機不需要裝DB,但不曉得要刪除哪個指令?(待測試),所以還是先5條都執行 ```python= sudo apt-get update # Feel free to change OpenJDK packages with your preferred JDK. sudo apt-get install build-essential openjdk-8-jdk-headless fp-compiler \ postgresql postgresql-client python3.6 cppreference-doc-en-html \ cgroup-lite libcap-dev zip # Only if you are going to use pip/venv to install python dependencies sudo apt-get install python3.6-dev libpq-dev libcups2-dev libyaml-dev \ libffi-dev python3-pip # Optional sudo apt-get install nginx-full python2.7 php7.2-cli php7.2-fpm \ phppgadmin texlive-latex-base a2ps haskell-platform rustc \ mono-mcs # Eric Add sudo apt-get install apache2-utils ``` ### 6) 登入 - IP為剛才的DB外部IP:<font color="Coral">35.221.191.162</font> ```python= sudo su - postgres psql -h 35.221.191.162 -U postgres # postgres的密碼,是最一開始建SQL時設定的那個 \l #列出所有資料庫 \q #離開postgres ```  * VM與Cloud SQL的連線設定已完成 --- ## Setup ### Database (Only for *MAIN*) ```python= psql -h 35.221.191.162 -U postgres create user cmsuser password 'xxxxxxx'; #請換成你的密碼 grant cmsuser to postgres; #GRANT <role> TO <master_user>; create database cmsdb with owner=cmsuser; ALTER SCHEMA public OWNER TO cmsuser; GRANT SELECT ON pg_largeobject TO cmsuser; #此步驟會失敗,但似乎不影響? \q #離開postgresSQL ```  - 改用cmsuser登入cmsdb,確認可登入 ```python= psql -h 35.221.191.162 -U cmsuser cmsdb ``` - 若密碼錯誤,先用postgres登入後,改密碼 ```python= ALTER USER cmsuser PASSWORD '輸入password'; ``` ### 改設定檔 - 步驟參考:https://blog.bigbinary.com/2016/01/23/configure-postgresql-to-allow-remote-connection.html - Allow both LAN IP and VM external IP - postgresql.conf - /etc/postgresql/10/main/postgresql.conf - 將listen_addresses 打開,並設定為 *  - pg_hba.conf - /etc/postgresql/10/main/pg_hba.conf  ```python= host all all 0.0.0.0/0 md5 host all all ::/0 md5 ``` - 改好後重啟 ```python= exit #若還是postgres身份,請退出postgres身份,回到root # service postgresql stop # service postgresql start service postgresql restart ``` - 確認是否OK - postgresql的port是5432,前面為0.0.0.0表示有設定成功 ```python= netstat -nlt ```  ## Set /etc/hosts (*For all machines*) ```python= 127.0.0.1 localhost #增加cms服務 127.0.0.1 main 127.0.0.1 web1 127.0.0.1 worker1 #增加DB外部IP 35.211.192.162 dbpsql ```  ## Download Related Packages ```python= git clone https://github.com/brianbbsu/cms-docker.git cd cms-docker git checkout YTP2020 ``` ## Edit Config Files for CMS (need to sync across all server) ### cms.conf - Copy from example.cms.conf - 修改 cmsuser密碼:在之前的步驟(create user cmsuser)所設定的密碼 - 修改 DB連線設定:剛才在hosts已設定為<font color="blue">dbpsql</font> ```python= cp example.cms.conf cms.conf ```  - Change the secret key in the WebServers section to any HEX string of the same length. *For security concern, the secret key should be unique between contests* * 可以之後再改 - Change the username and password in the ScoringService section to any username and password (should be the same in cms.ranking.conf) - 自行設定帳號密碼,需與cms.ranking.conf設定相同  ### cms.ranking.conf - Copy from example.cms.ranking.conf ```python= cp example.cms.ranking.conf cms.ranking.conf ``` - Change the username and the password to the ones set in cms.conf - 設定跟cms.conf相同的帳密  ## Init DB (Only for *MAIN*) ```python= ./shell.sh # Open a shell in docker cmsInitDB cmsAddAdmin -p admin admin # 新增 CMS Admin 帳號/密碼:admin / admin ``` - 若無Docker,自行安裝 ```python= apt install yum snap install docker ``` - cmsInitDB 跟 cmsAddAdmin 都成功後,退出 Container ```python= exit ``` ## 新增防火牆規則 - VPC網路 → 防火牆 → 建立防火牆規則 * web- 8888 * admin - 8889 * ranking - 8890  ## 設定VM套用此防火牆規則 - Computer Engine → VM執行個體 → 編輯 - 把http/https都打開 - 網路標記輸入剛設定的<font color="red">cms-all-port</font>  ## Run Server - 在VM之下,依序執行以下指令起不同的容器,MAIN、WEB、WORKER ```python= # source setup_env <main/web/worker> <contest_id> source setup_env main 1 docker-compose up -d source setup_env web 1 docker-compose up -d source setup_env worker 1 docker-compose up -d ``` - 確認目前Container狀態 - 發現cms-docker_contest0_1, cms-docker_proxy_1 兩個service沒起來 (Restarting ...) ```python= docker ps ``` - 查看Log ```python= # f48f07e9697d <container ID> docker logs f48f07e9697d ``` - 解決方式:要先在cmsdb的表contests中插入一筆contest_id紀錄 - 兩個Services起不來 - 原因是BB版的CMS image生成container時DB是空的,而BB版跑docker compose 需要用到contest_id,所以要先在DB中插入一筆contest_id紀錄。 - 先確認admin後台,可看到登入畫面 - IP為VM外部IP http://34.80.142.72:8889 - 使用剛才cmsAddAdmin建立的帳密,登入admin後台 - 若忘記建,可進入admin (cmsAdminWebServer 這台),重新建帳密 ```python= docker exec -ti 127716079ab0 bash cmsAddAdmin -p admin admin # 新增 CMS Admin 帳號/密碼:admin / admin ``` - 建立一筆Contest  - 再次觀察docker狀態,都啟起來了 ```python= docker ps ``` - 確認<font color="blue">比賽</font>頁面,可看到登入畫面 - 要有Contest,此頁面才打得開 http://34.80.142.72:8888 - 確認<font color="blue">排名</font>頁面,可看到登入畫面 http://34.80.142.72:8890 - admin後台,依據紅字訊息,修改cms.conf - 一直refresh頁面,紅字的example會變 - 改好後重啟 ```python= source setup_env web 1 docker-compose down docker-compose up -d ``` - 再次進入後台,確認已無紅字 ## -- END -- ###### tags:`GCP` `PostgreSQL` `CMS` `docker`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up