kubernetes 项目实战(nginx+ php + RDS(mysql))


我们将会从简入难一步一步的搭建这个系统,掌握 k8s 搭建项目的思路主要为理解k8s 的使用,不代表生产环境,因为有更简洁的方法。

首先我们要编写 Dockerfile 文件,在本机生成我们需要的基础镜像,生产环境下我们可以把镜像上传私有仓库,或者公共仓库中.

目的:把不经常变动的配置固定下来,可以更好的管理和配置整个项目。

Dockerfile 文件可以参考上个项目代码在这里

生成本地nginx 镜像

[root@k8s nginx]# docker build -t="wpic/nginx:latest" .
Sending build context to Docker daemon  8.192kB
Step 1/5 : FROM nginx
 ---> f6d0b4767a6c
Step 2/5 : MAINTAINER bushaoxun
 ---> Running in b3eff448d1ff
Removing intermediate container b3eff448d1ff
 ---> 3da757f54cea
Step 3/5 : EXPOSE 22 80
 ---> Running in f0e4256ef9e6
Removing intermediate container f0e4256ef9e6
 ---> 1138dec11c30
Step 4/5 : COPY wordpress.conf /etc/nginx/conf.d/default.conf
 ---> 13b4b3b01008
Step 5/5 : COPY nginx.conf /etc/nginx/nginx.conf
 ---> ea0b031273ae
Successfully built ea0b031273ae
Successfully tagged wpic/nginx:latest

生成本地php-fpm 镜像

docker build -t "wpic/php:7.4-fpm"

构建 Pod 运行 nginx 镜像

首先我们要构建一个 基于nginx 镜像的 Pod, yaml 文件如下

apiVersion: v1    # 为 wordpress 项目创建一个独立的命名空间
kind: Namespace
metadata:
  name: wordpress
---
apiVersion: v1
kind: Pod
metadata:
  name: wordpress
  namespace: wordpress
  labels:
    name: wordpress   # 为 Pod 设置标签,方便其他资源对象使用标签选择器
spec:
  containers:
  - name: wordpress
    image: wpic/nginx:latest
    imagePullPolicy: IfNotPresent  
    ports:
    - name: nginx
      containerPort: 80
      hostPort: 9000  # 映射容器端口到宿主机端口
  restartPolicy: OnFailure

 挂载宿主机目录到 Pod 中 (实现代码永久性存储)

apiVersion: v1
kind: Namespace
metadata:
  name: wordpress
---
apiVersion: v1
kind: Pod
metadata:
  name: wordpress
  namespace: wordpress
  labels:
    name: wordpress
spec:
  containers:
  - name: wordpress
    image: wpic/nginx:latest
    imagePullPolicy: IfNotPresent
    ports:
    - name: nginx
      containerPort: 80
      hostPort: 9000
    volumeMounts: 
    - name: wordpress  # 挂在本地存储到容器目录
      mountPath: "/usr/share/nginx/html"
  restartPolicy: OnFailure
  volumes:
  - name: wordpress  # 定义本地储存
    hostPath:
      path: "/var/www/html"

 添加 php-fpm 容器, 我们使用官方镜像 php:7.4-fpm

apiVersion: v1
kind: Namespace
metadata:
  name: wordpress
---
apiVersion: v1
kind: Pod
metadata:
  name: wordpress
  namespace: wordpress
  labels:
    name: wordpress
spec:
  containers:
  - name: nginx
    image: wpic/nginx:latest
    imagePullPolicy: IfNotPresent
    ports:
    - name: nginx
      containerPort: 80
      hostPort: 9000
    volumeMounts:
    - name: wordpress
      mountPath: "/usr/share/nginx/html"
  - name: php-fpm
    image: wpic/php:7.4-fpm
    lifecycle:   # 授予 wordpress 读写权限
      postStart:
        exec:
          command:
          - "chown"
          - "-R"
          - "www-data.www-data"
          - "/usr/share/nginx/html"
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 9000
    volumeMounts:    #必须在容器中挂载,和 nginx 配置有关
    - name: wordpress
      mountPath: "/usr/share/nginx/html"
  restartPolicy: OnFailure
  volumes:
  - name: wordpress
    hostPath:
      path: "/var/www/html"

 以 ConfigMap 的形式把nginx 配置文件挂载到 nginx 容器中 (生产环境中,我们一定要自己生成 特有的 docker 镜像,固定不常更改的配置文件到镜像中)

apiVersion: v1
kind: Namespace
metadata:
  name: wordpress
---
apiVersion: v1
kind: Pod
metadata:
  name: wordpress
  namespace: wordpress
  labels:
    name: wordpress
spec:
  containers:
  - name: nginx
    image: wpic/nginx:latest
    imagePullPolicy: IfNotPresent
    ports:
    - name: nginx
      containerPort: 80
      hostPort: 9000
    volumeMounts:
    - name: wordpress
      mountPath: "/usr/share/nginx/html"
    - name: configmap-nginx  # 挂载 nginx 配置文件
      mountPath: /etc/nginx/conf.d
  - name: php-fpm
    image: wpic/php:7.4-fpm
    lifecycle:    # 为 wordpress 授予读写的权限
      postStart:
        exec:
          command:
          - "chown"
          - "-R"
          - "www-data.www-data"
          - "/usr/share/nginx/html"
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 9000
    volumeMounts:
    - name: wordpress
      mountPath: "/usr/share/nginx/html"
  restartPolicy: OnFailure
  volumes:
  - name: wordpress
    hostPath:
      path: "/var/www/html"
  - name: configmap-nginx
    configMap:       #定义 ConfigMap 为数据卷
      name: nginx
      items:
      - key: wordpress.conf
        path: wordpress.conf

# 在命名空间 wordpress 中创建名字为 ConfigMap 对象, configmap-nginx 是目录,目录中存在配置文件 wordpress.conf.
 kubectl create configmap nginx --from-file=configmap-nginx -n wordpress
# wordpress.conf 文件内容如下
 server {
        listen       80;
        server_name  0.0.0.0;
        root   /usr/share/nginx/html;
        index  index.php index.html;

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

        location ~.*\.php$ {
           fastcgi_index index.php;
           try_files $uri =404;
           fastcgi_buffers      32 32K;
           fastcgi_buffer_size  32K;
           fastcgi_pass 127.0.0.1:9000;
           fastcgi_read_timeout 600; 
           #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
        }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM