k8s部署nginx+php+mysql

mysql部署参考我之前文档
http://t.csdn.cn/rwxd7

一.hostPath创建项目

1.编辑dockerfile

vi dockerfile
i
FROM docker.io/openshift/base-centos7:latest

#MAINTAINER feiyu "[email protected]"

RUN yum makecache

RUN yum -y install php-fpm php php-gd php-mysql php-mbstring php-xml php-mcrypt  php-imap php-odbc php-pear php-xmlrpc

RUN sed -i 's/listen = 127.0.0.1:9000/listen = 0.0.0.0:9000/' /etc/php-fpm.d/www.conf

RUN sed -i 's/listen.allowed_clients = 127.0.0.1/;listen.allowed_clients = 127.0.0.1/' /etc/php-fpm.d/www.conf

EXPOSE 9000

CMD ["/sbin/php-fpm"]

#编译

#编译包
docker build -t php:0.1 .


#打包镜像
docker save php:0.1 > php-0.1.tar

#加载镜像
docker load -i php-0.1.tar

2.部署php

编辑php-deloy-ser.yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-server
  labels:
    name: php-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: php-server
  template:
    metadata:
      labels:
        app: php-server
    spec:
      containers:
      - name: php-server
        image: php:0.1
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - mountPath: /var/www/html/
          name: nginx-data
        ports:
        - containerPort: 9000
      volumes:
      - name: nginx-data
        hostPath:
         path: /root/k8s/html
---
apiVersion: v1
kind: Service
metadata:
  name: php
spec:
  ports:
  - name: php
    port: 9000
    protocol: TCP
    targetPort: 9000
  selector:
    app: php-server

#安装

#部署
kubectl apply -f php-deloy-ser.yaml

#检查
kubectl get pod,deploy,svc |grep php

3.部署nginx

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-php
spec:
  selector:
    matchLabels:
      app: nginx-php
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-php
    spec:
      containers:
      - name: nginx-php
        image: nginx:1.23.1
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-data
          mountPath: /usr/share/nginx/html
        - name: nginx-conf
          mountPath: /etc/nginx/conf.d/
      volumes:
      - name: nginx-data
        hostPath:
         path: /root/k8s/html
      - name: nginx-conf
        hostPath:
         path: /root/k8s/conf
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-php
spec:
  type: NodePort
  ports:
  - name: nginx
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30004
  selector:
    app: nginx-php

#安装

#部署
kubectl apply -f nginx-deploy-ser.yaml

#检查
kubectl get pod,deploy,svc |grep nginx

新增nginx配置

cat > /root/k8s/conf/default.conf <

新建index.php测试页面

cat > /root/k8s/html/index.php <
EOF

4.访问测试页面,本机ip加nginx外网端口

192.168.1.xx:30004

二.StorageClass存储创建项目

1.创建pv

vi 01-pvc-nginx-php.yaml
i
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nginx-php-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
  # 存储类,具有相同存储类名称的pv和pvc才能进行绑定
  storageClassName: nfs-boge# 定义mysql的持久卷声明信息
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nginx-php-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
  # 存储类,具有相同存储类名称的pv和pvc才能进行绑定
  storageClassName: nfs-boge



#创建pv
kubectl apply -f 01-pvc-nginx-php.yaml

2.nginx和php配置文件

