├── .gitignore ├── README.md ├── mysql-svc.yaml ├── webserver-svc.yaml ├── mysql-pv-claim.yaml ├── Dockerfile ├── src └── index.php ├── webserver.yaml ├── docker-compose.yml ├── mysql.yaml └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Kubernetes Learning 2 | 3 | This repo is the part of the blog post [Deploy your first scaleable PHP/MySQL Web application in Kubernetes](http://blog.adnansiddiqi.me/deploy-your-first-scaleable-php-mysql-web-application-in-kubernetes/) -------------------------------------------------------------------------------- /mysql-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: mysql8-service 5 | labels: 6 | app: mysql8 7 | spec: 8 | ports: 9 | - port: 3306 10 | protocol: TCP 11 | selector: 12 | app: mysql8 13 | 14 | -------------------------------------------------------------------------------- /webserver-svc.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: web-service 5 | labels: 6 | run: web-service 7 | spec: 8 | type: LoadBalancer 9 | ports: 10 | - port: 80 11 | protocol: TCP 12 | selector: 13 | app: apache -------------------------------------------------------------------------------- /mysql-pv-claim.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: PersistentVolumeClaim 3 | metadata: 4 | name: mysql-pv-claim 5 | labels: 6 | app: mysql8 7 | spec: 8 | accessModes: 9 | - ReadWriteOnce 10 | resources: 11 | requests: 12 | storage: 5Gi #5 GB -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM php:7.3-apache 2 | #Install git 3 | RUN apt-get update \ 4 | && apt-get install -y git 5 | RUN docker-php-ext-install pdo pdo_mysql mysqli 6 | RUN a2enmod rewrite 7 | #Install Composer 8 | RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 9 | RUN php composer-setup.php --install-dir=. --filename=composer 10 | RUN mv composer /usr/local/bin/ 11 | COPY src/ /var/www/html/ 12 | EXPOSE 80 -------------------------------------------------------------------------------- /src/index.php: -------------------------------------------------------------------------------- 1 | "; 3 | $conn = new mysqli("mysql8-service", "root", ".sweetpwd.", "my_db"); 4 | // Check connection 5 | if ($conn->connect_error) { 6 | die("Connection failed: " . $conn->connect_error); 7 | } 8 | 9 | $sql = "SELECT name FROM users"; 10 | $result = $conn->query($sql); 11 | 12 | if ($result->num_rows > 0) { 13 | // output data of each row 14 | while($row = $result->fetch_assoc()) { 15 | echo $row['name']."
"; 16 | } 17 | } else { 18 | echo "0 results"; 19 | } 20 | $conn->close(); -------------------------------------------------------------------------------- /webserver.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: webserver 5 | labels: 6 | app: apache 7 | spec: 8 | replicas: 3 9 | selector: 10 | matchLabels: 11 | app: apache 12 | template: 13 | metadata: 14 | labels: 15 | app: apache 16 | spec: 17 | containers: 18 | - name: php-apache 19 | image: learningk8s_website 20 | imagePullPolicy: Never 21 | ports: 22 | - containerPort: 80 23 | volumeMounts: 24 | - name: hostvol 25 | mountPath: /var/www/html/ 26 | volumes: 27 | - name: hostvol 28 | hostPath: 29 | path: /data -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '2' 2 | services: 3 | mysql: 4 | image: mysql:8.0 5 | container_name: mysql-server-80 6 | command: --default-authentication-plugin=mysql_native_password 7 | volumes: 8 | - .:/application 9 | restart: always 10 | environment: 11 | - MYSQL_ROOT_PASSWORD=.sweetpwd. 12 | - MYSQL_DATABASE=my_db 13 | - MYSQL_USER=db_user 14 | - MYSQL_PASSWORD=.mypwd 15 | ports: 16 | - "8082:3306" 17 | website: 18 | container_name: php72 19 | build: 20 | context: ./ 21 | volumes: 22 | - /Development/PetProjects/LearningK8s/src:/var/www/html 23 | ports: 24 | - 8000:80 -------------------------------------------------------------------------------- /mysql.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 2 | kind: Deployment 3 | metadata: 4 | name: mysql 5 | spec: 6 | selector: 7 | matchLabels: 8 | app: mysql8 9 | strategy: 10 | type: Recreate 11 | template: 12 | metadata: 13 | labels: 14 | app: mysql8 15 | spec: 16 | containers: 17 | - image: mysql:8.0 18 | name: mysql 19 | imagePullPolicy: Never 20 | env: 21 | - name: MYSQL_ROOT_PASSWORD 22 | value: .sweetpwd. 23 | - name: MYSQL_DATABASE 24 | value: my_db 25 | - name: MYSQL_USER 26 | value: db_user 27 | - name: MYSQL_PASSWORD 28 | value: .mypwd 29 | args: ["--default-authentication-plugin=mysql_native_password"] 30 | ports: 31 | - containerPort: 3306 32 | name: mysql8 33 | volumeMounts: 34 | - name: mysql-persistent-storage 35 | mountPath: /var/lib/mysql 36 | volumes: 37 | - name: mysql-persistent-storage 38 | persistentVolumeClaim: 39 | claimName: mysql-pv-claim -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Adnan Siddiqi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------