Skip to content

Commit

Permalink
Remove all jq references and json-config patching, support some envir… (
Browse files Browse the repository at this point in the history
#66)

* Remove all jq references and json-config patching, support some environment variables for setting critical configs

* Remove jq from dockerfile

* Cleaned up docs

* Fixes mistake addition for example cluster
  • Loading branch information
StrongestNumber9 authored Jul 25, 2023
1 parent 551a9da commit 0650e03
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 313 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM rockylinux:8
COPY rpm/target/rpm/com.teragrep-k8s_01/RPMS/noarch/com.teragrep-k8s_01-*.rpm /rpm/
RUN dnf -y install jq /rpm/*.rpm && dnf clean all
RUN dnf -y install /rpm/*.rpm && dnf clean all
VOLUME /opt/teragrep/k8s_01/var
VOLUME /opt/teragrep/k8s_01/etc
WORKDIR /opt/teragrep/k8s_01
Expand Down
312 changes: 8 additions & 304 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,316 +2,20 @@

A container that will read mounted container logfiles, then enriches them using Kubernetes API server, and forwards them with RELP to wanted target server.

## Usage

### Service account

Create simple ServiceAccount.

Example:


[source,json]
----
{
"apiVersion": "v1",
"kind": "ServiceAccount",
"metadata": {
"name": "kubelogreader"
}
}
----

### Role
Create a Role with access to 'pods' and 'namespaces' resources, with verbs 'get', 'watch' and 'list'.

Example:

[source,json]
----
{
"apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "Role",
"metadata": {
"namespace": "default",
"name": "kubelogreader"
},
"rules": [
{
"apiGroups": [
""
],
"resources": [
"pods",
"namespaces"
],
"verbs": [
"get",
"watch",
"list"
]
}
]
}
----

### Role binding

Create Role Binding that contains the service account

Example:

[source,json]
----
{
"apiVersion": "rbac.authorization.k8s.io/v1",
"kind": "RoleBinding",
"metadata": {
"name": "kubelogreader",
"namespace": "default"
},
"subjects": [
{
"kind": "ServiceAccount",
"name": "kubelogreader"
}
],
"roleRef": {
"kind": "Role",
"name": "kubelogreader",
"apiGroup": "rbac.authorization.k8s.io"
}
}
----

### Service

Create a service for the service account to use

Example:

[source,json]
----
{
"apiVersion": "v1",
"kind": "Secret",
"metadata": {
"name": "kubelogreader",
"annotations": {
"kubernetes.io/service-account.name": "kubelogreader"
}
},
"type": "kubernetes.io/service-account-token"
}
----

### Volume mount

Create configMapGenerator called `app-config` using kustomize

[source,yaml]
----
configMapGenerator:
- name: app-config
files:
- config/config.json
- config/log4j2.xml
----

where the configs are

.config.json
[source,json]
----
{
"kubernetes": {
"logdir": "/var/log/containers",
"url": "https://127.0.0.1:8443",
"cacheExpireInterval": 300,
"cacheMaxEntries": 4096,
"labels": {
"hostname": {
"prefix": "prefix-",
"label": "host",
"fallback": "fallback-hostname"
},
"appname": {
"prefix": "prefix-",
"label": "app",
"fallback": "fallback-appname"
}
},
"logfiles": [
"first-pod_default_.*",
"second-pod_default_.*",
"third-pod_default_third-pod-one-.*",
"third-pod_default_third-pod-two-.*"
]
},
"relp": {
"target": "receiver.receiver.default",
"port": 1601,
"connectionTimeout": 5000,
"readTimeout": 5000,
"writeTimeout": 5000,
"reconnectInterval": 5000,
"outputThreads": 5
}
}
----

.log4j2.xml
[source,xml]
----
<?xml version="1.0" encoding="UTF-8" ?>
<Configuration monitorInterval="30" status="error">
<Appenders>
<Console name="STDOUT">
<PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss.SSS} [%level] [%logger] [%thread] %msg%ex%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="com.teragrep.k8s_01" level="INFO" additivity="false">
<AppenderRef ref="STDOUT" />
</Logger>
<Logger name="com.teragrep.k8s_01.KubernetesCachingAPIClient" level="DEBUG" additivity="false">
<AppenderRef ref="STDOUT" />
</Logger>
<Logger name="com.teragrep.rlo_12" level="INFO" additivity="false">
<AppenderRef ref="STDOUT" />
</Logger>
<Logger name="com.teragrep.rlo_13" level="INFO" additivity="false">
<AppenderRef ref="STDOUT" />
</Logger>
<Root level="DEBUG">
<AppenderRef ref="STDOUT" />
</Root>
</Loggers>
</Configuration>
----

### k8s_01 pod

Make the pod definition use the service account we just made.

[source,json]
----
{
<snip>
"spec": {
"serviceAccount": "kubelogreader",
"serviceAccountName": "kubelogreader",
<snip>
}
}
----
## Example Helm Chart usage

Mount the log containing volumes for runtime processing
Edit wanted namespace in `values.yaml`, then run `helm install kubelogreader .` to create and deploy an example instance. It uses `etc/config.json` and default `src/main/resources/log4j2.xml` for configurations and persistent volumeclaim for statestore.

[source,json]
----
{
"spec": {
<snip>
"volumes": [
{
"name": "app-config",
"configMap": {
"name": "app-config"
}
},
{
"name": "host-var-log-containers",
"hostPath": {
"path": "/var/log/containers",
"type": "Directory"
}
},
{
"name": "host-var-log-pods",
"hostPath": {
"path": "/var/log/pods",
"type": "Directory"
}
},
{
"name": "host-var-lib-docker-containers",
"hostPath": {
"path": "/var/lib/docker/containers",
"type": "Directory"
}
},
{
"name": "host-mnt-statefiles",
"hostPath": {
"path": "/mnt/statefiles",
"type": "DirectoryOrCreate"
}
}
],
<snip>
}
}
----
## Overriding configurations with environment variables

And also mount those volumes
Currently supported:

[source,json]
----
{
"spec": {
<snip>
"containers": [
<snip>
"volumeMounts": [
{
"mountPath": "/config/",
"name": "app-config"
},
{
"mountPath": "/var/log/containers",
"name": "host-var-log-containers",
"readOnly": true
},
{
"mountPath": "/var/log/pods",
"name": "host-var-log-pods",
"readOnly": true
},
{
"mountPath": "/var/lib/docker/containers",
"name": "host-var-lib-docker-containers",
"readOnly": true
},
{
"mountPath": "/opt/teragrep/k8s_01/var",
"name": "host-mnt-statefiles",
"readOnly": false
}
],
<snip>
]
}
}
----
- `K8S_01_RELP_TARGET` overwrites `.relp.target` from config.json

And start the k8s_01 image with for example the following arguments. It uses `KUBERNETES_SERVICE_HOST` with `KUBERNETES_SERVICE_PORT` environment variables to find out the API server, and `RELP_SERVICE_PORT_1601_TCP_ADDR` to find the port for the RELP target server.
- `K8S_01_RELP_PORT` overwrites `.relp.port` from config.json

[source,json]
----
"command": [
"/usr/bin/bash"
],
"args": [
"-c",
"jq --arg host \"${KUBERNETES_SERVICE_HOST}\" --arg port \"${KUBERNETES_SERVICE_PORT}\" --arg target \"${RELP_SERVICE_PORT_1601_TCP_ADDR}\" '.relp.target=$target | .kubernetes.url=(\"https://\" + $host + \":\" + $port)' /config/..data/config.json > /opt/teragrep/k8s_01/etc/config.json; cd /opt/teragrep/k8s_01 || exit 1; java -Dlog4j2.configurationFile=file:/config/..data/log4j2.xml -jar lib/k8s_01.jar;"
]
----
- `K8S_01_KUBERNETES_URL` overwrites `.kubernetes.url` from config.json

## Example test cluster usage

Read example project in `/example` directory for example usage

## Example Helm Chart usage

Edit wanted namespace in `values.yaml`, then run `helm install kubelogreader .` to create and deploy an example instance. It uses `etc/config.json` and default `log4j2.xml` for configurations and persistent volumeclaim for statestore.
You can find example cluster in `/example` directory
4 changes: 3 additions & 1 deletion example/README.md → example/README.adoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Usage

Run `run.sh` to start example cluster. Running the cluster requires `ghcr.io` image pulling secret to exist if image `ghcr.io/teragrep/k8s_01/app:latest` or `ghcr.io/teragrep/rlp_07/app:latest` is not available locally.
Run `run.sh` to start example cluster.

Run `stop.sh` to stop the example cluster.

The container `receiver` is the destination relp server and it will print to stdout any relp messages received. You can follow the logs with the command `kubectl logs -f receiver`

It automatically patches relp server and kubernetes url so it should work automatically as-is in minikube.
9 changes: 4 additions & 5 deletions example/combined.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,17 @@ spec:
containers:
- args:
- -c
- jq --arg host "${KUBERNETES_SERVICE_HOST}" --arg port "${KUBERNETES_SERVICE_PORT}"
--arg target "${RELP_SERVICE_PORT_1601_TCP_ADDR}" '.relp.target=$target | .kubernetes.url=("https://"
+ $host + ":" + $port)' /config/..data/config.json > /opt/teragrep/k8s_01/etc/config.json;
cd /opt/teragrep/k8s_01 || exit 1; exec /usr/bin/java -Dlog4j2.configurationFile=file:/config/..data/log4j2.xml
- export K8S_01_RELP_TARGET="${RELP_SERVICE_SERVICE_HOST}"; export K8S_01_RELP_PORT="${RELP_SERVICE_SERVICE_PORT}";
export K8S_01_KUBERNETES_URL="https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}";
cd /opt/teragrep/k8s_01 || exit 1; exec /usr/bin/java -Dlog4j2.configurationFile=file:etc/log4j2.xml
-jar lib/k8s_01.jar;
command:
- /usr/bin/bash
image: ghcr.io/teragrep/k8s_01/app:latest
imagePullPolicy: IfNotPresent
name: kubelogreader
volumeMounts:
- mountPath: /config/
- mountPath: /opt/teragrep/k8s_01/etc
name: app-config
- mountPath: /var/log/containers
name: host-var-log-containers
Expand Down
4 changes: 2 additions & 2 deletions example/pods/k8s_01.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"name": "kubelogreader",
"volumeMounts": [
{
"mountPath": "/config/",
"mountPath": "/opt/teragrep/k8s_01/etc",
"name": "app-config"
},
{
Expand Down Expand Up @@ -88,7 +88,7 @@
],
"args": [
"-c",
"jq --arg host \"${KUBERNETES_SERVICE_HOST}\" --arg port \"${KUBERNETES_SERVICE_PORT}\" --arg target \"${RELP_SERVICE_PORT_1601_TCP_ADDR}\" '.relp.target=$target | .kubernetes.url=(\"https://\" + $host + \":\" + $port)' /config/..data/config.json > /opt/teragrep/k8s_01/etc/config.json; cd /opt/teragrep/k8s_01 || exit 1; exec /usr/bin/java -Dlog4j2.configurationFile=file:/config/..data/log4j2.xml -jar lib/k8s_01.jar;"
"export K8S_01_RELP_TARGET=\"${RELP_SERVICE_SERVICE_HOST}\"; export K8S_01_RELP_PORT=\"${RELP_SERVICE_SERVICE_PORT}\"; export K8S_01_KUBERNETES_URL=\"https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}\"; cd /opt/teragrep/k8s_01 || exit 1; exec /usr/bin/java -Dlog4j2.configurationFile=file:etc/log4j2.xml -jar lib/k8s_01.jar;"
]
}
],
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/teragrep/k8s_01/KubernetesLogReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public static void main(String[] args) throws IOException {
return;
}
try {
appConfig.getKubernetes().handleOverrides();
appConfig.getRelp().handleOverrides();
appConfig.validate();
}
catch (InvalidConfigurationException e) {
Expand Down
Loading

0 comments on commit 0650e03

Please sign in to comment.