26. 容器化LNMP
26.1 nginx
| root@k8s-master1:~/k8s-data/dockerfile/web/magedu/wordpress/nginx |
| |
| FROM harbor.nbrhce.com/pub-images/nginx-base-wordpress:v1.20.2 |
| |
| ADD nginx.conf /apps/nginx/conf/nginx.conf |
| ADD run_nginx.sh /apps/nginx/sbin/run_nginx.sh |
| RUN mkdir -pv /home/nginx/wordpress |
| RUN useradd nginx && chown nginx.nginx /home/nginx/wordpress/ -R |
| |
| EXPOSE 80 443 |
| |
| CMD ["/apps/nginx/sbin/run_nginx.sh"] |
| root@k8s-master1:~/k8s-data/dockerfile/web/magedu/wordpress/nginx |
| |
| TAG=$1 |
| nerdctl build -t harbor.nbrhce.com/demo/wordpress-nginx:${TAG} . |
| echo "镜像制作完成,即将上传至Harbor服务器" |
| sleep 1 |
| nerdctl push harbor.nbrhce.com/demo/wordpress-nginx:${TAG} |
| echo "镜像上传完成" |
| root@k8s-master1:~/k8s-data/dockerfile/web/magedu/wordpress/nginx |
| |
| |
| |
| /apps/nginx/sbin/nginx |
| tail -f /etc/hosts |
| root@k8s-master1:~/k8s-data/dockerfile/web/magedu/wordpress/nginx |
| user nginx nginx; |
| worker_processes auto; |
| events { |
| worker_connections 1024; |
| } |
| http { |
| include mime.types; |
| default_type application/octet-stream; |
| |
| |
| |
| |
| |
| |
| |
| sendfile on; |
| keepalive_timeout 65; |
| client_max_body_size 10M; |
| client_body_buffer_size 16k; |
| client_body_temp_path /apps/nginx/tmp 1 2 2; |
| gzip on; |
| server { |
| listen 80; |
| server_name blog.nbrhce.com; |
| location / { |
| root /home/nginx/wordpress; |
| index index.php index.html index.htm; |
| } |
| |
| location ~ \.php$ { |
| root /home/nginx/wordpress; |
| fastcgi_pass 127.0.0.1:9000; |
| fastcgi_index index.php; |
| |
| fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; |
| include fastcgi_params; |
| } |
| error_page 500 502 503 504 /50x.html; |
| location = /50x.html { |
| root html; |
| } |
| } |
| } |
26.2 php
| root@k8s-master1:~/k8s-data/dockerfile/web/magedu/wordpress/php |
| |
| FROM harbor.nbrhce.com/baseimages/centos:7.9.2009 |
| |
| MAINTAINER zhangshijie@magedu.net |
| |
| RUN yum install -y https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm && yum install php56-php-fpm php56-php-mysql -y |
| ADD www.conf /opt/remi/php56/root/etc/php-fpm.d/www.conf |
| |
| RUN useradd nginx |
| ADD run_php.sh /usr/local/bin/run_php.sh |
| EXPOSE 9000 |
| |
| CMD ["/usr/local/bin/run_php.sh"] |
| root@k8s-master1:~/k8s-data/dockerfile/web/magedu/wordpress/php |
| |
| |
| |
| /opt/remi/php56/root/usr/sbin/php-fpm |
| |
| tail -f /etc/hosts |
| root@k8s-master1:~/k8s-data/dockerfile/web/magedu/wordpress/php |
| [www] |
| user = nginx |
| group = nginx |
| listen = 0.0.0.0:9000 |
| pm = dynamic |
| pm.max_children = 50 |
| pm.start_servers = 5 |
| pm.min_spare_servers = 5 |
| pm.max_spare_servers = 35 |
| slowlog = /opt/remi/php56/root/var/log/php-fpm/www-slow.log |
| php_admin_value[error_log] = /opt/remi/php56/root/var/log/php-fpm/www-error.log |
| php_admin_flag[log_errors] = on |
| php_value[session.save_handler] = files |
| php_value[session.save_path] = /opt/remi/php56/root/var/lib/php/session |
| php_value[soap.wsdl_cache_dir] = /opt/remi/php56/root/var/lib/php/wsdlcache |
26.3 YAML
| root@k8s-master1:~/k8s-data/yaml/magedu/wordpress |
| kind: Deployment |
| |
| apiVersion: apps/v1 |
| metadata: |
| labels: |
| app: wordpress-app |
| name: wordpress-app-deployment |
| namespace: wordpress |
| spec: |
| replicas: 1 |
| selector: |
| matchLabels: |
| app: wordpress-app |
| template: |
| metadata: |
| labels: |
| app: wordpress-app |
| spec: |
| containers: |
| - name: wordpress-app-nginx |
| image: harbor.nbrhce.com/demo/wordpress-nginx:v1 |
| imagePullPolicy: Always |
| ports: |
| - containerPort: 80 |
| protocol: TCP |
| name: http |
| - containerPort: 443 |
| protocol: TCP |
| name: https |
| |
| volumeMounts: |
| - name: wordpress |
| mountPath: /home/nginx/wordpress |
| readOnly: true |
| |
| - name: wordpress-app-php |
| image: harbor.nbrhce.com/demo/wordpress-php-5.6:v1 |
| |
| |
| imagePullPolicy: Always |
| ports: |
| - containerPort: 9000 |
| protocol: TCP |
| name: http |
| |
| volumeMounts: |
| - name: wordpress |
| mountPath: /home/nginx/wordpress |
| readOnly: false |
| volumes: |
| - name: wordpress |
| nfs: |
| server: 10.0.0.109 |
| path: /data/k8sdata/wordpress |
| |
| |
| --- |
| kind: Service |
| apiVersion: v1 |
| metadata: |
| labels: |
| app: wordpress-app |
| name: wordpress-app-spec |
| namespace: wordpress |
| spec: |
| type: NodePort |
| ports: |
| - name: http |
| port: 80 |
| protocol: TCP |
| targetPort: 80 |
| nodePort: 30031 |
| - name: https |
| port: 443 |
| protocol: TCP |
| targetPort: 443 |
| nodePort: 30033 |
| selector: |
| app: wordpress-app |
| root@k8s-deploy-ha:/data/k8sdata/wordpress |
| wordpress static |
| root@k8s-deploy-ha:/data/k8sdata/wordpress |
| <?php |
| phpinfo(); |
| ?> |
| root@k8s-deploy-ha:/data/k8sdata/wordpress |
| mysql> create database wordpress; |
| Query OK, 1 row affected (0.01 sec) |
| mysql> grant all privileges on wordpress.* to "wordpress"@"%" identified by "wordpress"; |
| Query OK, 1 row affected (0.01 sec) |
| mysql> flush privileges; |
| Query OK, 0 rows affected (0.03 sec) |
27. HPA