k8s实现LNMP完全容器化站点案例
php写 nginx读
cat Dockerfile
FROM centos:7.8.2003
RUN yum install -y vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop
docker build -t centos:7.8.2003make .
192.168.192.155:80/pub-images/nginx-base-wordpress:v1.14.2 root@slave002:/opt/k8s-data/dockerfile/web/pub-images/nginx-base-wordpress# cat Dockerfile #Nginx Base Image FROM centos:7.8.2003make ADD nginx-1.14.2.tar.gz /usr/local/src/ RUN cd /usr/local/src/nginx-1.14.2 && ./configure --prefix=/apps/nginx && make && make install && ln -sv /apps/nginx/sbin/nginx /usr/sbin/nginx &&rm -rf /usr/local/src/nginx-1.14.2.tar.gz
root@slave002:/opt/k8s-data/dockerfile/web/chuan/wordpress/nginx# cat nginx.conf |grep -v "#" |grep -v "^$" 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 blogs.chuan.net; 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; } } }
192.168.192.155:80/chuan/wordpress-nginx:v1
root@slave002:/opt/k8s-data/dockerfile/web/chuan/wordpress/nginx# cat Dockerfile FROM 192.168.192.155:80/pub-images/nginx-base-wordpress:v1.14.2 RUN groupadd nginx && useradd nginx -g nginx -s /sbin/nologin -M 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 chown nginx.nginx /home/nginx/wordpress/ -R EXPOSE 80 443 CMD ["/apps/nginx/sbin/run_nginx.sh"]
root@slave002:/opt/k8s-data/dockerfile/web/chuan/wordpress/php# cat www.conf |grep nginx user = nginx group = nginx
192.168.192.155:80/chuan/wordpress-php-5.6:v2
root@slave002:/opt/k8s-data/dockerfile/web/chuan/wordpress/php# cat Dockerfile #PHP Base Image FROM centos:7.8.2003make 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 -u 2019 RUN groupadd nginx && useradd nginx -g nginx -s /sbin/nologin -M ADD run_php.sh /usr/local/bin/run_php.sh EXPOSE 9000 CMD ["/usr/local/bin/run_php.sh"]
root@slave002:/opt/k8s-data/yaml/chuan/wordpress# cat wordpress.yaml kind: Deployment #apiVersion: extensions/v1beta1 apiVersion: apps/v1 metadata: labels: app: wordpress-app name: wordpress-app-deployment namespace: chuan spec: replicas: 1 selector: matchLabels: app: wordpress-app template: metadata: labels: app: wordpress-app spec: containers: - name: wordpress-app-nginx image: 192.168.192.155:80/chuan/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: false - name: wordpress-app-php image: 192.168.192.155:80/chuan/wordpress-php-5.6:v2 #imagePullPolicy: IfNotPresent imagePullPolicy: Always ports: - containerPort: 9000 protocol: TCP name: http volumeMounts: - name: wordpress mountPath: /home/nginx/wordpress readOnly: false volumes: - name: wordpress nfs: server: 192.168.192.156 path: /data/k8sdata/chuan/wordpress --- kind: Service apiVersion: v1 metadata: labels: app: wordpress-app name: wordpress-app-spec namespace: chuan 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@slave002:/opt/k8s-data/yaml/chuan/wordpress# kubectl get po -nchuan -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES wordpress-app-deployment-7cbcdd9df4-hn8dm 2/2 Running 0 90s 10.200.111.10 192.168.192.153 <none> <none>
Filesystem Size Used Avail Use% Mounted on 192.168.192.156:/data/k8sdata/chuan/wordpress 120G 8.1G 112G 7% /home/nginx/wordpress
[root@wordpress-app-deployment-7cbcdd9df4-hn8dm wordpress]# cat index.html chuan ph
[root@wordpress-app-deployment-646467bd94-tsq2w wordpress]# cat index.php <?php phpinfo(); ?>
[root@wordpress-app-deployment-646467bd94-tsq2w wordpress]# netstat -nutpl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8/nginx: master pro
https://cn.wordpress.org/download/releases 下载地址
root@ubuntu20:/data/k8sdata/chuan/wordpress# ls
index.php readme.html wp-activate.php wp-blog-header.php wp-config-sample.php wp-cron.php wp-links-opml.php wp-login.php wp-settings.php wp-trackback.php
license.txt wordpress wp-admin wp-comments-post.php wp-content wp-includes wp-load.php wp-mail.php wp-signup.php xmlrpc.php
#注意权限为nginx:nginx id号要和pod中nginx id号一致
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, 0 rows affected, 1 warning (0.01 sec)<br>mysql> FLUSH PRIVILEGES;
wordpress ZvxR5^V3oOEjpoB#tA
root@ubuntu20:/data/k8sdata/chuan/wordpress# find /data/k8sdata/chuan/ -name aa.jpg
/data/k8sdata/chuan/wordpress/wp-content/uploads/2021/aa.jpg