Docker健康检查报告"健康"总是

时间:2018-03-10 02:54:39

标签: docker dockerfile docker-container

我希望能够报告"不健康"当一个容器变成这样的时候(基于各种条件),现在我只是在偶数呼叫上返回500,在奇数编号的呼叫上返回200 OK。

我的docker文件如下所示:

FROM golang:alpine

RUN apk update
RUN apk add curl
RUN mkdir /service
COPY healthcheck.go /service
COPY ./counts /service

EXPOSE 9080

WORKDIR /service

HEALTHCHECK --interval=5s --timeout=500ms CMD curl --fail http://localhost:9080/health || exit 1

CMD ["go", "run", "/service/healthcheck.go"]               

使用docker inspect,我可以看到有超时(由代码引起)和状态Ok。然而," Health.Status"在检查节目中

"状态":"健康"

docker inspect输出:

        "Health": {
            "Status": "healthy",
            "FailingStreak": 1,
            "Log": [
                {
                    "Start": "2018-03-10T02:44:12.48947433Z",
                    "End": "2018-03-10T02:44:12.99252883Z",
                    "ExitCode": -1,
                    "Output": "Health check exceeded timeout (500ms)"
                },
                {
                    "Start": "2018-03-10T02:44:18.004402431Z",
                    "End": "2018-03-10T02:44:18.069316531Z",
                    "ExitCode": 0,
                    "Output": "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\nThis time it has to be healthy 252\n\r100    43  100    43    0     0  43000      0 --:--:-- --:--:-- --:--:-- 43000\nnext253"
                },
                {
                    "Start": "2018-03-10T02:44:23.078242333Z",
                    "End": "2018-03-10T02:44:23.583552633Z",
                    "ExitCode": -1,
                    "Output": "Health check exceeded timeout (500ms)"
                },
                {
                    "Start": "2018-03-10T02:44:28.593083534Z",
                    "End": "2018-03-10T02:44:28.665864034Z",
                    "ExitCode": 0,
                    "Output": "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n                                 Dload  Upload   Total   Spent    Left  Speed\n\r  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0\r100    43  100    43    0     0   7166      0 --:--:-- --:--:-- --:--:--  8600\n\nThis time it has to be healthy 254\nnext255"
                },
                {
                    "Start": "2018-03-10T02:44:33.671220836Z",
                    "End": "2018-03-10T02:44:34.177248436Z",
                    "ExitCode": -1,
                    "Output": "Health check exceeded timeout (500ms)"
                }
            ]
        }
    },

如何将容器报告为Unhealthy?

3 个答案:

答案 0 :(得分:1)

是的,您可以通过将HEALTHCHECK中的Dockerfile更改为下面的HEALTHCHECK --interval=5s --retries=1 --timeout=500ms CMD curl --fail http://localhost:9080/health || exit 1 ,让docker报告容器不健康:

