2017年4月7日星期五

OpenShift_073:部署 Spring Boot 应用(Template 方式)

环境:OCP 3.4

1. 复制 Spring Boot builder 镜像 (在 MAC 机器上操作)
docker save -o spring-boot-rhel7.tar.gz maping930883/spring-boot-rhel7:latest
scp spring-boot-rhel7.tar.gz root@192.168.56.112:/opt/ose/images/

2. 修改 Tag,并 Push 到本地镜像仓库 (在 Registry 机器上操作)
cd /opt/ose/images
docker load -i spring-boot-rhel7.tar.gz
docker tag maping930883/spring-boot-rhel7:latest registry.example.com:5000/maping930883/spring-boot-rhel7:latest
docker push registry.example.com:5000/maping930883/spring-boot-rhel7:latest

3. 克隆 springboot-sample-app 到本地 (在 MAC 机器上操作)
cd ~/mygit
git clone https://github.com/codecentric/springboot-sample-app.git
scp -r springboot-sample-app/ root@192.168.56.112:/opt/

4. 初始化 springboot-sample-app git 仓库(在 Registry 机器上操作)
mkdir -p /opt/git/repo/springboot-sample-app.git;
cd /opt/git/repo/springboot-sample-app.git;
git init --bare;
git update-server-info;
mv hooks/post-update.sample hooks/post-update;

5. 拷贝 springboot-sample-app 代码,并提交(在 Registry 机器上操作)
cd /opt
mv springboot-sample-app springboot-sample-app-demo
git clone file:///opt/git/repo/springboot-sample-app.git/

