8000 Add Kubernetes and GitHub Packages support. · jarnovanleeuwen/laravel-dock@4fa4d65 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4fa4d65

Browse files
Add Kubernetes and GitHub Packages support.
1 parent 84ba61a commit 4fa4d65

File tree

6 files changed

+346
-13
lines changed

6 files changed

+346
-13
lines changed

.env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
APP_ID=laraveldock
22
DEPLOY_SERVER=user@0.0.0.0
3-
DOCKER_REPOSITORY=registry.gitlab.com/yourorganisation/laravel-dock
3+
DOCKER_REPOSITORY=docker.pkg.github.com/yourorganisation/laravel-dock
44

5-
REGISTRY=registry.gitlab.com
5+
REGISTRY=docker.pkg.github.com
66
REGISTRY_USER=
77
REGISTRY_PASSWORD=
88

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.env
1+
/.idea/
2+
/.env

README.md

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Learning the concepts of [Docker](https://www.docker.com/), I created an example
1313
- Built-in support for Laravel's key concepts: **scheduling, queues, cache etc.**
1414
- Built-in Laravel Horizon for managing queue workers through configuration.
1515
- All configuration in source control (e.g. virtual hosts, OPcache, InnoDB parameters).
16+
- Compatible with GitHub Actions & Packages, DockerHub, GitLab and other CI/CD services.
1617

1718
# Installation
1819

@@ -22,6 +23,7 @@ Learning the concepts of [Docker](https://www.docker.com/), I created an example
2223
- Clone this repository
2324
- Copy `.env.example` to `.env` and edit the file to match your environment.
2425
- Run `./dock up`
26+
- Run `./dock exec composer install`
2527
- Run `./dock artisan migrate`
2628
- Visit http://localhost/status
2729

@@ -51,6 +53,8 @@ By default, Apache binds to port 80 and MySQL to port 3306. This, and much more,
5153

5254
# Setting up a multiserver environment
5355

56+
### Docker Swarm
57+
5458
Basically, any server with [Docker Swarm](https://docs.docker.com/engine/swarm/) that can be reached over SSH can be used. To give an idea on how easy it is to setup a multiserver environment, I will include the setup steps below. The steps apply to any cloud server provider, such as DigitalOcean or AWS.
5559

5660
- Create an SSH accessible 'manager' server and initialise the swarm: `docker swarm init --advertise-addr={Private IP}`.
@@ -60,19 +64,61 @@ Basically, any server with [Docker Swarm](https://docs.docker.com/engine/swarm/)
6064
- On the deployment server (or developer machine) specify `DEPLOY_SERVER`.
6165
- 🚀 Execute a rolling deployment using `./dock deploy`.
6266

63-
Notes: this is a simple setup for illustration purposes. In real production environment, an odd number of 3+ manager nodes is recommended for optimal fault tolerance. The same holds for replicating the web and database servers. Moreover, the swarm can be completely isolated from the Internet by only allowing Docker Swarm traffic between nodes and restricting all other traffic. Add a load balancer setup to forward public traffic on ports 80 and 443 to the swarm. Then, only the manager node would need to open port 22 (SSH) to known deployment hosts.
67+
Note: this is a simple setup for illustration purposes. In real production environment, an odd number of 3+ manager nodes is recommended for optimal fault tolerance. The same holds for replicating the web and database servers. Moreover, the swarm can be completely isolated from the Internet by only allowing Docker Swarm traffic between nodes and restricting all other traffic. Add a load balancer setup to forward public traffic on ports 80 and 443 to the swarm. Then, only the manager node would need to open port 22 (SSH) to known deployment hosts.
6468

65-
Opening the required ports on swarm nodes in UFW:
66-
```bash
67-
ufw allow 2377/tcp
68-
ufw allow 7946/tcp
69-
ufw allow 7946/udp
70-
ufw allow 4789/udp
71-
ufw reload
69+
### Kubernetes
70+
71+
Assuming you have a Kubernetes cluster, you can deploy the Laravel application, including MySQL, Redis, Horizon and a scheduler by applying the [`kubernetes.yaml`](https://github.com/jarnovanleeuwen/laravel-dock/blob/master/build/kubernetes.yaml) config. It has only been tested with DigitalOcean's persistent volumes, but the configuration can easily be updated for other cloud providers.
72+
73+
First, create secrets for the Docker registry and application keys and passwords.
74+
```sh
75+
kubectl create secret docker-registry regcred --docker-server=<Registry server> --docker-username=<Username> --docker-password=<Password>
76+
kubectl create secret generic app-secrets --from-literal=APP_KEY='8NTmvFb2YjzkhkvVNDU6Urd2l4tBCXem' --from-literal=DB_PASSWORD='<MySQL password>' --from-literal=REDIS_PASSWORD='<Redis password>'
77+
```
78+
79+
Then, deploy the application.
80+
```sh
81+
kubectl apply -f build/kubernetes.yaml
82+
```
83+
84+
Finally, you can run the migrations or any other artisan command.
85+
```sh
86+
kubectl exec -it svc/web -- php artisan migrate
7287
```
7388

7489
# CI/CD
75-
The `sut` service in `docker-compose.test.yml` can be used for automated testing. I have experimented with automated builds and tests on [Docker Hub](https://hub.docker.com/) and [GitLab.com](https://about.gitlab.com/product/continuous-integration/)'s CI/CD pipelines.
90+
The `sut` service in `docker-compose.test.yml` can be used for automated testing. I have experimented with automated builds and tests on [Docker Hub](https://hub.docker.com/) and [GitLab.com](https://about.gitlab.com/product/continuous-integration/)'s CI/CD pipelines. Recently, I have also added examples for GitHub's [Actions](https://github.com/features/actions) and [Packages](https://github.com/features/packages).
91+
92+
## GitHub
93+
### Actions
94+
Use the following GitHub Actions workflow to build and test your Docker image:
95+
96+
```yaml
97+
name: Build and Test
98+
on: push
99+
100+
jobs:
101+
build:
102+
name: Build
103+
runs-on: ubuntu-latest
104+
steps:
105+
- uses: actions/checkout@master
106+
- name: Build image
107+
run: docker build --file build/Dockerfile --target production --tag app-local .
108+
- name: Run PHPUnit
109+
run: docker-compose -f build/docker-compose.test.yml run sut
110+
```
111+
112+
### Packages
113+
The GitHub Package Registry can be used to store your Docker images. You should [Create a personal access token](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line) and edit your `.env` file accordingly:
114+
115+
```dotenv
116+
DOCKER_REPOSITORY=docker.pkg.github.com/<Organisation>/<Repository>/<Image>
117+
118+
REGISTRY=docker.pkg.github.com
119+
REGISTRY_USER=<Your GitHub username>
120+
REGISTRY_PASSWORD=<Your personal access token>
121+
```
76122

77123
## DockerHub
78124
In the *Automated builds* configuration section, make sure to set the *Dockerfile location* to `build/Dockerfile` in your build rules.

build/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ RUN useradd --create-home webdev
1313
WORKDIR /var/www/html
1414

1515
# Install dependencies
16-
RUN apt-get update && apt-get install -y mysql-client nano supervisor zip
16+
RUN apt-get update && apt-get install -y mariadb-client nano supervisor zip
1717

1818
# Install extensions
1919
RUN docker-php-ext-install opcache pcntl pdo_mysql

0 commit comments

Comments
 (0)
0