Skip to content

Commit b5e34f0

Browse files
committed
add node-exporter
1 parent 119ff91 commit b5e34f0

12 files changed

Lines changed: 254 additions & 51 deletions

File tree

configs/prometheus.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
global:
2+
scrape_interval: 15s
3+
evaluation_interval: 15s
4+
scrape_configs:
5+
- job_name: 'prometheus'
6+
static_configs:
7+
- targets: ['localhost:9090']
8+
- job_name: 'node-exporter'
9+
static_configs:
10+
- targets: ['kube-exporter.kube-stack:9100']
11+
rule_files:
12+
- /etc/prometheus/rules/*.rules
13+
alerting:
14+
alertmanagers:
15+
- static_configs:
16+
- targets: ['kube-alertmgr.kube-stack:9093']

src/main/java/io/github/kubesys/client/writers/ConfigMapWriter.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,15 @@ public ConfigMapWriter withData(String key, String value) {
2929
return this;
3030
}
3131

32+
public ConfigMapWriter withYamlData(String key, String value) throws Exception {
33+
ObjectNode data = getObjectValue("data");
34+
data.set(key, toObjectNode(value));
35+
return this;
36+
}
37+
3238
@Override
3339
public String getTemplate() {
3440
return TEMPLATE;
3541
}
3642

37-
public static void main(String[] args) throws Exception {
38-
ConfigMapWriter writer = new ConfigMapWriter("kube-database", "kube-system");
39-
String value = "|-\r\n"
40-
+ " @test \"Test Health\" {\r\n"
41-
+ " url=\"http://grafana/api/health\"\r\n"
42-
+ "\r\n"
43-
+ " code=$(wget --server-response --spider --timeout 10 --tries 1 ${url} 2>&1 | awk '/^ HTTP/{print $2}')\r\n"
44-
+ " [ \"$code\" == \"200\" ]\r\n"
45-
+ " }";
46-
writer.withData("username", "onceas").withData("run.sh", value.replaceAll("\r\n", "")).stream(System.out);
47-
}
4843
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (2023, ) Institute of Software, Chinese Academy of Sciences
3+
*/
4+
package io.github.kubesys.client.writers;
5+
6+
/**
7+
* @author wuheng@iscas.ac.cn
8+
* @since 2023/08/01
9+
* @version 1.0.2
10+
*
11+
*/
12+
public class DaemonSetWriter extends WorkloadWriter {
13+
14+
public DaemonSetWriter(String name, String namespace) throws Exception {
15+
super(new String[] {
16+
"#APIVERSION#", "apps/v1",
17+
"#KIND#", "DaemonSet",
18+
"#NAME#", name,
19+
"#NAMESPACE#", namespace,
20+
"#NUMBER#", String.valueOf(0)});
21+
}
22+
23+
}

src/main/java/io/github/kubesys/client/writers/DeploymentWriter.java

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
*/
44
package io.github.kubesys.client.writers;
55

6-
import io.github.kubesys.client.writers.WorkloadWriter.Env.ValueFrom;
7-
import io.github.kubesys.client.writers.WorkloadWriter.Env.ValueFrom.SecretKeyRef;
8-
96
/**
107
* @author wuheng@iscas.ac.cn
118
* @since 2023/08/01
@@ -19,37 +16,12 @@ public DeploymentWriter(String name, String namespace) throws Exception {
1916
}
2017

2118
public DeploymentWriter(String name, String namespace, int number) throws Exception {
22-
super(name, namespace, new String[] {
19+
super(new String[] {
2320
"#APIVERSION#", "apps/v1",
2421
"#KIND#", "Deployment",
2522
"#NAME#", name,
2623
"#NAMESPACE#", namespace,
2724
"#NUMBER#", String.valueOf(number)});
2825
}
2926

30-
public static void main(String[] args) throws Exception {
31-
DeploymentWriter writer = new DeploymentWriter("kube-database", "kube-system");
32-
writer
33-
// .withMasterEnbale()
34-
.withContainer(
35-
new Container("postgres", "postgres:14.5-alpine",
36-
new Env[] {
37-
new Env("POSTGRES_PASSWORD", new ValueFrom(new SecretKeyRef("kube-database", "password")))
38-
},
39-
new Port[] {
40-
new Port(5432)
41-
},
42-
new VolumeMount[] {
43-
new VolumeMount("data", "/var/lib/postgresql")
44-
}))
45-
.withContainer(
46-
new Container("adminer", "adminer:4.8.1-standalone",
47-
null,
48-
new Port[] {
49-
new Port(8080)
50-
},
51-
null))
52-
.withVolume("data", "kube-database")
53-
.stream(System.out);
54-
}
5527
}

src/main/java/io/github/kubesys/client/writers/KindWriter.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import java.io.PrintStream;
77

8+
import org.yaml.snakeyaml.DumperOptions;
9+
810
import com.fasterxml.jackson.databind.JsonNode;
911
import com.fasterxml.jackson.databind.ObjectMapper;
1012
import com.fasterxml.jackson.databind.node.ArrayNode;
@@ -36,7 +38,7 @@ public KindWriter(String name, String namespace) throws Exception {
3638
this.json = toObjectNode(getTemplate(), new String[] {"#NAME#", name, "#NAMESPACE#", namespace});
3739
}
3840

39-
public KindWriter(String name, String namespace, String[] kvs) throws Exception {
41+
public KindWriter(String[] kvs) throws Exception {
4042
this.writer = new KubernetesWriter();
4143
this.json = toObjectNode(getTemplate(), kvs);
4244
}
@@ -49,6 +51,11 @@ public void json(String path) {
4951
writer.writeAsJson(path, json);
5052
}
5153

54+
static DumperOptions getDumperOptionsWithPipe() {
55+
DumperOptions options = new DumperOptions();
56+
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.LITERAL);
57+
return options;
58+
}
5259
public void yaml(String path) {
5360
writer.writeAsYaml(path, json);
5461
}

src/main/java/io/github/kubesys/client/writers/WorkloadWriter.java

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ public abstract class WorkloadWriter extends KindWriter {
3131
+ " labels: \r\n"
3232
+ " name: \"#NAME#\"";
3333

34-
public WorkloadWriter(String name, String namespace, String[] kvs) throws Exception {
35-
super(name, namespace, kvs);
34+
public WorkloadWriter(String[] kvs) throws Exception {
35+
super(kvs);
36+
if (this.json.get("spec").get("replicas").asInt() == 0) {
37+
((ObjectNode) json.get("spec")).remove("replicas");
38+
}
3639
}
3740

3841
static final String MATSER = "nodeSelector:\r\n"
@@ -59,14 +62,41 @@ public WorkloadWriter withContainer(Container container) throws Exception {
5962
+ "persistentVolumeClaim:\r\n"
6063
+ " claimName: #PVC#";
6164

62-
public WorkloadWriter withVolume(String name, String pvc) throws Exception {
65+
public WorkloadWriter withPVCVolume(String name, String pvc) throws Exception {
6366
ArrayNode volumes = getArrayValue(getObjectValue(
6467
getObjectValue(getObjectValue("spec"), "template"), "spec"), "volumes");
6568
ObjectNode v = toObjectNode(VOLUME, new String[] {"#NAME#", name, "#PVC#", pvc});
6669
volumes.add(v);
6770
return this;
6871
}
6972

73+
static final String HOST = "name: #NAME#\r\n"
74+
+ "hostPath:\r\n"
75+
+ " path: #PATH#";
76+
77+
public WorkloadWriter withHostVolume(String name, String path) throws Exception {
78+
ArrayNode volumes = getArrayValue(getObjectValue(
79+
getObjectValue(getObjectValue("spec"), "template"), "spec"), "volumes");
80+
ObjectNode v = toObjectNode(HOST, new String[] {"#NAME#", name, "#PATH#", path});
81+
volumes.add(v);
82+
return this;
83+
}
84+
85+
static final String CONFIGMAP = "name: #NAME#\r\n"
86+
+ "configMap:\r\n"
87+
+ " name: #CONFIGMAP_NAME#\r\n"
88+
+ " items:\r\n"
89+
+ " - key: #CONFIGMAP_KEY#\r\n"
90+
+ " path: #PATH#";
91+
92+
public WorkloadWriter withConfigMapVolume(String name, String cm, String cmKey, String path) throws Exception {
93+
ArrayNode volumes = getArrayValue(getObjectValue(
94+
getObjectValue(getObjectValue("spec"), "template"), "spec"), "volumes");
95+
ObjectNode v = toObjectNode(CONFIGMAP, new String[] {"#NAME#", name, "#CONFIGMAP_NAME#", cm, "#CONFIGMAP_KEY#", cmKey, "#PATH#", path});
96+
volumes.add(v);
97+
return this;
98+
}
99+
70100
@Override
71101
public String getTemplate() {
72102
return TEMPLATE;
@@ -83,10 +113,22 @@ public static class Container {
83113

84114
Env[] env;
85115

116+
String[] args;
117+
86118
Port[] ports;
87119

88120
VolumeMount[] volumeMounts;
89121

122+
public Container(String name, String image, String[] args, Env[] env, Port[] ports, VolumeMount[] volumeMounts) {
123+
super();
124+
this.name = name;
125+
this.image = image;
126+
this.args = args;
127+
this.env = env;
128+
this.ports = ports;
129+
this.volumeMounts = volumeMounts;
130+
}
131+
90132
public Container(String name, String image, Env[] env, Port[] ports, VolumeMount[] volumeMounts) {
91133
super();
92134
this.name = name;
@@ -143,6 +185,14 @@ public String getImagePullPolicy() {
143185
public void setImagePullPolicy(String imagePullPolicy) {
144186
this.imagePullPolicy = imagePullPolicy;
145187
}
188+
189+
public String[] getArgs() {
190+
return args;
191+
}
192+
193+
public void setArgs(String[] args) {
194+
this.args = args;
195+
}
146196

147197
}
148198

src/test/java/io/github/kubesys/client/install/stack/KubeBackendTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55

66

77
import io.github.kubesys.client.writers.DeploymentWriter;
8-
import io.github.kubesys.client.writers.SecretWriter;
98
import io.github.kubesys.client.writers.ServiceWriter;
109
import io.github.kubesys.client.writers.WorkloadWriter.Container;
11-
import io.github.kubesys.client.writers.WorkloadWriter.Env;
1210
import io.github.kubesys.client.writers.WorkloadWriter.Port;
13-
import io.github.kubesys.client.writers.WorkloadWriter.Env.ValueFrom;
14-
import io.github.kubesys.client.writers.WorkloadWriter.Env.ValueFrom.SecretKeyRef;
1511

1612
/**
1713
* @author wuheng@iscas.ac.cn

src/test/java/io/github/kubesys/client/install/stack/KubeDatabaseTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ public static void main(String[] args) throws Exception {
6565
new Port[] {
6666
new Port(8080)
6767
},
68-
null))
69-
.withVolume(StackCommon.VOLUME_DATA, NAME)
68+
new VolumeMount[] {
69+
new VolumeMount(StackCommon.VOLUME_DATA, "/var/lib/postgresql")
70+
}))
71+
.withPVCVolume(StackCommon.VOLUME_DATA, NAME)
7072
.stream(System.out);
7173

7274
ServiceWriter service = new ServiceWriter(NAME, StackCommon.NAMESPACE);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright (2022, ) Institute of Software, Chinese Academy of Sciences
3+
*/
4+
package io.github.kubesys.client.install.stack;
5+
6+
7+
import io.github.kubesys.client.writers.DaemonSetWriter;
8+
import io.github.kubesys.client.writers.ServiceWriter;
9+
import io.github.kubesys.client.writers.WorkloadWriter.Container;
10+
import io.github.kubesys.client.writers.WorkloadWriter.Port;
11+
import io.github.kubesys.client.writers.WorkloadWriter.VolumeMount;
12+
13+
/**
14+
* @author wuheng@iscas.ac.cn
15+
* @since 2023/08/02
16+
* @version 1.0.3
17+
*
18+
* get real Url from <code>KubernetesRuleBase</code>
19+
*
20+
*/
21+
public class KubeExporterTest {
22+
23+
static final String NAME = "kube-exporter";
24+
25+
static final String EXPORTER = "node-exporter";
26+
27+
static final String EXPORTER_IMAGE = "prom/node-exporter:v1.6.1";
28+
29+
static final String VOLUME_PROCEFS = "procfs";
30+
31+
static final String VOLUME_SYSFS = "sysfs";
32+
33+
public static void main(String[] args) throws Exception {
34+
35+
36+
DaemonSetWriter ds = new DaemonSetWriter(NAME, StackCommon.NAMESPACE);
37+
38+
ds.withMasterEnbale()
39+
.withContainer(new Container(EXPORTER, EXPORTER_IMAGE,
40+
new String[] {
41+
"--path.procfs=/host/proc",
42+
"--path.sysfs=/host/sys",
43+
"--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc)($$|/)"
44+
},
45+
null,
46+
new Port[] {
47+
new Port(9100)
48+
},
49+
new VolumeMount[] {
50+
new VolumeMount(VOLUME_PROCEFS, "/host/proc"),
51+
new VolumeMount(VOLUME_SYSFS, "/host/sys")
52+
}))
53+
.withHostVolume(VOLUME_PROCEFS, "/proc")
54+
.withHostVolume(VOLUME_SYSFS, "/sys")
55+
.stream(System.out);
56+
57+
ServiceWriter service = new ServiceWriter(NAME, StackCommon.NAMESPACE);
58+
service.withType("NodePort").withSelector(NAME)
59+
.withPort(9100, 30303, "node-exporter")
60+
.stream(System.out);
61+
}
62+
}

src/test/java/io/github/kubesys/client/install/stack/KubeMessageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static void main(String[] args) throws Exception {
5858
new VolumeMount[] {
5959
new VolumeMount(StackCommon.VOLUME_DATA, "/var/lib/rabbitmq")
6060
}))
61-
.withVolume(StackCommon.VOLUME_DATA, NAME)
61+
.withPVCVolume(StackCommon.VOLUME_DATA, NAME)
6262
.stream(System.out);
6363

6464
ServiceWriter service = new ServiceWriter(NAME, StackCommon.NAMESPACE);

0 commit comments

Comments
 (0)