WEBVTT

00:00:01.540 --> 00:00:04.150
During this course, I will use Visual Studio Code,

00:00:04.260 --> 00:00:08.000
but as long as you have a basic text editor and a terminal,

00:00:08.020 --> 00:00:09.850
you can easily follow along.

00:00:10.640 --> 00:00:12.560
Just as I mentioned in the previous clip,

00:00:12.560 --> 00:00:15.460
we will use Docker to deploy the Kafka components.

00:00:15.840 --> 00:00:19.350
And to be more precise, we'll use a docker‑compose file.

00:00:20.040 --> 00:00:24.560
A docker‑compose file is a descriptor of all the components we want to deploy.

00:00:25.240 --> 00:00:25.760
Here,

00:00:25.770 --> 00:00:30.160
we have no more and no less than 11 components that are going to be deployed,

00:00:30.640 --> 00:00:34.080
three zookeepers, three brokers, a schema‑registry,

00:00:34.080 --> 00:00:39.060
Kafka Connect, ksqldb‑server and cli, and finally,

00:00:39.070 --> 00:00:41.580
a custom application called audit‑logger.

00:00:42.640 --> 00:00:45.680
We'll go through each application and explain what it does.

00:00:45.700 --> 00:00:48.760
But first, I would like to talk a bit about security setup.

00:00:49.540 --> 00:00:50.700
In this example,

00:00:50.710 --> 00:00:54.360
we'll be using Mutual TLS to secure the connection between components.

00:00:54.740 --> 00:00:59.750
But, since all of them are using a JVM to run, we need a bit of a special setup.

00:01:00.540 --> 00:01:02.850
Let's take, for example, the Kafka broker.

00:01:03.340 --> 00:01:06.850
As you may notice, there are a lot of properties that need to be added.

00:01:07.640 --> 00:01:11.260
In fact, if you are interested in setting up a secure Kafka environment,

00:01:11.640 --> 00:01:15.560
I recommend one of my other courses, Securing a Kafka Cluster.

00:01:15.940 --> 00:01:16.440
In it,

00:01:16.450 --> 00:01:19.110
I explain how to properly set up different security

00:01:19.110 --> 00:01:22.240
mechanisms such as encryption, authentication,

00:01:22.250 --> 00:01:24.660
and authorization with a Kafka cluster.

00:01:25.840 --> 00:01:26.960
Back to our demo.

00:01:27.100 --> 00:01:30.250
The key aspect you need to notice is the volumes mount.

00:01:30.840 --> 00:01:32.250
Using a Docker volume,

00:01:32.250 --> 00:01:35.780
I'm mounting my local secrets folder on the Docker

00:01:35.780 --> 00:01:37.270
container at the path /etc/kafka/secrets.

00:01:37.330 --> 00:01:45.560
Then, all secrets from this path are referenced in the properties.

00:01:46.240 --> 00:01:48.740
We'll soon see exactly what those secrets are,

00:01:48.800 --> 00:01:51.660
but first, we need to do another preparatory step.

00:01:52.340 --> 00:01:54.950
We need to add some entries in the hosts file.

00:01:55.740 --> 00:02:00.260
On UNIX systems, the hosts file is placed in the etc directory.

00:02:01.040 --> 00:02:07.180
On Windows, the path is C:\Windows\System32\drivers\etc\hosts.

00:02:07.180 --> 00:02:13.560
For every broker and zookeeper instance, we need to add a loopback address.

00:02:14.140 --> 00:02:17.520
This setup is required because we are deploying multiple instances

00:02:17.520 --> 00:02:20.460
over brokers and zookeepers on the same machine.

00:02:21.340 --> 00:02:25.060
With this in place, we can finally check what's inside the secrets folder.

00:02:26.040 --> 00:02:28.030
In the secret folder, there are two files,

00:02:28.030 --> 00:02:32.060
create_secrets script and a password.txt.

00:02:32.740 --> 00:02:35.660
The password.txt file contains the password we're

00:02:35.660 --> 00:02:40.160
going to use to protect our secret, which is going to be notsecret.

00:02:40.940 --> 00:02:44.200
The create_secrets script can be used to create all

00:02:44.200 --> 00:02:46.160
secrets required by our setup.

00:02:46.840 --> 00:02:50.170
To run the script, we can simply execute it from the TERMINAL.

00:02:51.440 --> 00:02:55.060
We will be prompted a couple of times if we trust this certificate,

00:02:55.440 --> 00:02:58.560
and we simply have to write yes, and then press Enter.

00:02:59.540 --> 00:03:01.850
So what is this script actually creating?

00:03:02.740 --> 00:03:06.460
First, the script is creating a self‑signed Certificate Authority.

00:03:07.040 --> 00:03:08.200
Using the CA,

00:03:08.200 --> 00:03:12.860
the script then signs a certificate for each component I mentioned before.

00:03:13.740 --> 00:03:14.300
Now,

00:03:14.310 --> 00:03:17.010
Java applications are using two cryptographic

00:03:17.010 --> 00:03:20.160
containers called KeyStores and TrustStores.

00:03:20.740 --> 00:03:21.480
Basically,

00:03:21.490 --> 00:03:24.820
a KeyStore contains the certificate that will be used to

00:03:24.820 --> 00:03:28.060
identify a server and a public/private key pair.

00:03:28.940 --> 00:03:31.690
The key pair is used to encrypt the communication

00:03:31.690 --> 00:03:33.560
between the server and the client.

00:03:34.340 --> 00:03:37.960
The TrustStore, however, only contains the CA certificate.

00:03:38.840 --> 00:03:42.960
After everything is complete, we should have two JKS files per component,

00:03:42.970 --> 00:03:45.100
a KeyStore, and TrustStore.

00:03:45.100 --> 00:03:47.160
So, we can start deploying the components.

00:03:47.840 --> 00:03:50.710
Since the docker‑compose file describes what components we

00:03:50.710 --> 00:03:55.460
want to deploy and how to deploy them, we only need to run a single command.

00:03:56.440 --> 00:03:59.050
But first, let me switch back to the main directory.

00:03:59.540 --> 00:04:04.050
Here, we need to run the following command docker compose up ‑d.

00:04:04.930 --> 00:04:05.360
And that's it.

00:04:06.240 --> 00:04:08.860
Now, depending on how fast your computer is,

00:04:08.960 --> 00:04:11.440
it might take a couple of minutes to deploy everything,

00:04:11.460 --> 00:04:16.260
so I suggest waiting for at least 1 or 2 minutes before proceeding further.

00:04:17.440 --> 00:04:21.550
To check everything is up and running, we can use the docker ps command.

00:04:22.240 --> 00:04:24.760
We need to check that all containers are up.

00:04:25.840 --> 00:04:30.480
I can already see three zookeepers, the schema‑registry, and the audit‑logger.

00:04:30.720 --> 00:04:34.750
And then, if I scroll up a bit, the three brokers, Kafka Connect.

00:04:35.140 --> 00:04:39.460
And if I scroll up even more, ksqldb and ksqldb‑cli.

00:04:40.240 --> 00:04:44.710
In total, we have 11 components, and all of them are up and running.

00:04:44.980 --> 00:04:45.660
Excellent.

00:04:46.940 --> 00:04:49.820
Now, we can jump into the first feature we're going to build,

00:04:49.850 --> 00:04:52.550
ingesting audit logs into Apache Kafka.

00:04:53.140 --> 00:04:56.350
Before ingesting logs, we actually need to create them,

00:04:56.840 --> 00:04:58.850
that's the role of the audit‑logger.

00:04:59.340 --> 00:04:59.880
In fact,

00:04:59.890 --> 00:05:03.940
the audit‑logger is nothing more than an HHTP server that

00:05:03.940 --> 00:05:06.860
creates an audit log when a resource is created.

00:05:07.840 --> 00:05:11.560
To execute the HTTP request, we're going to use curl.