vi 02-configMap-nginx-php.yaml
i
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  default.conf: |
    server {
      listen 80;
      server_name _;

      location / {
        root /var/www/html;
        index index.html index.htm index.php;
      }

      location ~ \.php$ {
        root /var/www/html;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
    }

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: php-config
data:
  php.ini: |
    memory_limit = 256M
    display_errors = Off
    error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
    post_max_size = 32M
    upload_max_filesize = 32M
    max_execution_time = 60
    max_input_time = 60
    date.timezone = "Asia/Shanghai"
    [opcache]
    opcache.enable = 1
    opcache.memory_consumption = 256
    opcache.interned_strings_buffer = 16
    opcache.max_accelerated_files = 10000
    opcache.validate_timestamps = 1
    opcache.revalidate_freq = 60


#memory_limit:指定 PHP 进程可以使用的内存上限,单位为兆字节。
#display_errors:指定是否在浏览器中显示 PHP 错误信息。设置为 Off 则不会显示错误信息。
#error_reporting:指定 PHP 报告的错误级别。E_ALL 表示报告所有错误,~E_DEPRECATED 和 ~E_STRICT 表示不报告过时和严格模式的错误。
#post_max_size:指定允许上传的 POST 数据的最大大小,单位为兆字节。
#upload_max_filesize:指定允许上传的单个文件的最大大小,单位为兆字节。
#max_execution_time:指定 PHP 进程的最长执行时间,单位为秒。如果 PHP 脚本执行时间超过此值,则 PHP 进程将被终止。
#max_input_time:指定 PHP 处理输入数据(例如 POST 数据和上传文件)的最长时间,单位为秒。
#date.timezone:指定时区,这里设置为亚洲/上海。
#opcache.enable:指定是否启用 PHP OPCache,OPCache 是 PHP 5.5 以上版本的一个内置的缓存模块,可以提高 PHP 的性能。
#opcache.memory_consumption:指定 OPCache 可以使用的最大内存量,单位为兆字节。
#opcache.interned_strings_buffer:指定用于缓存字符串的缓冲区大小。
#opcache.max_accelerated_files:指定可以缓存的 PHP 文件的最大数量。
#opcache.validate_timestamps:指定是否检查文件的修改时间以确定缓存是否过期。设置为 1 则表示开启此功能。
#opcache.revalidate_freq:指定多长时间重新验证一次缓存中的文件,单位为秒。


#创建configMap
kubectl apply -f 02-configMap-nginx-php.yaml


3.创建php

vi 03-php-deloy-svc.yaml
i
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-server
  labels:
    name: php-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: php-server
  template:
    metadata:
      labels:
        app: php-server
    spec:
      containers:
      - name: php-server
        image: php:0.1
        imagePullPolicy: IfNotPresent
        env:
        - name: TZ
          value: Asia/Shanghai
        volumeMounts:
        - mountPath: /var/www/html/
          name: nginx-data
        - mountPath: /usr/local/etc/php/conf.d
          name: php-config
        ports:
        - containerPort: 9000
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
          claimName: nginx-php-pvc
      - name: php-config
        configMap:
          name: php-config
---
apiVersion: v1
kind: Service
metadata:
  name: php
spec:
  ports:
  - name: php
    port: 9000
    protocol: TCP
    targetPort: 9000
  selector:
    app: php-server


kubectl apply -f 03-php-deloy-svc.yaml

4.创建nginx

vi 04-nginx-deploy-svc.yaml
i
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-php
spec:
  selector:
    matchLabels:
      app: nginx-php
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-php
    spec:
      containers:
      - name: nginx-php
        image: nginx:1.23.1
        imagePullPolicy: IfNotPresent
        env:
        - name: TZ
          value: Asia/Shanghai
        ports:
        - containerPort: 80
        volumeMounts:
        - name: nginx-data
          mountPath: /usr/share/nginx/html
        - name: nginx-config
          mountPath: /etc/nginx/conf.d/
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
          claimName: nginx-php-pvc
      - name: nginx-config
        configMap:
          name: nginx-config
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-php
spec:
  type: NodePort
  ports:
  - name: nginx
    port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30004
  selector:
    app: nginx-php



kubectl apply -f 04-nginx-deploy-svc.yaml


5.测试Nginx访问php

cd /nfs_dir/default-nginx-php-pvc-pvc-7adf8df9-a01a-4584-9ae8-bcbec6929796

cat > index.php <
EOF


#测试url
http://ingress-http-test2.com:30004/

6.测试php连接mysql数据库


vi db.php
i
connect_error) {
    die("连接失败:" . $conn->connect_error);
}

// 执行查询语句
$sql = "SELECT * FROM user";
$result = $conn->query($sql);

// 检查查询结果是否为空
if ($result->num_rows > 0) {
    // 查询成功,返回yes
    echo "YES\n";
} else {
    // 查询失败,返回NO
    echo "NO\n";
}

// 关闭数据库连接
$conn->close();
?>



#测试url
http://ingress-http-test2.com:30004/db.php

你可能感兴趣的:(k8s,php,nginx,kubernetes)