{"id" : "eew7-9yf2", "name" : "Student Loan Complaints", "averageRating" : 0, "createdAt" : 1434385453, "description" : "Each week we send thousands of consumers' complaints about financial products and services to companies for response. Complaints are listed in the database after the company responds or after they’ve had the complaint for 15 calendar days, whichever comes first.<br><br>\r\n\r\nWe publish the consumer’s description of what happened if the consumer opts to share it and after taking steps to remove personal information. See our <a href=\"http://files.consumerfinance.gov/a/assets/201503_cfpb_Narrative-Scrubbing-Standard.pdf\">Scrubbing Standard</a> for more details<br><br>\r\n\r\nWe don’t verify all the facts alleged in these complaints, but we take steps to confirm a commercial relationship. We may remove complaints if they don’t meet all of the publication criteria. Data is refreshed nightly.<br><br>\r\n\r\n<a href=\"http://www.consumerfinance.gov/complaintdatabase/\">More about the Consumer Complaint Database</a> | <a href=\"http://www.consumerfinance.gov/complaint/data-use/\">How we use complaint data</a> | <a href=\"http://www.consumerfinance.gov/complaintdatabase/technical-documentation/\">Technical documentation</a>", "displayType" : "table", "downloadCount" : 1461, "hideFromCatalog" : false, "hideFromDataJson" : false, "indexUpdatedAt" : 1520460881, "moderationStatus" : true, "modifyingViewUid" : "s6ew-h6mp", "newBackend" : false, "numberOfComments" : 0, "oid" : 22199210, "provenance" : "official", "publicationAppendEnabled" : false, "publicationDate" : 1467818298, "publicationGroup" : 2819740, "publicationStage" : "published", "rowsUpdatedAt" : 1520454464, "rowsUpdatedBy" : "pjxg-ve4m", "tableId" : 11756201, "totalTimesRated" : 0, "viewCount" : 4630, "viewLastModified" : 1456271502, "viewType" : "tabular", "grants" : [ {  "inherited" : true, "type" : "viewer", "flags" : [ "public" ]} ], "metadata" : {  "rdfSubject" : "0", "richRendererConfigs" : {    "fatRow" : {      "columns" : [{"styles" : {"width" : "27%"        },        "rows" : [ {"fields" : [ { "tableColumnId" : 28249505, "type" : "columnLabel"}, { "tableColumnId" : 28249505, "type" : "columnData"} ]},{"fields" : [ { "tableColumnId" : 28249497, "type" : "columnLabel"}, { "tableColumnId" : 28249497, "type" : "columnData"} ]},{"fields" : [ { "tableColumnId" : 28249498, "type" : "columnLabel"}, { "tableColumnId" : 28249498, "type" : "columnData"} ]},{"fields" : [ { "tableColumnId" : 28249499, "type" : "columnLabel"}, { "tableColumnId" : 28249499, "type" : "columnData"} ]},{"fields" : [ { "tableColumnId" : 28249500, "type" : "columnLabel"}, { "tableColumnId" : 28249500, "type" : "columnData"} ]},{"fields" : [ { "tableColumnId" : 28249502, "type" : "columnLabel"}, { "tableColumnId" : 28249502, "type" : "columnData"} ]},{"fields" : [ { "tableColumnId" : 28249503, "type" : "columnLabel"}, { "tableColumnId" : 28249503, "type" : "columnData"} ]},{"fields" : [ { "tableColumnId" : 28249504, "type" : "columnLabel"}, { "tableColumnId" : 28249504, "type" : "columnData"}] }]}, {        "styles" : {"width" : "40%"        },        "rows" : [ {"fields" : [ { "tableColumnId" : 28249493, "type" : "columnLabel"}, { "tableColumnId" : 28249493, "type" : "columnData"} ]},{"fields" : [ { "tableColumnId" : 28249495, "type" : "columnLabel"}, { "tableColumnId" : 28249495, "type" : "columnData"}] }]}, {        "styles" : {"width" : "30%"        },        "rows" : [ {"fields" : [ { "tableColumnId" : 28249508, "type" : "columnLabel"}, { "tableColumnId" : 28249508, "type" : "columnData"} ]},{"fields" : [ { "tableColumnId" : 28249507, "type" : "columnLabel"}, { "tableColumnId" : 28249507, "type" : "columnData"} ]},{"fields" : [ { "tableColumnId" : 28249509, "type" : "columnLabel"}, { "tableColumnId" : 28249509, "type" : "columnData"} ]},{"fields" : [ { "tableColumnId" : 28249510, "type" : "columnLabel"}, { "tableColumnId" : 28249510, "type" : "columnData"} ]},{"fields" : [ { "tableColumnId" : 28249511, "type" : "columnLabel"}, { "tableColumnId" : 28249511, "type" : "columnData"} ]},{"fields" : [ { "tableColumnId" : 28249492, "type" : "columnLabel"}, { "tableColumnId" : 28249492, "type" : "columnData"}] }]      } ]    }  }, "availableDisplayTypes" : [ "table", "fatrow", "page" ], "renderTypeConfig" : {    "visible" : {      "table" : true    }  }}, "owner" : {  "id" : "dfzt-mv86", "displayName" : "CFPB Administrator", "screenName" : "CFPB Administrator", "type" : "interactive", "flags" : [ "organizationMember" ]}},, "rights" : [ "read" ], "tableAuthor" : {  "id" : "pjxg-ve4m", "displayName" : "Shannon Files", "screenName" : "Shannon Files", "type" : "interactive", "flags" : [ "organizationMember" ]}},





