应用开发入门

    WordPress 后端存储需要依赖数据库,实例使用的 MySQL 数据库,该应用需要创建 2 个 docker 镜像。

    由于需要实现配置变更及管理组件的自动升级,不能直接使用已有的 docker 镜像,需要进行一些改造,Docker 镜像默认启动的进程不能是应用本身的进程,而应该是 ,由 supervisor 来启动 drone 服务。

    1、将 drone 和 nsenter 添加到镜像,然后安装 supervisor 并进行适当的配置。

    • drone 是 OpenPitrix 的 Agent 服务,与 OpenPitrix 调度系统进行交互。
    • nsenter 用来在 Docker 内进入 Host 执行一些指令,比如挂载硬盘、ssh 免密登录等。
    • supervisor 用来为 drone 的自动升级提供支持服务。可在制作 Docker 镜像时从 镜像中拷贝获取 drone 和 nsenter 这两个可执行文件以及 supervisor 相关的启动脚本:

    Dockerfile

    其中 start-supervisord.sh 的内容如下:

    1. #!/bin/sh
    2. exec supervisord -n

    2、制作 MySQL 镜像并上传到镜像仓库,请预先准备好 Docker 镜像仓库( ${registry} 替换为您实际的镜像仓库的名字)。

    1. $ docker build -t ${registry}/mysql .
    2. $ docker push ${registry}/mysql

    第二步:制作 Wordpress 镜像

    conf.d/mysql_hostname.toml

    1. [template]
    2. src = "mysql_hostname.tmpl"
    3. dest = "/opt/openpitrix/conf/mysql_hostname"
    4. keys = [
    5. "/",
    6. ]

    templates/mysql_hostname.tmpl

    说明:tmpl 模版文件决定应用配置文件内容。drone 读取 metadata service 并刷新这些变量的值,如此例 range 这一行是读取 Mysql 节点的 IP 信息。了解更多参见 。

    2、新建目录,并将上一步准备的两个模板文件拷贝到 /etc/confd/ 目录下。然后,可在制作 Docker 镜像时从 openpitrix/openpitrix:metadata 镜像中拷贝获取 drone 和 nsenter 可执行文件以及 supervisor 相关的启动脚本:

    Dockerfile

    1. FROM wordpress
    2. COPY --from=openpitrix/openpitrix:metadata /usr/local/bin/drone /usr/local/bin/
    3. COPY --from=openpitrix/openpitrix:metadata /usr/bin/nsenter /usr/bin/
    4. RUN apt-get update && apt-get install -y supervisor
    5. COPY --from=openpitrix/openpitrix:metadata /etc/supervisor.d/drone.ini /etc/supervisor/conf.d/drone.conf
    6. COPY --from=openpitrix/openpitrix:metadata /usr/local/bin/start-drone.sh /usr/local/bin/start-drone.sh
    7. COPY start-supervisord.sh /usr/local/bin/
    8. ENTRYPOINT ["start-supervisord.sh"]
    9. RUN mkdir -p /etc/confd/conf.d/
    10. RUN mkdir -p /etc/confd/templates/
    11. COPY conf.d/* /etc/confd/conf.d/
    12. COPY templates/* /etc/confd/templates/
    1. $ docker build -t ${registry}/wordpress .
    2. $ docker push ${registry}/wordpress

    准备应用配置包

    以下命令默认下载最新的客户端工具,拷贝文件到指定的 bin 目录。以 v0.2.3 为例,执行命令时应替换为实际的下载版本号:

    1. $ curl -L https://git.io/GetOpenPitrixBin | sh -
    2. $ cd openpitrix-v0.2.3-bin/
    3. $ cp * /usr/local/bin/

    第二步:创建应用目录

    其中 cluster.json.tmpl,config.json,package.json 这三个配置文件是必须的,详见 开发模板规范

    Wordpress/cluster.json.tmpl

    1. {
    2. "name": "{{.cluster.name}}",
    3. "description": "{{.cluster.description}}",
    4. "subnet": "{{.cluster.subnet}}",
    5. "nodes": [
    6. {
    7. "role": "Mysql",
    8. "container": {
    9. "type": "docker",
    10. "image": "${registry}/mysql"
    11. },
    12. "count": 1,
    13. "cpu": "{{.cluster.Mysql.cpu}}",
    14. "memory": "{{.cluster.Mysql.memory}}",
    15. "services": {
    16. "start": {
    17. "cmd": "MYSQL_ROOT_PASSWORD=wordpress MYSQL_ROOT_HOST=% docker-entrypoint.sh mysqld & sleep 10"
    18. }
    19. }
    20. },
    21. {
    22. "role": "Wordpress",
    23. "type": "docker",
    24. "image": "${registry}/wordpress"
    25. },
    26. "cpu": "{{.cluster.Wordpress.cpu}}",
    27. "memory": "{{.cluster.Wordpress.memory}}",
    28. "services": {
    29. "start": {
    30. "order": 1,
    31. "cmd": "WORDPRESS_DB_PASSWORD=wordpress WORDPRESS_DB_HOST=`cat /opt/openpitrix/conf/mysql_hostname` docker-entrypoint.sh apache2-foreground & sleep 10"
    32. }
    33. }
    34. }
    35. ]
    36. }

    Wordpress/config.json

    1. {
    2. "type": "array",
    3. "properties": [
    4. {
    5. "key": "cluster",
    6. "description": "WORDPRESS properties",
    7. "type": "array",
    8. "properties": [
    9. {
    10. "key": "name",
    11. "label": "name",
    12. "description": "The name of the WORDPRESS service",
    13. "type": "string",
    14. "default": "Wordpress",
    15. "required": false
    16. },
    17. {
    18. "key": "description",
    19. "label": "description",
    20. "description": "The description of the WORDPRESS service",
    21. "type": "string",
    22. "default": "",
    23. "required": false
    24. },
    25. {
    26. "key": "subnet",
    27. "label": "Subnet",
    28. "description": "Choose a subnet to join",
    29. "type": "string",
    30. "default": "",
    31. "required": true
    32. },
    33. {
    34. "key": "Mysql",
    35. "label": "Mysql",
    36. "description": "MySql properties",
    37. "type": "array",
    38. "properties": [
    39. {
    40. "key": "cpu",
    41. "label": "CPU",
    42. "description": "CPUs of each node",
    43. "type": "integer",
    44. "default": 1,
    45. "range": [1,2,4,8,16],
    46. "required": true
    47. },
    48. {
    49. "key": "memory",
    50. "label": "Memory",
    51. "default": 2048,
    52. "range": [2048,8192,16384,32768,49152],
    53. "required": true
    54. }
    55. ]
    56. },
    57. {
    58. "key": "Wordpress",
    59. "label": "Wordpress",
    60. "description": "Wordpress properties",
    61. "type": "array",
    62. "properties": [
    63. {
    64. "key": "cpu",
    65. "label": "CPU",
    66. "description": "CPUs of each node",
    67. "type": "integer",
    68. "default": 1,
    69. "range": [1,2,4],
    70. "required": true
    71. },
    72. {
    73. "key": "memory",
    74. "label": "Memory",
    75. "description": "memory of each node (in MB)",
    76. "type": "integer",
    77. "default": 1024,
    78. "range": [1024,2048,4096],
    79. "required": true
    80. }
    81. ]
    82. }
    83. ]
    84. }
    85. ]
    86. }

    Wordpress/package.json

    1. {
    2. "api_version":"v1",
    3. "name":"Wordpress",
    4. "version":"0.1.0",
    5. "app_version":"4.9.7",
    6. "keywords": [
    7. "wordpress",
    8. "blog",
    9. "http",
    10. "web",
    11. "application",
    12. "php",
    13. "cms"
    14. ],
    15. "description":"Web publishing platform for building blogs and websites.",
    16. "icon": "https://openpitrix.pek3a.qingstor.com/icon/wordpress_icon.png",
    17. "home": "http://www.wordpress.com/",
    18. "maintainers": [
    19. {
    20. "email": "openpitrix@yunify.com",
    21. "name": "openpitrix"
    22. }
    23. ],
    24. "screenshots": [
    25. "https://openpitrix.pek3a.qingstor.com/icon/wordpress_snapshot1.png",
    26. "https://openpitrix.pek3a.qingstor.com/icon/wordpress_snapshot2.png"
    27. }

    第四步:打包应用

    回到 Wordpress 上级目录,执行打包命令: