[DevOps] Jenkins๋ฅผ ์ด์šฉํ•œ CI/CD Pipeline ๊ตฌ์ถ• - Jenkins + Infrastructure as Code ์™€์˜ ์—ฐ๋™ - Ansible

2022. 9. 7. 07:24ใ†System ์ž‘์—…์‹ค/DevOps

728x90
๋ฐ˜์‘ํ˜•

 

 

 

์ด ๋‚ด์šฉ์€ ์ธํ”„๋Ÿฐ - Jenkins๋ฅผ ์ด์šฉํ•œ CI/CD Pipeline ๊ตฌ์ถ• ๊ฐ•์˜๋ฅผ ๋ณด๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ ์ž…๋‹ˆ๋‹ค.


ํ•ด๋‹น ๋‚ด์šฉ์˜ Code ๋‚ด์šฉ์€ ์ด ๊ณณ์—์„œ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.






๐Ÿ—‚ ๋ชฉ์ฐจ

โ— [DevOps] Jenkins๋ฅผ ์ด์šฉํ•œ CI/CD Pipeline ๊ตฌ์ถ• - ์ž๋™ํ™” ๋„๊ตฌ์˜ ์‚ฌ์šฉ
โ— [DevOps] Jenkins๋ฅผ ์ด์šฉํ•œ CI/CD Pipeline ๊ตฌ์ถ• - Jenkins + Infrastructure as Code ์™€์˜ ์—ฐ๋™
โ— [DevOps] Jenkins๋ฅผ ์ด์šฉํ•œ CI/CD Pipeline ๊ตฌ์ถ• - Jenkins + Ansible + Kubernetes ์™€์˜ ์—ฐ๋™
โ— [DevOps] Jenkins๋ฅผ ์ด์šฉํ•œ CI/CD Pipeline ๊ตฌ์ถ• - Advenced Jenkins ์‚ฌ์šฉ โ‘  - Pipeline
โ— [DevOps] Jenkins๋ฅผ ์ด์šฉํ•œ CI/CD Pipeline ๊ตฌ์ถ• - Advenced Jenkins ์‚ฌ์šฉ โ‘ก - SonarQube
โ— [DevOps] Jenkins๋ฅผ ์ด์šฉํ•œ CI/CD Pipeline ๊ตฌ์ถ• - Advenced Jenkins ์‚ฌ์šฉ โ‘ข - Multi Nodes
โ— [DevOps] Jenkins๋ฅผ ์ด์šฉํ•œ CI/CD Pipeline ๊ตฌ์ถ• - ์ƒ์šฉ ํด๋ผ์šฐ๋“œ ํ™˜๊ฒฝ์— ๋ฐฐํฌํ•˜๊ธฐ
โ— [DevOps] Jenkins๋ฅผ ์ด์šฉํ•œ CI/CD Pipeline ๊ตฌ์ถ• - ๋ถ€๋ก


 






 

๐Ÿš€ Jenkins์™€ Infrastructure as Code (IaC) ์—ฐ๋™

    ๐Ÿ”ฝ  ๊ฐœ์š”

        ๐Ÿ“ฆ ์†Œ๊ฐœ


Infrastructure as Code (IaC)๋Š” ์ฝ”๋“œํ˜• ์ธํ”„๋ผ๋ผ๊ณ  ๋ถˆ๋ฆฌ๋ฉฐ, ์ˆ˜๋™ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์•„๋‹Œ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์ธํ”„๋ผ๋ฅผ ๊ด€๋ฆฌํ•˜๊ณ , ํ”„๋กœ๋น„์ €๋‹ํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ด์š”.

IaC๋ฅผ ์ด์šฉํ•˜๋ฉด ์ธํ”„๋ผ ์‚ฌ์–‘์„ ๋‹ด์€ ๊ตฌ์„ฑ ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌ์„ฑ์„ ํŽธ์ง‘ํ•˜๊ณ , ๋ฐฐํฌํ•˜๊ธฐ๊ฐ€ ๋” ์‰ฌ์›Œ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
๋˜ํ•œ, ๋งค๋ฒˆ ๋™์ผํ•œ ํ™˜๊ฒฝ์„ ํ”„๋กœ๋น„์ €๋‹ํ•˜๋„๋ก ๋ณด์žฅํ•  ์ˆ˜ ์žˆ์–ด์š”.

IaC๋Š” ๊ตฌ์„ฑ ์‚ฌ์–‘์„ ์ฝ”๋“œํ™” ํ•˜๊ณ , ๋ฌธ์„œํ™” ํ•จ์œผ๋กœ ๊ตฌ์„ฑ ๊ด€๋ฆฌ๋ฅผ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ตฌ์„ฑ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ฌธ์„œํ™”ํ•˜์ง€ ์•Š๊ณ , ์ž„์‹œ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ์ผ์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ์–ด์š”.

๋ฒ„์ „ ์ œ์–ด๋Š” IaC์˜ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ด์—์š”. ๋‹ค๋ฅธ ์†Œํ”„ํŠธ์›จ์–ด ์†Œ์Šค ์ฝ”๋“œ ํŒŒ์ผ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ตฌ์„ฑ ํŒŒ์ผ๋„ ์†Œ์Šค ์ œ์–ด๊ฐ€ ํ•„์š”ํ•˜๋‹ต๋‹ˆ๋‹ค. ์ฝ”๋“œ๋กœ ์ธํ”„๋ผ๋ฅผ ๋ฐฐํฌํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์ธํ”„๋ผ๋ฅผ ๋ชจ๋“ˆ์‹ ๊ตฌ์„ฑ ์š”์†Œ๋กœ ๋ถ„ํ™œํ•˜๊ณ , ์ž๋™ํ™”๋ฅผ ํ†ตํ•ด ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ๊ฒฐํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป์ด์—์š”.

IaC๋กœ ์ธํ”„๋ผ ํ”„๋กœ๋น„์ €๋‹์„ ์ž๋™ํ™”ํ•˜๊ฒŒ ๋˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•˜๊ฑฐ๋‚˜, ๋ฐฐํฌํ•  ๋•Œ๋งˆ๋‹ค ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์„œ๋ฒ„, ์šด์˜์ฒด์ œ, ์Šคํ† ๋ฆฌ์ง€, ๊ธฐํƒ€ ์ธํ”„๋ผ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ˆ˜๋™์œผ๋กœ ํ”„๋กœ๋น„์ €๋‹ํ•˜๊ณ , ๊ด€๋ฆฌํ•  ํ•„์š”๊ฐ€ ์—†์–ด์ ธ์š”.

์ด๋Ÿฌํ•œ ์ž‘์—…์€ ์ˆ˜๋™์œผ๋กœ ์ง„ํ–‰ํ•  ์ˆ˜๋„ ์žˆ๊ณ , Ansible๊ณผ ๊ฐ™์€ ์ž๋™ํ™” ํˆด์„ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ์–ด์š”.





1. ์‹œ์Šคํ…œ, ํ•˜๋“œ์›จ์–ด ๋˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌ์„ฑ์ •๋ณด๋ฅผ ํŒŒ์ผ(์Šคํฌ๋ฆฝํŠธ)์„ ํ†ตํ•ด ๊ด€๋ฆฌ ๋ฐ ํ”„๋กœ๋น„์ €๋‹.

2. IT Infrastructure, Bare Metal Server ๋“ฑ ๋ฌผ๋ฆฌ ์žฅ๋น„ ๋ฐ ๊ฐ€์ƒ ๋จธ์‹ ๊ณผ ๊ด€๋ จ๋œ ๊ตฌ์„ฑ ์ž์› ๊ด€๋ฆฌ.

