From 4d4c6dbaa3d3dc368777e7a4484f8cef9423fb47 Mon Sep 17 00:00:00 2001 From: Kekskurse Date: Thu, 26 Sep 2019 16:06:03 +0200 Subject: [PATCH] Init --- .env.example | 2 +- .../2019_09_26_123622_create_jobs_table.php | 36 +++++++++ ..._09_26_123631_create_failed_jobs_table.php | 35 ++++++++ docker-compose.yml | 14 +++- docker/Dockerfile-nginx | 4 - resources/build/manual.sh | 17 ++++ {docker => resources/docker}/Dockerfile-fpm | 2 +- resources/docker/Dockerfile-nginx | 4 + resources/docker/Dockerfile-queueworker | 8 ++ resources/docker/nginx-kube.conf | 21 +++++ {docker => resources/docker}/nginx.conf | 0 {docker => resources/docker}/start-fpm.sh | 0 resources/docker/start-queueworker.sh | 9 +++ resources/kubernetes/certificate.yml | 19 +++++ resources/kubernetes/config-app.yml | 17 ++++ resources/kubernetes/config-mysql-backup.yml | 11 +++ resources/kubernetes/config-mysql.yml | 10 +++ resources/kubernetes/cron-mysql-backup.yml | 20 +++++ resources/kubernetes/deployment-mysql.yml | 27 +++++++ .../kubernetes/deplyoment-queueworker.yml | 20 +++++ resources/kubernetes/deplyoment-webapp.yml | 26 ++++++ resources/kubernetes/ingress-webapp.yml | 16 ++++ resources/kubernetes/namespace.yml | 4 + resources/kubernetes/service-mysql.yml | 15 ++++ resources/kubernetes/service-webapp.yml | 15 ++++ resources/views/index.php | 79 +++++++++++++------ 26 files changed, 400 insertions(+), 31 deletions(-) create mode 100644 database/migrations/2019_09_26_123622_create_jobs_table.php create mode 100644 database/migrations/2019_09_26_123631_create_failed_jobs_table.php delete mode 100644 docker/Dockerfile-nginx create mode 100755 resources/build/manual.sh rename {docker => resources/docker}/Dockerfile-fpm (66%) create mode 100644 resources/docker/Dockerfile-nginx create mode 100644 resources/docker/Dockerfile-queueworker create mode 100644 resources/docker/nginx-kube.conf rename {docker => resources/docker}/nginx.conf (100%) rename {docker => resources/docker}/start-fpm.sh (100%) create mode 100644 resources/docker/start-queueworker.sh create mode 100644 resources/kubernetes/certificate.yml create mode 100644 resources/kubernetes/config-app.yml create mode 100644 resources/kubernetes/config-mysql-backup.yml create mode 100644 resources/kubernetes/config-mysql.yml create mode 100644 resources/kubernetes/cron-mysql-backup.yml create mode 100644 resources/kubernetes/deployment-mysql.yml create mode 100644 resources/kubernetes/deplyoment-queueworker.yml create mode 100644 resources/kubernetes/deplyoment-webapp.yml create mode 100644 resources/kubernetes/ingress-webapp.yml create mode 100644 resources/kubernetes/namespace.yml create mode 100644 resources/kubernetes/service-mysql.yml create mode 100644 resources/kubernetes/service-webapp.yml diff --git a/.env.example b/.env.example index ccac5db..2f39e40 100644 --- a/.env.example +++ b/.env.example @@ -16,4 +16,4 @@ DB_USERNAME=dev DB_PASSWORD=dev CACHE_DRIVER=file -QUEUE_CONNECTION=sync +QUEUE_CONNECTION=mysql diff --git a/database/migrations/2019_09_26_123622_create_jobs_table.php b/database/migrations/2019_09_26_123622_create_jobs_table.php new file mode 100644 index 0000000..58d7715 --- /dev/null +++ b/database/migrations/2019_09_26_123622_create_jobs_table.php @@ -0,0 +1,36 @@ +bigIncrements('id'); + $table->string('queue')->index(); + $table->longText('payload'); + $table->unsignedTinyInteger('attempts'); + $table->unsignedInteger('reserved_at')->nullable(); + $table->unsignedInteger('available_at'); + $table->unsignedInteger('created_at'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('jobs'); + } +} diff --git a/database/migrations/2019_09_26_123631_create_failed_jobs_table.php b/database/migrations/2019_09_26_123631_create_failed_jobs_table.php new file mode 100644 index 0000000..d432dff --- /dev/null +++ b/database/migrations/2019_09_26_123631_create_failed_jobs_table.php @@ -0,0 +1,35 @@ +bigIncrements('id'); + $table->text('connection'); + $table->text('queue'); + $table->longText('payload'); + $table->longText('exception'); + $table->timestamp('failed_at')->useCurrent(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('failed_jobs'); + } +} diff --git a/docker-compose.yml b/docker-compose.yml index eb6eb6f..2d29e89 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,9 +1,17 @@ version: '3.2' services: + queueworker: + build: + context: ./ + dockerfile: resources/docker/Dockerfile-queueworker + volumes: + - ./:/app + links: + - mariadb php: build: context: ./ - dockerfile: docker/Dockerfile-fpm + dockerfile: resources/docker/Dockerfile-fpm volumes: - ./:/app links: @@ -11,10 +19,10 @@ services: nginx: build: context: ./ - dockerfile: docker/Dockerfile-nginx + dockerfile: resources/docker/Dockerfile-nginx volumes: - ./:/app - - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf + - ./resources/docker/nginx.conf:/etc/nginx/conf.d/default.conf ports: - 8083:80 links: diff --git a/docker/Dockerfile-nginx b/docker/Dockerfile-nginx deleted file mode 100644 index 6853f8a..0000000 --- a/docker/Dockerfile-nginx +++ /dev/null @@ -1,4 +0,0 @@ -FROM nginx:1.15.12-alpine - -COPY ./ /app -COPY ./docker/nginx.conf /etc/nginx/conf.d/default.conf diff --git a/resources/build/manual.sh b/resources/build/manual.sh new file mode 100755 index 0000000..587e49c --- /dev/null +++ b/resources/build/manual.sh @@ -0,0 +1,17 @@ +cd ../../ +rm -r -f storage/logs/l* +chmod uog+rwx storage/logs +rm -r -f vendor +composer install --no-dev +docker build -t docker.keks.cloud/sampleapp/nginx:latest -f resources/docker/Dockerfile-nginx . +docker build -t docker.keks.cloud/sampleapp/fpm:latest -f resources/docker/Dockerfile-fpm . +docker build -t docker.keks.cloud/sampleapp/queueworker:latest -f resources/docker/Dockerfile-queueworker . + +docker push docker.keks.cloud/sampleapp/nginx:latest +docker push docker.keks.cloud/sampleapp/fpm:latest +docker push docker.keks.cloud/sampleapp/queueworker:latest + +rm -r -f vendor +composer install + +cd resources/build diff --git a/docker/Dockerfile-fpm b/resources/docker/Dockerfile-fpm similarity index 66% rename from docker/Dockerfile-fpm rename to resources/docker/Dockerfile-fpm index e5aff4f..9c86a5c 100644 --- a/docker/Dockerfile-fpm +++ b/resources/docker/Dockerfile-fpm @@ -5,4 +5,4 @@ WORKDIR /app COPY ./ /app -CMD ["sh", "/app/docker/start-fpm.sh"] +CMD ["sh", "/app/resources/docker/start-fpm.sh"] diff --git a/resources/docker/Dockerfile-nginx b/resources/docker/Dockerfile-nginx new file mode 100644 index 0000000..deecd54 --- /dev/null +++ b/resources/docker/Dockerfile-nginx @@ -0,0 +1,4 @@ +FROM nginx:1.15.12-alpine + +COPY ./ /app +COPY ./resources/docker/nginx-kube.conf /etc/nginx/conf.d/default.conf diff --git a/resources/docker/Dockerfile-queueworker b/resources/docker/Dockerfile-queueworker new file mode 100644 index 0000000..4fc3212 --- /dev/null +++ b/resources/docker/Dockerfile-queueworker @@ -0,0 +1,8 @@ +FROM php:7.3.5-cli-alpine3.9 + +RUN docker-php-ext-install pdo pdo_mysql +WORKDIR /app + +COPY ./ /app + +CMD ["sh", "/app/resources/docker/start-queueworker.sh"] diff --git a/resources/docker/nginx-kube.conf b/resources/docker/nginx-kube.conf new file mode 100644 index 0000000..6e684e4 --- /dev/null +++ b/resources/docker/nginx-kube.conf @@ -0,0 +1,21 @@ + server { + index index.php index.html; + server_name php-docker.local; + error_log /var/log/nginx/error.log; + access_log /var/log/nginx/access.log; + root /app/public; + + location / { + try_files $uri $uri/ /index.php?$query_string; + } + + location ~ \.php$ { + try_files $uri /index.php =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass localhost:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } + } diff --git a/docker/nginx.conf b/resources/docker/nginx.conf similarity index 100% rename from docker/nginx.conf rename to resources/docker/nginx.conf diff --git a/docker/start-fpm.sh b/resources/docker/start-fpm.sh similarity index 100% rename from docker/start-fpm.sh rename to resources/docker/start-fpm.sh diff --git a/resources/docker/start-queueworker.sh b/resources/docker/start-queueworker.sh new file mode 100644 index 0000000..16d250f --- /dev/null +++ b/resources/docker/start-queueworker.sh @@ -0,0 +1,9 @@ +cd /app +until nc -z -v -w30 mariadb 3306 +do + echo "Waiting for database connection..." + # wait for 5 seconds before check again + sleep 5 +done +sleep 5 +php /app/artisan queue:work diff --git a/resources/kubernetes/certificate.yml b/resources/kubernetes/certificate.yml new file mode 100644 index 0000000..36aa0ec --- /dev/null +++ b/resources/kubernetes/certificate.yml @@ -0,0 +1,19 @@ +apiVersion: certmanager.k8s.io/v1alpha1 +kind: Certificate +metadata: + name: sample.app.keks.cloud + namespace: sampleapp +spec: + secretName: sample-app-keks-cloud-tls + acme: + config: + - dns01: + provider: cf-dns + domains: + - 'sample.app.keks.cloud' + commonName: 'sample.app.keks.cloud' + dnsNames: + - sample.app.keks.cloud + issuerRef: + kind: ClusterIssuer + name: letsencrypt-prod diff --git a/resources/kubernetes/config-app.yml b/resources/kubernetes/config-app.yml new file mode 100644 index 0000000..8c80ff4 --- /dev/null +++ b/resources/kubernetes/config-app.yml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: env-app + namespace: sampleapp +data: + APP_DEBUG: "false" + DB_CONNECTION: "mysql" + DB_HOST: "mysql" + DB_PORT: "3306" + DB_PORT: "oauth" + DB_USERNAME: "oauth" + DB_PASSWORD: "oauth" + QUEUE_CONNECTION: "mysql" + APP_TIMEZONE: "UTC" + APP_KEY: "abc" + APP_URL: "https://sample.app.keks.cloud" diff --git a/resources/kubernetes/config-mysql-backup.yml b/resources/kubernetes/config-mysql-backup.yml new file mode 100644 index 0000000..ea0630b --- /dev/null +++ b/resources/kubernetes/config-mysql-backup.yml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: env-mysql-backup + namespace: sampleapp +data: + MYSQL_HOST: "mariadb" + MYSQL_PORT: "3306" + PROJEKT_NAME: "sampleapp" + MYSQL_USER: "root" + MYSQL_PASSWORD: "example" diff --git a/resources/kubernetes/config-mysql.yml b/resources/kubernetes/config-mysql.yml new file mode 100644 index 0000000..0827501 --- /dev/null +++ b/resources/kubernetes/config-mysql.yml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: env-mysql + namespace: sampleapp +data: + MYSQL_USER: "dev" + MYSQL_PASSWORD: "dev" + MYSQL_DATABASE: "dev" + MYSQL_ROOT_PASSWORD: "example" diff --git a/resources/kubernetes/cron-mysql-backup.yml b/resources/kubernetes/cron-mysql-backup.yml new file mode 100644 index 0000000..e5d8fe5 --- /dev/null +++ b/resources/kubernetes/cron-mysql-backup.yml @@ -0,0 +1,20 @@ +apiVersion: batch/v1beta1 +kind: CronJob +metadata: + name: mysql-backup + namespace: sampleapp +spec: + schedule: "10 * * * *" + jobTemplate: + spec: + template: + spec: + containers: + - name: mysql-backup + image: docker.keks.cloud/backup/mysql:latest + envFrom: + - configMapRef: + name: env-mysql-backup + restartPolicy: OnFailure + imagePullSecrets: + - name: docker-keks-cloud diff --git a/resources/kubernetes/deployment-mysql.yml b/resources/kubernetes/deployment-mysql.yml new file mode 100644 index 0000000..3bbae21 --- /dev/null +++ b/resources/kubernetes/deployment-mysql.yml @@ -0,0 +1,27 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: mariadb + namespace: sampleapp +spec: + replicas: 1 + template: + metadata: + labels: + app: mariadb + spec: + containers: + - name: mariadb + image: mariadb:10.4.4 + ports: + - containerPort: 3306 + envFrom: + - configMapRef: + name: env-mysql + volumeMounts: + - mountPath: /var/lib/mysql + name: mysql + volumes: + - name: mysql + persistentVolumeClaim: + claimName: mysql diff --git a/resources/kubernetes/deplyoment-queueworker.yml b/resources/kubernetes/deplyoment-queueworker.yml new file mode 100644 index 0000000..26a5d0a --- /dev/null +++ b/resources/kubernetes/deplyoment-queueworker.yml @@ -0,0 +1,20 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: queueworker + namespace: sampleapp +spec: + replicas: 2 + template: + metadata: + labels: + app: webapp + spec: + containers: + - name: php + image: docker.keks.cloud/sampleapp/queueworker:latest + envFrom: + - configMapRef: + name: env-app + imagePullSecrets: + - name: docker-keks-cloud diff --git a/resources/kubernetes/deplyoment-webapp.yml b/resources/kubernetes/deplyoment-webapp.yml new file mode 100644 index 0000000..e3ec1b8 --- /dev/null +++ b/resources/kubernetes/deplyoment-webapp.yml @@ -0,0 +1,26 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: webapp + namespace: sampleapp +spec: + replicas: 2 + template: + metadata: + labels: + app: webapp + spec: + containers: + - name: php + image: docker.keks.cloud/sampleapp/fpm:latest + ports: + - containerPort: 9000 + envFrom: + - configMapRef: + name: env-app + - name: nginx + image: docker.keks.cloud/sampleapp/nginx:latest + ports: + - containerPort: 80 + imagePullSecrets: + - name: docker-keks-cloud diff --git a/resources/kubernetes/ingress-webapp.yml b/resources/kubernetes/ingress-webapp.yml new file mode 100644 index 0000000..8aea373 --- /dev/null +++ b/resources/kubernetes/ingress-webapp.yml @@ -0,0 +1,16 @@ +apiVersion: extensions/v1beta1 +kind: Ingress +metadata: + name: webapp + namespace: sampleapp +spec: + rules: + - host: sample.app.keks.cloud + http: + paths: + - backend: + serviceName: service-webapp + servicePort: 80 + path: / + tls: + - secretName: sample-app-keks-cloud-tls diff --git a/resources/kubernetes/namespace.yml b/resources/kubernetes/namespace.yml new file mode 100644 index 0000000..1e467a6 --- /dev/null +++ b/resources/kubernetes/namespace.yml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: sampleapp diff --git a/resources/kubernetes/service-mysql.yml b/resources/kubernetes/service-mysql.yml new file mode 100644 index 0000000..c33efc1 --- /dev/null +++ b/resources/kubernetes/service-mysql.yml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + annotations: + field.cattle.io/targetWorkloadIds: '["deployment:sampleapp:mariadb"]' + name: mariadb + namespace: sampleapp +spec: + ports: + - port: 3306 + protocol: TCP + targetPort: 3306 + type: ClusterIP +status: + loadBalancer: {} diff --git a/resources/kubernetes/service-webapp.yml b/resources/kubernetes/service-webapp.yml new file mode 100644 index 0000000..63df4db --- /dev/null +++ b/resources/kubernetes/service-webapp.yml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + annotations: + field.cattle.io/targetWorkloadIds: '["deployment:sampleapp:webapp"]' + name: service-webapp + namespace: sampleapp +spec: + ports: + - port: 80 + protocol: TCP + targetPort: 80 + type: ClusterIP +status: + loadBalancer: {} diff --git a/resources/views/index.php b/resources/views/index.php index c473b68..2da720b 100644 --- a/resources/views/index.php +++ b/resources/views/index.php @@ -6,28 +6,63 @@ - +