00:05:12.340 --> 00:05:16.630
Here, we are creating a resource of type vm called server‑1.

00:05:18.040 --> 00:05:21.460
In fact, let's create another one, this time with a different name.

00:05:22.340 --> 00:05:23.880
To authenticate the user,

00:05:23.880 --> 00:05:27.440
we're using a simple basic authentication header that

00:05:27.440 --> 00:05:31.060
contains a base64 encoded username and password.

00:05:31.940 --> 00:05:32.560
Excellent.

00:05:33.540 --> 00:05:36.870
Both of these requests returned a 201 Created response,

00:05:36.870 --> 00:05:39.150
but where exactly can we see the logs?

00:05:40.240 --> 00:05:42.860
The logs can be found in the logs folder that now

00:05:42.860 --> 00:05:45.750
contains a file called audit.log.

00:05:46.840 --> 00:05:49.450
The structure of the audit.log is pretty simple,

00:05:49.840 --> 00:05:53.260
we have the date and time for when the resource was created,

00:05:53.530 --> 00:05:57.180
the username alongside the IP of the person that created it,

00:05:57.450 --> 00:06:00.700
the resource type, the action the user performed,

00:06:00.970 --> 00:06:02.950
and finally, the name of the resource.

00:06:04.040 --> 00:06:05.720
Now that we have the logs in place,

00:06:05.940 --> 00:06:08.360
it is time to ingest them in the Kafka cluster.

00:06:09.140 --> 00:06:12.050
The way we are going to do this is by using Kafka Connect.

00:06:13.040 --> 00:06:15.630
Kafka Connect is specifically designed for sending

00:06:15.630 --> 00:06:18.650
data to and from a Kafka cluster.

00:06:19.440 --> 00:06:20.110
However,

00:06:20.120 --> 00:06:24.560
Kafka Connect is not the only tool that can transfer logs to a Kafka cluster.

00:06:24.940 --> 00:06:27.900
Other solutions such as Logstash, Beats,

00:06:28.030 --> 00:06:32.340
Apache Flume, Fluentd, and many others may work just fine,

00:06:32.500 --> 00:06:35.950
so check with your company if any of these tools are already used.

00:06:37.240 --> 00:06:40.150
Kafka Connect leverages the concept of a connector.

00:06:40.840 --> 00:06:45.360
Basically, a connector is a task that defines where data is coming from.

00:06:45.940 --> 00:06:51.460
In this scenario, we're using a FileStreamSource connector called audit‑logs.

00:06:52.440 --> 00:06:56.070
The audit‑logs connector will take the data from the audit.log

00:06:56.070 --> 00:06:59.160
file and publish it to the audit‑logs topic.

00:07:00.140 --> 00:07:04.220
The file is mounted inside the Docker container using a Docker volume,

00:07:04.230 --> 00:07:08.260
so Kafka Connect has access to the log file we just saw.

00:07:09.940 --> 00:07:13.750
Now, Kafka uses topics to keep different data in buckets.

00:07:14.640 --> 00:07:18.660
Just like a database is using tables to separate different kinds of data,

00:07:19.040 --> 00:07:22.460
Kafka uses topics to separate events from one another.

00:07:23.140 --> 00:07:25.360
To actually see the contents of a topic,

00:07:25.360 --> 00:07:29.590
we need to use ksqldb, which is composed of two components,

00:07:29.780 --> 00:07:31.560
a CLI and a server.

00:07:32.440 --> 00:07:37.290
Right now, we're using Docker to open a CLI that connects to the server.

00:07:37.290 --> 00:07:41.730
Ksqldb allows us to query and manipulate data in Kafka

00:07:41.790 --> 00:07:44.450
by using an SQL‑like query language.

00:07:45.440 --> 00:07:50.560
For example, we can print all the contents of a topic using the PRINT statement.

00:07:51.440 --> 00:07:52.920
And there they are.

00:07:52.920 --> 00:07:56.370
We can see that the two audit‑log entries we produced

00:07:56.380 --> 00:07:59.350
earlier are now present in the Kafka cluster.
