- ã¯ããã«
- Loki ã«ã¤ãã¦
- ã¯ã¨ãªã®ç¨®é¡
- OpenShift Logging ã¨ãã¦ã®æ§ç¯
- ç°¡åãªã¢ã¯ã»ã¹ç¢ºèª
- ãããã«
ã¯ããã«
Red Hat Advent Calendar 2023 ã® 12æ14æ¥ã®è¨äºã§ããOpenShift Cluster Logging 㧠Loki ãå©ç¨ã§ããããã«ãªã£ã¦ãããã°ããæéãçµã¡ã段ã ã¨å©ç¨ããã¤ã¤ããã®ã§ã¯ãªããã¨æãã¾ãã æ¬è¨äºã§ã¯ãOpenShift Cluster Logging (Loki) ã OpenShift Web Console ããã§ã¯ãªãããã㦠CLI çµç±ã§ã¢ã¯ã»ã¹ãã¦ã¿ã¾ãã
Loki ã«ã¤ãã¦
Loki 㯠Grafana Loki ã¨ããå称ã§ãPrometheus ããã¤ã³ã¹ãã¤ã¢ãããã¹ã±ã¼ã©ããªãã£ãå¯ç¨æ§ããã«ãããã³ãã«å¯¾å¿ãããã°åéã·ã¹ãã ã§ãã ä»ã®ãã°ã·ã¹ãã ã¨ç°ãªãããã°èªèº«ã«ã¯ã¤ã³ããã¯ã¹ãæãããã¡ã¿ãã¼ã¿ã®ã¿ãã¤ã³ããã¯ã¹åãã¦ç®¡çãã¾ãã ãã°ã¯ã¡ã¿ãã¼ã¿ã¨ãã¦ã©ãã«ãæã¤ãã¨ã§ç®¡çããã¾ããåãã©ãã«ããã¤ãã°ã®ã»ããã Log Stream ã¨å¼ã³ã¾ãã
ãã°ãã¼ã¿ã¯å§ç¸®ããããªãã¸ã§ã¯ãã¹ãã¬ã¼ã¸ã«æ ¼ç´ããããããã°ãã¼ã¿ãå¹çããä¿æã§ããä»çµã¿ã¨ãªã£ã¦ãã¾ãã
ã¯ã¨ãªã®ç¨®é¡
Loki ã®æ¤ç´¢ã¯ LogQL ãå©ç¨ãã¾ããOpenShift ã® Web Console ããå¼ã³åºãã¨ãã«ä½¿ãã¾ããæ ¼ç´ããããã°ããç¹å®ã®ã¡ãã»ã¼ã¸ãªã©ãçµãè¾¼ãã§è¦ãéã«å©ç¨ãã¾ããLogQL 㯠2ã¤ã®ç¨®é¡ãããããããã Log ã¯ã¨ãªã¨ Metric ã¯ã¨ãªã«å¥ãã¾ãã
Log ã¯ã¨ãª
ãã°ãã¼ã¿ãè¿å´ãã¾ãããã°ã¡ãã»ã¼ã¸ã®çµãè¾¼ã¿ãªã©ã§ç®çã®ã¨ã©ã¼æ å ±ãªã©ãæ¤ç´¢ããã¨ãã«æ´»èºãã¾ã
Metric ã¯ã¨ãª
Log ã¯ã¨ãªã®æ¡å¼µçã§ãã°ã®æ¤ç´¢çµæããã¼ã¹ã«æ¼ç®ãããã¨ãç®çã§ãã
OpenShift Logging ã¨ãã¦ã®æ§ç¯
ã¾ãã¯è£½åããã¥ã¡ã³ãã«å¾ããªãããLoki ãå©ç¨ãã Logging ãæ§ç¯ãã¦ããã¾ãã
ã¾ã㯠Operator ã®å°å ¥ãããOperator 㯠Loki Operator 㨠OpenShift Cluster Logging Operator ã® 2 ã¤ãå¿ è¦ã¨ãªãã¾ãããããããã¤ã³ã¹ãã¼ã«ãã¾ãã
Operator ã®å°å ¥
Loki Operator ãã¤ã³ã¹ãã¼ã«ãã Namespace ã®ä½æ
$ cat << EOF | oc create -f - apiVersion: v1 kind: Namespace metadata: name: openshift-operators-redhat annotations: openshift.io/node-selector: "" labels: openshift.io/cluster-monitoring: "true" EOF
Cluster Logging Operator ãã¤ã³ã¹ãã¼ã«ãã Namespace ã®ä½æ
$ cat <<EOF | oc create -f - apiVersion: v1 kind: Namespace metadata: name: openshift-logging annotations: openshift.io/node-selector: "" labels: openshift.io/cluster-monitoring: "true" EOF
OpenShift Cluster Logging ç¨ã® OperatorGroup 㨠Subscription ãä½æãã
OperatorGroup ã®ä½æ
$ cat <<EOF | oc create -f - apiVersion: operators.coreos.com/v1 kind: OperatorGroup metadata: name: cluster-logging namespace: openshift-logging spec: targetNamespaces: - openshift-logging EOF
ç¶ãã¦ãSubscription ãä½æãã¾ãã
$ cat <<EOF | oc create -f - apiVersion: operators.coreos.com/v1alpha1 kind: Subscription metadata: name: cluster-logging namespace: openshift-logging spec: spec: channel: stable-5.8 installPlanApproval: Automatic name: cluster-logging source: redhat-operators sourceNamespace: openshift-marketplace startingCSV: cluster-logging.v5.8.0 EOF
次ã«ãLoki Operator ãã¤ã³ã¹ãã¼ã«ãã¾ããCluster Logging Operator åæ§ã« Subscription ãä½æãã¾ãã
$ cat << EOF | oc create -f - apiVersion: operators.coreos.com/v1alpha1 kind: Subscription metadata: name: loki-operator namespace: openshift-operators-redhat spec: channel: stable-5.8 installPlanApproval: Automatic name: loki-operator source: redhat-operators sourceNamespace: openshift-marketplace EOF
Loki ã®ãããã¤
Loki 㯠Object Storage ãå¿ è¦ã¨ãã¾ããä»åã®æ§ç¯ç°å¢ã¯ AWS ãå©ç¨ãã¦ããã®ã§ãé©å½ã« AWS S3 ã®ãã±ãããä½æãã¦å©ç¨ãã¾ãã Object Storage ã¸ã®ã¢ã¯ã»ã¹æ å ±ã¯ Secret ãå©ç¨ãã¦æå®ãããããæ¥ç¶æ å ±ã以ä¸ã®ããã«ç¨æãã¾ãã
$ cat <<EOF | oc create -f - apiVersion: v1 kind: Secret metadata: name: logging-loki-s3 namespace: openshift-logging stringData: access_key_id: <ACCESS KEY> access_key_secret: <ACCESS_KEY_SECRET> bucketnames: <BUCKET_NAME> endpoint: https://s3.ap-southeast-2.amazonaws.com region: ap-southeast-2 EOF
次ã«ãLokiStack Custom Resource ãä½æãã¾ããä»åä½æãã LokiStack ã®ãµã¤ãºã¯ 1x.small
ã¨ãã¾ãã
$ cat <<EOF | oc create -f - apiVersion: loki.grafana.com/v1 kind: LokiStack metadata: name: logging-loki # 1 namespace: openshift-logging spec: size: 1x.small #2 storage: schemas: - version: v12 effectiveDate: '2022-06-01' secret: name: logging-loki-s3 #3 type: s3 #4 storageClassName: gp3-csi #5 tenants: mode: openshift-logging EOF
ãã©ã¡ã¿ã®ç°¡åãªèª¬æ
- logging-loki ã¨ããååã使ç¨ãã¦ãã ããã
- Loki ã®å±éãµã¤ãºãé¸æãã¦ãã ããã
- ãã°ã¹ãã¬ã¼ã¸ã«ä½¿ç¨ããã·ã¼ã¯ã¬ãããæå®ãã¦ãã ããã
- 対å¿ããã¹ãã¬ã¼ã¸ã¿ã¤ããæå®ãã¦ãã ããã
- ä¸æçãªã¹ãã¬ã¼ã¸ã«æ¢åã®ã¹ãã¬ã¼ã¸ã¯ã©ã¹ã®ååãå ¥åãã¦ãã ãããæé©ãªããã©ã¼ãã³ã¹ãå¾ãããã«ã¯ããããã¯ã¹ãã¬ã¼ã¸ãå²ãå½ã¦ãã¹ãã¬ã¼ã¸ã¯ã©ã¹ãæå®ãã¦ãã ãããã¯ã©ã¹ã¿ã§å©ç¨å¯è½ãªã¹ãã¬ã¼ã¸ã¯ã©ã¹ã¯ãoc get storageclasses ã³ãã³ãã使ç¨ãã¦ãªã¹ãã¢ããã§ãã¾ãã
Cluster Logging ã¨ãã¦å©ç¨ãã
ãããã¤ãã Loki ã OpenShift Cluster Logging ã¨ãã¦å©ç¨ãã¾ãã
$ cat << EOF | oc create -f - apiVersion: logging.openshift.io/v1 kind: ClusterLogging metadata: name: instance namespace: openshift-logging spec: logStore: type: lokistack lokistack: name: logging-loki collection: type: vector EOF
ç°¡åãªã¢ã¯ã»ã¹ç¢ºèª
Web Console ããã®ã¢ã¯ã»ã¹
ããã¾ã§ã§ãLoki ãå©ç¨ã§ããããã«ãªã£ã¦ããã¯ããªã®ã§ãWeb Console ãããç°¡åã«ç¢ºèªãã¦ã¿ã¾ãã Web Console ã® Administrator ã®ç»é¢ããã Observe > Logs ãé¸æããã¨ç»é¢ã表示ããã¾ãã
CLI ããã®ã¢ã¯ã»ã¹
ããã§æ¬é¡ã§ããä»å CLI ã¢ã¯ã»ã¹ã試ããã£ããã¨ãªã£ãã®ã¯ãå®ã¯ OpenShift Web Console ããã¯ã¾ã Metric ã¯ã¨ãªãå®è¡ãããã¨ãã§ãã¾ããããã®ãããMetric ã¯ã¨ãªãå®è¡ããã«ã¯ã©ããã¦ã CLI çµç±ã§ã®å¼ã³åºããå¿ è¦ã¨ãªãã¾ãã
ã¢ã¯ã»ã¹æ¹æ³ã®ç¢ºèª
Loki ã¸ã®ã¢ã¯ã»ã¹ã¯ Route çµç±ã§è¡ãã¾ãã
$ oc get route -n openshift-logging NAME HOST/PORT PATH SERVICES PORT TERMINATION WILDCARD logging-loki logging-loki-openshift-logging.apps.samplecluster.sandboxABCDE.opentlc.com logging-loki-gateway-http public reencrypt None
å¼ã³åºãæ¹ã確èªããããã試ãã« Route ã«ã¢ã¯ã»ã¹ãã¦ã¿ã¾ããLoki 㯠OpenShift ã«ããèªè¨¼ãå©ç¨ãã¦ãããããOpenShift ã®ã¦ã¼ã¶ã¼ã®ãã¼ã¯ã³ãå©ç¨ãã¦ã¢ã¯ã»ã¹ãã¦ã¿ã¾ãã
$ curl -k -s -H 'Authorization: Bearer <TOKEN>' https://logging-loki-openshift-logging.apps.samplecluster.sandboxABCDE.opentlc.com { "paths": [ "/api/logs/v1/{tenant}/*" ] }
{tenant}
ãæå®ããå¿
è¦ãããããã§ããããã³ãã¨ã¯ä½ã§ãããããå®ã¯ãOpenShift Cluster Logging ã§ã¯ Loki ã®ãã«ãããã³ãæ©è½ãå©ç¨ãã¦ãinfrastracture ãã°ãapplication ãã°ãaudit ãã°ãåãã¦æ ¼ç´ãã¦ãã¾ãã
ãã®ãããããã§ãã {tenant}
㯠infrastructure
ãapplication
ãaudit
ã¨ãªãã¾ãã
Loki ã® API ã®ãã¹ã¯è£½åããã¥ã¡ã³ãã«è¨è¼ããã以ä¸ã®ããã«ãªã£ã¦ãã¾ãããã¹ã®æå¾ã«ãã query_range
ã¯å¼ã³åºãAPIã«ãã代ããã¾ãããä»å㯠Metric ã¯ã¨ãªãå¼ã³åºãã®ã§ãã¡ããå©ç¨ãã¾ãã
GET /loki/api/v1/query_range
ãããåãããã¨æ¬¡ã®ãããªURLãå¼ã³åºããã¨ã§ãcurl
ã§ã¢ã¯ã»ã¹ã§ãããã¨ããããã¾ãã
https://logging-loki-openshift-logging.apps.samplecluster.sandboxABCDE.opentlc.com/api/logs/v1/{tenant}/loki/api/v1/query_range
Metric ã¯ã¨ãªãå¼ã³åºãã¦ã¿ã
LogQL ã¯ç©ºç½ãã·ã³ã°ã«ã¯ã©ã¼ããªã©ãå«ããããURLã¨ã³ã³ã¼ãããå½¢ã§æå®ãã¾ããcurl ã³ãã³ãã§ã¯ãã©ã¡ã¿æå®ã«ããURLã¨ã³ã³ã¼ããè¡ããã¨ã¯ã§ãã¾ãããäºãã¨ã³ã³ã¼ããã¦ããã¾ãã
ä»åãã¹ãããã®ã¯æ¬¡ã® Metric ã¯ã¨ãªã§ãã Namespace æ¯ã« log_type=infrastructure ã®ãã°æ°ãã«ã¦ã³ãããã¨ãããã®ã§ãã
sum by (kubernetes_namespace_name) (count_over_time({ log_type="infrastructure" } [5m]) )
curl ã³ãã³ãã«ããã¨ä»¥ä¸ã®ãããªæãã§ãã
$ curl -G -k -s -H 'Authorization: Bearer sha256~_hJqP4Ksy5Opcs1_37s2fC2_XFXWigO6iXJEDBpM_nA' https://logging-loki-openshift-logging.apps.samplecluster.sandboxABCDE.opentlc.com/api/logs/v1/infrastructure/loki/api/v1/query_range --data 'query=sum%20by%20(kubernetes_namespace_name)%20(count_over_time(%7B%20log_type%3D%22infrastructure%22%20%7D%20%5B5m%5D)%20)' --data-urlencode step=2m --data-urlencode start=1702467769 --data-urlencode end=1702471369
å¼ã³åºãçµæ
{ "status": "success", "data": { "resultType": "matrix", "result": [ { "metric": { "kubernetes_namespace_name": "openshift-apiserver" }, "values": [ [ 1702469160, "1" ], [ 1702469280, "1" ], [ 1702469400, "5" ], [ 1702469520, "4" ], [ 1702469640, "4" ], [ ...
ãã¾ãåå¾ã§ããããã§ãã
LogCLI
次ã«ãLoki ã® CLI LogCLI ã§ã®ã¢ã¯ã»ã¹ã試ãã¾ãã ã³ãã³ãã¯ã以ä¸ã®ãã¼ã¸ãåèã«äºãæºåãã¦ãã ããã
ç°å¢å¤æ°ã§æ¥ç¶å ã¨èªè¨¼æ å ±ãè¨å®ãã¾ããå¼ã³åºãããããã³ãæ¯ã«å¼ã³åºãå ã¯å¤ãã¦ãã ããã
$ export LOKI_BEARER_TOKEN="<TOKEN>" $ export LOKI_ADDR=https://logging-loki-openshift-logging.apps.samplecluster.sandboxABCDE.opentlc.com/api/logs/v1/infrastructure
ã³ãã³ããå®è¡ãã¾ããOpenShift ã®è¨¼ææ¸ã¯èªå·±ç½²å証ææ¸ãå©ç¨ãã¦ãããããTLSã®ãã§ãã¯ã¯ã¹ãããããããã«ãã¾ãã
$ logcli query --tls-skip-verify 'sum by (kubernetes_namespace_name) (count_over_time({ log_type="infrastructure" } [5m]) )'
åºåçµæãåå¾ã§ãã¾ããã curl ã§åå¾ããã¨ãã¨ç°ãªããresult ãæ½åºããã¦ã¾ãã
[ { "metric": { "kubernetes_namespace_name": "openshift-apiserver" }, "values": [ [ 1702470154, "4" ], [ 1702470168, "4" ], [ 1702470182, "4" ], [ 1702470196, "4" ], [ 1702470210, ...
ãããã«
ä»åã¯ãCLI ãç¨ã㦠Loki ã¸ã®ã¢ã¯ã»ã¹ããã¦ã¿ã¾ãããããã«ããç¾å¨ã¯ Web Console ããå©ç¨ã§ããªã Metric ã¯ã¨ãªã®çµæãåå¾ã§ãããã¨ã試ãã¾ããã ã¾ã çºå±éä¸ã®ã³ã³ãã¼ãã³ãã§ã¯ããã¾ããããããã£ãå½¢ã§ãã°ã®è§£æãã§ããããã«ãªã£ã¦ããã¨ãã°ãå©ç¨ããéç¨ã調æ»ãªã©ãé²ã¿ããã§ãã