【云原生】Dockerfile制作WordPress镜像,实现Compose + K8s编排部署

news2024/9/23 1:30:03

文章目录

  • 👹 关于作者
  • 前言
  • 环境准备
    • 目录结构
  • dockerfile制作镜像
    • yum 脚本
    • Dockerfile-mariadb 镜像
    • Dockerfile-service 镜像
    • docker compose 编排
  • K8s部署
    • svc
    • deploy
  • ✊ 最后

在这里插入图片描述

👹 关于作者


大家好,我是秋意零。

😈 CSDN作者主页

  • 😎 博客主页

👿 简介

  • 👻 普通本科生在读
  • 在校期间参与众多计算机相关比赛,如:🌟 “省赛”、“国赛”,斩获多项奖项荣誉证书
  • 🔥 各个平台,秋意临 账号创作者
  • 🔥 云社区 创建者
点赞、收藏+关注下次不迷路!

欢迎加入云社区


前言

今天给各位带来一个出色网站、博客系统 WordPress,不过不使用 Docker Hub 提供的 WordPress Docker镜像,我们使用 Dockerfile 自己制作,实现 LNMP WordPress 运行环境,并将 WordPress 部署再其基础之上

为什么不使用 Docker Hub 提供的 WordPress 镜像部署呢?

环境准备

  • Linux 7.5
  • docker v23.0.1
  • docker compose v2.17.0
  • WordPress v6.2

注意:这里的环境是博主使用环境,不限于此

新手小白教程

Centos7.5安装教程
Docker安装教程
Docker-Compose安装教程

目录结构

[root@master01 ~]# tree docker
docker
├── db.sh   #数据库启动、配置脚本
├── default.conf  #nginx配置文件,配置支持 php 
├── docker-compose.yaml  # compose 文件
├── Dockerfile-mariadb  # maraidb dockerfile文件
├── Dockerfile-service  # nginx+php+wordpress dockerfile文件
├── wordpress-6.2-zh_CN.zip  # wordpress安装包
├── wp-config.php  # wordpress配置文件,这里主要配置数据库部分
└── yum.sh  #yum源配置脚本

0 directories, 8 files

dockerfile制作镜像

yum 脚本

yum脚本是两个 dockerfile 文件公用的脚本,因为这里都是使用 yum 安装的服务

# 清除默认yum
rm -rf /etc/yum.repos.d/*

# 阿里云 centos7 yum
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

#nginx yum
cat  > /etc/yum.repos.d/nginx.repo << EOF
[nginx]
name=nginx
baseurl=https://nginx.org/packages/centos/7/x86_64/
gpgcheck=0
enabled=1
EOF

#mariadb yum
cat > /etc/yum.repos.d/mariadb.repo <<EOF
[mariadb]
name=mariadb
baseurl=https://mirrors.tuna.tsinghua.edu.cn/mariadb/mariadb-10.5.19/yum/centos/7/x86_64/
gpgcheck=0
enabled=1
EOF

#php yum
yum -y install epel-release
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

Dockerfile-mariadb 镜像

yum.sh、db.sh 是 Dockerfile-mariadb 构建镜像时所需要的文件

db.sh 启动配置脚本

db.sh 是数据库启动、设置密码、创建数据库以及授权的脚本

cat > db.sh << EOF
#!/bin/bash
mysql_install_db --user=root
mysqld_safe --user=root &
sleep 3
mysqladmin -u root password '000000'
mysql -uroot -p000000 -e "create database wordpress;"
mysql -uroot -p000000 -e "grant all on wordpress.* to root@localhost identified by '000000';"
mysql -uroot -p000000 -e "grant all on wordpress.* to root@'%' identified by '000000';"
EOF

Dockerfile-mariadb

cat > Dockerfile-mariadb << EOF
FROM centos:centos7.9.2009
MAINTAINER qyl
COPY yum.sh /opt/
COPY db.sh /opt
RUN sh /opt/yum.sh && yum clean all
RUN yum install -y mariadb-server
RUN sh /opt/db.sh
EXPOSE 3306
CMD ["mysqld_safe","--user=root"]
EOF

构建镜像

docker build -t wp-mariadb:v1 -f Dockerfile-mariadb .

Dockerfile-service 镜像

yum.sh、default.conf 、wp-config.php 是 Dockerfile-service 构建镜像时所需要的文件

default.conf

这是配置 nginx 能代理 php 网页的配置

cat  > default.conf << EOF
server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm; #修改部分
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        root           /usr/share/nginx/html;  #修改部分
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name; #修改部分
        include        fastcgi_params;
    }
}
EOF

wp-config.php

在安装 wordpress 时 wordpress 配置文件无法自动写入时,使用这种方式手动写入(主要配置数据库部分)

注意:数据库部分的配置

在这里插入图片描述

cat > wp-config.php << EOF
<?php
/**
 * The base configuration for WordPress
 *
 * The wp-config.php creation script uses this file during the installation.
 * You don't have to use the web site, you can copy this file to "wp-config.php"
 * and fill in the values.
 *
 * This file contains the following configurations:
 *
 * * Database settings
 * * Secret keys
 * * Database table prefix
 * * ABSPATH
 *
 * @link https://wordpress.org/documentation/article/editing-wp-config-php/
 *
 * @package WordPress
 */

// ** Database settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'wordpress' ); # 根据自己数据库修改

/** Database username */
define( 'DB_USER', 'root' );  # 根据自己数据库修改

/** Database password */
define( 'DB_PASSWORD', '000000' ); # 根据自己数据库修改

/** Database hostname */
define( 'DB_HOST', 'mariadb:3306' );  # 这个 mariadb 对应 compose 里面的服务名

/** Database charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );

/** The database collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );

/**#@+
 * Authentication unique keys and salts.
 *
 * Change these to different unique phrases! You can generate these using
 * the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}.
 *
 * You can change these at any point in time to invalidate all existing cookies.
 * This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
define( 'AUTH_KEY',         'put your unique phrase here' );
define( 'SECURE_AUTH_KEY',  'put your unique phrase here' );
define( 'LOGGED_IN_KEY',    'put your unique phrase here' );
define( 'NONCE_KEY',        'put your unique phrase here' );
define( 'AUTH_SALT',        'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT',   'put your unique phrase here' );
define( 'NONCE_SALT',       'put your unique phrase here' );

/**#@-*/

/**
 * WordPress database table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix = 'wp_';

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the documentation.
 *
 * @link https://wordpress.org/documentation/article/debugging-in-wordpress/
 */
define( 'WP_DEBUG', false );

/* Add any custom values between this line and the "stop editing" line. */



/* That's all, stop editing! Happy publishing. */

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
        define( 'ABSPATH', __DIR__ . '/' );
}

/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';
EOF

Dockerfile-service

cat > Dockerfile-service << EOF
FROM centos:centos7.9.2009
MAINTAINER qyl
WORKDIR /opt

COPY wordpress-6.2-zh_CN.zip ./
COPY yum.sh /opt/
RUN sh /opt/yum.sh
RUN yum clean all &&\
    yum install -y nginx unzip  &&\
    unzip ./wordpress-6.2-zh_CN.zip

# 安装php7.4环境
RUN yum install -y php74-php-devel php74-php php74-php-cli php74-php-common php74-php-gd php74-php-ldap php74-php-mbstring php74-php-mcrypt php74-php-pdo php74-php-mysqlnd  php74-php-fpm php74-php-opcache php74-php-pecl-redis php74-php-pecl-mongodb php74-php-fpm

#修改 php-fpm 的用户和组为nginx
RUN sed -i 's/^user\ =\ apache/user\ =\ nginx/g' /etc/opt/remi/php74/php-fpm.d/www.conf && \
    sed -i 's/^group\ =\ apache/group\ =\ nginx/g' /etc/opt/remi/php74/php-fpm.d/www.conf &&\
    php74 -v

