Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Домашнее задание №4 - Броузерное приложение и балансировщик #1169

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
phpcs.xml
.github
.idea
125 changes: 125 additions & 0 deletions balancer/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
version: '3'

services:

# Контейнер с Nginx-балансировщиком
nginx-balancer:
# имя будущего образа
image: nginx
build:
context: ./docker/nginx
dockerfile: Dockerfile
# имя контейнера после запуска
container_name: nginx-balancer
# проброс портов
ports:
- "80:80"
volumes:
- ./nginx/balancer.conf:/etc/nginx/conf.d/app.local.conf
networks:
- app-network

# Контейнеры с Nginx
nginx1:
# имя будущего образа
image: nginx
build:
context: ./docker/nginx
dockerfile: Dockerfile
# имя контейнера после запуска
container_name: nginx1
# проброс портов, т.к. 80й уже занят балансировщиком, указываем 81
ports:
- "81:80"
volumes:
- ./src/:/data/app.local
- ./nginx/nginx1.conf:/etc/nginx/conf.d/app.local.conf
networks:
- app-network

nginx2:
# имя будущего образа
image: nginx
build:
context: ./docker/nginx
dockerfile: Dockerfile
# имя контейнера после запуска
container_name: nginx2
# проброс портов
ports:
- "82:80"
volumes:
- ./src/:/data/app.local
- ./nginx/nginx2.conf:/etc/nginx/conf.d/app.local.conf
networks:
- app-network

nginx3:
# имя будущего образа
image: nginx
build:
context: ./docker/nginx
dockerfile: Dockerfile
# имя контейнера после запуска
container_name: nginx3
# проброс портов
ports:
- "83:80"
volumes:
- ./src/:/data/app.local
- ./nginx/nginx3.conf:/etc/nginx/conf.d/app.local.conf
networks:
- app-network

app1:
build:
context: ./fpm
dockerfile: Dockerfile
image: balance/php
container_name: app1
volumes:
- ./src/:/data/app.local
networks:
- app-network

app2:
build:
context: ./fpm
dockerfile: Dockerfile
image: balance/php
container_name: app2
volumes:
- ./src/:/data/app.local
networks:
- app-network

app3:
build:
context: ./fpm
dockerfile: Dockerfile
image: balance/php
container_name: app3
volumes:
- ./src/:/data/app.local
networks:
- app-network

memcached01:
image: memcached:latest
container_name: memcached01
ports:
- "11201:11211"
networks:
- app-network

memcached02:
image: memcached:latest
container_name: memcached02
ports:
- "11202:11211"
networks:
- app-network

networks:
app-network:
driver: bridge
61 changes: 61 additions & 0 deletions balancer/fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
FROM php:8.1.22-fpm-alpine

#RUN docker-php-ext-install sockets

#RUN set -x \
# && addgroup -g 101 -S nginx \
# && adduser -u 101 -D -S -G nginx nginx

ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS \
&& pecl install uploadprogress \
&& docker-php-ext-enable uploadprogress \
&& apk del .build-deps $PHPIZE_DEPS \
&& chmod uga+x /usr/local/bin/install-php-extensions && sync \
&& install-php-extensions bcmath \
bz2 \
calendar \
# curl \
exif \
# fileinfo \
# ftp \
gd \
gettext \
# imagick \
imap \
intl \
ldap \
# mbstring \
mcrypt \
memcache \
memcached \
# mongodb \
mysqli \
opcache \
# openssl \
# pdo \
pdo_pgsql \
pdo_mysql \
# pgsql \
redis \
soap \
# sodium \
sysvsem \
sysvshm \
xmlrpc \
xsl \
zip \
# && echo -e "\n opcache.enable=1 \n opcache.enable_cli=1 \n opcache.memory_consumption=128 \n opcache.interned_strings_buffer=8 \n opcache.max_accelerated_files=4000 \n opcache.revalidate_freq=60 \n opcache.fast_shutdown=1" >> /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini \
# && echo -e "\n xdebug.remote_enable=1 \n xdebug.remote_host=localhost \n xdebug.remote_port=9000" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
# && echo -e "\n xhprof.output_dir='/var/tmp/xhprof'" >> /usr/local/etc/php/conf.d/docker-php-ext-xhprof.ini \
&& cd ~ \
&& php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
&& php -r "copy('https://composer.github.io/installer.sig', 'signature');" \
&& php -r "if (hash_file('SHA384', 'composer-setup.php') === trim(file_get_contents('signature'))) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
&& php composer-setup.php --install-dir=/usr/local/bin --filename=composer \
&& php -r "unlink('composer-setup.php');"

COPY ./php.ini /usr/local/etc/php/conf.d/php-custom.ini

WORKDIR /data/app.local
2 changes: 2 additions & 0 deletions balancer/fpm/php.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
session.save_handler=memcache
session.save_path="tcp://memcache01:11211, tcp://memcache02:11211"
Binary file added balancer/images/Балансировщик.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions balancer/nginx/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM ubuntu:latest
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Есть образ с уже установленым nginx лучше использовать его.
https://hub.docker.com/_/nginx


RUN apt-get update

RUN apt-get install -y nginx

WORKDIR /data

VOLUME /data

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]

43 changes: 43 additions & 0 deletions balancer/nginx/app.local.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
upstream php-fpm-backend {
least_conn;
server php-fpm-01:9000 max_fails=3 fail_timeout=30s;
server php-fpm-02:9000 max_fails=3 fail_timeout=30s;
server php-fpm-03:9000 max_fails=3 fail_timeout=30s;
server php-fpm-04:9000 max_fails=3 fail_timeout=30s;
}

server {
# указали 80 порт для соединения
listen 80;

# указали домен для нашего приложения
server_name app.local;

# задаем корневую директорию
root /data/app.local/public;

# старторый файл
index index.php index.html;

# при обращении к статическим файлам логи не нужны, равно как и обращение к fpm
location ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}

# помним про единую точку доступа
# все запросы заворачиваются в корневую директорию root на index.php
location / {
try_files $uri $uri/ /index.php;
}

# и наконец правило обращения к php-fpm
location ~* .php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass php-fpm-backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
21 changes: 21 additions & 0 deletions balancer/nginx/balancer.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
upstream nginx-webservers {
least_conn;
server nginx1:80;
server nginx2:80;
server nginx3:80;
}

server {
# указываем 80 порт для соединения
listen 80;
# нужно указать, какому доменному имени принадлежит наш конфиг
server_name app.local;

# перенаправляем на указанный список серверов в блоке upstream
location / {
proxy_pass http://nginx-webservers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
34 changes: 34 additions & 0 deletions balancer/nginx/nginx1.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
server {
# указываем 80 порт для соединения
listen 80;
# нужно указать, какому доменному имени принадлежит наш конфиг
server_name app.local;

# задаем корневую директорию
root /data/app.local/public;

# стартовый файл
index index.php index.html;

# при обращении к статическим файлам логи не нужны, равно как и обращение к fpm
location ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}

# помним про единую точку доступа
# все запросы заворачиваются в корневую директорию root на index.php
location / {
try_files $uri $uri/ /index.php?$query_string;
}

# и наконец правило обращения к php-fpm
location ~* .php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass app1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
34 changes: 34 additions & 0 deletions balancer/nginx/nginx2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
server {
# указываем 80 порт для соединения
listen 80;
# нужно указать, какому доменному имени принадлежит наш конфиг
server_name app.local;

# задаём корневую директорию
root /data/app.local/public;

# стартовый файл
index index.php index.html;

# при обращении к статическим файлам логи не нужны, равно как и обращение к fpm
location ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}

# помним про единую точку доступа
# все запросы заворачиваются в корневую директорию root на index.php
location / {
try_files $uri $uri/ /index.php?$query_string;
}

# и наконец правило обращения к php-fpm
location ~* .php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass app2:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
34 changes: 34 additions & 0 deletions balancer/nginx/nginx3.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
server {
# указываем 80 порт для соединения
listen 80;
# нужно указать, какому доменному имени принадлежит наш конфиг
server_name app.local;

# задаём корневую директорию
root /data/app.local/public;

# стартовый файл
index index.php index.html;

# при обращении к статическим файлам логи не нужны, равно как и обращение к fpm
location ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}

# помним про единую точку доступа
# все запросы заворачиваются в корневую директорию root на index.php
location / {
try_files $uri $uri/ /index.php?$query_string;
}

# и наконец правило обращения к php-fpm
location ~* .php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass app3:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
1 change: 1 addition & 0 deletions balancer/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/
Loading
Loading