 Hello everyone. Welcome to the inaugural Kubernetes Community Day Africa. I hope you are enjoying the event so far. Let me introduce myself. My name is Rafael Natali and I will be talking about Kubernetes, Ansible, Automation, Infrastructure as Code and how all they work together. A little bit about myself. I am currently a senior DevOps engineer at Marionette Consulting. I have been working with Cloud and DevOps for about five years now across different industries and geographies. Before that, I was a assistant administrator working mainly with mid-ware technologies. Also, I am an AWS and GCP certified professional and more relevant for today's topic. I am a certified Kubernetes administrator. Okay. So let's get started. Infrastructure as Code. Infrastructure as Code is an intrinsic part of the DevOps culture and best practices. The goal is to guarantee configuration consistency and traceability across environments. There are a plethora of tools that can help us to code our infrastructure. Ansible is one of the most well-known automation tools in the DevOps space. It's a simple and powerful tool that we're going to be using today for provision or Kubernetes cluster on Google Cloud. Okay. So the first step to have our code up and running is to create a couple of folders for our Ansible. I will create just an Ansible folder and a couple of subfolders, inventory and rows. Okay. Inside of the inventory folder, we'll create what we call inventory file. In our case today, the inventory file will have the declaration of all the variables that we are going to be using through our code. I have a section here for GCP-related information where I define my zone, my region, I will define my project ID, my service accounts, and also a section where I will be defining some variables for our Kubernetes cluster. So for example, the name, I can say KCD Africa and the number of nodes. Let's create a cluster with three nodes. We have 100 gigabytes of disk, solid state and machine type N1 standard 2. Okay. Those are the variables that we are going to be using throughout. We'll see when it starts to go with the rows where we're going to be using that. Okay. So as for the inventory file, I will be using a file that I created beforehand just for security reasons. I will not be showing here, but has the same variables that I show in the example before. Okay. After the inventory file, we are going to be using the rows, the Ansible rows to configure the Ansible tasks to provision our cluster. Rows are basically a set of instructions also known as tasks that Ansible will execute in this specific order to achieve the desired state. It will be clear when we start to code it. Okay. So the first row I'm going to be creating is a row that will create a GCP network, a VPC in my project. So I create a folder called network inside of the folders, the folder rows, sorry, and tasks. Okay. So I don't want to use the default network of my GCP project to host my Kubernetes cluster. In that case, I am first creating a specific GCP network. Okay. The tasks is just a Yemo file that should, must actually exist inside of the tasks folder. Name should be main. And this task, I just have a name for the task, create GCP network. And here I declare the module that the Ansible module that we are going to be using. The Ansible module, as we can see here in the documentation, is a piece of code that Ansible uses to know what to do when connect to GCP. So here we can see in the documentation all the parameters that I can use in the case for the network. We have an example, which is pretty much the same that we are using here, and all the return values. So when we create the GCP network, we can return, we can register this in a variable, for example, the name, as we are going to see here in the row. Okay. So the module is a piece of code that Ansible knows what to do in the GCP. I will come back to the roles, to the tasks later on. Before that, I will just create another row for Kubernetes cluster. Okay. Create a row, the same as I did before. I just create another folder, Kubernetes, create a tasks. Inside of these tasks, I will create another main.emo. And now I will have two Ansible modules. Okay. So in order to create a Kubernetes cluster in GCP, I need to use two Ansible modules. I need to use the Google Cloud GCP container cluster. And after, I need to use the Google Cloud GCP container notebook to create a notebook inside of the Kubernetes cluster. Okay. Again, I will come back this later when the cluster is creating. And the last step is to create Ansible playbook inside of the Ansible folder. Here I will create a file called create Kubernetes.emo. Again, another demo file. And the playbook. The Ansible playbook is a file that we created to execute the roles. It's an ordered list of tasks, or in that case, roles that we use to execute the same code every time the same way, the same manner. It's important to highlight that, as I mentioned, it's an ordered list. So when I declare here the roles, I will first create the network, and then I will create the Kubernetes cluster. Because I need the network created first, and then create the Kubernetes because the Kubernetes exists inside of this network. Okay. So now we use the Ansible playbook comment to execute this playbook. And the playbook will call the two roles, the network and the Kubernetes roles, and we'll execute all the steps. So as we can see here, it starts the play, create info, which is this one. It's Ansible find the roles, and it finds the first row is the network. Okay. So the network, the network is here, natural tasks main.emo. So Ansible knows that you need to use this Ansible module, the Google Cloud GCP compute network to create the network. So it will use the name network dash cluster name. We can see here, this is a Genia 2 type of variable. And I replace this value cluster name with the name of the cluster name that I specified previously in the inventory file. Remember, I put KCD Africa. So Ansible will ask GCP to create a network called network dash KCD dash Africa. It will create all the subnetworks automatically because I said this is true. In the project ID, project again, Genia 2 type, it's going to replace with the project ID that I specify in the inventory file with the credential file again that I use in the inventory file. Okay. This state, this state for this task is to be present, which means that I'm telling Ansible that whenever it runs, it needs to ensure that the network exists. Okay. So for now, I already run the first time it changed, which means that it already created. We can see here in the GCP console already created with all the subnetworks. So that's why it's changed it. Okay. Because previous it doesn't exist and now it exists. If for whatever reason, this network is removed, it's deleted, and we run this Ansible again, it will recreate. Okay. And if we run the Ansible again and Ansible see that the network already exists, it doesn't do anything. It doesn't recreate or delete or do anything. You just say, okay, the state is present. So I'm okay with that. In this, using this keyword register, I am registering all the outcomes of this module. Remember, when I show you guys the return values here in the documentation. So all these values, how to create a subnetwork description, name, all of this is registered in this variable. And this is important because when I am creating the Kubernetes cluster, I use this. Okay. Now, as we can see here in the playbook, it is running the task of the row Kubernetes and the task Kubernetes cluster, which is this first one, which use this module, the Google Cloud GCP container cluster. Again, the same in the documentation. It creates the cluster, the very first part of the cluster. There are several, all the parameters that we can specify through the console, we can specify here. Okay. As you can see, a lot of things. So what I did here, I use the name, which is the same name that I specify in the event refile, the KCD Africa, the number of nodes that I want is three. The location is the zone. Europe was true in my case. Here is the important part, is the different part, network. Which network I'm going to create the Kubernetes cluster. I don't want to create in the default one. I want to create the one that I created before. So that's why I registered here. I use the variable network with the return value name to create this Kubernetes cluster. So I associating my Kubernetes cluster with my network. If I don't specify anything, it will create in the default network. But I don't want that. My project ID is coming from the event refile. I'm out dedicating using the service account again that I specify in the event refile. The state is present. So if forever is for any reason, this cluster is deleted. When I rerun the sensible it will create. Or if I run in a project ID that doesn't, or in a zone or a network that doesn't have this cluster, it will create. To remove, we just need to create another role with another playbook and change this to absent. So if it exists, it will change the status, the state from present to absent. It will remove. Okay. So this is the first part. Let's jump to the console and see what is happening in our Kubernetes cluster console. Okay. You see that we have a cluster named KCD Africa in your request to see with all the information here for the network. You see it's creating in the network that we specified previously. That's great. Now, the second part is the notebook. So this module only creates the cluster but doesn't have any notes. That's why we need the second module, the Google Cloud GCP container notebook. Again, I'm specifying a name. Let's just jump for the documentation just for you guys to see again. In the same way, there are several parameters you can specify. I just choose a couple of ones for this presentation, for simplicity. So the name is notepool and the cluster name, the initial node count is the same that I specified before. And the cluster, which cluster I am going to associate my notebook. I'm going to associate with the cluster that I created in the step before, the Kubernetes cluster. Okay. And I will create with the disk size of 100 gigabytes, SSD, the machine, N1, N2 and again, present. So I want this to be present. Okay. So we can see here in the terminal that it already run. So it already created the Kubernetes cluster. It already creates the Kubernetes notebook. So three are okay. Three are changed. So if we go to the Kubernetes cluster engine, we can see the node is here. It's created. It has all the three nodes. For those who don't know, these nodes are nothing else than three compute engines instance that GCP creates automatically for us. We don't need to worry about that. See? Those are the three nodes of my Kubernetes cluster. Okay. Now just to show you what happens if I run this Ansible Playbook again. Now all the resources that we want already exist. So I don't want Ansible to do anything. You see? So when we rerun this with this information and Ansible checks that everything already exists in GCP, you just say, okay. Okay. Okay. So everything is okay because all these states are true. So I have a cluster. I have the node pool. I have the network. So everything is okay. Ansible doesn't need to do any change in the environment. Okay. And now just to show how do we connect. I'll just increase a little bit my screen and how do we, if you go here in the cluster, I'll just connect. I will copy the gcloud command to connect with my cluster. I copy here, paste. Okay. My kube config was configured. And now I can just, oops, kube CTL, get nodes. And here they are, the same three nodes that we saw in the console before. If I do kube CTL, get namespaces, I can see all the default namespaces, get pods, manage them, dash n, kube system. Do we have anything in the kube system running? Oh, we have. Great. Let's go. That's good. From this point on, you can manage the Kubernetes cluster with kube CTL. No problems at all. Okay. So that's what I wanted to show to you today. This presentation was just to demonstrate the use of infrastructure as code to install, set up, and configure a simple Kubernetes cluster on Google Cloud platform. And also to show some essential concepts from Ansible like rows and playbooks and how to configure and structure them to provision research in the cloud. Much, much more is possible with Ansible, GCP, Kubernetes. Obviously, this is just a starting point to you be able to organize your Kubernetes configuration such as namespace, deployments, and policies as code. Okay. So thank you very much for being with me today and continue enjoying the Kubernetes community Africa. Bye.