3. ๋ฒ„์ „ ๊ด€๋ฆฌ๋ฅผ ํ†ตํ•œ ์ž์› ๊ด€๋ฆฌ.

 


1. Infrastructure as Code(IaC)๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „

IaC๊ฐ€ ๋„์ž…๋˜๊ธฐ ์ด์ „์—๋Š” ๋‹จ๋ง๊ธฐ(Laptop, Desktop ๋“ฑ)์„ ํ†ตํ•ด Server๋ฅผ ๊ด€๋ฆฌํ•ด์ค˜์•ผ ํ•ด์š”.
๋งŒ์•ฝ ํŠน์ • Server๋ฅผ ์‚ฌ์šฉ ๋ชปํ•  ์ •๋„์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น Server๋ฅผ ๋–ผ์–ด๋‚ด๊ณ , ์ƒˆ๋กœ์šด Server๋ฅผ ๋„์ž…ํ•ด์•ผ ํ•ด์š”.
์ด ๋ชจ๋“  ์ž‘์—…์„ ํ„ฐ๋ฏธ๋„ ๋“ฑ์„ ์ด์šฉํ•ด์„œ ์ง์ ‘ ์ˆ˜๋™์œผ๋กœ ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.





2. Infrastructure as Code(IaC)๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ํ›„


๋จผ์ € Ansible๊ณผ ๊ฐ™์€ ์†Œํ”„ํŠธ์›จ์–ด๋ฅผ ๋„์ž…ํ•˜๊ฒŒ ๋˜๋ฉด Ansible๊ณผ Server๋ฅผ ์—ฐ๋™ ์‹œ์ผœ์ฃผ๊ณ , Server์˜ ๋ชฉ๋ก(Inventory)์™€ Server๋“ค์„ ๊ด€๋ฆฌํ•จ์— ์žˆ์–ด ํ•„์š”ํ•œ ์ž‘์—…๋“ค์˜ ์ ˆ์ฐจ๋ฅผ ๋‹ด์•„ ๋†“๋Š” File (Play Books)๋ฅผ ์ง€์ •ํ•ด ์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ Server ํ•œ ๋Œ€๊ฐ€ ์‚ฌ์šฉ ๋ชปํ•  ์ •๋„์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹น Server๋ฅผ ๋–ผ์–ด๋‚ด๊ณ , ๋‹ค๋ฅธ Server๋ฅผ ๊ตฌ์ถ•ํ–ˆ์„ ๋•Œ, ์ž๋™์œผ๋กœ ํ•„์š”ํ•œ ์„ค์ •์ด๋‚˜, ํ™˜๊ฒฝ๋“ค์„ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•  ์ˆ˜ ์žˆ์–ด์š”.



์ฃผ๋‹ˆํ•˜๋ž‘์ด ๊ณต๋ถ€ํ•ด ๋ณผ Ansible์€ Agnet๊ฐ€ ํ•„์š” ์—†๋‹ค๋Š” ํŠน์ง•์ด ์žˆ์–ด์š”.
๊ฐ๊ฐ์˜ Server์— Python์ด ์„ค์น˜๋˜์–ด ์žˆ๋‹ค๋ฉด Python์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Network Module์„ ํ†ตํ•ด ๊ด€๋ฆฌ Server์™€ ํ†ต์‹ ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•  ์ˆ˜ ์žˆ์–ด์š”.

 

 

 

    ๐Ÿ”ฝ  Ansible

        ๐Ÿ“ฆ Ansible์ด๋ž€?

1. ์—ฌ๋Ÿฌ ๊ฐœ์˜ Server๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ํ™˜๊ฒฝ ๊ตฌ์„ฑ ์ž๋™ํ™” ๋„๊ตฌ
   โˆ™ Configuration Management, Deployment & Orchestration Tool.
   โˆ™ IT Infrastructure Automatically.

2. Push ๊ธฐ๋ฐ˜ Service

3. Simple, Agentless ๋ฐฉ์‹

4. Ansible์„ ํ†ตํ•ด ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ
   โˆ™ ์„ค์น˜ : apt-get, yum, homebrew ๋“ฑ.
   โˆ™ File & Script ๋ฐฐํฌ : copy.
   โˆ™ Download : get_url, git ๋“ฑ.
   โˆ™ ์‹คํ–‰ : shell, task





 

 

 

 

        ๐Ÿ“ฆ Ansible ์„ค์น˜

1. Ansible Server ์„ค์น˜ (RedHat ๊ธฐ๋ฐ˜ Linux)

yum install ansible
728x90


์ •์ƒ ์„ค์น˜ ๋ฐ Version ํ™•์ธ

ansible --version



2. ํ™˜๊ฒฝ ์„ค์ • ํŒŒ์ผ : /etc/ansible/ansible.cfg

3. Ansible์—์„œ ์ ‘์†ํ•˜๋Š” Host List(๋Œ€์ƒ ์„œ๋ฒ„ ๋ชฉ๋ก) : /etc/ansible/hosts


๊ฐ•์˜์—์„œ๋Š” ๊ฐ•์‚ฌ๋‹˜์ด ๊ตฌ์ถ•ํ•ด ๋†“์€ docker Container๊ฐ€ ์žˆ์–ด์š”.

 


์ฃผ๋‹ˆํ•˜๋ž‘์€ Linux ๊ธฐ๋ฐ˜์—์„œ ๋Œ์•„ํ•˜๋Š” Synology NAS์— Docker๋ฅผ ์ด์šฉํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์œ„์˜ Image๋ฅผ Pull ๋ฐ›๋„๋ก ํ•  ๊ฒƒ์ด์—์š”.




์œ„์˜ Image๋ฅผ ๋ฐ›์œผ์…จ๋‹ค๋ฉด Container ๊ธฐ๋™์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด ์ฃผ์‹œ๋ฉด ๋˜์š”.

sudo docker run --privileged -itd -p 20022:22 -p 8081:8080 -e container=docker -v /sys/fs/cgroup:/sys/fs/cgroup edowon0623/ansible:latest /usr/sbin/init
๋ฐ˜์‘ํ˜•





๊ทธ๋Ÿฐ ๋’ค ansible Container๋กœ ์—ฐ๊ฒฐํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด ์ฃผ์‹œ๋ฉด ๋˜์š”.

ssh root@localhost -p 20022 (or docker exec -it ansible-server bash)



root Password : P@ssw0rd


๋˜ํ•œ ํ•ด๋‹น Container ์•ˆ์—๋Š” Docker๊ฐ€ ์„ค์น˜ ๋˜์–ด ์žˆ๋Š”๋ฐ, ๊ธฐ๋™์ค‘์ด์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์€ ์ž‘์—…์ด ํ•„์š”ํ•ด์š”.


vim /etc/sysconfig/docker



์œ„์— 4๋ฒˆ์งธ ์ค„ OPTIONS๊ฐ€ ์žˆ๋Š” ๊ณณ์— --selinux-enabled=false๋กœ ์ˆ˜์ • ํ•ด ์ฃผ์„ธ์š”.

 

sed -i -e 's/overlay2/vfs/g' /etc/sysconfig/docker-storage


์œ„ ๋ช…๋ น์–ด๋Š” /etc/sysconfig/docker-storage์—์„œ ์•ž์— ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ์ž‘์—…์ด์—์š”.

์œ„์™€ ๊ฐ™์ด Docker๋ฅผ ๊ธฐ๋™ํ•ด ์ฃผ๋ฉด ๋˜์š”.







๐Ÿ’ก ์ฐธ๊ณ  ์‚ฌํ•ญ


์ตœ์ดˆ ์ฃผ๋‹ˆํ•˜๋ž‘์€ Sysnology NAS์— Docker๋ฅผ ์ด์šฉํ•˜์—ฌ Ubuntu 22.04 LTS Container๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.

์ด ๊ณณ์— ssh, net-tools, systemctl๋ฅผ ๋จผ์ € ์„ค์น˜ํ•ด ์ค„๊ฒŒ์š”.



Synology NAS์— SSH๋กœ ๋ถ™์€ ๋’ค Docker Container๋ฅผ ๋จผ์ € ํ™•์ธ ํ•ด ์ฃผ์—ˆ์–ด์š”.


๊ทธ๋Ÿฐ ๋’ค ํ•ด๋‹น Container ID๋ฅผ ํ†ตํ•ด ์ ‘์†ํ•ด ์ค๋‹ˆ๋‹ค.


Docker๋ฅผ ์ด์šฉํ•ด์„œ Ubuntu๋ฅผ ์ด์šฉํ•  ๋•Œ, ๋ช‡ ๊ฐ€์ง€ ์„ค์น˜ํ•ด์ค˜์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค์ด ์žˆ์–ด์š”.
๊ทธ ๋‚ด์šฉ์— ๋Œ€ํ•ด์„œ๋Š” ์ด ๊ณณ์— ์ค€๋น„ํ•ด ๋‘˜๊ฒŒ์š”.

๋˜ํ•œ, Ubuntu 22.04 LTS์— ansible์„ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์€ ์ด ๊ณณ์— ์ค€๋น„ํ•ด ๋‘˜๊ฒŒ์š”.

 









        ๐Ÿ“ฆ Ansible ์„ค์ •๊ณผ ์ž‘๋™ ๊ณผ์ •

์ตœ์ดˆ Docker Host Server์—์„œ ํ˜„์žฌ ๊ตฌ๋™์ค‘์ธ Container์— ์ •๋ณด๋ฅผ ํ™•์ธํ•ด ๋ณผ๊ฒŒ์š”.

sudo docker network inspect bridge


์œ„์™€ ๊ฐ™์ด IP ์ •๋ณด๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.



Ansible Container์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ๋Œ€์ƒ Container ๋“ค์„ ๋“ฑ๋กํ•ด ์ฃผ์—ˆ์–ด์š”.

vim /etc/ansible/hosts

 

 





        ๐Ÿ“ฆ Ansible ๊ธฐ๋ณธ ๋ช…๋ น์–ด

Ansible์—์„œ ๊ฐ๊ฐ์˜ ๋Œ€์ƒ Server์— SSH๋ฅผ ํ†ตํ•ด์„œ ์—ฐ๊ฒฐ์ด ๋˜๋„๋ก ํ•ด์•ผํ•ด์š”.
์ด ๋•Œ, ๋งค๋ฒˆ ๊ณ„์ •์„ ์ž…๋ ฅํ•˜๊ธฐ์—๋Š” ๋งค์šฐ ๋ฒˆ๊ฑฐ๋กญ๊ธฐ ๋•Œ๋ฌธ์— SSH Key๋ฅผ ์ƒ์„ฑํ•œ ๋’ค Ansible Container์—์„œ ๊ฐ๊ฐ์˜ ๋Œ€์ƒ Server์— ๋ฐฐํฌ๋ฅผ ํ•ด์ฃผ๋„๋ก ํ•  ๊ฒƒ์ด์—์š”.

ssh-keygen

 

์œ„์™€ ๊ฐ™์ด Ansible Container์—์„œ ssh Key๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.


ssh-copy-id {์ ‘์†ํ•  ๊ณ„์ •๋ช…}@{์ ‘์† Server IP์ฃผ์†Œ} -p {port ๋ฒˆํ˜ธ}

 

์œ„์™€ ๊ฐ™์ด Ansible Container์—์„œ ๋Œ€์ƒ Server๋กœ keygen์„ ํ†ตํ•ด ๋งŒ๋“ค์—ˆ๋˜ ๊ณต๊ฐœํ‚ค๋ฅผ ๋Œ€์ƒ ์„œ๋ฒ„์—๊ฒŒ ์ „๋‹ฌํ•ด ์ฃผ์—ˆ์–ด์š”.

์ด๋Ÿฐ ๋’ค ssh๋ฅผ ์ ‘์†ํ•œ๋‹ค๊ณ  ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ง„ํ–‰๋˜์š”.


root ๊ณ„์ •์— ์•”ํ˜ธ๋ฅผ ๋ฌป์ง€ ์•Š๊ณ , ๋ฐ”๋กœ ์ ‘์†๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.




์ด๋ฒˆ์—๋Š” Ansible Container ์ž๊ธฐ ์ž์‹ ์—๊ฒŒ๋„ ์œ„์™€ ๊ฐ™์ด ์ž‘์—…์„ ํ•ด ๋‘์—ˆ์–ด์š”.

 



Ansible Module - ์‹คํ–‰ Option

1. -i (--inventory-file) : ์ ์šฉ ๋  ๋Œ€์ƒ Server๋“ค์— ๋Œ€ํ•œ File ์ •๋ณด.
   โˆ™ ์ด Options์„ ์ด์šฉํ•˜๊ฒŒ ๋˜๋ฉด /etc/ansible/hosts๊ฐ€ ์•„๋‹Œ ์ง€์ •ํ•œ ์œ„์น˜์— File์„ ์ฝ์–ด ์ ์šฉ ๋Œ€์ƒ Server ๊ด€๋ฆฌ.

2. -m (--mocule-name) : Module ์„ ํƒ.

3. -k (--ask-pass) : ๊ด€๋ฆฌ์ž ์•”ํ˜ธ ์š”์ฒญ.

4. -K (--ask-become-pass) : ๊ด€๋ฆฌ์ž ๊ถŒํ•œ ์ƒ์Šน.

5. --list-hosts : ์ ์šฉ๋˜๋Š” ๋Œ€์ƒ Server ๋ชฉ๋ก ์ •๋ณด.

6. ๋ฉฑ๋“ฑ์„ฑ ํŠน์ง•
   โˆ™ ๊ฐ™์€ ์„ค์ •์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ ์šฉํ•˜๋”๋ผ๋„ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์ง€์ง€ ์•Š๊ณ , ํ•œ๋ฒˆ๋งŒ ์ ์šฉ๋˜๋Š” ์„ฑ์งˆ.
      - ์˜ˆ์‹œ:) echo -e "[junyharanggroup]\n172.20.10.22" >> /etc/ansible/hosts

 

 

cat /etc/ansible/hosts


๋ฉฑ๋“ฑ์„ฑ Test๋ฅผ ์œ„ํ•ด ์œ„์™€ ๊ฐ™์ด echo ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•ด์„œ ์•ž์— ๋‚ด์šฉ์„ /etc/ansible/hosts๋กœ ์‚ฝ์ž…ํ•ด ์ฃผ์—ˆ์–ด์š”.

์œ„ echo ๋ช…๋ น์–ด๋ฅผ ํ•œ๋ฒˆ ๋” ์‹คํ–‰ ํ•œ ๋’ค ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด


๋™์ผํ•œ ๊ฐ’์ด ๋‘ ๊ฐœ ๋“ค์–ด๊ฐ„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.









        ๐Ÿ“ฆ Ansible Module

Ansible Module ๋ฌธ์„œ


์ตœ์ดˆ Ansible Module์„ ํ•œ๋ฒˆ Test ํ•ด๋ณผ๊ฒŒ์š”.

ansible all -m ping

 

Ansible Container์— ๋“ฑ๋ก๋œ ๋Œ€์ƒ Server๋“ค์—๊ฒŒ ๋ชจ๋‘ ping Test๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ์Šต์ด์—์š”.

์œ„์— 172.17.0.5๋Š” ssh port๊ฐ€ ๋‹ฌ๋ผ์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ์—์š”.

์œ„์— ๋ช…๋ น์–ด์—์„œ all์€ group์„ ์ง€์ •ํ•˜๋Š” ๋ถ€๋ถ„์ด์—์š”. all์ด๋ผ๊ณ  ์“ฐ๋ฉด ๋ชจ๋“  group(/etc/ansible/hosts)์„ ์ง€์นญํ•˜๋Š” ๊ฒƒ์ด์—์š”.



์œ„์™€ ๊ฐ™์ด ํŠน์ • group ์ด๋ฆ„์„ ๋„ฃ์–ด์„œ Test ํ•ด๋ณผ ์ˆ˜ ์žˆ์–ด์š”.



์ด๋ฒˆ์—๋Š” ๊ฐ ๋Œ€์ƒ Server์—๊ฒŒ ๋ช…๋ น์–ด๋ฅผ ์ „๋‹ฌํ•ด ๋ณด๋„๋ก ํ• ๊ฒŒ์š”.

ansible all -m shell -a "free -h"



free -h ๋ช…๋ น์–ด๋Š” Memory ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ช…๋ น์–ด์—์š”.

์œ„์™€ ๊ฐ™์ด ๋Œ€์ƒ Server๋“ค์— Memory๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.



์ด๋ฒˆ์—๋Š” File์„ ํ•œ๋ฒˆ ์ „์†กํ•ด ๋ณผ๊ฒŒ์š”.


์ตœ์ดˆ test.txt๋ฅผ touch ๋ช…๋ น์–ด๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ , ์œ„์™€ ๊ฐ™์ด ๊ธ€์ž๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์—ˆ์–ด์š”.



๋Œ€์ƒ Server์— /tmp์— ์–ด๋–ค ๋‚ด์šฉ๋“ค์ด ์žˆ๋Š”์ง€ ๋จผ์ € ํ™•์ธํ•ด ์ค„๊ฒŒ์š”.
ํ˜„์žฌ๋Š” ์•„๋ฌด๊ฒƒ๋„ ์—†์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

ansible all -m copy -a "src=./text dest=/tmp"

 

์œ„ ๋ช…๋ น์–ด๋Š” ๋ชจ๋“  ๋Œ€์ƒ Server๋“ค์˜ group์— File์„ ๋ณต์‚ฌํ•  ๊ฑด๋ฐ, "ํ˜„์žฌ Directory์˜ text๋ผ๋Š” File์„ ๊ฐ๊ฐ์˜ ๋Œ€์ƒ Server์˜ /tmp Directory์— ๋ณต์‚ฌํ•ด๋ผ" ๋ผ๋Š” ์˜๋ฏธ์—์š”.



๋Œ€์ƒ Server์— File์ด ๋“ค์–ด์˜จ ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.




์ด๋ฒˆ์—๋Š” Program์„ ์„ค์น˜ํ•ด ๋ณด๋„๋ก ํ• ๊ฒŒ์š”.


๋จผ์ € Ubuntu ๋Œ€์ƒ Server์— httpd๊ฐ€ ์„ค์น˜ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ์„ ํ•ด ์ฃผ์—ˆ์–ด์š”.

 


๊ทธ๋ฆฌ๊ณ , RedHat Linux ๊ธฐ๋ฐ˜์ธ Ansible Container๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ™•์ธํ•ด ์ฃผ์—ˆ์–ด์š”.


ansible devops -m yum -a "name=httpd state=present"

 

์œ„์˜ ๋ช…๋ น์–ด๋Š” yum์„ ํ†ตํ•ด์„œ httpd๋ฅผ ์„ค์น˜ํ•˜๋ผ๋Š” ๋ช…๋ น์–ด์—์š”.

Ubuntu์—์„œ๋Š” yum ๋ช…๋ น์–ด๊ฐ€ ๋จน์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๊ณ , Ansible Container๋ฅผ CentOS์ด๊ธฐ ๋•Œ๋ฌธ์— ์ •์ƒ ์„ค์น˜๊ฐ€ ๋˜์—ˆ๋‹ค๊ณ  ๋‚˜์™€์š”.




 

Ansible Container์— httd ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์„ค์น˜ ๋˜์—ˆ์–ด์š”!

 

        ๐Ÿ“ฆ Ansible Playbook

1. ์‚ฌ์šฉ์ž๊ฐ€ ์›ํ•˜๋Š” ๋‚ด์šฉ์„ ๋ฏธ๋ฆฌ ์ž‘์„ฑํ•ด ๋†“์€ ํŒŒ์ผ
   โˆ™ ์˜ˆ์‹œ :) ์„ค์น˜, ํŒŒ์ผ ์ „์†ก, ์„œ๋น„์Šค ์žฌ์‹œ์ž‘ ๋“ฑ.
   โˆ™ ์˜ˆ์‹œ :) ๋‹ค์ˆ˜ ๋Œ€์ƒ Server๋“ค์— ๋ฐ˜๋ณต ์ž‘์—… ์ฒ˜๋ฆฌ  ์‹œ.

2. Playbook
   โˆ™ vim {playbook ์ด๋ฆ„}.yml ์ž‘์„ฑ.
   โˆ™ ansible-playbook {playbook ์ด๋ฆ„}.yml
   โˆ™ cat /etc/ansible/hosts


vim ~/first-playbook.yml


yaml ํ˜•์‹์œผ๋กœ ์œ„์™€ ๊ฐ™์ด Playbook์„ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.

๋จผ์ € ํ•ด๋‹น playbook์˜ ์ด๋ฆ„์„ name์œผ๋กœ ์ง€์ •์„ ํ•ด ์ฃผ์—ˆ๊ณ , Host ๋ถ€๋ถ„์—๋Š” ๋Œ€์ƒ Server์˜ IP๋ฅผ ๋„ฃ์„์ˆ˜๋„ ์žˆ๊ณ , /etc/ansible/hosts์— ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š” group ์ด๋ฆ„์„ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, ์ฃผ๋‹ˆํ•˜๋ž‘์€ group ์ด๋ฆ„์„ ๋„ฃ์–ด์ฃผ์—ˆ์–ด์š”.

tasks์— ์–ด๋–ค ์ž‘์—…์„ ํ•  ๊ฒƒ์ธ์ง€ ๋ช…์‹œ๋ฅผ ํ•  ๊ฒƒ์ธ๋ฐ, ์ž‘์—… ์ด๋ฆ„์„ name์—๋‹ค๊ฐ€ ์ž…๋ ฅํ•ด ์ฃผ๊ณ , ํ•ด๋‹น ์ž‘์—…์˜ ๋Œ€์ƒ ์œ„์น˜๋ฅผ path์— ์ง€์ •ํ•ด์ฃผ๊ณ , block์€ ๋ฐ˜๋“œ์‹œ |๋กœ ์‹œ์ž‘์„ ํ•ด์ฃผ์–ด์•ผ ํ•ด์š”. ๊ทธ๋ฆฌ๊ณ , ์ž‘์—… ๋‚ด์šฉ์„ ์ž‘์„ฑํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

ansible-playbook first-playbook.yml

 

playbook์„ ์‹คํ–‰ํ•˜๋ ค๊ณ  ํ–ˆ๋”๋‹ˆ ์œ„์™€ ๊ฐ™์ด ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๊ณ  ๋‚˜์™€์š”.


yaml ํ˜•์‹์€ ๋“ค์—ฌ์“ฐ๊ธฐ๊ฐ€ ๊ต‰์žฅํžˆ ์ค‘์š”ํ•ด์š”. ๋“ค์—ฌ์“ฐ๊ธฐ ๋ฌธ์ œ๋กœ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋˜ ๊ฒƒ์ด์—์š”.



์œ„์™€ ๊ฐ™์ด ์ˆ˜์ •์„ ํ•ด์ฃผ๊ณ , ๋‹ค์‹œ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•ด ์ค„๊ฒŒ์š”.


ansible-playbook first-playbook.yml




cat /etc/ansible/hosts


์œ„์™€ ๊ฐ™์ด playbook์—์„œ ์ž…๋ ฅํ•œ ๋‚ด์šฉ์ด ์ž…๋ ฅ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.


์ด๋ฒˆ์—๋Š” ๋ฉฑ๋“ฑ์„ฑ์— ๋Œ€ํ•ด Test ํ•ด๋ณผ๊ฒŒ์š”.

ansible-playbook first-playbook.yml


๋‹ค์‹œ ํ•œ๋ฒˆ playbook์„ ๋™์ž‘์‹œ์ผœ ๋ณด์•˜์–ด์š”.


cat /etc/ansible/hosts


๋Œ€์ƒ Server์—์„œ ๋‹ค์‹œ ํ™•์ธํ•ด ๋ณด๋ฉด ํ•ด๋‹น ๋‚ด์šฉ์ด ์ถ”๊ฐ€๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ , ํ•˜๋‚˜๋งŒ ์ž…๋ ฅ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.



๋˜ ๋‹ค๋ฅธ Test๋ฅผ ์ง„ํ–‰ํ•ด ๋ณผ๊ฒŒ์š”.

vim ~/playbook-sample1.yml

 

์œ„์™€ ๊ฐ™์ด yaml์„ ํ•˜๋‚˜ ๋” ๋งŒ๋“œ๋Š”๋ฐ, ์ด๋ฒˆ์—๋Š” copy๋ฅผ ํ†ตํ•ด ansible์— ~/(home Directory) ์•ˆ์— sample.txt File์„ ๊ฐ๊ฐ์˜ ๋Œ€์ƒ Server๋“ค์˜ /tmp ์•ˆ์— ๋ณต์‚ฌํ•˜๊ณ , ํ•ด๋‹น File์˜ ์†Œ์œ ์ฃผ๋ฅผ root๋กœ ํ•˜๊ณ , ๊ถŒํ•œ์„ 644(rw-r-r)๋กœ ์ฃผ๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์—์š”.




์ด ์ „์— ๋งŒ๋“ค์—ˆ๋˜ test.txt File ์ด๋ฆ„์„ sample.txt๋กœ ๋ฐ”๊พธ์–ด ์ฃผ์—ˆ์–ด์š”.


ls -al

 

๋Œ€์ƒ Server์—๋Š” ํ˜„์žฌ sample.txt๊ฐ€ ์—†๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.



ansible-playbook playbook-sample1.yml

 

์‹คํ–‰์„ ํ–ˆ๋Š”๋ฐ, ์œ„์™€ ๊ฐ™์ด ์‹คํŒจ๋ฅผ ํ•˜์˜€์–ด์š”.

์•„๋ฌด๋ž˜๋„ playbook-sample1์— ์˜คํƒ€๊ฐ€ ์žˆ๋Š” ๊ฑฐ ๊ฐ™์•„์š”


์ด ๋ถ€๋ถ„์— modE์— E ๋ถ€๋ถ„์„ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์„œ ๋‹ค์‹œ ์ง„ํ–‰ํ•ด ๋ณผ๊ฒŒ์š”.


 



ansible-playbook playbook-sample1.yml


์ •์ƒ ์ง„ํ–‰๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.





๋Œ€์ƒ Server๋ฅผ ํ™•์ธํ•ด ๋ณด๋‹ˆ sample.txt๊ฐ€ ์ƒ๊ธด๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.
์†Œ์œ ์ฃผ๋„ root์ด๊ณ , 644(rw-r-r)๋กœ ๊ถŒํ•œ์ด ์ ์šฉ ๋˜์—ˆ์–ด์š”!




์ด๋ฒˆ์—๋Š” Program์„ ๋‚ด๋ ค๋ฐ›๊ธฐ ํ•˜๋Š” ์˜ˆ์ œ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณผ๊ฒŒ์š”.

vim ~/playbook-sample2.yml

 

์ด๋ฒˆ์—๋Š” tasks์˜ name์ด ๋‘ ๊ฐœ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์–ด์š”.
์ด ์˜๋ฏธ๋Š” ์ž‘์—…์„ ๋‘ ๊ฐ€์ง€๋ฅผ ํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์—์š”.

๋จผ์ € ์ฒซ๋ฒˆ์งธ ์ž‘์—…์€ /opt/tomcat9 ์ด๋ผ๋Š” Directory๋ฅผ ๋งŒ๋“œ๋Š” ๋ถ€๋ถ„์ด๊ณ , ๊ถŒํ•œ์„ 755(rwxr-xr-x)๋ฅผ ์ฃผ๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์—์š”.

๊ทธ๋ฆฌ๊ณ , ๋‘๋ฒˆ์งธ์—์„œ๋Š” url์—์„œ tar File์„ ๋‚ด๋ ค ๋ฐ›์„๊ฑด๋ฐ, ๋ฐ›์„ ์œ„์น˜๋Š” /opt/tomcat9 ์ด๋ผ๋Š” ์˜๋ฏธ์ด๊ณ , ์ด ์—ญ์‹œ ๊ถŒํ•œ์€ 755(rwxr-xr-x)๋กœ ํ•  ๊ฒƒ์ด๋ผ๋Š” ๋œป์ด๊ณ , ํ•ด๋‹น File์ด ์ •์ƒ(์œ„, ๋ณ€์กฐ ์—ฌ๋ถ€)์ธ์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด checksum์„ ์ด์šฉํ•ด์„œ ํ™•์ธํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์—์š”.

 

 


๋จผ์ € ๋Œ€์ƒ Server์— ํ•ด๋‹น Directory๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ๋ณด์•˜์–ด์š”.



ansible-playbook playbook-sample2.yml



๋Œ€์ƒ ํ•˜๋‚˜๋Š” ์„ฑ๊ณตํ•˜๊ณ  ํ•˜๋‚˜๋Š” ์‹คํŒจํ•˜์˜€์–ด์š”.


์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๋œ ๋Œ€์ƒ Server์—๋Š” ์œ„์™€ ๊ฐ™์ด tomcat ์„ค์น˜ ์••์ถ• File์ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์–ด์š”.



 





    ๐Ÿ”ฝ  Ansible๊ณผ Jenkins

        ๐Ÿ“ฆ ์—ฐ๋™ํ•˜๊ธฐ

์ด๋ฒˆ์—๋Š” Jenkins์—์„œ Ansible์„ ์—ฐ๋™ํ•ด ๋ณผ๊ฒŒ์š”.


์ตœ์ดˆ Jenkins ๊ด€๋ฆฌ์—์„œ ์‹œ์Šคํ…œ ์„ค์ •์œผ๋กœ ๋“ค์–ด๊ฐ€ ์ค„๊ฒŒ์š”.



๊ธฐ์กด์— ์‹ค์Šตํ–ˆ๋˜ ๋‚ด์šฉ์€ ์ง€์šฐ๊ณ , ์ƒˆ๋กญ๊ฒŒ ์ด ๋ถ€๋ถ„์— ์ž…๋ ฅ์„ ํ•ด ์ค„๊ฒŒ์š”.


hostname -i


๊ทธ ์ „์— Ansible Container์˜ IP๋ฅผ ํ™•์ธํ•ด ์ฃผ์—ˆ์–ด์š”.





