2022. 7. 6. 17:40ㆍCloud/Ansible
server의 갯수가 1, 2 대 라면 관리자 한명이 붙어서 관리하면 되지만, 100개, 1000개가 된다면 관리하기가 굉장히 힘들 것 다. 클러스터에 존재하는 많은 서버들에게 동일한 환경을 배포, 제공해야한다. 이럴때 IaaC개념을 활용해 나타난 기능이 앤서블 이라는 기능이다.
앤서블은 여러 개의 서버를 효율적으로 관리하게 할 수 있는 환경 구성 자동화 도구를 의미한다. 오픈스소로써 무료로 사용할 수 있다. 플레이북 이라는 파일에 실행할 구성을 미리 선언해 두면 필요할대마다 자동으로 실행 시킬 수 있는 것이 가장 큰 특징이다. WEB server의 구성과 DB server의 구성을 미리 선언한다면, 관리자들은 필요할때마다 그 구성대로 서버의 설정을 배포할 수 있게 해 주는것이다.
앤서블의 3요소
인벤토리의 수행위치
인벤토리는 앤서블에 의해 제어될 대상을 정의한다. 일반적으로 hosts.ini파일에 정의되며, 여러 서버들의 ssh접근, ip포트, 리눅스 사용자와 같은 접속 정보를 정의한다.
플레이북이 수행할 명령
플레이북(각본)은 인벤토리 파일에서 정의한 대상들이 무엇을 수행할 것인지 정의하는 역할을 하며, yaml포맷으로 설정한다. 앤서블을 사용하려면 반드시 인벤토리와 플레이북의 조합을 할 줄 알아야 한다.
모듈의 수행
모듈은 플레이북에서 어떻게 task가 수행되는지를 정의하는 요소를 의미한다. 타겟 호스트로 실제 작업을 처리하는 단위로 이 모듈이라는 개념을 활용한다. 앤서블은 기본적으로 python code를 호출하여 실행하기에 python이 필수적으로 필요하다.
베이그런트
VM을 간편하게 만들어주는 도구이다. Ruby로 구축되었으며, 일종의 오픈소스 제품이다. vmware, 버츄얼박스 등의 도구를 좀 더 쉽게 만들 수 있도록 도와주는 도구이다.
Vagrant by HashiCorp
Vagrant enables users to create and configure lightweight, reproducible, and portable development environments.
www.vagrantup.com
베이그런트를 사용하고자 한다면 설치 후 CMD창을 열어서 아래의 명령어를 실행하도록 하자.
>cd c:\HashiCorp
>vagrant init
https://app.vagrantup.com/boxes/search?provider=virtualbox
>notepad Vagrantfile
==> 매모장 수정
config.vm.box = "centos/7"
config.vm.network "public_network"
>vagrant up ==> up, 즉 사용가능하도록 함
>vagrant ssh ==> 베이그런트를 접속가능
>vagrant destroy ==> 베이그런트에서 생성한 VM삭제
실습에서는 총 5개의 서버를 사용할것이며 각각의 스팩은 아래와 같다.
VM : Oracle VM Virtualbox
OS : Centos7_mini, ubuntu18
CUP : 1 Core
RAM : 1024 MB
HDD : 128GB(동적할당)
main Ansible server : Centos7
server name : Ansible-node01,02, ubnut-node01,02, Ansible-server
ip
Ansible-server 192.168.1.23/20
Ansible-node01 192.168.1.22/20
Ansible-node02 192.168.1.21/20
ubnut-node01 192.168.0.229/20
ubnut-node02 192.168.1.24/20
이 후 아래의 명령어를 실행하 설치하도록 하자.
# yum install epel-release -y
# yum --enablerepo=epel -y install ansible
# ansible --version
인벤토리 설정을 해 보자.
# vi /etc/ansible/hosts
로 접근 후 최 하단에 그룹과 ip를 부여하도록 하자.
# ansible all -m ping
==> hosts내부의 모든 ip에 대해 ping을 할 것이다. 최초로 사용시 붉은색의 표시가 나타나는데 이때 YES라는 know_hosts에 적용시키는 과정을 거쳐야한다.
# ansible all -m ping -k
==> -k= ssh의 password를 물어본다
# ansible centos -m ping -k
==> hosts내부의 모든 centos라는 그룹에 대해 ping을 할 것이다.
# ansible ubuntu -m ping -k
==> hosts내부의 모든 ubunut라는 그룹에 대해 ping을 할 것이다.
# echo "192.168.1.22" >> inventory.list
==> inventory.list에 192.168.1.22(centos7) ip를 등록
# echo "192.168.0.229" >> inventory.list
==> inventory.list에 192.168.0.229(ubnunt) ip를 등록
# ansible all -i inventory.list -m ping -k
inventory.list에 있는 모든 ip로 ssh password ping을 보낸다.
# ansible all --list-hosts
==> hosts내부의 모든 hosts를 확인한다.
# ansible all -m shell -a "uptime" -k
==> shell이라는 모듈을 통해서 -a(인수)를 사용할건데 그 인수는 uptime이다.
# ansible all -m shell -a "df -h" -k
==> df -h 명령어를 사용한다.
# ansible all -m user -a "name=kosa" -k
모든 서버에 kosa라는 user를 만든다.
# ansible all -m shell -a "tail -n 1 /etc/passwd" -k
==> 만들어진 계정을 확인한다.
# ansible all -m user -a "name=kosa state=absent" -k
==> kosa라는 계정을 지운다.
# ansible all -m yum -a "name=httpd state=present" -k
==> httpd 설치하기
# curl https://www.nginx.com/ -o index.html
==> index.html이라고 파일 추출하기.
# ansible centos -m copy -a "src=index.html dest=/var/www/html/index.html" -k
==> 추출된 파일을 centos그룹에 있는 var/www/html/index.html로 배포
# ansible centos -m service -a "name=httpd state=started" -k
==>centos그룹에 있는 http 서비스 실행
# ansible centos -m shell -a "systemctl status firewalld" -k
==> 방화벽 상태확인
# ansible centos -m shell -a "systemctl start firewalld" -k
==> 방화벽 실행하기
# ansible centos -m shell -a "firewall-cmd --permanent --zone=public --add-service=http" -k
==> firewall-cmd --permanent --zone=public --add-service=http 명령어 실행
# ansible ubuntu -m apt -a "name=apache2 state=present" -k
==> 우분투 그룹에 apache2를 설치
# curl https://www.nginx.com/ -o index.html
# ansible ubuntu -m copy -a "src=index.html dest=/var/www/html/index.html" -k
==> 상동
# ansible ubuntu -m service -a "name=apache2 state=stopped" -k
==> 아파치 멈추기
# ansible ubuntu -m service -a "name=apache2 state=started" -k
# ansible ubuntu -m apt -a "name=apache2 state=absent" -k
==> 아파치 제거하기
ssh key를 활용해 -k를 입력할 때마다 비밀번호를 입력하지 않아도 된다.
# ssh-keygen -t rsa
# ssh-copy-id root@192.168.1.22
# ssh-copy-id root@192.168.0.229
플레이 북 만들기
# vi apache_install.yml
==> yml파일을 생성해 만든다.
---
- name: Install apache on centos
hosts: centos
gather_facts: no
tasks:
- name: install apache web server
yum: name=httpd state=present
- name: upload default index.html for web server
get_url: url=https://www.nginx.com dest=/var/www/html/ mode=0644
- name: start apache web server
service: name=httpd state=started
- name: Install apache on ubuntu
hosts: ubuntu
gather_facts: no
tasks:
- name: install apache web server
apt: name=apache2 state=present
- name: upload default index.html for web server
get_url: url=https://www.nginx.com dest=/var/www/html/ mode=0644
- name: start apache web server
service: name=apache2 state=started
이 후 플레이북 명령어로 실행한다.
# ansible-playbook apache_install.yml
삭제는 아래의 명령어를 사용하면 된다.
# vi apache_remove.yml
- name: Remove apache on centos
hosts: centos
gather_facts: no
tasks:
- name: remove apache web server
yum: name=httpd state=absent
- name: Remove apache on ubuntu
hosts: ubuntu
gather_facts: no
tasks:
- name: remove apache web server
apt: name=apache2 state=absent
'Cloud > Ansible' 카테고리의 다른 글
Ansible 활용 NFS 및 Wordpress설치하기 (0) | 2022.07.07 |
---|---|
Ansible 환경 설정 자동화 (0) | 2022.07.07 |