RUN rm -rf /usr/share/nginx/html/* &&\
    cp -rf ./wordpress/* /usr/share/nginx/html/

COPY default.conf /etc/nginx/conf.d/default.conf
COPY wp-config.php /usr/share/nginx/html/
EXPOSE 80

# 为了进入特权模式,所要运行的环境
CMD /usr/sbin/init
EOF

构建镜像

docker build -t wp-service:v1 -f Dockerfile-service .

docker compose 编排

使用上述构建的镜像编排容器

cat > docker-compose.yaml << EOF
version: '3'
services:
  mariadb:
    image: wp-mariadb:v1
    container_name: db
    ports:
    - 3306:3306
  wordpress:
    image: wp-service:v1
    container_name: wordpress
    privileged: true # 开启容器特权模式
    ports:
    - 80:80
    links:
    - mariadb  # 服务名
    depends_on:
    - mariadb
EOF

compose 启动容器

[root@master01 docker]# docker compose up -d
[+] Running 3/3
 ✔ Network docker_default  Created                                                                                    0.1s
 ✔ Container db            Started                                                                                    0.6s
 ✔ Container wordpress     Started                                                                                    1.3s


[root@master01 docker]# docker compose ps
NAME                IMAGE               COMMAND                  SERVICE             CREATED              STATUS              PORTS
db                  wp-mariadb:v1       "mysqld_safe --user=…"   mariadb             About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp
wordpress           wp:v1               "/bin/sh -c /usr/sbi…"   wordpress           About a minute ago   Up About a minute   0.0.0.0:80->80/tcp, :::80->80/tcp

进入 wordpress 容器启动 php-fpm、nginx

因为这里使用的 yum 源安装的(其他方式也可以),没有php-fpm二进制命令启动 php-fpm 服务,而使用 systemctl 启动服务需要权限,也就是 compose 里面的 privileged: true 字段和 Dockerfile-service 里面的 CMD /usr/sbin/init,这样就能保证在容器使用systemctl启动服务了

docker exec -it wordpress bash
systemctl restart php74-php-fpm
systemctl restart nginx

浏览器访问 80 端口,安装 wordpress

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

K8s部署

svc

使用 svc 暴露服务

[root@master01 wordpress]# cat service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: mariadb
  name: mariadb
spec:
  clusterIP: None
  selector:
    app: mariadb
  type: ClusterIP
  ports:
  - port: 3306

---
apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
  type: NodePort

deploy

包含 mariadb 和 wordpress 服务

[root@master01 wordpress]# cat deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mariadb
  name: mariadb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mariadb
  template:
    metadata:
      labels:
        app: mariadb
    spec:
      containers:
      - image: wp-mariadb:v1
        name: mariadb
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: wordpress
  name: wordpress
spec:
  replicas: 1
  selector:
    matchLabels:
      app: wordpress
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - image: wp-service:v1
        name: wordpress
        securityContext:
          privileged: true

进入WordPress pod 启动 php-fpm、nginx 服务

[root@master01 wordpress]# kubectl get pod
NAME                         READY   STATUS        RESTARTS      AGE
mariadb-5cd9b8655d-cq8nd     1/1     Running       0             4s
wordpress-744964c4cb-sk47g   1/1     Running       0             4s
[root@master01 wordpress]# kubectl exec -it pod/wordpress-744964c4cb-sk47g -- bash
[root@wordpress-744964c4cb-sk47g opt]# systemctl restart php74-php-fpm
[root@wordpress-744964c4cb-sk47g opt]# systemctl restart nginx

浏览器访问

[root@master01 wordpress]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        21d
mariadb      ClusterIP   None            <none>        3306/TCP       10h
wordpress    NodePort    10.97.150.148   <none>        80:32214/TCP   10h

在这里插入图片描述

✊ 最后


👏 我是秋意临,欢迎大家一键三连、加入云社区

👋 我们下期再见(⊙o⊙)!!!


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/465056.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

lambda的toMap是不是要注意点,线上事故

异常回顾 先看代码&#xff1a; dbTaxiDrivers.ifPresent((drivers) -> { map.putAll(drivers.stream() .collect(Collectors.toMap(TaxiDriverInfo::getOperationId, item -> item))); }); 相信很多为了减少2层for循环&#xff0c…

✨✨✨ ❃ ♕ ꕥ Xpath解析html获取表情符号,丰富你的文章 ꧁ ꧂꧁ ꧂

✨✨✨ ❃ ♕ ꕥ Xpath解析html获取表情符号&#xff0c;丰富你的文章 ꧁ ꧂꧁ ꧂ 1. 推荐几个好玩的表情符号网站2. xpath解析html获取表情3. xpath解析html源码3.1 parse_li.py3.2 symbol2.html 参考 1. 推荐几个好玩的表情符号网站 &#x1f495; &#x1f9da; &#x1f6…

让科幻照进现实,未来出行革命

在“新四化”&#xff08;电动化、网联化、智能化、共享化&#xff09;和“碳中和、碳达峰”双碳目标下&#xff0c;中国汽车产业正经历着前所未有之大变局。作为“智能化”核心之一的自动驾驶&#xff0c;在监管、技术和商业化方面持续积累、不断完善&#xff0c;即将迈入发展…

案例1:Java超市管理系统设计与实现开题报告

博主介绍&#xff1a;✌全网粉丝30W,csdn特邀作者、博客专家、CSDN新星计划导师、java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专…

浏览器【控制台】的小妙招-dom复制

了解前端开发的朋友应该对浏览器的控制台非常熟悉&#xff0c;毕竟日常里除了wife就是跟浏览器相处的最久了。 1、唤出控制台 打开一个网页&#xff1a; 按下键盘【F12】或者鼠标在网页任意位置【右键- 检查】&#xff0c;即可唤出浏览器的【控制台】 2、日常开发使用之【复制…

QuickTime Player + BlackHole解决Mac不能录内部声音

背景 在用mac录制屏幕时&#xff0c;发现不能录入电脑内的声音。 App Store中有一些收费的屏幕录制软件&#xff08;也需要安装特定的虚拟声卡&#xff09;。 现在实现完全免费的屏幕录制&#xff0c;QuickTime Player BlackHole组合&#xff0c;QuickTime Player是mac自带…

Android 12系统源码_CarService(一)CarService的基本架构

前言 1、Google官网上是这样介绍汽车架构的。 Car App&#xff1a;包括OEM和第三方开发的AppCar API&#xff1a;内有包含 CarSensorManager 在内的 API。位于 /platform/packages/services/Car/car-lib。CarService&#xff1a;系统中与车相关的服务&#xff0c;位于 /platfo…

报错记录:构造方法获取不了@Value的值,问题刨析与解决方案

报错记录&#xff1a;构造方法获取不了Value的值&#xff0c;问题刨析与解决方案 有时我们需要在构造函数中初始化属性&#xff0c;之前老的项目是用的I/O流来获取配置文件的值&#xff0c;目前配置转为线上&#xff0c;使用Apollo来获取值&#xff0c;由于获取Apollo的值被封装…

浅述 国产仪器 1761程控模块电源

1761程控模块电源是在自动测试环境中提供偏置功率和对部件或最终产品提供激励的理想设备&#xff0c;是测试系统必备的测试仪器。适用于研发、设计、生产制造等自动测试领域。 1761程控模块电源为用户选配电源提供了灵活性&#xff0c;根据需要可选购1&#xff5e;8种&#xff…

6.登录token

登录时生产token和refreshtoken&#xff0c;请求时带上token&#xff0c;后台校验&#xff0c;通过的话则进行处理&#xff0c;否则返回错误信息&#xff08;token失效过期等等&#xff09;&#xff0c;校验不通过会调用刷新token的接口&#xff0c;重新获取token&#xff0c;如…

< 封装公共导出模块:配合element实现提示 >

封装公共导出模块 &#x1f449; 前言&#x1f449; 一、原理&#x1f449; 二、实现案例&#x1f449; 三、效果演示往期内容 &#x1f4a8; &#x1f449; 前言 在 Vue elementUi 开发中&#xff0c;我们偶尔会遇到需要导出的列表&#xff0c;或者指定位置的导出内容。在一…

神经网络基础-手写数字识别

手写数字识别神经网络 基本原理 图像本质上被认为是一个矩阵&#xff0c;每个像素点都是一个对应的像素值&#xff0c;相当于在多维数据上进行相关的归类或者其他操作。 线性函数 线性函数的一个从输入到输出的映射&#xff0c;用于给目标一个每个类别对应的得分。 图像 ( …

leetcode刷题(9)二叉树(3)

各位朋友们&#xff0c;提前祝大家五一劳动节快乐啊&#xff01;&#xff01;&#xff01;今天我为大家分享的是关于leetcode刷题二叉树相关的第三篇我文章&#xff0c;让我们一起来看看吧。 文章目录 1.二叉树的层序遍历题目要求做题思路代码实现 2.从前序与中序遍历序列构造二…

Authing 正式发布应用集成网关 - Authing Gateway

2023 年 2月&#xff0c; Authing 推出了身份领域的 PaaS化应用集成网关 - Authing Gateway 。 Authing Gateway 提供将原有应用快速集成到 Authing 身份云产品的能力&#xff0c;在扩充身份认证方式的同时&#xff0c;提高资源的安全性和数据的隐私可靠性。 01.Authing Gatew…

如何查看声卡、pcm设备以及tinyplay、tinymix、tinycap的使用

命令列表 功能命令查看当前录音进程状态dumpsys media.audio_flinger查看当前音频策略状态dumpsys media.audio_policy查看pcm节点信息cat /proc/asound/pcm查看声卡信息cat /proc/asound/cards查看声卡物理设备节点ls /dev/snd/驱动层录音命令tinycap xx.wav -D 0 -d 1 -c 2 …

【Java EE】-博客系统(前端页面)

作者&#xff1a;学Java的冬瓜 博客主页&#xff1a;☀冬瓜的主页&#x1f319; 专栏&#xff1a;【JavaEE】 分享: 且视他人如盏盏鬼火&#xff0c;大胆地去走你的道路。——史铁生《病隙碎笔》 主要内容&#xff1a;博客系统 登陆页面&#xff0c;列表页面&#xff0c;详情页…

OpenAI推企业版ChatGPT,英伟达造AI安全卫士

GPT现在已经进入了淘金时代。虽然全球涌现出成千上万的大模型或ChatGPT变种&#xff0c;但一直能挣钱的人往往是卖铲子的人。 这不&#xff0c;围绕暴风眼中的大模型&#xff0c;已经有不少企业&#xff0c;开始研究起了大模型的“铲子”产品&#xff0c;而且开源和付费两不误…

【C++】——string的功能介绍及使用

前言&#xff1a; 在上期&#xff0c;我们简单的介绍了关于 模板和STL &#xff0c;今天我就带领大家学习一下关于 【string】类。本期&#xff0c;我们主要讲解的是关于 【string】的基本介绍以及【string】类的常用接口说明。有了以上的基本认识之后&#xff0c;在下期&…

全球SPD市场迎来黄金时代,中国领跑全球增长

近日&#xff0c;专注于前沿领域的国际咨询机构ICV发布了全球单光子探测器市场研究报告&#xff0c;报告分析了单光子探测器&#xff08;SPD&#xff09;市场&#xff0c;包括产品定位、下游应用、主要供应商、市场情况和未来趋势等各个方面&#xff0c;以进行分析和预测。 研究…

微服务 - kong安装,API网关设计(原理篇)

概述 微服务实践的第二个关键组件&#xff0c;微服务API网关设计&#xff0c;API网关是对微服务做统一的鉴权、限流、黑白名单、负载均衡等功能实现,这篇我们先来介绍Api网关的意义和安装kong/konga需要的组件。 网关的作用和意义 网关可以使得服务本身更专注自己的领域&…