create table complex5 (
id STRING,
name STRING,
averageRating FLOAT,
createdAt FLOAT,
description STRING,
displayType STRING,
downloadCount INT,
hideFromCatalog BOOLEAN,
hideFromDataJson BOOLEAN,
indexUpdatedAt FLOAT,
moderationStatus BOOLEAN,
newBackend BOOLEAN,
numberOfComments INT,
oid INT,
provenance STRING,
publicationAppendEnabled BOOLEAN,
publicationDate FLOAT,
publicationGroup INT,
publicationStage STRING,
rowClass STRING,
rowsUpdatedAt FLOAT,
rowsUpdatedBy STRING,
tableId INT,
totalTimesRated INT,
viewCount INT,
viewLastModified FLOAT,
viewType STRING,
grants ARRAY<MAP<STRING, STRING>>,
metadata STRUCT<rdfSubject:STRING, richRendererConfigs:STRUCT<fatRow:STRUCT<columns:ARRAY<STRUCT<styles:MAP<STRING,STRING>,rows:ARRAY<MAP<STRING,ARRAY<STRUCT<tableColumnId:INT,type:String>>>>>>>> , rowLabel:STRING, availableDisplayTypes: ARRAY<STRING>, renderTypeConfig:MAP<STRING, MAP<STRING, BOOLEAN>>>,
owner STRUCT<id:STRING, displayName:STRING, screenName:STRING, type:STRING, flags:ARRAY<STRING>>,
rights ARRAY<STRING>,
tableAuthor MAP<STRING, STRING>
)ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe';
  

如果一次检查的时间超过超时秒,那么   该支票被视为失败。

     

健康检查需要重试连续失败   容器被认为是不健康的。

     

(参考:https://docs.docker.com/engine/reference/builder/#healthcheck

默认情况下,docker将尝试重试3次,并且当连续三次失败时,该容器将被视为不健康。此时,您在偶数编号的请求上返回状态500,在奇数编号的请求上返回状态200。当它失败时(在偶数编号的请求上),docker将再次重试,这次它将是一个奇数编号的请求,因此它将容器报告为健康。

通过将重试次数设置为1,当第一次尝试失败时,docker会将容器报告为运行状况不佳,并等待5秒再次尝试进行运行状况检查。

答案 1 :(得分:0)

结果是 - 解决方案。

更改了此处列出的Dockerfile:

FROM golang:alpine

RUN apk update
RUN apk add curl
RUN mkdir /service
COPY healthcheck.go /service
COPY ./counts /service

EXPOSE 9080

WORKDIR /service

HEALTHCHECK --interval=5s --timeout=500ms --retries=1 CMD curl --fail http://localhost:9080/health || exit 1                                              

CMD ["go", "run", "/service/healthcheck.go"]

答案 2 :(得分:0)

花点时间魔术,不要卷曲或任何其他外部物品:

高山图像中使用的ubuntu(例如“ nc”)和busybox“ nc”版本之间存在差异。

重点是常规nc等待响应,而从busybox接缝的响应则不响应。

因此,我使用{...}将'printf'和'sleep'封装到单个子外壳中,该子外壳在nc旁边传递。

这样做,nc有机会从端点获得响应并将其通过管道传递给grep。

grep的退出状态决定健康状态。

HEALTHCHECK --interval=1s --timeout=5s --retries=3 \ CMD { printf "GET /fpm-ping HTTP/1.0\r\n\r\n"; sleep 0.5; } | nc -w 1 127.0.0.1 8080 | grep pong

相关问题