์œ„์™€ ๊ฐ™์ด ์ž…๋ ฅ์„ ํ•ด์ฃผ๊ณ , Test Configuration์„ ๋ˆŒ๋Ÿฌ์ฃผ๋‹ˆ Success๊ฐ€ ๋œฌ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.

 

 

        ๐Ÿ“ฆ Jenkins + Ansible Playbook ์‚ฌ์šฉํ•˜๊ธฐ

์ตœ์ดˆ ์œ„์™€ ๊ฐ™์ด ์ƒˆ๋กœ์šด Item์„ ํ•˜๋‚˜ ์ƒ์„ฑํ• ๊ฒŒ์š”.

์ด์ „์— Docker Project์—์„œ ๋ช‡๊ฐ€์ง€๋งŒ ์ˆ˜์ •ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— Copy Item์—์„œ Docker Project Item ๋‚ด์šฉ์„ ๊ฐ€์ ธ์˜ค๋„๋ก ํ• ๊ฒŒ์š”.


๋นŒ๋“œ ํ›„ ์กฐ์น˜์—์„œ ๊ธฐ์กด์— ์žˆ์—ˆ๋˜ ๋‚ด์šฉ์€ ๋ชจ๋‘ ์ง€์›Œ์ค„๊ฒŒ์š”.


๊ทธ๋Ÿฐ ๋’ค Send Build ... ๋ฅผ ์„ ํƒํ•ด ์ค๋‹ˆ๋‹ค.




Name ๋ถ€๋ถ„์€ Jenkins ๊ด€๋ฆฌ ์‹œ์Šคํ…œ ์„ค์ •์—์„œ SSH ๋Œ€์ƒ Container๋ฅผ ์œ„์™€ ๊ฐ™์ด ansible-host๋ผ๊ณ  ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”. ๊ทธ๊ฑธ ์„ ํƒํ•ด ์ฃผ๊ณ ,

Source files์—๋Š” /taget์— ๋ชจ๋“  war File์„ ์ง€์ •ํ•˜๊ณ , Remove prefix์—๋Š” target Directory๋Š” ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ง€์ •ํ•ด์„œ ๋ณต์‚ฌ๋˜์ง€ ์•Š๋„๋ก ํ•ด์ฃผ์—ˆ์–ด์š”.

Remote Directory๋Š” Ansible Container์— war File์„ ์–ด๋””์— ์ €์žฅํ• ๊ฑด์ง€๋ฅผ ์ •ํ•˜๋Š” ๊ณณ์ธ๋ฐ, ssh๋ฅผ root๋กœ ์ ‘์†ํ•˜๊ฒŒ ๋˜๋ฉด ๋ฐ”๋กœ /root Directory๋กœ ์ ‘์†ํ•˜๊ฒŒ ๋˜์š”. ๊ทธ๋ž˜์„œ ๊ทธ๋ƒฅ ๊ทธ ๊ณณ์„ ์ง€์ •ํ•ด ์ค€ ๊ฒƒ์ด์—์š”.

์ด๋ ‡๊ฒŒ๋งŒ ํ•˜๊ณ , ์ผ๋‹จ Apply -> Save ํ•ด์ค„๊ฒŒ์š”.




Build๋ฅผ ํ•ด๋ณด๋‹ˆ ์ •์ƒ์ ์œผ๋กœ Build๊ฐ€ ๋˜์—ˆ์–ด์š”.


 
Ansible Container์— war File์ด ์ƒ๊ธด๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.


vim ~/first-devops-playbook1.yml

 

Ansible Container์— ์ƒˆ๋กญ๊ฒŒ playbook์„ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.
์ด playbook์€ war File์ด Jenkins๋ฅผ ํ†ตํ•ด ์˜ค๋ฉด ๊ทธ๊ฑธ ๊ฐ€์ง€๊ณ , docker Image๋ฅผ buildํ•˜๋Š” ๋ช…๋ น์–ด๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์–ด์š”.

๋งˆ์ง€๋ง‰์œผ๋กœ Target Directory๋Š” root Directory๋กœ ์„ค์ •์„ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด์—์š”.

hosts๋Š” ๋ชจ๋“  group๋ฅผ ๋Œ€์ƒ์œผ๋กœ ํ–ˆ์–ด์š”.


vim ~/Dockerfile

 

/root Directory์— Dockerfile์„ ๋งŒ๋“ค์–ด ์ฃผ๋Š”๋ฐ ์œ„์™€ ๊ฐ™์ด ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”. tomcat 9.0 Version์„ ๊ฐ€์ง€๊ณ , ํ˜„์žฌ Directory์— ์žˆ๋Š” hello-world.war File์„ docker๋ฅผ ๋งŒ๋“ค์–ด ๊ทธ ์•ˆ์— /usr/local/tomcat/webapps์— ๋ณต์‚ฌํ•ด๋ผ ๋ผ๋Š” ๋‚ด์šฉ์ด์—์š”.



vim ~/hosts

 

์ด๋ฒˆ์—๋Š” /etc/ansible/hosts๊ฐ€ ์•„๋‹Œ ์œ„์™€ ๊ฐ™์ด hosts๋ฅผ ๋งŒ๋“ค์–ด Ansible Container ์ž๊ธฐ ์ž์‹ ์„ ๋“ฑ๋กํ•ด ์ฃผ์—ˆ์–ด์š”.



ansible-playbook -i hosts first-devops-playbook.yml

์œ„์—์„œ ๋งŒ๋“  playbook์„ ์‹คํ–‰ ์‹œ์ผœ ๋ณด์•˜์–ด์š”.

-i hosts๋ผ๋Š” ๋ถ€๋ถ„์€ /etd/ansible/hosts๋ฅผ ์“ธ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ฃผ๋‹ˆํ•˜๋ž‘์ด ์œ„์—์„œ ๋งŒ๋“  hosts๋ฅผ ์“ฐ๊ฒ ๋‹ค๋Š” ์˜๋ฏธ์—์š”.


docker images


์œ„์™€ ๊ฐ™์ด docker image๋งŒ ์ •์ƒ์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.




์ด๋ฒˆ์—๋Š” Docker Container๊นŒ์ง€ ์ƒ์„ฑํ•ด ๋ณผ๊ฒŒ์š”.

vim ~/first-devops-playbook.yml


์œ„์—์„œ ๋งŒ๋“  cicd-project-ansible ์ด๋ผ๋Š” ์ด๋ฆ„์˜ image๋ฅผ ๊ฐ€์ง€๊ณ , Container๋ฅผ ๋งŒ๋“œ๋Š” ๋ช…๋ น์–ด๋ฅผ ์ถ”๊ฐ€ํ•ด ์คฌ์–ด์š”.


-d ๋Š” Background์—์„œ ์ž‘์—…์„ ํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๊ณ , --name Options ๋’ค์— ํ•ด๋‹น Container ์ด๋ฆ„์„ ๋„ฃ์–ด์ฃผ๊ณ , Port Fowardingํ•  ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด -p Options ๋’ค์— ์ž…๋ ฅ์„ ํ•ด์ฃผ๊ณ , Image ์ด๋ฆ„์„ ๋„ฃ์–ด์ฃผ์—ˆ์–ด์š”.




ํ˜„์žฌ ์กด์žฌํ•˜๋Š” Docker Image ๋“ค์„ ํ™•์ธํ•ด ์ฃผ์—ˆ๊ณ , ๊ธฐ๋™์ค‘์ธ Container๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ด ์ฃผ์—ˆ์–ด์š”.



ansible-playbook -i hosts first-devops-playbook.yml


์œ„์™€ ๊ฐ™์ด ์ž‘์—…์ด ์„ฑ๊ณตํ•˜์˜€์–ด์š”!