cp springboot-sample-app-demo/* springboot-sample-app -rf;
cp springboot-sample-app-demo/.sti springboot-sample-app -rf;
cp springboot-sample-app-demo/.htaccess springboot-sample-app -rf;
cp springboot-sample-app-demo/.gitignore springboot-sample-app -rf;
cd springboot-sample-app;
git add .;
git commit -m 'initial upload';
git push origin master;

6. 验证 springboot-sample-app git 仓库是否创建成功(在 Master 机器上操作)
cd /tmp
git clone http://git.example.com/git/springboot-sample-app.git/

7. 首次部署 springboot-sample-app 应用 (在 Master 机器上操作)
docker pull registry.example.com:5000/maping930883/spring-boot-rhel7
oc new-app registry.example.com:5000/maping930883/spring-boot-rhel7~http://git.example.com/git/springboot-sample-app.git
本来想先生成应用,然后根据自动创建 IS 和其它资源,导出 template 大致的样子再修改,
但是报告如下错误:
Cloning "http://git.example.com/git/springboot-sample-app.git" ...
error: build error: fatal: dumb http transport does not support --depth
估计是因为我的 git 仓库不是真正的 git server。

只好手工创建 template 啦。

8. 创建 Image Stream (在 Master 机器上操作)
oc create -f spring-boot-rhel7-is.json -n openshift
其中 spring-boot-rhel7-is.json 内容如下:
{
    "kind": "ImageStream",
    "apiVersion": "v1",
    "metadata": {
        "name": "spring-boot-rhel7",
        "creationTimestamp": null
    },
    "spec": {
        "dockerImageRepository": "registry.example.com:5000/maping930883/spring-boot-rhel7",
        "tags": [
            {
                "name": "latest",
                "annotations": null,
                "from": {
                    "kind": "DockerImage",
                    "name": "registry.example.com:5000/maping930883/spring-boot-rhel7"
                },
                "generation": 1,
                "importPolicy": {
                    "insecure": true
                }
            }
        ]
    }
}

确认 TAGS 已经打上,执行 oc get is spring-boot-rhel7 -n openshift
输出如下:
NAME                DOCKER REPO                                                TAGS      UPDATED
spring-boot-rhel7   registry.example.com:5000/maping930883/spring-boot-rhel7   latest    2 seconds ago

9. 创建 Template
oc create -f spring-boot-rhel7-s2i-template.json -n openshift
其中 spring-boot-rhel7-s2i-template.json 内容如下:
{
    "kind": "Template",
    "apiVersion": "v1",
    "metadata": {
        "name": "spring-boot-rhel7-s2i",
        "creationTimestamp": null,
        "annotations": {
            "iconClass": "icon-java",
            "description": "Application template for Spring Boot Web applications built using S2I.",
            "tags": "java,spring boot,rhel7,s2i",
            "version": "1.0"
        }
    },
    "objects": [
        {
            "apiVersion": "v1",
            "kind": "Service",
            "metadata": {
                "annotations": {
                    "description": "The web server's http port."
                },
                "labels": {
                    "application": "${APPLICATION_NAME}"
                },
                "name": "${APPLICATION_NAME}"
            },
            "spec": {
                "ports": [
                    {
                        "port": 8080,
                        "targetPort": 8080
                    }
                ],
                "selector": {
                    "deploymentConfig": "${APPLICATION_NAME}"
                }
            }
        },
        {
            "apiVersion": "v1",
            "id": "${APPLICATION_NAME}-http",
            "kind": "Route",
            "metadata": {
                "annotations": {
                    "description": "Route for application's http service."
                },
                "labels": {
                    "application": "${APPLICATION_NAME}"
                },
                "name": "${APPLICATION_NAME}"
            },
            "spec": {
                "host": "${HOSTNAME_HTTP}",
                "to": {
                    "name": "${APPLICATION_NAME}"
                }
            }
        },
        {
            "apiVersion": "v1",
            "kind": "ImageStream",
            "metadata": {
                "labels": {
                    "application": "${APPLICATION_NAME}"
                },
                "name": "${APPLICATION_NAME}"
            }
        },
        {
            "apiVersion": "v1",
            "kind": "BuildConfig",
            "metadata": {
                "labels": {
                    "application": "${APPLICATION_NAME}"
                },
                "name": "${APPLICATION_NAME}"
            },
            "spec": {
                "output": {
                    "to": {
                        "kind": "ImageStreamTag",
                        "name": "${APPLICATION_NAME}:latest"
                    }
                },
                "source": {
                    "contextDir": "${CONTEXT_DIR}",
                    "git": {
                        "ref": "${SOURCE_REPOSITORY_REF}",
                        "uri": "${SOURCE_REPOSITORY_URL}"
                    },
                    "type": "Git"
                },
                "strategy": {
                    "sourceStrategy": {
                        "forcePull": true,
                        "from": {
                            "kind": "ImageStreamTag",
                            "name": "spring-boot-rhel7:latest",
                            "namespace": "${IMAGE_STREAM_NAMESPACE}"
                        },
                        "env": [
                           {
                               "name": "MAVEN_MIRROR_URL",
                               "value": "${MAVEN_MIRROR_URL}"
                           }
                       ]
                    },
                    "type": "Source"
                },
                "triggers": [
                    {
                        "github": {
                            "secret": "${GITHUB_WEBHOOK_SECRET}"
                        },
                        "type": "GitHub"
                    },
                    {
                        "generic": {
                            "secret": "${GENERIC_WEBHOOK_SECRET}"
                        },
                        "type": "Generic"
                    },
                    {
                        "imageChange": {},
                        "type": "ImageChange"
                    },
                    {
                        "type": "ConfigChange"
                    }
                ]
            }
        },
        {
            "apiVersion": "v1",
            "kind": "DeploymentConfig",
            "metadata": {
                "labels": {
                    "application": "${APPLICATION_NAME}"
                },
                "name": "${APPLICATION_NAME}"
            },
            "spec": {
                "replicas": 1,
                "selector": {
                    "deploymentConfig": "${APPLICATION_NAME}"
                },
                "strategy": {
                    "type": "Recreate"
                },
                "template": {
                    "metadata": {
                        "labels": {
                            "application": "${APPLICATION_NAME}",
                            "deploymentConfig": "${APPLICATION_NAME}"
                        },
                        "name": "${APPLICATION_NAME}"
                    },
                    "spec": {
                        "containers": [
                            {
                                "env": [
                                    {
                                       "name": "MAVEN_MIRROR_URL",
                                       "value": "${MAVEN_MIRROR_URL}"
                                    }
                                ],
                                "image": "${APPLICATION_NAME}",
                                "imagePullPolicy": "Always",
                                "name": "${APPLICATION_NAME}",
                                "ports": [
                                    {
                                        "containerPort": 8778,
                                        "name": "jolokia",
                                        "protocol": "TCP"
                                    },
                                    {
                                        "containerPort": 8080,
                                        "name": "http",
                                        "protocol": "TCP"
                                    }
                                ]
                            }
                        ],
                        "terminationGracePeriodSeconds": 60
                    }
                },
                "triggers": [
                    {
                        "imageChangeParams": {
                            "automatic": true,
                            "containerNames": [
                                "${APPLICATION_NAME}"
                            ],
                            "from": {
                                "kind": "ImageStreamTag",
                                "name": "${APPLICATION_NAME}:latest"
                            }
                        },
                        "type": "ImageChange"
                    },
                    {
                        "type": "ConfigChange"
                    }
                ]
            }
        }
    ],
    "parameters": [
        {
            "name": "APPLICATION_NAME",
            "description": "The name for the application.",
            "value": "springboot-sample-app",
            "required": true
        },
        {
            "name": "HOSTNAME_HTTP",
            "description": "Custom hostname for http service route.  Leave blank for default hostname, e.g.: \u003capplication-name\u003e-\u003cproject\u003e.\u003cdefault-domain-suffix\u003e"
        },
        {
            "name": "SOURCE_REPOSITORY_URL",
            "description": "Git source URI for application",
            "value": "http://git.example.com/git/springboot-sample-app.git",
            "required": true
        },
        {
            "name": "SOURCE_REPOSITORY_REF",
            "description": "Git branch/tag reference",
            "value": "master"
        },
        {
            "name": "CONTEXT_DIR",
            "description": "Path within Git project to build; empty for root project directory.",
            "value": "/"
        },
        {
            "name": "GITHUB_WEBHOOK_SECRET",
            "description": "GitHub trigger secret",
            "generate": "expression",
            "from": "[a-zA-Z0-9]{8}",
            "required": true
        },
        {
            "name": "GENERIC_WEBHOOK_SECRET",
            "description": "Generic build trigger secret",
            "generate": "expression",
            "from": "[a-zA-Z0-9]{8}",
            "required": true
        },
        {
            "name": "IMAGE_STREAM_NAMESPACE",
            "description": "Namespace in which the ImageStreams for Red Hat Middleware images are installed. These ImageStreams are normally installed in the openshift namespace. You should only need to modify this if you've installed the ImageStreams in a different namespace/project.",
            "value": "openshift",
            "required": true
        },
        {
            "name": "MAVEN_MIRROR_URL",
            "description": "maven mirror url",
            "value": "http://192.168.56.1:8081/nexus/content/groups/public/",
            "required": true
        }
    ],
    "labels": {
        "template": "spring-boot-rhel7-s2i"
    }
}

10. 选择 spring-boot-rhel7-s2i 模板,发布应用
经过前面的“艰苦努力”,现在发布 Spring Boot 应用变得超级简单!

11. 清理并重做
如果有错,执行以下命令清理,然后重新执行上述步骤
oc delete bc/springboot-sample-app is/springboot-sample-app dc/springboot-sample-app routes/springboot-sample-app svc/springboot-sample-app
oc delete is spring-boot-rhel7 -n openshift
oc delete template spring-boot-rhel7-s2i -n openshift
docker rmi registry.example.com:5000/maping930883/spring-boot-rhel7:latest

没有评论: