diff --git a/ z2/Dockerfile b/ z2/Dockerfile new file mode 100644 index 0000000..9fe75f9 --- /dev/null +++ b/ z2/Dockerfile @@ -0,0 +1,24 @@ +# исп официальный образ Nginx + + +FROM nginx:latest + +# коп конфиг файлы в конт + +COPY nginx/nginx.conf /etc/nginx/nginx.conf +COPY nginx/default.conf /etc/nginx/conf.d/default.conf + +# коп содержимое веб-приложения в конт + + +COPY ./app /usr/share/nginx/html + +# откр порт для веб-сервера + + +EXPOSE 80 + +# запускаем нгингс + + +CMD ["nginx", "-g", "daemon off;"] diff --git a/ z2/README.md b/ z2/README.md new file mode 100644 index 0000000..55750c9 --- /dev/null +++ b/ z2/README.md @@ -0,0 +1,67 @@ +# Docker Web Application with MySQL and Nginx + +## Requirements +- Docker +- Docker Compose + +## Description +This web application consists of two services: +1. A **web service** using Nginx to serve static HTML files. +2. A **database service** using MySQL, which persists its data in a Docker volume. + +## Docker Volumes and Networks +- **Volume**: `db_data` - used to persist MySQL data. +- **Network**: `app-network` - internal network for the services to communicate. + +## Configuration +- **Nginx**: Serves static HTML files from the `/html` directory. +- **MySQL**: Configured with `MYSQL_ROOT_PASSWORD` and a default database `mydb`. + +## How to Use + +### Preparing the app +Run the following command to prepare the application: +```bash +./prepare-app.sh +``` + +### Starting the app +To start the app, run: +```bash +./start-app.sh +``` + +The app will be available at `http://localhost:5000`. + +### Stopping the app +To stop the app, run: +```bash +./stop-app.sh +``` + +### Removing the app +To remove all resources related to the app, run: +```bash +./remove-app.sh +``` + +## Example usage + +```bash +# Prepare the app +./prepare-app.sh +Preparing app... + +# Start the app +./start-app.sh +Running app ... +The app is available at http://localhost:5000 + +# Stop the app +./stop-app.sh +Stopping app... + +# Remove the app +./remove-app.sh +Removed app. +``` diff --git a/ z2/deployment.yaml b/ z2/deployment.yaml new file mode 100644 index 0000000..5b0833c --- /dev/null +++ b/ z2/deployment.yaml @@ -0,0 +1,22 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: web-deployment + namespace: webapp +spec: + replicas: 1 + selector: + matchLabels: + app: web + template: + metadata: + labels: + app: web + spec: + containers: + - name: nginx + image: web-app:latest + imagePullPolicy: Never + ports: + - containerPort: 80 + diff --git a/ z2/docker-compose.yml b/ z2/docker-compose.yml new file mode 100644 index 0000000..8275641 --- /dev/null +++ b/ z2/docker-compose.yml @@ -0,0 +1,32 @@ +version: "3.7" +services: + web: + build: + context: . + ports: + - "5000:80" + volumes: + - ./app:/usr/share/nginx/html + - ./nginx/nginx.conf:/etc/nginx/nginx.conf + - ./nginx/default.conf:/etc/nginx/conf.d/default.conf + networks: + - app-network + restart: always + db: + image: mysql:5.7 + environment: + MYSQL_ROOT_PASSWORD: yankasabutski + volumes: + - db_data:/var/lib/mysql + networks: + - app-network + restart: always + +volumes: + db_data: + +networks: + app-network: + driver: bridge + + \ No newline at end of file diff --git a/ z2/namespace.yaml b/ z2/namespace.yaml new file mode 100644 index 0000000..1163e4d --- /dev/null +++ b/ z2/namespace.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: webapp diff --git a/ z2/prepare-app.sh b/ z2/prepare-app.sh new file mode 100644 index 0000000..bf3a84f --- /dev/null +++ b/ z2/prepare-app.sh @@ -0,0 +1,14 @@ +#!/bin/bash +docker build -t web-app:latest . + + +# включаем созд контейнеров + + +docker-compose build + +# создание сети и томов + + +docker network create app-network +docker volume create db_data diff --git a/ z2/remove-app.sh b/ z2/remove-app.sh new file mode 100644 index 0000000..372d506 --- /dev/null +++ b/ z2/remove-app.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +# удал все конт тома и сети + + + +docker-compose down --volumes --remove-orphans +docker network rm app-network +docker volume rm db_data diff --git a/ z2/service.yaml b/ z2/service.yaml new file mode 100644 index 0000000..a1bfc07 --- /dev/null +++ b/ z2/service.yaml @@ -0,0 +1,26 @@ +--- +apiVersion: v1 +kind: Service +metadata: + name: web-service + namespace: webapp +spec: + selector: + app: web + ports: + - protocol: TCP + port: 80 + targetPort: 80 + type: NodePort + +--- +apiVersion: v1 +kind: Service +metadata: + name: postgres + namespace: webapp +spec: + ports: + - port: 5432 + selector: + app: postgres diff --git a/ z2/start-app.sh b/ z2/start-app.sh new file mode 100644 index 0000000..bb6696a --- /dev/null +++ b/ z2/start-app.sh @@ -0,0 +1,22 @@ +#!/bin/bash +kubectl apply -f namespace.yaml +kubectl apply -f deployment.yaml +kubectl apply -f statefulset.yaml +kubectl apply -f service.yaml + + +# запуск конт в фоновом режиме + + +docker-compose up -d + + +# вывод на каком порте доступно приложение + + +echo "The app is available at http://localhost:5000" +#!/bin/bash +kubectl apply -f namespace.yaml +kubectl apply -f deployment.yaml +kubectl apply -f statefulset.yaml +kubectl apply -f service.yaml diff --git a/ z2/statefulset.yaml b/ z2/statefulset.yaml new file mode 100644 index 0000000..ce38d7a --- /dev/null +++ b/ z2/statefulset.yaml @@ -0,0 +1,66 @@ +apiVersion: v1 +kind: PersistentVolume +metadata: + name: pg-pv + namespace: webapp +spec: + capacity: + storage: 1Gi + accessModes: + - ReadWriteOnce + hostPath: + path: "/mnt/data/pgdata" + +--- +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: pg-pvc + namespace: webapp +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi + +--- +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: postgres + namespace: webapp +spec: + selector: + matchLabels: + app: postgres + serviceName: "postgres" + replicas: 1 + template: + metadata: + labels: + app: postgres + spec: + containers: + - name: postgres + image: postgres:15 + env: + - name: POSTGRES_DB + value: mydb + - name: POSTGRES_USER + value: user + - name: POSTGRES_PASSWORD + value: password + ports: + - containerPort: 5432 + volumeMounts: + - name: pg-storage + mountPath: /var/lib/postgresql/data + volumeClaimTemplates: + - metadata: + name: pg-storage + spec: + accessModes: ["ReadWriteOnce"] + resources: + requests: + storage: 1Gi diff --git a/ z2/stop-app.sh b/ z2/stop-app.sh new file mode 100644 index 0000000..091ba63 --- /dev/null +++ b/ z2/stop-app.sh @@ -0,0 +1,12 @@ +#!/bin/bash +kubectl delete -f service.yaml +kubectl delete -f statefulset.yaml +kubectl delete -f deployment.yaml +kubectl delete -f namespace.yaml + + +# ост все конт + + + +docker-compose down