2017年3月27日星期一

OpenShift_067:使用 Hawkular APM 监控容器中的 Java Web 应用(Template 方式)

环境:OCP 3.4

使用 Dockerfile 方式部署 Java Web 应用太麻烦,而且不通用。
本文使用 Template 方式部署。

1. 创建 Builder Image 
mkdir tomcat8-hawkular-apm
cd tomcat8-hawkular-apm

vim Dockerfile
内容如下:
FROM registry.example.com:5000/jboss-webserver-3/webserver30-tomcat8-openshift:latest
COPY hawkular-apm-agent-opentracing.jar $HOME
USER root
RUN chmod 777 $HOME/hawkular-apm-agent-opentracing.jar
RUN chmod -R 777 $HOME
USER 1001
说明:这里以 jboss-webserver-3/webserver30-tomcat8-openshift:latest 作为 Base Image。

docker build -t="maping930883/tomcat8-hawkular-apm" .

2. 修改 Tag,并 Push 到本地镜像仓库
docker tag maping930883/tomcat8-hawkular-apm:latest registry.example.com:5000/maping930883/tomcat8-hawkular-apm:latest
docker push registry.example.com:5000/maping930883/tomcat8-hawkular-apm:latest

3. 创建 Image Stream
参考 jboss-webserver30-tomcat8-openshift Image Stream
oc export is jboss-webserver30-tomcat8-openshift -n openshift -o json

修改完后,创建自己的 Image Stream
oc create -f tomcat8-hawkular-apm-is.json -n openshift
其中 tomcat8-hawkular-apm-is.json 内容如下:
{
    "kind": "ImageStream",
    "apiVersion": "v1",
    "metadata": {
        "name": "tomcat8-hawkular-apm",
        "creationTimestamp": null
    },
    "spec": {
        "dockerImageRepository": "registry.example.com:5000/maping930883/tomcat8-hawkular-apm",
        "tags": [
            {
                "name": "latest",
                "annotations": null,
                "from": {
                    "kind": "DockerImage",
                    "name": "registry.example.com:5000/maping930883/tomcat8-hawkular-apm"
                },
                "generation": 1,
                "importPolicy": {
                    "insecure": true
                }               
            }     
        ]
    }
}

确认 TAGS 已经打上,执行 oc get is tomcat8-hawkular-apm -n openshift
输出如下:
NAME                   DOCKER REPO                                                   TAGS      UPDATED
tomcat8-hawkular-apm   registry.example.com:5000/maping930883/tomcat8-hawkular-apm   latest    3 hours ago
如果 TAGS 没打上,执行 oc import-image tomcat8-hawkular-apm --insecure -n openshift

4. 创建 Template
参考 jws30-tomcat8-basic-s2i 模板
oc export template jws30-tomcat8-basic-s2i -n openshift -o json

修改 jws30-tomcat8-basic-s2i 模板
oc edit template jws30-tomcat8-basic-s2i -n openshift
把 name: jboss-webserver30-tomcat8-openshift:1.2
改为 name: jboss-webserver30-tomcat8-openshift:latest

修改完后,创建自己的模板
oc create -f tomcat8-hawkular-apm-s2i-template.json -n openshift
其中 tomcat8-hawkular-apm-s2i-template.json 内容如下:
{
    "kind": "Template",
    "apiVersion": "v1",
    "metadata": {
        "name": "tomcat8-hawkluar-apm-s2i",
        "creationTimestamp": null,
        "annotations": {
            "iconClass": "icon-java",
            "description": "Application template for Java Web applications built using S2I and supporting Hawkular APM.",
            "tags": "java,tomcat8,hawkular,apm",
            "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": "tomcat8-hawkular-apm: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": "JWS_ADMIN_USERNAME",
                                        "value": "${JWS_ADMIN_USERNAME}"
                                    },
                                    {
                                        "name": "JWS_ADMIN_PASSWORD",
                                        "value": "${JWS_ADMIN_PASSWORD}"
                                    },
                                    {
                                        "name": "HAWKULAR_APM_URI",
                                        "value": "${HAWKULAR_APM_URI}"
                                    },
                                    {
                                        "name": "HAWKULAR_APM_USERNAME",
                                        "value": "${HAWKULAR_APM_USERNAME}"
                                    },
                                    {
                                        "name": "HAWKULAR_APM_PASSWORD",
                                        "value": "${HAWKULAR_APM_PASSWORD}"
                                    },
                                    {
                                        "name": "JAVA_OPTS",
                                        "value": "${JAVA_OPTS}"
                                    },
                                    {
                                       "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"
                                    }
                                ],
                                "readinessProbe": {
                                    "exec": {
                                        "command": [
                                            "/bin/bash",
                                            "-c",
                                            "curl -s -u ${JWS_ADMIN_USERNAME}:${JWS_ADMIN_PASSWORD} 'http://localhost:8080/manager/jmxproxy/?get=Catalina%3Atype%3DServer\u0026att=stateName' |grep -iq 'stateName *= *STARTED'"
                                        ]
                                    }
                                }
                            }
                        ],
                        "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": "mybank",
            "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/mybank.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": "JWS_ADMIN_USERNAME",
            "description": "JWS Admin User",
            "generate": "expression",
            "from": "[a-zA-Z0-9]{8}",
            "required": true
        },
        {
            "name": "JWS_ADMIN_PASSWORD",
            "description": "JWS Admin Password",
            "generate": "expression",
            "from": "[a-zA-Z0-9]{8}",
            "required": true
        },
        {
            "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": "HAWKULAR_APM_URI",
            "description": "hawkular apm uri",
            "value": "http://hawkular-apm-hawkular.apps.example.com",
            "required": true
        },
        {
            "name": "HAWKULAR_APM_USERNAME",
            "description": "hawkular apm username",
            "value": "admin",
            "required": true
        },
        {
            "name": "HAWKULAR_APM_PASSWORD",
            "description": "hawkular apm password",
            "value": "password",
            "required": true
        },
        {
            "name": "JAVA_OPTS",
            "description": "java options",
            "value": "-javaagent:/home/jboss/hawkular-apm-agent-opentracing.jar=boot:/home/jboss/hawkular-apm-agent-opentracing.jar -Djboss.modules.system.pkgs=org.jboss.byteman,org.hawkular.apm.agent.opentracing,io.opentracing,org.hawkular.apm.client.opentracing",
            "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": "tomcat8-hawkular-apm-s2i"
    }
}

5. 选择 tomcat8-hawkluar-apm-s2i 模板,发布应用
经过前面的“艰苦努力”,现在发布应用变得超级简单!
并且发布的应用,可以使用 Hawkular APM 监控,真是太棒了!

6. 清理并重做
如果有错,执行以下命令清理,然后重新执行上述步骤
oc delete bc/mybank is/mybank dc/mybank routes/mybank svc/mybank
oc delete is tomcat8-hawkular-apm -n openshift
oc delete template tomcat8-hawkluar-apm-s2i -n openshift
docker rmi registry.example.com:5000/maping930883/tomcat8-hawkular-apm:latest

参考文献:
1. https://blog.openshift.com/performance-metrics-apm-spring-boot-microservices-openshift/
2. https://github.com/lbroudoux/spring-boot-hello
 

没有评论: