Skip to content

Commit 8202551

Browse files
authored
Merge pull request #850 from crowdsecurity/helm/revamp
improve kubernetes doc (first iteration)
2 parents cfd2223 + 17ea5ac commit 8202551

File tree

5 files changed

+141
-83
lines changed

5 files changed

+141
-83
lines changed

crowdsec-docs/sidebarsUnversioned.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,11 @@ const sidebarsUnversionedConfig: SidebarConfig = {
515515
label: "Wordpress",
516516
id: "bouncers/wordpress",
517517
},
518+
{
519+
type: "doc",
520+
label: "Traefik",
521+
id: "bouncers/traefik",
522+
},
518523
{
519524
type: "link",
520525
label: "Third Party",
34.2 KB
Loading

crowdsec-docs/unversioned/bouncers/ingress-nginx.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import RemediationSupportBadges from '@site/src/components/remediation-support-b
2424

2525
<RemediationSupportBadges
2626
Mode
27-
Appsec
27+
Appsec
28+
Metrics
2829
/>
2930

3031
A lua Remediation Component for Ingress Nginx Controller.
@@ -429,4 +430,4 @@ The timeout to send data from the Remediation Component to the AppSec Component.
429430
APPSEC_PROCESS_TIMEOUT=500 # default
430431
```
431432

432-
The timeout to process the request from the Remediation Component to the AppSec Component.
433+
The timeout to process the request from the Remediation Component to the AppSec Component.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
id: traefik
3+
title: Traefix
4+
sidebar_position: 5
5+
---
6+
7+
import Tabs from '@theme/Tabs';
8+
import TabItem from '@theme/TabItem';
9+
import useBaseUrl from '@docusaurus/useBaseUrl';
10+
import RemediationSupportBadges from '@site/src/components/remediation-support-badge';
11+
12+
13+
<p align="center">
14+
<img src={useBaseUrl('/img/traefik.logo.png')} alt="CrowdSec" title="CrowdSec" width="400" height="300" />
15+
</p>
16+
<p align="center">
17+
<img src="https://img.shields.io/badge/build-pass-green"></img>
18+
<img src="https://img.shields.io/badge/tests-pass-green"></img>
19+
</p>
20+
<p align="center">
21+
&#x1F4DA; <a href="#installation/">Documentation</a>
22+
&#x1F4A0; <a href="https://hub.crowdsec.net">Hub</a>
23+
&#128172; <a href="https://discourse.crowdsec.net">Discourse </a>
24+
</p>
25+
26+
<RemediationSupportBadges
27+
Prometheus
28+
/>
29+
30+
### Traefik on kubernetes
31+
32+
Traefik expects a resource of "Middleware" type named "bouncer", which we will create now.
33+
34+
Here is bouncer-middleware.yaml:
35+
36+
```yaml
37+
apiVersion: traefik.io/v1alpha1
38+
kind: Middleware
39+
metadata:
40+
name: bouncer
41+
namespace: traefik
42+
spec:
43+
plugin:
44+
bouncer:
45+
enabled: true
46+
crowdsecMode: stream
47+
crowdsecLapiScheme: https
48+
crowdsecLapiHost: crowdsec-service.crowdsec:8080
49+
crowdsecLapiKey: mysecretkey12345
50+
```
51+
52+
You can see all the configuration options in the [bouncer documentation](https://plugins.traefik.io/plugins/6335346ca4caa9ddeffda116/crowdsec-bouncer-traefik-plugin).
53+
You can also refer to a [full traefik and CrowdSec stack on kubernetes](https://raw.githubusercontent.com/maxlerebourg/crowdsec-bouncer-traefik-plugin/main/examples/kubernetes/README.md)
54+
55+
Now, you can install the remediation component:
56+
57+
```bash
58+
kubectl apply -f bouncer-middleware.yaml
59+
```

crowdsec-docs/unversioned/getting_started/installation/kubernetes.mdx

Lines changed: 74 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ import CodeBlock from '@theme/CodeBlock';
1212

1313
# Kubernetes Deployment
1414

15-
Before getting started, it is advised to read the [introduction](/unversioned/getting_started/introduction.mdx) page to understand the prerequisites and concepts for running CrowdSec.
15+
Before getting started, it is advised to read the
16+
[introduction](/unversioned/getting_started/introduction.mdx) page to understand
17+
the prerequisites and concepts for running CrowdSec.
1618

1719
## Requirements
1820

1921
- [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl)
2022
- [Helm](https://helm.sh/docs/intro/install/)
2123

24+
Even if an installation could be possible without Helm, it's not documented for now.
25+
2226
## Helm Repository Installation
2327

2428
Add the CrowdSec helm repository to your Helm installation:
@@ -58,7 +62,33 @@ lapi:
5862
value: "k8s linux test"
5963
```
6064
61-
If you want more information about the configuration, you can check the default [values.yaml](https://artifacthub.io/packages/helm/crowdsec/crowdsec#values)
65+
Acquisition is done by reading logs directly from pods. You select which pods to
66+
watch thanks to `namespace` and `podName`, and you have to tag the logs with a
67+
program so CrowdSec knows which parser should handle them. For example, if you
68+
set program: nginx, the nginx parser will pick them up. CrowdSec will
69+
automatically attach to the right pods and feed the logs into the right parsers.
70+
71+
<details>
72+
<summary>Why `program` and not `type` ?</summary>
73+
74+
In standard standalone setups, documentation states that the labels should be
75+
name `type` with the type being the parsed log program (eg nginx, traefik). A
76+
transformation from `type` to `program` is done by the first stage parser
77+
`crowdsecurity/syslog-logs` which is not relevant in a Kubernetes context.
78+
</details>
79+
80+
<details>
81+
<summary>How collections fit in kubernetes environment?</summary>
82+
83+
Collections are "recipes" for understanding logs; they don’t find pods on their
84+
own. You choose which pods to read, and you tag those logs with a program (like
85+
nginx or traefik). When the tag matches what a collection expects, its rules
86+
run; if it doesn’t, they stay idle. One log stream can match several collections
87+
if the tags fit.
88+
</details>
89+
90+
If you want more information about the configuration, you can check the default
91+
[values.yaml](https://artifacthub.io/packages/helm/crowdsec/crowdsec#values)
6292

6393
Then, you can install the Security Engine with the following command:
6494

@@ -80,18 +110,33 @@ crowdsec-agent-kf9fr 1/1 Running 0 34s
80110
crowdsec-lapi-777c469947-jbk9q 1/1 Running 0 34s
81111
```
82112

83-
### Install Remediation Component
113+
### A word About Source IPs
84114

85-
:::info
86-
Depends which ingress controller you are using, you can install the remediation component.
87-
:::
115+
For CrowdSec to do its job in Kubernetes, it needs to see the real client IP. If
116+
not, every request will just look like it’s coming from your ingress controller
117+
or load balancer, and CrowdSec won’t know who the actual attacker is. To fix
118+
this, you need to make sure the original IP gets passed through. Depending on
119+
your setup, that could mean turning on the proxy-protocol in your ingress,
120+
setting externalTrafficPolicy: Local on Services, or tweaking things like
121+
real_ip_header and set_real_ip_from if you’re using NGINX. The exact steps
122+
depend on your stack, but the main idea is simple: CrowdSec needs the real IP,
123+
not the proxy’s.
124+
125+
### A Word About Remediation Component
88126

89-
First you need to already have an ingress controller installed in your cluster (we consider that you installed it using helm).
127+
Installing the CrowdSec Engine as a local API and log processors is very useful
128+
to detect aggressive behaviors, but no remediation action will be taken upon it.
129+
To get remediation actions, one has to install remediation component. As of now
130+
remediation can only happen at ingress level.
90131

91132
For now, we support:
92133

93-
* Traefik
94-
* Nginx
134+
* [Ingress Nginx](/bouncers/ingress-nginx.mdx)
135+
* [Traefik Ingress](/bouncers/traefik.mdx)
136+
137+
Please note that the [Traefik Kubernetes Ingress (Third party
138+
development)](https://plugins.traefik.io/plugins/6335346ca4caa9ddeffda116/crowdsec-bouncer-traefik-plugin))
139+
is maintained outside CrowdSec
95140

96141
Before installing the remediation component, you need to generate API key to communicate with the LAPI.
97142

@@ -102,7 +147,8 @@ If you **have persistentVolumes enabled** in `values.yaml`, you can generate the
102147
kubectl -n crowdsec exec -it crowdsec-lapi-<pod-id> -- cscli bouncers add my-bouncer-name
103148
```
104149

105-
Else you **don't have persistentVolumes enabled**, you need to specify your key in the `values.yaml` file:
150+
Else you **don't have persistentVolumes enabled**, you need to specify your key
151+
in the crowdsec helm `values.yaml` file:
106152

107153
```yaml
108154
lapi:
@@ -119,84 +165,31 @@ lapi:
119165
```
120166
:::
121167

122-
Then, you can install the remediation component with the following command:
168+
### A word about databases
123169

170+
By default, CrowdSec uses a SQLite database, which does not support replication.
171+
In a Kubernetes environment, this limitation prevents the Local API from being
172+
replicated.
124173

125-
#### Traefik
174+
For production deployments on Kubernetes, we recommend using a database engine
175+
that can be deployed in a replicated or highly available way, such as MariaDB or
176+
PostgreSQL. You can leverage existing operators to manage these databases:
177+
* [mariadb operator](https://mariadb.com/resources/blog/get-started-with-mariadb-in-kubernetes-and-mariadb-operator/)
178+
* [postgresql operator](https://github.com/cloudnative-pg/cloudnative-pg)
126179

127-
Traefik expects a resource of "Middleware" type named "bouncer", which we will create now.
180+
Configuration those databases is out of scope of this documentation.
181+
<!-- We want to create blogpost where we do it -->
128182

129-
Here is bouncer-middleware.yaml:
130-
131-
```yaml
132-
apiVersion: traefik.io/v1alpha1
133-
kind: Middleware
134-
metadata:
135-
name: bouncer
136-
namespace: traefik
137-
spec:
138-
plugin:
139-
bouncer:
140-
enabled: true
141-
crowdsecMode: stream
142-
crowdsecLapiScheme: https
143-
crowdsecLapiHost: crowdsec-service.crowdsec:8080
144-
crowdsecLapiKey: mysecretkey12345
145-
```
146-
147-
You can see all the configuration options in the [bouncer documentation](https://plugins.traefik.io/plugins/6335346ca4caa9ddeffda116/crowdsec-bouncer-traefik-plugin).
183+
:::warning
148184

149-
Now, you can install the remediation component:
185+
SQLite may be suitable for testing or low traffic clusters, but it is not
186+
recommended for Kubernetes production deployments. Besides the lack of
187+
replication, SQLite can also become a performance bottleneck under heavy load.
150188

151-
```bash
152-
kubectl apply -f bouncer-middleware.yaml
153-
```
154-
155-
#### Nginx
156-
157-
:::info
158-
We supposed that you have already installed the Nginx ingress controller using this [helm chart](https://artifacthub.io/packages/helm/ingress-nginx/ingress-nginx).
159189
:::
160190

161-
We need to patch ingress-nginx helm chart to add and enable [the crowdsec lua plugin](https://github.com/crowdsecurity/cs-openresty-bouncer).
162-
You can put this configuration example in a file `crowdsec-ingress-nginx.yaml`:
163-
164-
```yaml
165-
controller:
166-
extraVolumes:
167-
- name: crowdsec-bouncer-plugin
168-
emptyDir: {}
169-
extraInitContainers:
170-
- name: init-clone-crowdsec-bouncer
171-
image: crowdsecurity/lua-bouncer-plugin
172-
imagePullPolicy: IfNotPresent
173-
env:
174-
- name: API_URL
175-
value: "http://crowdsec-service.crowdsec.svc.cluster.local:8080"
176-
- name: API_KEY
177-
value: "mysecretkey12345"
178-
- name: BOUNCER_CONFIG
179-
value: "/crowdsec/crowdsec-bouncer.conf"
180-
command: ['sh', '-c', "sh /docker_start.sh; mkdir -p /lua_plugins/crowdsec/; cp -R /crowdsec/* /lua_plugins/crowdsec/"]
181-
volumeMounts:
182-
- name: crowdsec-bouncer-plugin
183-
mountPath: /lua_plugins
184-
extraVolumeMounts:
185-
- name: crowdsec-bouncer-plugin
186-
mountPath: /etc/nginx/lua/plugins/crowdsec
187-
subPath: crowdsec
188-
config:
189-
plugins: "crowdsec"
190-
lua-shared-dicts: "crowdsec_cache: 50m"
191-
```
192-
193-
Once we have this patch we can upgrade the ingress-nginx chart
194-
195-
```bash
196-
helm -n ingress-nginx upgrade -f ingress-nginx-values.yaml -f crowdsec-ingress-bouncer.yaml ingress-nginx ingress-nginx/ingress-nginx
197-
```
198-
199-
200191
## Next Steps?
201192

202-
Great, you now have CrowdSec installed on your system. Within the [post installation steps](/getting_started/next_steps.mdx) you will find the next steps to configure and optimize your installation.
193+
Great, you now have CrowdSec installed on your system. Within the [post
194+
installation steps](/getting_started/next_steps.mdx) you will find the next
195+
steps to configure and optimize your installation.

0 commit comments

Comments
 (0)