Docker Container ๊นŒ์ง€ ์ •์ƒ์ ์œผ๋กœ ๋งŒ๋“ค์–ด ์ง„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.




์ด์ œ ์œ„ ๋‚ด์šฉ์ด ์ž๋™ํ™” ์ฒ˜๋ฆฌ๊ฐ€ ๋˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด Jenkins์— ํž˜์„ ๋นŒ๋ฆฌ๋ ค๊ณ  ํ•ด์š”.
๊ทธ ์ „์— ๋งŒ๋“ค์–ด์ง„ Image์™€ Container๋Š” ๋ชจ๋‘ ์ง€์›Œ์ฃผ๋„๋ก ํ• ๊ฒŒ์š”.


๋จผ์ € Docker Container๋ฅผ ์ค‘์ง€์‹œํ‚ค๊ณ , ์‚ญ์ œ ํ•ด์ฃผ์—ˆ์–ด์š”.

๊ทธ๋Ÿฐ ๋’ค Image๊นŒ์ง€ ์‚ญ์ œํ•ด ์ค€ ๋ชจ์Šต์ด์—์š”.



์ž˜ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด ์ฃผ์—ˆ์–ด์š”.




ํ•ด๋‹น Item์˜ ๊ตฌ์„ฑ์— ๋“ค์–ด์™€์„œ Exec command์— ์•„๋ž˜ ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์–ด์š”.

ansible-playbook -i hosts first-devops-playbook.yml



์ด์ œ ์ €์žฅ์„ ํ•œ ๋’ค ๋‹ค์‹œ Build๋ฅผ ํ•ด ๋ณผ๊ฒŒ์š”.



์ž‘์—…์ด ์„ฑ๊ณต์ ์œผ๋กœ ๋๋‚ฌ๋‹ค๊ณ  ํ•ด์š”!



Docker Image๋ž‘ Container๋„ ์ •์ƒ์ ์œผ๋กœ ์ƒ์„ฑ๋œ ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”!




์ด๋ ‡๊ฒŒ Web Service๋„ ์ •์ƒ์ ์œผ๋กœ ๊ตฌ๋™๋˜๋Š” ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.





์ด๋ฒˆ์—๋Š” Github์—์„œ push even๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ด ์žˆ๋‹ค๋ฉด ์ž๋™์œผ๋กœ Build๊ฐ€ ๋˜๊ฒŒ ์„ค์ •ํ•ด ์ค„๊ฒŒ์š”.



ํ•ด๋‹น Project ๊ตฌ์„ฑ์— Poll SCM์— ์œ„์™€ ๊ฐ™์ด ์„ค์ •์„ ํ•ด ์ฃผ์—ˆ์–ด์š”.




์œ„์— 5๋ฒˆ์งธ, 11 ~ 12๋ฒˆ์งธ Code๋ฅผ ์•ฝ๊ฐ„ ์ˆ˜์ •ํ•ด ๋ณด์•˜์–ด์š”.

Github์— push๋ฅผ ํ•ด ๋ณผ๊ฒŒ์š”!






์ง€๊ธˆ ๋นŒ๋“œ๋ฅผ ๋ˆ„๋ฅด์ง€ ์•Š์•˜์ง€๋งŒ, ์œ„์™€ ๊ฐ™์ด ์ž๋™์œผ๋กœ Build ์ž‘์—…์ด ๊ฑธ๋ฆฌ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.



 

ํ•˜์ง€๋งŒ Build๋Š” ์‹คํŒจํ•˜๊ณ  ๋ง์•˜์–ด์š”.

์‹คํŒจํ•œ ์ด์œ ๋Š” ๊ธฐ์กด์— ๊ธฐ๋™์ค‘์ธ Docker Container๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ, ๋™์ผํ•œ ์ด๋ฆ„์œผ๋กœ Container๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐœ์ƒํ•œ ๋ฌธ์ œ์—์š”.


vim ~/first-devops-playbook.yml


์œ„์™€ ๊ฐ™์ด Docker Container๋ฅผ ์ค‘์ง€ํ•˜๊ณ , ์‚ญ์ œํ•˜๊ณ , Image ๋˜ํ•œ ์‚ญ์ œํ•˜๋Š” ๋ช…๋ น์–ด๋ฅผ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ์–ด์š”.

ignore_errors: yes๋Š” ๋งŒ์•ฝ ์ค‘์ง€ ๋ช…๋ น์–ด๋ฅผ ๋‚ด๋ ธ๋Š”๋ฐ, ์ค‘์ง€ํ•  ๋Œ€์ƒ Container๊ฐ€ ์—†์œผ๋ฉด Error๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜์š”. ์ด ๋•Œ ์ด Error๋ฅผ ๋ฌด์‹œํ•˜๊ฒŒ ํ•ด์„œ ์ž‘์—…์„ ๊ณ„์† ์ด์–ด ๋‚˜๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€ํ•œ ๋ถ€๋ถ„์ด์—์š”.



Code๋ฅผ ์‚ด์ง ์ˆ˜์ •ํ•ด์„œ ๋‹ค์‹œ push๋ฅผ ํ•ด์ฃผ์—ˆ์–ด์š”.




์ž‘์—…์ด ์„ฑ๊ณตํ•˜์˜€์–ด์š”!



Docker Image์™€ Container๋„ ๋งŒ๋“ค์–ด์ง„์ง€ ์–ผ๋งˆ ์•ˆ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.



Web Service๋„ ๊ธ€์ž๊ฐ€ ๋น„๋ก ๊นจ์กŒ์ง€๋งŒ, ์ •์ƒ ๊ตฌ๋™ ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”!

 









        ๐Ÿ“ฆ Use Ansible Docker Image Management

์ด๋ฒˆ์—๋Š” ์ฃผ๋‹ˆํ•˜๋ž‘์ด ์ž‘์—…ํ•œ Docker Image๋ฅผ Docker Hub์— Upload ํ•˜๋Š” ์ž‘์—…์„ ํ•ด ๋ณผ๊ฒŒ์š”.


ํ˜„์žฌ ์ฃผ๋‹ˆํ•˜๋ž‘์— Docker Hub Repository์—๋Š” ์•„๋ฌด๊ฒƒ๋„ ์—†์–ด์š”.


docker tag {Docker Image ์ด๋ฆ„} {Docker Hub ๊ณ„์ •}/{Docker Image ์ด๋ฆ„}


์ตœ์ดˆ ์œ„์™€ ๊ฐ™์ด Docker Hub ๊ณ„์ •๋ช…์„ ์ด์šฉํ•œ Image๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.

docker images

 

docker login


๊ทธ๋Ÿฐ ๋’ค docker hub์— Loginํ•˜๊ธฐ ์œ„ํ•ด ์œ„์™€ ๊ฐ™์ด ์ž‘์—…์„ ํ•ด ์ฃผ์—ˆ์–ด์š”.


docker push {Docker Hub ๊ณ„์ •}/{Docker Image ์ด๋ฆ„}


์œ„์˜ ๋ฐฉ๋ฒ•์œผ๋กœ Image๋ฅผ Docker Hub์— ์˜ฌ๋ ค์ค„ ์ˆ˜ ์žˆ์–ด์š”.



๋‹ค์‹œ Docker Hub๋ฅผ ํ™•์ธํ•ด ๋ณด๋‹ˆ ์œ„์™€ ๊ฐ™์ด Image๊ฐ€ ์˜ฌ๋ผ๊ฐ„ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.


์œ„ ์ž‘์—…์„ ์ž๋™ํ™” ํ•˜๊ธฐ ์œ„ํ•ด ์ƒˆ๋กœ์šด playbook์„ Ansible Container์— ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.

