docker 환경으로의 전환을 생각하여 익숙해지고자 이것저것 테스트 하는 중
테스트부터 하고 proxy 서버에 만들어둔 플레이북으로 제대로 실행되게 만들자
서버 구성
docker-proxy (172.16.1.38):
- Ansible, Docker 설치
- Nginx 설치 및 로드 밸런서 역할
docker1 (172.16.3.100):
- nginx1, Tomcat1, MariaDB1 컨테이너
docker2 (172.16.3.101):
- nginx2, Tomcat2, MariaDB2 컨테이너
- docker1과 이중화 설정
폴더 구조
/inventory/
├── playbooks/
│ ├── service.yml
│ ├── roles/
│ │ ├── apache/
│ │ │ ├── tasks/
│ │ │ │ └── main.yml
│ │ │ ├── files/
│ │ │ │ ├── Dockerfile
│ │ │ │ └── index.html
│ │ ├── tomcat/
│ │ │ ├── tasks/
│ │ │ │ └── main.yml
│ │ │ ├── files/
│ │ │ │ └── Dockerfile
│ │ ├── mariadb/
│ │ │ ├── tasks/
│ │ │ │ └── main.yml
│ │ │ ├── files/
│ │ │ │ └── Dockerfile
│ │ ├── nginx/
│ │ │ ├── tasks/
│ │ │ │ └── main.yml
│ │ │ ├── files/
│ │ │ │ ├── Dockerfile
│ │ │ │ └── nginx.conf
/etc/ansible/hosts
Ansible 로 각 호스트에 필요 컨테이너 배포
[root@docker-proxy playbooks]# cat service.yml
---
- hosts: proxy
become: yes
roles:
- role: nginx
- hosts: docker_hosts
become: yes
roles:
- role: apache
- role: tomcat
- role: mariadb
Apache playbook
[root@docker-proxy roles]# cat apache/files/Dockerfile
FROM httpd:latest
COPY index.html /usr/local/apache2/htdocs/
[root@docker-proxy roles]# cat apache/files/index.html
<html>
<body>
<h1>It works on Apache Server</h1>
</body>
</html>
[root@docker-proxy roles]# cat apache/tasks/main.yml
---
- name: Build Apache Docker image
community.docker.docker_image:
name: apache-server
tag: latest
source: build
build:
path: /inventory/playbooks/roles/apache/files
- name: Run Apache container
community.docker.docker_container:
name: apache_server
image: apache-server:latest
state: started
ports:
- "8080:80"
tomcat playbook
[root@docker-proxy roles]# cat tomcat/files/Dockerfile
FROM tomcat:latest
[root@docker-proxy roles]# cat tomcat/tasks/main.yml
---
- name: Pull Tomcat image
community.docker.docker_image:
name: tomcat
source: pull
- name: Run Tomcat container
community.docker.docker_container:
name: tomcat_server
image: tomcat
state: started
ports:
- "8081:8080"
mariadb playbook
[root@docker-proxy roles]# cat mariadb/files/Dockerfile
FROM mariadb:latest
[root@docker-proxy roles]# cat mariadb/tasks/main.yml
---
- name: Pull MariaDB image
community.docker.docker_image:
name: mariadb
source: pull
- name: Run MariaDB container
community.docker.docker_container:
name: mariadb_server
image: mariadb
state: started
ports:
- "3306:3306"
env:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: docker_test
프록시 테스트
NGINX proxy (172.16.1.38)
Docker 이미지 빌드
# -t 옵션: 빌드한 이미지에 태그를 붙입니다. 여기서는 "nginx-proxy"라는 태그를 사용합니다.
[root@docker-proxy files]# pwd
/inventory/playbooks/roles/nginx/files
[root@docker-proxy files]# docker build -t nginx-proxy .
Docker 컨테이너 실행
# -d 옵션: 컨테이너를 백그라운드에서 실행합니다.
# --name 옵션: 컨테이너의 이름을 지정합니다. 여기서는 "nginx"로 지정합니다.
# -p 옵션: 호스트의 포트 80을 컨테이너의 포트 80에 바인딩합니다.
[root@docker-proxy files]# docker run -d --name nginx -p 80:80 nginx-proxy
컨테이너 접속
[root@docker-proxy files]# docker exec -it nginx /bin/bash
Apache1 (172.16.3.100)
이미지 빌드
[root@docker1 apache_container]# pwd
/root/apache_container
[root@docker1 apache_container]# docker build -t apache-server .
실행
옵션: 컨테이너를 백그라운드에서 실행합니다.
# --name 옵션: 컨테이너의 이름을 지정합니다. 여기서는 "apache_server"로 지정합니다.
# -p 옵션: 호스트의 포트 8080을 컨테이너의 포트 80에 바인딩합니다.
# docker run -d --name apache_server -p 8080:80 apache-server
nginx 설정에서 각 apache 서버로 8080 포트로 쏘게 설정해두었다.
apache 는 호스트에서 8080으로 받아 컨테이너에 80으로 전달한다.
[root@docker1 apache_container]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
453ff315d58a apache-server "httpd-foreground" 4 seconds ago Up 3 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp apache_server
[root@docker1 apache_container]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 56591/docker-proxy
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 49959/sshd: /usr/sb
tcp6 0 0 :::8080 :::* LISTEN 56600/docker-proxy
tcp6 0 0 :::22 :::* LISTEN 49959/sshd: /usr/sb
Apache2 (172.16.3.101)
1번과 동일 방식으로 진행
nginx 로드밸런싱 테스트를 위해 index.html 파일에 1번은 server1 , 2번은 server2 로 적어두었다.
[root@docker2 apache_container]# cat index.html
<html>
<body>
<h1>It works on Apache Server2</h1>
</body>
</html>
curl http://172.16.1.38 해보면 server1 server2 가 번갈아 나오는걸 확인할 수 있다.