2、实现k8s一个pod多个容器实现LNMP的wordpress服务

准备PHP镜像

docker pull php:5.6.40-fpm
docker tag php:5.6.40-fpm harbor.magedu.net/linux36/php:5.6.40-fpm 
docker push harbor.magedu.net/linux36/php:5.6.40-fpm

自制PHP镜像
tree /opt/k8s-data/dockerfile/linux36/wordpress/php
├── build-command.sh
├── Dockerfile
├── run_php.sh
└── www.conf

Dockerfile文件内容

#PHP Base Image
FROM harbor.magedu.net/baseimage/centos-base:v7.6
RUN user add -u 2001 && rpm -ivh 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
ADD run_php.sh /usr/local/bin/run_php.sh
EXPOSE 9000
CMD ["/usr/local/bin/run_php.sh"]

www.conf文件

user = nginx
group = nginx

run_php.sh脚本

#!/bin/bash
/opt/remi/php56/root/usr/sbin/php-fpm
#/opt/remi/php56/root/usr/sbin/php-fpm --nodaemonize
tail -f /etc/hosts

build-command脚本

#!/bin/bash
TAG=$1
docker build -t harbor.magedu.net/linux36/wordpress-php-5.6:${TAG} .
echo "镜像制作完成,即将上传至Harbor服务器"
sleep 1
docker push harbor.magedu.net/linux36/wordpress-php-5.6:${TAG}
echo "镜像上传完成"

执行构建PHP 业务镜像
bash build-command

准备Nginx镜像
tree /opt/k8s-data/dockerfile/linux36/wordpress/nginx
├── build-command.sh
├── Dockerfile
├── index.html
├── nginx.conf
└── run_nginx.sh

Dockerfile文件内容

#Nginx Base Image
FROM harbor.magedu.net/pub-images/nginx-base:v1.14.2 
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
ADD run_nginx.sh /usr/local/nginx/sbin/run_nginx.sh
RUN mkdir -pv /home/nginx/wordpress
RUN chown nginx.nginx /home/nginx/wordpress/ -R
EXPOSE 80 443
CMD ["/usr/local/nginx/sbin/run_nginx.sh"] 

nginx.conf文件

server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root    /home/nginx/wordpress;
            index   index.php index.html index.htm;
            #if ($http_user_agent ~ "ApacheBench|WebBench|TurnitinBot|Sogou web spider|Grid Service") {
            #    proxy_pass http://www.baidu.com;
            #    #return 403;
            #}
        }
        location ~ \.php$ {
            root           /home/nginx/wordpress;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
             include        fastcgi_params;
        }
...

run_nginx.sh脚本

#!/bin/bash
#echo "nameserver 10.20.254.254" > /etc/resolv.conf
/usr/local/nginx/sbin/nginx
tail -f /etc/hosts

build-command脚本

#!/bin/bash
TAG=$1
docker build -t harbor.magedu.net/linux36/wordpress-nginx:${TAG} .
echo "镜像制作完成,即将上传至Harbor服务器"
sleep 1
docker push  harbor.magedu.net/linux36/wordpress-nginx:${TAG}
echo "镜像上传完成"

执行构建nginx 业务镜像
bash build-command

运行WordPress

/opt/k8s-data/yaml/linux36/wordpress/wordpress.yaml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  labels:
    app: wordpress-app
  name: wordpress-app-deployment
  namespace: linux36
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress-app
  template:
    metadata:
      labels:
        app: wordpress-app
    spec:
      containers:
      - name: wordpress-app-nginx
        image: harbor.magedu.net/linux36/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: harbor.magedu.net/linux36/wordpress-php-5.6:v1 
        #command: ["/apps/tomcat/bin/run_tomcat.sh"]
        #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.7.108
          path: /data/k8sdata/linux36/wordpress 


---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: wordpress-app
  name: wordpress-app-spec
  namespace: linux36
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

kubectl apply -f .

k8s中MySQL创建数据库

kubectl exec -it mysql-0 sh -n linux36
mysql> CREATE DATABASE wordpress;
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO "wordpress"@"%" IDENTIFIED BY "wordpress";

访问wordpress
http://ip:port/wp-admin/setup-config.php

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。