vim ~/create-cicd-project-image-playbook.yml





๊ทธ๋Ÿฐ ๋’ค playbook์„ ์‹คํ–‰ ์‹œ์ผœ ์ฃผ๋‹ˆ ์œ„์™€ ๊ฐ™์ด ์ •์ƒ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๊ฐ€ ๋˜์—ˆ์–ด์š”!



Docker Hub์—๋„ Image๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์˜ฌ๋ผ๊ฐ”์–ด์š”!



์ด๋ฒˆ์—๋Š” Container๋ฅผ ์ƒ์„ฑํ•˜๋Š” Playbook ์ž‘์—…์„ ํ•ด๋ณผ๊ฒŒ์š”.



์ตœ์ดˆ ์œ„์™€ ๊ฐ™์ด ๊ธฐ์กด์— ๋งŒ๋“ค์—ˆ๋˜ playbook File ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•ด ์ฃผ์—ˆ์–ด์š”.

vim ~/create-cicd-devops-container.yml


์ˆ˜์ •ํ•œ ๋‚ด์šฉ์€ Docker Hub์—์„œ Image๋ฅผ Pull ๋ฐ›์•„ ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด ์ฃผ์—ˆ๊ณ , Docker๋ฅผ ๊ธฐ๋™ํ•  ๋•Œ, Image ์ด๋ฆ„์— ์ฃผ๋‹ˆํ•˜๋ž‘์˜ ๊ณ„์ •๋ช…์„ ๋ถ™ํ˜€์ค€ ๊ฒƒ์ด์—์š”.



๊ธฐ์กด์— ๋ฐ›์•˜๋˜ Image๋Š” ์ผ๋‹จ ์ œ๊ฑฐ๋ฅผ ํ•ด ์ฃผ์—ˆ์–ด์š”.



Docker๊ฐ€ ์„ค์น˜๋œ CentOS ๊ธฐ๋ฐ˜ Container๋ฅผ ๋Œ€์ƒ Server๋กœ Testํ•˜๊ธฐ ์œ„ํ•ด ์ค€๋น„ํ•ด ๋‘์—ˆ๊ณ , Docker๋ฅผ ๊ธฐ๋™์‹œ์ผœ ์ฃผ์—ˆ์–ด์š”.


ํ•ด๋‹น Container์—๋Š” ๊ธฐ๋™์ค‘์ธ Container๋‚˜, Image๋Š” ์—†์–ด์š”.


์ค€๋น„๋œ ๋Œ€์ƒ Container๋Š” ์œ„์™€ ๊ฐ™์€ IP๋ฅผ ์“ฐ๊ณ  ์žˆ๊ณ , Ansible์— Customํ•˜๊ฒŒ ๋งŒ๋“  hosts ํŒŒ์ผ์— ๋“ฑ๋ก์‹œ์ผœ ์ค„๊ฒŒ์š”.

vim ~/hosts


์œ„์™€ ๊ฐ™์ด ๋“ฑ๋ก์„ ํ•ด ์ฃผ์—ˆ์–ด์š”.

 




๋จผ์ € ์ง„ํ–‰ํ•˜๋ ค๊ณ  ํ•˜๋Š” ์ž‘์—…์€ Docker Image๋ฅผ ๋งŒ๋“ค๊ณ , Pushํ•˜๋Š” ๋ช…๋ น์–ด๊ฐ€ ๋“ค์–ด๊ฐ„ playbook์„ ์‹คํ–‰์‹œํ‚ฌ๊ฑด๋ฐ, hosts์— ๋“ฑ๋ก๋œ ๋ชจ๋“  Server์— ํ•ด๋‹น ์ž‘์—…์ด ๋จน์ง€ ์•Š๊ณ , Ansible Container๋งŒ ํ•ด๋‹น ๋ช…๋ น์–ด๊ฐ€ ๋จน๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด --limit Option๊ณผ ํ•จ๊ป˜ Container IP๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ์–ด์š”.




์œ„์™€ ๊ฐ™์ด Ansible Container์—์„œ๋งŒ ์ž‘์—…์ด ์ง„ํ–‰๋˜์—ˆ๊ณ , Image๋ฅผ ๋งŒ๋“ค๊ณ , Docker Hub์— Pushํ•ด ์ฃผ๊ณ , ์ง€์›Œ์ฃผ๋Š” ์ž‘์—…์„ ํ•ด ์ฃผ์—ˆ์–ด์š”.




Image๊ฐ€ ๋ฐฉ๊ธˆ ์˜ฌ๋ผ์˜จ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.





์ด๋ฒˆ์—๋Š” ์ƒˆ๋กœ ๋งŒ๋“ค์—ˆ๋˜ Docker Container์— ๊ธฐ์กด์— ๊ตฌ๋™์ค‘์ด๋˜ Docker Container ์ค‘์ง€, ์‚ญ์ œ Image ์‚ญ์ œ๋ฅผ ํ•˜๊ณ  ๋‚œ ๋’ค Docker Hub์—์„œ Image๋ฅผ ๋ฐ›๊ณ , ๊ตฌ๋™ ์‹œํ‚ค๋Š” ์ž‘์—…์ด ๋“ค์–ด๊ฐ„ Playbook์„ ์‹คํ–‰ ์‹œ์ผœ ๋ณด์•˜์–ด์š”.

์œ„์˜ Error๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์€ ์ค‘์ง€, ์‚ญ์ œํ•  ๋Œ€์ƒ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— Error๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”๋ฐ, Ignore_Error๋ฅผ yes๋กœ ์ฃผ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ •์ƒ์ธ ๊ฒƒ์ด์—์š”.



์œ„์™€ ๊ฐ™์ด Docker Container์— ์ •์ƒ์ ์œผ๋กœ Image์™€ Container๊ฐ€ ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.




ํ•ด๋‹น ์ž‘์—…์„ ๋‹ค์‹œ ์‹ค์‹œํ•˜๊ฒŒ ๋˜๋ฉด Docker Container์™€ Image๊ฐ€ ์กด์žฌํ•˜๊ณ  ์žˆ๋Š” ์ƒํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— Error ์—†์ด ์ฒ˜๋ฆฌ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.




        ๐Ÿ“ฆ Use Ansible Playbook Create Docker Container

Jenkins๋ฅผ ์ด์šฉํ•ด์„œ ์œ„์— ์ž‘์—…๋“ค์„ ์ž๋™ํ™” ํ•ด ๋ณด๋„๋ก ํ• ๊ฒŒ์š”.

๋จผ์ € ์ƒˆ๋กœ์šด Item์„ ๋งŒ๋“ค์–ด ์ฃผ๋„๋ก ํ• ๊ฒŒ์š”.




Exec command ๋ถ€๋ถ„์— ์œ„์—์„œ Testํ•œ ๋ช…๋ น์–ด๋“ค์„ ๋„ฃ์–ด์ค„๊ฒŒ์š”.

๊ทธ ์™ธ์— ๋‚ด์šฉ๋“ค์€ ๋ฐ”๋€Œ์ง€ ์•Š์„ ๊ฒƒ์ด์—์š”.




Code๋ฅผ ์‚ด์ง ๋˜ ์ˆ˜์ •ํ•˜๊ณ , Push๋ฅผ ํ•ด ์ฃผ์—ˆ์–ด์š”.



์ž‘์—…์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ˆ˜ํ–‰ ๋˜์—ˆ์–ด์š”!




Docker Container์—๋„ ์ •์ƒ์ ์œผ๋กœ ์ž‘์—…์ด ์™„๋ฃŒ ๋œ ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์–ด์š”.



 

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•