<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Joseignacio Carretero on Medium]]></title>
        <description><![CDATA[Stories by Joseignacio Carretero on Medium]]></description>
        <link>https://medium.com/@joseignacio.carretero?source=rss-9f463fd493bb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*R-WV16qF42vbZFvi</url>
            <title>Stories by Joseignacio Carretero on Medium</title>
            <link>https://medium.com/@joseignacio.carretero?source=rss-9f463fd493bb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 09:22:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@joseignacio.carretero/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[LSOF: List the Opened files]]></title>
            <link>https://medium.com/@joseignacio.carretero/lsof-list-the-opened-filesd-87ee35229074?source=rss-9f463fd493bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/87ee35229074</guid>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Joseignacio Carretero]]></dc:creator>
            <pubDate>Tue, 21 Apr 2026 11:56:20 GMT</pubDate>
            <atom:updated>2026-04-21T12:19:42.920Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H4kp7pRFrdU-PsQ6iY8J8g.png" /></figure><p>lsof is a small and interesting utility that is able of finding all the open files in our Linux system. We need to bear in mind that for Linux everything is a file - According to lsof man page:</p><pre>An open file may be a regular file, a directory, a block special file, a<br>character special file, an executing text reference, a library, a stream or a<br>network file (Internet socket, NFS file or UNIX domain socket.) A specific <br>file or all the files in a file system may be selected by path.</pre><p>Note that lsof sometimes shows some output in stderr (specially regarding overlay filesystems as the ones used by docker). So it is quite convenient to redirect all the err output to /dev/null.</p><h3>Opened regular files.</h3><p>This is the basic thing we can do with lsof. There are 2 main questions here. On the one hand, we can query about a file or directory - What process and user id has opened a file or have files opened in a given directory</p><pre>## This one tells what process is using the file<br>lsof &lt;file&gt;<br><br>## What process/users have files opened in the directory<br>lsof +D &lt;dir&gt;</pre><p>On the other hand, we can query what files has opened an user or a process</p><pre># Files opened by an user<br>lsof -u &lt;user&gt;<br><br># Files opened by a process PID<br>lsof -p &lt;pid&gt;</pre><h3>Understanding lsof output</h3><p>lsof will show the output with the following fileds as shown in the header:</p><pre>COMMAND   PID USER  FD      TYPE   DEVICE  SIZE/OFF       NODE NAME</pre><p>Command - The command typed. It can be zsh, lsof, vim, ...</p><p>PID - The process ID which opened a file.</p><p>USER - The name of the user which opened the file</p><p>FD - File Descriptor - This one shows the file descriptor in the process and the mode that it is opened:</p><ul><li>0 - stdin</li><li>1- stdout</li><li>2 - stderr</li><li>n - File number, usually you&#39;ll find some letters referencing the permissions:</li><li>r read, w write, u update (read and write)</li></ul><p>TYPE - Type of file. It doesn&#39;t have to be something in the hard disk. There are many different file types.</p><ul><li>cwd - Current working directory.</li><li>rtd - Root directory (Where the process was started)</li><li>txt - Text file. This is usually the program binary file.</li><li>mem - Memory mapped file (usually dynamic libraries)</li><li>REG- Regular file</li><li>DIR- Directory</li><li>CHR - This is a special fie of type character (e.g. /dev/tty, /dev/pts/0)</li><li>FIFO - Pipe for interprocess communication.</li><li>UNIX - UNIX domain socket.</li><li>IPv4/IPv6 - Network connection.</li><li>DEL - These files are usually not in the disk but they are usually in shared memory <em>/dev/shm</em> (e.g. brave browser) or files only existing in memory but not in the disk (<em>/memfd:pulseaudio)</em>.</li></ul><pre># Example: CHR files used by my zsh shell<br>❯ lsof -p 17062 2&gt;/dev/null | grep CHR<br>zsh     17062 jicg   0u   CHR  136,0      0t0         3 /dev/pts/0<br>zsh     17062 jicg   1u   CHR  136,0      0t0         3 /dev/pts/0<br>zsh     17062 jicg   2u   CHR  136,0      0t0         3 /dev/pts/0<br>zsh     17062 jicg  10u   CHR  136,0      0t0         3 /dev/pts/0</pre><p>DEVICE - It shows minor and mayor numbers of the device. It identifies the device or device partition where the file is stored</p><pre># And following my previous example, where we had the device with values<br># 136,0 -- So I can list those devices in /dev: <br># The devuce /dev/pts corresponds to 0, 1, 2 (stdin, stdout and stderr).<br>❯ ls -l /dev/pts | grep 136<br>crw--w---- 1 jicg tty  136, 0 abr 16 10:30 0<br>crw--w---- 1 jicg tty  136, 1 abr 16 10:05 1<br>crw--w---- 1 jicg tty  136, 2 abr 16 10:30 2<br><br># Another one I usually see in my testing is 253,0 - My nvme device.<br>❯ ls -l /dev/ | grep 253<br>brw-rw----   1 root disk      253,     0 abr 16 09:52 dm-0</pre><p>SIZE/OFF - For regular files, this one shows the size of the file and the OFFSET pointer in the process.</p><p>NODE - This one is the inode, basically a number representing the inode of the regular file, or if we are waching the sockets, it can show the protocol (TCP/UDP), etc.</p><p>NAME - The name (descriptive name) or what is it opened. It can be a name of a file, it can be an ip address/port if we are watching the network connections.</p><h3>Deleted files</h3><p>Sometimes, we can find “deleted” files, this means that a file has been deleted but the process which opened it still haven’t closed it. This file will disappear when the process closes it or when the process dies.</p><pre>tcpdump port 143 -w /tmp/f.pcap &amp;<br><br>rm /tmp/f.pcap<br># We can see the process now ant the deleted mark<br>lsof -p 390635 2&gt;/dev/null | grep deleted<br>tcpdump 390635 root   5w      REG   0,36        0     10619 /tmp/f.pcap (deleted)</pre><p>We know that in Linux the name of the file (a tag in a directory) and the file are separated. Linux will only remove the file content when:</p><ul><li>It is not pointed by any name (link).</li><li>No process keeps it open.</li></ul><p><strong>Zombie file </strong>— Are deleted files still opened by a process, it might be weird but it has some useful usecases:</p><ul><li>Updating libraries (.so) while a program is still running. The file is removed from disk, but as the original library file is still opened, the file is not still removed from disk. The process can continue running.</li><li>Security and privacy — Sometimes a file is created and removed from disk so it can’t be seen with a single ls command.</li></ul><p>So, sometimes, you need to be aware that if you remove a huge log file because you are running out of disk space, this space will not be freed until the process is restarted.</p><h3>Sockets and network connections</h3><p>We can also watch for opened network connections using lsof - From Linux perspective sockets have their own file descriptor and this means that they are treated as opened files.</p><p>We can see these network connections using lsof -i - for TCP or UDP sockets</p><pre># watch connections (opened port)<br>#    lsof -i [:port]  --- <br>#<br>#  -n (-n prevents name resolition - DNS - Shows only IP)<br>#  -P (-P prevents converting ports to protocol names)<br><br>❯ sudo lsof -i :22<br>COMMAND PID USER FD   TYPE DEVICE SIZE/OFF NODE NAME<br>sshd    964 root 6u  IPv4   9690      0t0  TCP *:ssh (LISTEN)<br>sshd    964 root 7u  IPv6   9692      0t0  TCP *:ssh (LISTEN)<br>❯ sudo lsof -Pi :22<br>COMMAND PID USER FD   TYPE DEVICE SIZE/OFF NODE NAME<br>sshd    964 root 6u  IPv4   9690      0t0  TCP *:22 (LISTEN)<br>sshd    964 root 7u  IPv6   9692      0t0  TCP *:22 (LISTEN)</pre><p>We can see UNIX sockets using lsof -U.</p><p>There is much more regarding the parameter -i -</p><pre># List all TCP sockets in LISTEN state:<br>sudo lsof -iTCP -sTCP:LISTEN -n -P<br><br># List UDP sockets<br>sudo lsof -iUDP -n -P<br><br># Listar all TCP and UDP sockets<br>sudo lsof -i -n -P</pre><p>So, the information shown by lsof could also be used to have a good grasp of what ports are opened in our system and the connections in our system. It is not as complete as ss output, but it is quite valuable.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87ee35229074" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Openstack: Some first steps (revisited with Opentofu)]]></title>
            <link>https://medium.com/@joseignacio.carretero/openstack-some-first-steps-revisited-with-opentofu-b083eddf3e55?source=rss-9f463fd493bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/b083eddf3e55</guid>
            <category><![CDATA[opentofu]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[openstack]]></category>
            <dc:creator><![CDATA[Joseignacio Carretero]]></dc:creator>
            <pubDate>Wed, 15 Apr 2026 08:27:09 GMT</pubDate>
            <atom:updated>2026-04-15T08:27:09.470Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*myrokHS21WRmcA9DJKpbiA.png" /></figure><p>I you can also read this story in my website: <a href="https://jicg.eu/openstack-some-first-steps-tofu-revisited/">https://jicg.eu/openstack-some-first-steps-tofu-revisited/</a></p><p>I wrote an article regarding some initial configuration of Openstack, explaining the very firsts steps required:</p><p><a href="https://medium.com/@joseignacio.carretero/openstack-some-first-steps-c97d6784edb1">Openstack: Some first steps</a></p><p>I strongly recommend reading that article in order to understand how things are done manually. I would also recommend reading the article I wrote about installing Openstack locally using Terraform/OpenTofu.</p><p><a href="https://medium.com/@joseignacio.carretero/provisioning-infrastructure-with-terraform-and-libvirt-for-openstack-deployment-a4a6e4c2690f">Provisioning Infrastructure with Terraform and Libvirt for OpenStack Deployment</a></p><p>The code for this article can be found in the repo <a href="https://github.com/jicarretero/deploy-openstack-tofu">https://github.com/jicarretero/deploy-openstack-tofu</a> — In the directories 03-terraform-openstack-configuration-as-adminand 04-terraform-openstack-create-vm-as-user. There is a lot in the Readme.md files which can help with the previous steps.</p><p>In this article, I am going to explain now how can I configure the newly installed Openstack using OpenTofu (Terraform) and how and user can start with their first virtual server. The Openstack installation process can be found in the same repo.</p><p>As I usually type tofu or terraform randomly, I&#39;ve created my alias for terraform. Yes, I know, it is clearer if I get used to type tofu, but I got tired of repeating the same wrong command once and again. So, the alias:</p><pre>alias terraform=tofu</pre><p>I’ll follow all the steps from a newly installed and unconfigured Openstack to the deployment of a first virtual server by a non-admin user. This is the roadmap for this article.</p><ul><li>A short introduction about the Terraform Openstack Provider.</li><li>I’ll add a couple images to Glance, so I can create later new Virtual Servers.</li><li>I’ll add a few “Flavors”, so I can tell my future Virtual Servers how much RAM, vCPUs and disk they can use.</li><li>I’ll create a couple of Networks, for sure my future Virtual machines will be much more interesting if they can be accessed from the they internet and they can connect to other servers.</li><li>I’ll create a new user. Yes, it is interesting doing things as an admin user, but I’d better create a new non admin user. Simply for security reasons.</li><li>Finally, as non admin user, I’ll show how to create a new Virtual Machines.</li></ul><h3>Openstack Provider for Terraform/OpenTofu</h3><p>You can check the <a href="https://registry.terraform.io/providers/terraform-provider-openstack/openstack/latest/docs">documentation for the Terraform Openstack Provider</a>.</p><p>To configure the driver we need to define the following:</p><pre># Define required providers<br>terraform {<br>  required_version = &quot;&gt;= 0.14.0&quot;<br>  required_providers {<br>    openstack = {<br>      source  = &quot;terraform-provider-openstack/openstack&quot;<br>      version = &quot;~&gt; 1.53.0&quot;<br>    }<br>  }<br>}</pre><p>and run</p><pre>terraform init</pre><p>The configuration part goes like this:</p><pre># Configure the OpenStack Provider<br># Not needed if the environment variables are defined before running<br># &quot;tofu apply|destroy|state xx&quot;<br>provider &quot;openstack&quot; {<br>  user_name   = &quot;admin&quot;<br>  tenant_name = &quot;admin&quot;<br>  password    = &quot;xxxx&quot;<br>  auth_url    = &quot;http://os-admin.openstack.mine:5000/v3&quot;<br>  region      = &quot;corporario&quot;<br>}</pre><p>However, in Openstack, one standard way to configure all kind of clients (cli, python, this provider, etc.) is using environment variables — If we use those environment variables, we can skip that configuration for the Openstack provider.</p><pre>export OS_USERNAME=&quot;admin&quot;<br>export OS_PROJECT_NAME=&quot;admin&quot;<br>export OS_AUTH_URL=http://os-admin.openstack.mine:5000<br>export OS_REGION_NAME=&quot;corporario&quot;<br>export OS_PROJECT_DOMAIN_ID=&quot;default&quot;<br>export OS_INTERFACE=public<br>export OS_IDENTITY_API_VERSION=3<br><br># As I deployed Openstack using kolla-ansible, I can get the admin password<br># from the &quot;password.yml&quot; file.<br>export OS_PASSWORD=$(awk &#39;$1 ~ /keystone_admin_password/ {print $2}&#39; /etc/kolla/passwords.yml)</pre><p>And to load all those variables, I run it using source:</p><pre>source ~/keystone-rc-admin.sh</pre><h3>Add Images to glance</h3><h3>Manual Image downloads</h3><p>Terraform is not designed for running local commands. We’ll later need download a couple images (something like blueprint templates to create new virtual servers) and upload them to Openstack. Some of these images are compressed and we need to uncompress them before uploading them to Openstack.</p><p>So, Lets download and uncompress those images — I’ll do it in my /var/lib/libvirt/base-image-pool/ directory.</p><pre>cd /var/lib/libvirt/base-image-pool/<br><br># Download cirros<br>wget https://download.cirros-cloud.net/0.6.3/cirros-0.6.3-x86_64-disk.img<br># Download Ubuntu-noble (24.04)<br>wget https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img<br># Download fedora coreos<br>wget https://builds.coreos.fedoraproject.org/prod/streams/stable/builds/43.20260217.3.1/x86_64/fedora-coreos-43.20260217.3.1-qemu.x86_64.qcow2.xz<br># I need to descompress before uploading to Openstack.<br>unxz fedora-coreos-43.20260217.3.1-qemu.x86_64.qcow2.xz</pre><p>We could upload the images from internet to Glance without local downloading, yes! (except for the Fedora-coreos…xz, compressed images are not supported by Glance). However, destroying and creating images would waste a bit too much bandwidth downloading once and again the same file into glance.</p><h3>Upload Image to Glance</h3><p>In order to upload a new image to glance, I can add the following hclcode:</p><pre># basic example - Not used later.<br>resource &quot;openstack_images_image_v2&quot; &quot;cirros&quot; {<br>  name             = &quot;cirros&quot;<br>  <br>  local_file_path  = &quot;/var/lib/libvirt/base-image-pool/cirros-0.6.3-x86_64-disk.img&quot;<br>  # alternative to local_file_path... but download from internet<br>  # image_source_url = &quot;https://download.cirros-cloud.net/0.6.3/cirros-0.6.3-x86_64-disk.img&quot;<br>  <br>  container_format = &quot;bare&quot;<br>  disk_format      = &quot;qcow2&quot;<br>  visibility       = &quot;public&quot;<br>  properties = {<br>    key = &quot;value&quot;<br>  }<br>}</pre><p>… Three times. Or I could define a new variable with type map and iterate over the elements:</p><pre># List of images to upload - <br><br>variable &quot;images&quot; {<br>   description = &quot;List of images to upload with some of their properties&quot;<br>   type = map(object({<br>      # The filename to use as image.<br>      filename = string<br>      # The size of the Hard disk to use (in Gigabytes)<br>      hd = number<br>      # The amount of RAM to use (in Megabytes).<br>      ram = number<br>   }))<br>   default = {<br>      cirros = {<br>         filename = &quot;/var/lib/libvirt/base-image-pool/cirros-0.6.3-x86_64-disk.img&quot;<br>         hd = 1<br>         ram  = 512<br>      }<br>      ubuntu-2404 = {<br>         filename = &quot;/var/lib/libvirt/base-image-pool/noble-server-cloudimg-amd64.img&quot;<br>         hd = 10<br>         ram  = 2048<br>      }<br>      fedoracore-43 = {<br>         filename = &quot;//var/lib/libvirt/base-image-pool/fedora-coreos-43.20260217.3.1-qemu.x86_64.qcow2&quot;<br>         hd = 30<br>         ram  = 4096<br>      }<br>   }<br>}<br>resource &quot;openstack_images_image_v2&quot; &quot;images&quot; {<br>  for_each = var.images<br>  name             = each.key<br>  local_file_path  = each.value.filename<br>  container_format = &quot;bare&quot;<br>  disk_format      = &quot;qcow2&quot;<br>  visibility       = &quot;public&quot;<br>  /* The following are optional */<br>  min_disk_gb = each.value.hd<br>  min_ram_mb  = each.value.ram<br>  /* Whatever property you want to know / add (optional) */<br>  properties = {<br>    jicg = &quot;Please visit https://jicg.eu&quot;<br>  }<br>}</pre><p>These are equivalent to Openstack command openstack image create:</p><pre># NO NEED TO RUN THIS - It is an example<br># Equivalent 1 command example of the 3 VM creation above.<br>openstack image create &quot;cirros-removeme&quot; \<br>--file /var/lib/libvirt/base-image-pool/cirros-0.6.3-x86_64-disk.img \<br>--disk-format qcow2 --container-format bare \<br>--min-disk 10 \<br>--min-ram 2049 \<br>--property jicg=&#39;Please visit https://jicg.eu&#39; \<br>--public</pre><p>After running terraform apply, we&#39;ll have the images there:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/0*TlENth3msMqNVU27.png" /></figure><h3>Add Flavors</h3><p>I will add 3 flavors:</p><ul><li><strong>tiny</strong> — 1vCPU, 1Gb RAM, 10Gb Hd</li><li><strong>small</strong> — 1vCPU, 2Gb RAM, 20Gb Hd</li><li><strong>medium</strong> — 2vCPU, 4Gb RAM, 40Gb Hd</li></ul><pre># List of flavors to be created<br>variable &quot;flavors&quot; {<br>  description = &quot;A list of flavors to be created&quot;<br>  type = map(object({<br>    ram   = number<br>    vcpus = number<br>    disk  = number<br>  }))<br><br>default = {<br>    tiny = {<br>      ram   = 1024<br>      vcpus = 1<br>      disk  = 10<br>    }<br>    small = {<br>      ram   = 2048<br>      vcpus = 1<br>      disk  = 20<br>    }<br>    medium = {<br>      ram   = 4096<br>      vcpus = 2<br>      disk  = 40<br>    }<br>  }<br>}<br><br># Create the flavors. Iterate in the flavors with the &quot;for_each var.flavors&quot;<br>resource &quot;openstack_compute_flavor_v2&quot; &quot;flavors&quot; {<br>  for_each = var.flavors<br>  name  = each.key<br>  ram   = each.value.ram<br>  vcpus = each.value.vcpus<br>  disk  = each.value.disk<br>  # Optional: public visibility<br>  is_public = true<br>}</pre><p>Once defined, we can apply with tofu apply. This resources are equivalent to those created using openstack flavor create</p><pre># NO NEED TO RUN THIS - It is an example<br>openstack flavor create --ram 1024 \<br>--disk 10 --vcpus 1 --public <br>....</pre><p>So, applying the changes:</p><pre>tofu apply</pre><p>We can check that Opentofu has created the flavors using openstack flavor list.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/783/0*W394Or7_ka7bJKyY.png" /></figure><h3>Add Networking.</h3><p>As well as in the previous article, we’ll create 2 types of networks:</p><ul><li><strong>Internal</strong> — A local network which will be available for everybody, but it will be only used for internal communication between Virtual Servers.</li><li><strong>External</strong> — Connected to the internet so our local servers can reach out.</li></ul><p><strong>Internal network</strong></p><pre># Create an internal Network -- A shared network for everybody<br>resource &quot;openstack_networking_network_v2&quot; &quot;internal&quot; {<br>  name           = &quot;internal&quot;<br>  admin_state_up = &quot;true&quot;<br>  shared = &quot;true&quot;<br>}<br><br># Create a Subnetwork in our shared network<br>resource &quot;openstack_networking_subnet_v2&quot; &quot;subnet_int_net&quot; {<br>  name       = &quot;subnet-int-net&quot;<br>  network_id = openstack_networking_network_v2.internal.id<br>  cidr       = &quot;192.168.192.0/24&quot;<br>  ip_version = 4<br>  gateway_ip = &quot;192.168.192.1&quot;<br>  enable_dhcp = true<br>  dns_nameservers = [<br>        &quot;8.8.8.8&quot;,<br>  ]<br>  allocation_pool {<br>        end   = &quot;192.168.192.254&quot;<br>        start = &quot;192.168.192.3&quot;<br>  }<br>}</pre><p>Which equivalent Openstack commands are:</p><pre># DO NOT RUN THIS - It is only an example<br><br># Create Network<br>openstack network create  --share ...<br><br># One network must have at least one subnet to be usable.<br>openstack subnet create --network ... \<br>--allocation-pool start=192.168.192.3,end=192.168.192.254 \<br>--dns-nameserver 8.8.8.8 --gateway 192.168.192.1 --dhcp \<br>--subnet-range 192.168.192.0/24 ....</pre><p><strong>External network</strong></p><pre># Create an internal Network -- A shared network for everybody<br>resource &quot;openstack_networking_network_v2&quot; &quot;ext_net&quot; {<br>  name           = &quot;ext-net&quot;<br>  admin_state_up = &quot;true&quot;<br>  external = &quot;true&quot;<br>  segments {<br>    network_type     = &quot;flat&quot;<br>    physical_network = &quot;physnet1&quot;<br>    segmentation_id  = 0<br>  }<br>}<br><br># Create a Subnetwork in our shared network<br>resource &quot;openstack_networking_subnet_v2&quot; &quot;sub_ext_net&quot; {<br>  name       = &quot;sub-ext-net&quot;<br>  network_id = openstack_networking_network_v2.ext_net.id<br>  cidr       = &quot;10.202.254.0/24&quot;<br>  ip_version = 4<br>  gateway_ip = &quot;10.202.254.1&quot;<br>  enable_dhcp = true<br>  dns_nameservers = [&quot;8.8.8.8&quot;]<br>  allocation_pool {<br>        end   = &quot;10.202.254.254&quot;<br>        start = &quot;10.202.254.16&quot;<br>  }<br>}</pre><p>And the openstack-cli equivalent would be:</p><pre># DO NOT RUN THIS - It is an example.<br>openstack network create --external \<br>--provider-network-type flat \<br>--provider-physical-network physnet1 ....<br><br># Create the subnet - <br>openstack subnet create --network .... \<br>--allocation-pool start=10.202.254.16,end=10.202.254.254 \<br>--dns-nameserver 8.8.8.8 --gateway 10.202.254.1 \<br>--subnet-range 10.202.254.0/24 ....</pre><p>Once defined both networks in our .tf files, we have to run the magic command again.</p><pre>tofu apply</pre><p>And you will see with the command openstack network list that there are 2 new net networs (ext-net and internal) - Don&#39;t care if you see a 3rd one in the picture, at this stage we can consider it as &quot;fully internal to Openstack&quot;.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/0*_-6QxgtA9M3_Ufrf.png" /></figure><h3>Router Creation</h3><p>Once we have the external and internal networks, let’s connect both of them with one router, so the internal network can reach the Internet.</p><p>There are 2 steps here: The first one creating the router and the second one connecting the internal network to the router:</p><pre># Create a router with the gateway placed in the external network.<br>resource &quot;openstack_networking_router_v2&quot; &quot;rt_ext&quot; {<br>  name = &quot;rt-ext&quot;<br>  external_network_id = openstack_networking_network_v2.ext_net.id<br>}<br><br># Router interface to subnet<br>resource &quot;openstack_networking_router_interface_v2&quot; &quot;rt_ext_interface&quot; {<br>  router_id = openstack_networking_router_v2.rt_ext.id<br>  subnet_id = openstack_networking_subnet_v2.subnet_int_net.id<br>}</pre><p>The equivalent commands in Openstack-CLI are:</p><pre># DO NOT RUN THIS - It is only an equivalent example<br># Create a router named rt-ext<br>openstack router create --centralized ...<br><br># Set the external gateway (gateway to the Internet) in the external netw<br>openstack router set ... --external-gateway  extenal-network<br># Add the subnet (the internal one)<br>openstack router add subnet routert-external subnetwork-internal</pre><p>And I have a router connected to bot networks, <em>ext-net</em> and <em>internal</em>. I can get a visual representation of that in Openstack dashboard (with the user admin):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ouFQQEdjjkGEzVkZ.png" /></figure><h3>Create User and Project</h3><p>A last step is to create a non admin user, with its project and add it to the “member” group. The <em>member</em> role is already created by Openstack in the installation process. We should assign the role “member” to the newly created user in the newly created project.</p><p>In order to get the data from the member role (what Openstack knows about it), we define the data block</p><pre>data &quot;openstack_identity_role_v3&quot; &quot;member&quot; {<br>    name = &quot;member&quot;<br>}<br># Use later with openstack_identity_role_v3.member_id</pre><p>Here it is the full creation of the user, the project and the role assignment — I’ve set the <em>user_name</em>, the <em>project_name</em> and the <em>user_password</em> as variables. You can override them using environment variables TF_VAR_user_password, TF_VAR_project_name and TF_VAR_user_name.</p><pre># Set variables<br>variable &quot;user_password&quot; {<br>  description = &quot;Password of the user user&quot;<br>  type        = string<br>  sensitive   = true<br><br># Remove the following line and <br>  # &#39;export TF_VAR_user_password=&quot;mysecretpassword&quot;&#39; instead<br>  default = &quot;mysecretpassword&quot;<br>}<br>variable &quot;project_name&quot; {<br>  type = string<br>  default = &quot;jicg-project&quot;<br>}<br>variable &quot;user_name&quot; {<br>  type = string<br>  default = &quot;jicg&quot;<br>}<br><br># openstack pproject create --domain default user_project<br>resource &quot;openstack_identity_project_v3&quot; &quot;user_project&quot; {<br>  name        = var.project_name<br>  description = &quot;A new user project&quot;<br>}<br># openstack user create --password misecretpassword user<br>resource &quot;openstack_identity_user_v3&quot; &quot;user&quot; {<br>  default_project_id = openstack_identity_project_v3.user_project.id<br>  name               = var.user_name<br>  description        = &quot;A non admin user to deploy things&quot;<br>  password = var.user_password<br>}<br># Query the role &quot;member&quot; - So I can access it using<br># data.openstack_identity_role_v3.member...<br># Openstack cli, this hcl snippet would translate to:<br>#     openstack role show member<br>data &quot;openstack_identity_role_v3&quot; &quot;member&quot; {<br>    name = &quot;member&quot;<br>}<br># Openstack role assignment list<br>resource &quot;openstack_identity_role_assignment_v3&quot; &quot;role_assignment_1&quot; {<br>  user_id    = openstack_identity_user_v3.user.id<br>  project_id = openstack_identity_project_v3.user_project.id<br>  role_id    = data.openstack_identity_role_v3.member.id<br>}</pre><p>This basically does what Openstack cli would do if you run the following commands:</p><pre># DO NOT RUN THIS - It is an example<br><br>openstack project create --domain default jicg_project<br>openstack user create --password mysecretpassword jicg<br>openstack role add --user jicg --project jicg_project member</pre><h3>Create a new VM as non-admin user</h3><p>Until new, we’ve simply configured Openstack to be usable. And we created a new user. We’ll use the configuration variables to run everything. Please, change with your values:</p><pre>export OS_REGION_NAME=&quot;corporario&quot;<br>export OS_USERNAME=&#39;jicg&#39;<br>export OS_PASSWORD=mysecretpassword<br>export OS_AUTH_URL=http://os-admin.openstack.mine:5000/v3<br>export OS_PROJECT_NAME=jicg-project<br><br>export OS_PROJECT_DOMAIN_ID=default<br>export OS_USER_DOMAIN_ID=default<br>export OS_IDENTITY_API_VERSION=3</pre><p>In order to configure a Virtual Machine in Openstack we need to</p><ul><li>Create a <strong>keypair</strong> (to ssh to our virtual server).</li><li>Create at least one <strong>security group</strong> with its <strong>security rules</strong>, because by default, Openstack behaves like a fully closed firewall. We need to open ports</li><li>Add a floating IP to the VM, so we can access the VM from outside of the Openstack deployment.</li></ul><h3>Create a keypair</h3><p>This is the minimal way to create a private key, however there is a big issue with it: The private key will be in the file terraform.tfstate.</p><pre># Not the recommended way<br><br># The private key will be stored in terraform.state<br>resource &quot;openstack_compute_keypair_v2&quot; &quot;test_keypair&quot; {<br>     name = &quot;test_keypair&quot;<br>}</pre><p>If you are curious, in this case, we could extract the private key using jq:</p><pre>jq -r &quot;.resources[]|select(.name==\&quot;test_keypair\&quot;).instances[].attributes.private_key&quot; terraform.tfstate</pre><p>Let’s be serious — That’s not a good idea nor a good practice. It is better to use a local private key generated using ssh-keygen. I will use my ed25519 public key in this case:</p><pre>locals {<br>  // I will use my id_ed25519.pub file. I will load it from the file<br>  ssh_public_key = file(pathexpand(&quot;~/.ssh/id_ed25519.pub&quot;))<br>}<br><br>resource &quot;openstack_compute_keypair_v2&quot; &quot;jicg_keypair&quot; {<br>     name = &quot;jicg_keypair&quot;<br>     public_key = local.ssh_public_key<br>}</pre><p>We can create many keypairs, but it could suffice with only one for all of our servers.</p><h3>Set up (one) security group and its rules</h3><p>Security Groups consist of a set of firewall rules to allow some IPs (or all) to access some ports in our server using TCP or UDP, or allow connecting with the host using ICMP — ping</p><p>The basic ingress rules for TCP would allow port 22 (ssh), 80 (http), 443 (https) from everywhere 0.0.0.0/0</p><pre>resource &quot;openstack_networking_secgroup_v2&quot; &quot;basic_secgroup&quot; {<br>    name = &quot;basic_secgroup&quot;<br>    description = &quot;A basic secgroup for a demo&quot;<br>}</pre><p>Once we have the security group defined, we need to define some rules:</p><pre>resource &quot;openstack_networking_secgroup_v2&quot; &quot;secgroup_1&quot; {<br>  name        = &quot;secgroup_1&quot;<br>  description = &quot;A first example of security group&quot;<br>}<br><br>resource &quot;openstack_networking_secgroup_rule_v2&quot; &quot;secgroup_rules_1&quot; {<br>  for_each          = var.open_ports<br>  direction         = &quot;ingress&quot;<br>  ethertype         = &quot;IPv4&quot;<br>  protocol          = each.value.protocol<br>  port_range_min    = each.value.port_range_min<br>  port_range_max    = each.value.port_range_max<br>  remote_ip_prefix  = each.value.remote_ip_prefix<br>  security_group_id = openstack_networking_secgroup_v2.secgroup_1.id<br>}</pre><p>The open_ports variable is defined to open 3 TCP ports, one UDP and one ICMP (for ping):</p><pre>variable &quot;open_ports&quot; {<br>  description = &quot;List of open ports that I will add the security group&quot;<br>  type = map(object({<br>    protocol         = string<br>    port_range_min   = number<br>    port_range_max   = number<br>    remote_ip_prefix = string<br>  }))<br><br>default = {<br>    ssh = {<br>      protocol         = &quot;tcp&quot;<br>      port_range_min   = 22<br>      port_range_max   = 22<br>      remote_ip_prefix = &quot;0.0.0.0/0&quot;<br>    }<br>    http = {<br>      protocol         = &quot;tcp&quot;<br>      port_range_min   = 80<br>      port_range_max   = 80<br>      remote_ip_prefix = &quot;0.0.0.0/0&quot;<br>    }<br>    https = {<br>      protocol         = &quot;tcp&quot;<br>      port_range_min   = 443<br>      port_range_max   = 443<br>      remote_ip_prefix = &quot;0.0.0.0/0&quot;<br>    }<br>    openvpn = {<br>      protocol         = &quot;udp&quot;<br>      port_range_min   = 1194<br>      port_range_max   = 1194<br>      remote_ip_prefix = &quot;0.0.0.0/0&quot;<br>    }<br>    ping = {<br>      protocol         = &quot;icmp&quot;<br>      port_range_min   = 0<br>      port_range_max   = 0<br>      remote_ip_prefix = &quot;0.0.0.0/0&quot;<br>    }<br>  }<br>}</pre><p>If we query the security rules from our security group:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/778/0*3HddK6A_YqqjC8wS.png" /></figure><p>We can see a couple of egress rules – These rules are defined by default by Openstack, and they are not managed by Terraform. They are there for the incoming traffic, but if we want to manage with Terraform everything, we could insert the following line to the creation of the <strong>security group</strong>:</p><pre>delete_default_rules = true</pre><p>And we’ll need to create the outgoing traffic rules (egress rules) with Terraform. I really don’t care about them.</p><p>A last note about creating Security rules: This is the firewall for your Virtual Server (servers), so you should pay close attention to the rules you define and open only what you need — Rules like “open TCP for 0.0.0.0/0 from port 1 to 65535” are rubbish.</p><h3>Create a floating IP</h3><p>The floating ips are public IPs, by default (in my configuration at least) all virtual servers will be assigned a IP in a local network not accessible from outside Openstack. This is the reason why we assign a floating ip (public IP) to our server, to access it from outside the Openstack installation.</p><p>The only thing we need to do is applying this one.</p><pre>variable &quot;network_pool&quot; {<br>    type = string<br>    default = &quot;ext-net&quot;<br>}<br><br>resource &quot;openstack_networking_floatingip_v2&quot; &quot;floatip_1&quot; {<br>  pool = var.network_pool<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zTiZ4hyWFTPxpDVK.png" /></figure><h3>New server: An Ubuntu in this case.</h3><p>We’ll run a new server with:</p><ul><li>Network internal - A public resource our admin defined above.</li><li>Our security group secgroup_1</li><li>Our keypair</li><li>The small flavor (2 vCPUs, 20Gb HD, 2GB RAM)</li></ul><p>So, the very basic configuration here is:</p><pre>resource &quot;openstack_compute_instance_v2&quot; &quot;first_ubuntu&quot; {<br>  name            = &quot;first_ubuntu&quot;<br>  image_name      = var.image<br>  flavor_name     = var.flavor<br>  key_pair        = &quot;demo_keypair&quot;<br>  security_groups = [&quot;secgroup_1&quot;]<br><br>network {<br>    name = var.public_network<br>  }<br>}<br>## Here we get information about the port related with our network<br>data &quot;openstack_networking_port_v2&quot; &quot;first_ubuntu_port&quot; {<br>    device_id = openstack_compute_instance_v2.first_ubuntu.id<br>    network_id = openstack_compute_instance_v2.first_ubuntu.network.0.uuid<br>}<br>## And associate the port (data above) with the floating ip we created<br>## previously<br>resource &quot;openstack_networking_floatingip_associate_v2&quot; &quot;fip_vm&quot; {<br>  floating_ip = openstack_networking_floatingip_v2.floatip_1.address<br>  port_id     = data.openstack_networking_port_v2.first_ubuntu_port.id<br>}</pre><p>After applying this we can see that our server has a public IP and we can check that the server is there using openstack server list and we can ssh to the virtual server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4GFvPPBOPqJ9vBfg.png" /></figure><p>Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b083eddf3e55" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Openstack: Some first steps]]></title>
            <link>https://medium.com/@joseignacio.carretero/openstack-some-first-steps-c97d6784edb1?source=rss-9f463fd493bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/c97d6784edb1</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[openstack]]></category>
            <dc:creator><![CDATA[Joseignacio Carretero]]></dc:creator>
            <pubDate>Fri, 06 Mar 2026 19:17:33 GMT</pubDate>
            <atom:updated>2026-03-17T19:23:42.195Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HrkdqD6ez-3qU_mM-_5N9Q.png" /></figure><p>Yesterday I published an article about deploying a local Openstack infrastructure using OpenTofu/Terraform and Libvirt. Howerver, once Openstack is Up and Running we need to do something with it.</p><p><a href="https://medium.com/@joseignacio.carretero/provisioning-infrastructure-with-terraform-and-libvirt-for-openstack-deployment-a4a6e4c2690f">Provisioning Infrastructure with Terraform and Libvirt for OpenStack Deployment</a></p><p>This article will introduce the first steps we might need to do something useful with Openstack. Today, a non-admin user will create some virtual servers connected to the network with some features in terms of vCPUS, RAM and Hard disk size. We we’ll connect them using ssh.</p><p>You can also read this story in my website: <a href="https://jicg.eu/openstack-some-first-steps/">https://jicg.eu/openstack-some-first-steps/</a></p><p>What are the steps I’ll folow?</p><ul><li>Local installation of Openstack CLI — As admin (and of course as user) I want to type commands in the command line (Yes, there is a working console… but I feel well typing commands).</li><li>Add a couple images to Glance — If I want to deploy a virtual server, I need to deploy one or more cloud-images in my Openstack which can be used to create new virtual servers. Basically, <strong>Glance</strong> is the component which registers, stores and retrieves Virtual Servers images.</li><li>Add “Flavors” — Which are definitions of the capacities a Virtual Server will provide. For a flavor we define its name, the disk size, the RAM size, the number of virtual CPUs we need for the flavor, etc.</li><li>Create Networks — This is important, when we create a new Virtual Server we have to connect it to the internet, to a local network, etc. If there are no networks, the server could be isolated and for sure, we won’t be able to connect/login it.</li><li>Create/Manage users — It is interesting to have several users. At least one non-admin user.</li><li>Create servers — Finally, we we’ll create some servers with our non-admin user. In order to do this, the user will need a Keypair and a security group.</li></ul><h3>Local Openstack CLI tools installation</h3><h4>Software installation</h4><p>These tools require python3, virtualenv and pip to get installed:</p><pre># Create the Virtual environment to install openstack client cli tools<br>virtualenv ~/.venv/openstack<br><br># activate the virtual environment<br>source ~/.venv/openstack/bin/activate<br><br># Upgrade pip<br>pip install -U pip<br><br># Install the clients.<br>pip install python-openstackclient</pre><h4>Configure environment variables</h4><p>These openstack cli tools need some environment variables to work. We can download them from the Openstack dashboard login with the user admin and its password.</p><p>If you installed Openstack using kolla-ansible, the admin password can be found this way in the passwords.yml`file:</p><pre>awk &#39;$1 ~ /keystone_admin_password/ {print $2}&#39; /etc/kolla/passwords.yml</pre><p>So, login in <a href="http://os-admin.openstack.mine/">http://os-admin.openstack.mine</a> — in the top right we click on “admin” and click in “openstack RC File” to download our variables file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/1*8PPnAJOKlLJQ3KWbVDbHNg.png" /></figure><p>It is should be something like this (the original one “reads” the admin password from the keyboard, but in this case, I will use the awk onliner above — I name this file as keystone-rc-admin:</p><pre>#!/usr/bin/env bash<br># To use an OpenStack cloud you need to authenticate against the Identity<br># service named keystone, which returns a **Token** and **Service Catalog**.<br># The catalog contains the endpoints for all services the user/tenant has<br># access to - such as Compute, Image Service, Identity, Object Storage, Block<br># Storage, and Networking (code-named nova, glance, keystone, swift,<br># cinder, and neutron).<br>#<br># *NOTE*: Using the 3 *Identity API* does not necessarily mean any other<br># OpenStack API is version 3. For example, your cloud provider may implement<br># Image API v1.1, Block Storage API v2, and Compute API v2.0. OS_AUTH_URL is<br># only for the Identity API served through keystone.<br>export OS_AUTH_URL=http://os-admin.openstack.mine:5000<br># With the addition of Keystone we have standardized on the term **project**<br># as the entity that owns the resources.<br>export OS_PROJECT_ID=30082b34457f419b8695a5c1da74662b<br>export OS_PROJECT_NAME=&quot;admin&quot;<br>export OS_USER_DOMAIN_NAME=&quot;Default&quot;<br>if [ -z &quot;$OS_USER_DOMAIN_NAME&quot; ]; then unset OS_USER_DOMAIN_NAME; fi<br>export OS_PROJECT_DOMAIN_ID=&quot;default&quot;<br>if [ -z &quot;$OS_PROJECT_DOMAIN_ID&quot; ]; then unset OS_PROJECT_DOMAIN_ID; fi<br># unset v2.0 items in case set<br>unset OS_TENANT_ID<br>unset OS_TENANT_NAME<br># In addition to the owning entity (tenant), OpenStack stores the entity<br># performing the action as the **user**.<br>export OS_USERNAME=&quot;admin&quot;<br># With Keystone you pass the keystone password.<br># echo &quot;Please enter your OpenStack Password for project $OS_PROJECT_NAME as user $OS_USERNAME: &quot;<br># read -sr OS_PASSWORD_INPUT<br># export OS_PASSWORD=$OS_PASSWORD_INPUT<br>OS_PASSWORD=$(awk &#39;$1 ~ /keystone_admin_password/ {print $2}&#39; /etc/kolla/passwords.yml)<br># If your configuration has multiple regions, we set that information here.<br># OS_REGION_NAME is optional and only valid in certain environments.<br>export OS_REGION_NAME=&quot;corporario&quot;<br># Don&#39;t leave a blank variable, unset it if it was empty<br>if [ -z &quot;$OS_REGION_NAME&quot; ]; then unset OS_REGION_NAME; fi<br>export OS_INTERFACE=public<br>export OS_IDENTITY_API_VERSION=3</pre><h4>Test it!</h4><p>Let me introduce the concept of keystone — It is the authentication and authorization service in Openstack. And all those variables above are (roughly) to identify yourself with keystone. To check that openstack-cli is working and that the variables are well defined, we could issue a token from keystone.</p><pre># Activate our Openstack-cli virtual environment<br>source ~/.venv/openstack/bin/activate<br><br># load the variables we need to start working<br>source ./keystone-rc-admin<br><br># Issue one token from keystone.<br>openstack token issue</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*le9ACgbQsRjOkGYSbgaFBw.png" /></figure><p>It works!!</p><h3>Add images to Glance</h3><p>Glance is the image repository of Openstack. We need images to instanciate new virtual servers.</p><p>Let’s download 2 different images — One to make simple tests (cirros image) and an Ubuntu 24.04.</p><pre># Download cirros img ~21Mb size<br>wget -O /tmp/cirros-6.3.0.img https://download.cirros-cloud.net/0.6.3/cirros-0.6.3-x86_64-disk.img<br><br># Download Ubuntu 24.04 ~600Mb size<br>wget -O /tmp/ubuntu-noble-24.04.img https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img</pre><p>Once we’ve locally downloaded them, we upload them to openstack:</p><pre># Upload cirros - This image is very small. It needs few RAM Mb, and small HD<br># suffice to run servers based on this image.<br>openstack image create &quot;cirros&quot; \<br>--file /tmp/cirros-6.3.0.img \<br>--disk-format qcow2 --container-format bare \<br>--public</pre><pre># Upload Ubuntu image - It won&#39;t fit everywhere<br># --- It will require 20Gb of Hd (--min-disk)<br># --- 2Gb of RAM (2*1024Mb)<br>openstack image create &quot;ubuntu-24.04&quot; \<br>--file /tmp/ubuntu-noble-24.04.img \<br>--disk-format qcow2 --container-format bare \<br>--min-disk 20 \<br>--min-ram $((1024*2)) \<br>--public<br></pre><p>To check the images, we can run openstack image list`:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/521/1*P7QH4F-wD9P2y6_dUwcHng.png" /></figure><h4>Cheatsheet commands to Glance</h4><pre># Getting help<br>openstack help image<br><br># List images:<br>openstack image list<br><br># Create images (openstack help image create):<br>openstack image create &lt;name&gt; \<br>--file &lt;local_file_image&gt; \<br>--disk-format qcow2 --container-format bare \<br>[--public \]<br>[--min-disk &lt;n_in_Gb&gt; \]<br>[--min-ram &lt;n_in_Mb&gt; \]<br><br># Show details from an image<br>openstack image show &lt;id_or_name&gt;<br><br># Delete an image<br>openstack image delete &lt;id_or_name&gt;<br><br># Save a local copy of an image<br>openstack image save --file local_copy.img &lt;id_or_name&gt;<br></pre><h3>Add Flavors</h3><p>The flavors define the Hardware configuration of a virtual machine in terms of vCPUS, RAM and Disk. By default there are none, so, we are going to create 3 “rather small” flavors.</p><ul><li><strong>tiny</strong> — 1vCPU, 1Gb RAM, 10Gb Hd</li><li><strong>small</strong> — 1vCPU, 2Gb RAM, 20Gb Hd</li><li><strong>medium</strong> — 2vCPU, 4Gb RAM, 40Gb Hd</li></ul><pre>openstack flavor create --ram $((1*1024)) \<br>--disk 10 --vcpus 1 --public tiny<br><br>openstack flavor create --ram $((2*1024)) \<br>--disk 20 --vcpus 1 --public small<br><br>openstack flavor create --ram $((4*1024)) \<br>--disk 40 --vcpus 2 --public medium</pre><p>We can list the flavors openstack flavor list -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*_TODEraOYHCPIJnlkQdJIw.png" /></figure><h4>Cheatsheet commands to flavors</h4><pre># Getting help<br>openstack help flavor <br><br># Create flavors: openstack help flavor create<br>openstack flavor create --ram $((1*1024)) \<br>--disk 10 --vcpus 1 --public tiny<br><br># Listing of flavors <br>openstack flavor list<br><br># Show details of a flavor<br>openstack flavor show &lt;flavor_name_or_id&gt;<br><br># Delete a flavor<br>openstack flavor delete &lt;flavor_name_or_id&gt;</pre><h3>Create networks and routers</h3><p>This is the complicated part. A lot of long articles, tutorials, etc. should be written about networking in Openstack (and in kubernetes and everywhere). So, we are not going to deep here.</p><p>In this part we have to create the networks and a router to route traffic from VMs to the internet, however, I need to make an introductory part point to prepare the networks to be able to properly run everything. Sorry for that.</p><h4>Prepare host routers (not openstack part, but much needed)</h4><p>Before we start, I want to remember the architecture of our openstack deployment:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PSX9Ak8Zg-LvVEPutLaffA.png" /></figure><p>The rest of this point describes how to prepare the Host (laptop) to be able to deal with tagged traffic (vlans). If we hadn’t deployed things with vlans we wouldn’t need this part.</p><p>So, in order to let our Openstack servers and our os-routing server communicate among them, we need ensure that the bridge br-os is able to filter vlan traffic. We will also need this vlan-filtering in our br-os-ext router. And, I will need to make all the traffic in the “violet” network to be tagged.</p><p>Basically, I need 2 scripts. The first one to be run before starting the VMs:</p><pre># Run this one before the VMs start<br><br># Create the bridge for the bridged &quot;osnet&quot; Network<br>sudo ip link add br-os type bridge<br><br><br># Let br-os and br-os-ext be aware of vlans<br>sudo ip link add br-os type bridge vlan_filtering 1<br>sudo ip link add br-os-ext type bridge vlan_filtering 1<br>sudo ip link set br-os up<br>sudo ip link set br-os-ext up<br><br># Ensure the openstack networks have started.<br>virsh net-start os-external<br>virsh net-start osnet<br><br># Add a route for the 172.27.30.0/24 -- So I can connect the servers.<br>sudo ip route add 172.27.30.0/24 via 192.168.122.246</pre><p>The second script has to be run when our Openstack servers (os-admin and compute-x) are running.</p><pre># In<br># Create a veth (virtual ethernet)<br>sudo ip link add veth0-br type veth peer name veth0-app<br><br># tag it with vlan50<br>sudo ip link add link veth0-app name veth0-app.50 type vlan id 50<br><br># Connect veth to the br-os-ext bridge - Tag with vlan50<br>sudo brctl addif br-os-ext veth0-br<br>sudo bridge vlan add vid 50 dev veth0-br<br><br>sudo ip link set veth0-br up<br>sudo ip link set veth0-app up<br>sudo ip link set veth0-app.50 up<br><br># Remove router IP from br-os-ext and put it in the veth0-app.50<br>sudo ip addr del 10.202.254.1/24 dev br-os-ext<br>sudo ip addr add 10.202.254.1/24 dev veth0-app.50<br><br># Tag vlan for all the bridges where the Openstack VMs are connected<br># to the server in br-os-ext.<br>for a in $(sudo ip a | awk &#39;/vnet.* br-os-ext / { print gensub(&quot;:&quot;,&quot;&quot;,&quot;g&quot;,$2) }&#39;); do<br>  sudo bridge vlan add vid 50 dev &quot;$a&quot;<br>done<br>sudo bridge vlan add vid 50 dev veth0-br</pre><h4>Creating the Openstack networks</h4><p>We’ll create 2 types of networks:</p><ul><li>Internal — Which will be used for communications between all the virtual servers and the virtual routers, virtual dhcp servers, etc.</li><li>External — Connected to the internet. This will provide access outside the openstack environment to the virtual servers (i.e. Internet).</li></ul><p>After creating the networks, we have to create a virtual router connecting both networks. This way, servers connected to the internal network can connect to the internet.</p><pre># Create the internal network - A shared (--share) one usable for everybody.<br>openstack network create  --share internal<br><br># One network must have at least one subnet to be usable.<br>openstack subnet create --network internal \<br>--allocation-pool start=192.168.192.3,end=192.168.192.254 \<br>--dns-nameserver 8.8.8.8 --gateway 192.168.192.1 --dhcp \<br>--subnet-range 192.168.192.0/24 sub-int-net</pre><pre># Public network creation:<br>#   --external - means it is external<br>openstack network create --external \<br>--provider-network-type flat \<br>--provider-physical-network physnet1 ext-net<br><br># Create the subnet - <br>openstack subnet create --network ext-net \<br>--allocation-pool start=10.202.254.16,end=10.202.254.254 \<br>--dns-nameserver 8.8.8.8 --gateway 10.202.254.1 \<br>--subnet-range 10.202.254.0/24 sub-ext-net</pre><h4>Router creation</h4><p>We need to connect the external network with the internal one, so the outbound traffic in the internal network can be routed to the external network which has internet connection.</p><p>In order to create the router we need the following commands</p><pre># Create a router named rt-ext<br>openstack router create --centralized rt-ext<br><br># Set the external gateway (gateway to the Internet) in the external netw<br>openstack router set rt-ext --external-gateway  ext-net<br><br># Add the subnet (the internal one)<br>openstack router add subnet rt-ext sub-int-net   </pre><p>From the dashboard we can see the Openstack network topology — 2 networks connected with one router:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*Q1VzSO9IGF1z8bdnQwzGpQ.png" /></figure><h4>Cheatsheet for networking</h4><pre># Getting help<br>openstack help network<br>openstack help router<br>openstack help subnet<br><br># Show the Networking Agents. Useful to understand the state<br># of the different networking components.<br>openstack network agent list<br><br># Get a list of the networks<br>openstack network list<br><br># Get the details of a network<br>openstack network show &lt;network_id_or_name&gt;<br><br># Delete a network<br>openstack network delete &lt;network_id_or_name&gt;<br><br># Get a list of subnets<br>openstack subnet list<br><br># Get a list of subnets belonging to a network<br>openstack subnet list --network &lt;network_id_or_name&gt;<br><br># Show the details of a subnet<br>openstack subnet show &lt;subnet_id_or_name&gt;<br><br># Delete a subnetwork<br>openstack subnet delete &lt;subnet_id_or_name&gt;<br><br># List the routers<br>openstack router list<br><br># Get the details of a router<br>openstack router show &lt;router_id_or_name&gt;<br><br># Remove the connection of a subnet with a router<br>openstack router remove subnet &lt;router&gt; &lt;subnet&gt;<br><br># Remove on router&#39;s gateway<br>openstack router unset --external-gateway &lt;router&gt;</pre><h3>Create user and project</h3><p>Basically, in openstack we have the following idea: One user has one (or more) roles in one (or more) project.</p><p>There are some basic concepts here:</p><ul><li>Resources (vms, networks, routers, etc.) usually belong to <strong>projects</strong>.</li><li>An user has one or more <strong>roles</strong> in one or more projects.</li></ul><p>By default there are some roles predefined:</p><pre>openstack role list<br>+----------------------------------+------------------+<br>| ID                               | Name             |<br>+----------------------------------+------------------+<br>| 085a93166ba34e78b37e4ca44daead9a | heat_stack_user  |<br>| 3e246a32a0e543fa9c95b006b85bf288 | reader           |<br>| 6750779c86964430b9106200a126ade2 | manager          |<br>| a172d6d5e0544496bcc3e8dc2b634d42 | admin            |<br>| e9e4edba65644da2ad6e80a216b774a1 | member           |<br>| f2f9731c3c544b6fb38d4c4aa10f0141 | heat_stack_owner |<br>| fe0363daa08f417e95f058b4fd0a8db2 | service          |<br>+----------------------------------+------------------+</pre><p>We are going to define one project and an user.</p><pre># 1st we create a new project called jicg_project<br>openstack project create --domain default jicg_project<br><br># 2nd we create a new user called jicg. In this case <br># setting a password. This can be changed in the console.<br>openstack user create --password mysecretpassword jicg<br><br># 3rd we assign the role &quot;member&quot; to the user jicg <br># in the project jicg_project<br>openstack role add --user jicg --project jicg_project member</pre><p>If we want to use this user with openstack-cli tools, we need to define new variables, and source ~/keystone-rc-jicg :</p><pre>#!/usr/bin/env bash<br>export OS_AUTH_URL=http://os-admin.openstack.mine:5000<br>export OS_PROJECT_ID=7c2ad952915744409cc1c175b432a66b<br>export OS_PROJECT_NAME=&quot;jicg_project&quot;<br>export OS_USER_DOMAIN_NAME=&quot;Default&quot;<br>if [ -z &quot;$OS_USER_DOMAIN_NAME&quot; ]; then unset OS_USER_DOMAIN_NAME; fi<br>export OS_PROJECT_DOMAIN_ID=&quot;default&quot;<br>if [ -z &quot;$OS_PROJECT_DOMAIN_ID&quot; ]; then unset OS_PROJECT_DOMAIN_ID; fi<br># unset v2.0 items in case set<br>unset OS_TENANT_ID<br>unset OS_TENANT_NAME<br>export OS_USERNAME=&quot;jicg&quot;<br>export OS_PASSWORD=mysecretpassword<br>export OS_REGION_NAME=&quot;corporario&quot;<br>if [ -z &quot;$OS_REGION_NAME&quot; ]; then unset OS_REGION_NAME; fi<br>export OS_INTERFACE=public<br>export OS_IDENTITY_API_VERSION=3</pre><p>From now on, we will use this non-admin user.</p><h3>Create our first Virtual Server: Nova</h3><p>In openstack, <strong>Nova</strong> is the compute service, responsible for creating, managing and deleting virtual machine instances.</p><ul><li>Takes images stored in Openstack Image Service (Glance).</li><li>Applies the flavors (CPU, RAM, disk configuration)</li><li>Manages the VMs lifecycle (start, stop, reboot, terminate)</li></ul><p>There are a few steps we have to do before creating a new VM.</p><ul><li>Creating one or more security groups and Security rules: Since Openstack acts as a fully closed firewall, we need to define what to open. We can use the same security groups for all the VMs (although this is not recommended).</li><li>Creating a keypair (public/private keys) so we can ssh in the VMs. We can use the same keypair for every VM.</li><li>Allocating a Floating IP (public IP) to the project, so we can connect the VM from outside Openstack.</li><li>Launch the 1st VM.</li></ul><h4>Create the security group and security rules</h4><p>As I said previously, Openstack acts as a closed firewall and we need open some ports for inbound traffic. An important one is ssh port.</p><pre># Create a SG with the name demosecgroup<br>openstack security group create demosecgroup<br><br># In the security group we add security rules.<br># In this case we&#39;ll open port 22 and ping.<br><br># Open tcp port 22 for everybody (network 0.0.0.0/0)<br># for incoming traffic.<br>openstack security group rule create \<br>--remote-ip 0.0.0.0/0 \<br>--protocol tcp \<br>--dst-port 22 \<br>--ingress \<br>demosecgroup<br><br># The same can be done for port HTTP (80) and HTTPS (443).<br><br># This is to enable ping<br>openstack security group rule create \<br>--remote-ip 0.0.0.0/0 \<br>--protocol icmp \<br>--ingress \<br>demosecgroup</pre><h4>Create the keypairs</h4><p>This will create a new private key so we can access our virtual machines using ssh. When creating a new VM, the public key will bi inserted.</p><pre>openstack  keypair create demokeypair &gt; demokeypair.pem<br><br># The file needs to have some &quot;restricted permissions&quot;<br>chmod 400 demokeypair.pem</pre><p>If you loose this file you won’t be able to access your virtual machines using this key.</p><h4>Allocate a new public IP</h4><p>We allocate a new public IP which is an IP in the external network. This IP will be assigned to the virtual server, so the traffic going to this public IP will be routed to the Virtual Machine and the other way around, all the outgoing traffic from the Virtual Machine will be routed using this public IP.</p><pre># ext-net is the name of the external network we created before<br>openstack floating ip create ext-net</pre><p>In my case I got the IP 10.202.254.38 —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gxz45l9suxtAYEg0kV9c1A.png" /></figure><h4>Create the 1st Virtual machine</h4><p>We’ve been defining a lot of things to get here — To create a first virtual server.</p><pre># Create a server using all things we previously defined:<br># - tiny flavor (1cpu, 1Gb RAM, 10G HD)<br># - image cirros<br># - demosecgroup (security group previously defined)<br># - network internal (no internet connection)<br># - key pair name demokeypair<br>openstack server create \<br>--flavor tiny \<br>--image cirros \<br>--security-group demosecgroup \<br>--network internal \<br>--key-name demokeypair \<br>cirrosvm</pre><p>after a few seconds, we can see that the server we created has started:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*bEDknJHfZ-ic7i9HxWr4uw.png" /></figure><p>So, now I will assign the IP to the cirrosvm server so we can access it using our public IP:</p><pre>openstack server add floating ip cirrosvm 10.202.254.38</pre><p>I could access the virtual server using SSH with my private key:</p><pre>ssh -i ./demokeypair.pem cirros@10.202.254.38</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*YntDfpHj1SM4pZtm3aGMxA.png" /></figure><h3>Last words</h3><p>We created an Openstack cloud provider which can be used locally. It is not the easiest deployment. It deals with several compute nodes, vlans, routers, etc. — And it is usable. Well, I understand that we can’t deploy a large number of virtual servers, but we can do a lot of things with it.</p><p>Maybe I could write a second part. Or even get further. I will think about it.</p><p>Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c97d6784edb1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Provisioning Infrastructure with Terraform and Libvirt for OpenStack Deployment]]></title>
            <link>https://medium.com/@joseignacio.carretero/provisioning-infrastructure-with-terraform-and-libvirt-for-openstack-deployment-a4a6e4c2690f?source=rss-9f463fd493bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/a4a6e4c2690f</guid>
            <category><![CDATA[libvirt]]></category>
            <category><![CDATA[kolla-ansible]]></category>
            <category><![CDATA[openstack]]></category>
            <category><![CDATA[terraform]]></category>
            <dc:creator><![CDATA[Joseignacio Carretero]]></dc:creator>
            <pubDate>Wed, 04 Mar 2026 23:35:12 GMT</pubDate>
            <atom:updated>2026-03-06T19:22:48.738Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*Ek9HKlHYSOYX4ClLPGGNgQ.png" /></figure><p>This article gets deeper building a complete virtual infrastructure using OpenTofu/Terraform and libvirt, which will serve as the foundation for a full OpenStack deployment via Kolla-Ansible. By the end, you’ll have 5 running VMs — one router, one controller, and three compute nodes — with OpenStack up and accessible.</p><p>So, the goal of this article is deploying some interesting local architecture with OpenTofu/Terraform and install Openstack on that local virtual infrastructure using <a href="https://docs.openstack.org/kolla-ansible/latest/">kolla-ansible</a>.</p><p>You can read this article for free in my website: <a href="https://jicg.eu/provisioning-infrastructure-with-terraform-and-libvirt-for-openstack-deployment/">https://jicg.eu/provisioning-infrastructure-with-terraform-and-libvirt-for-openstack-deployment/</a></p><p>The article will be structured like this:</p><ul><li>Some hints before you begin.</li><li>Architecture overview for the deployment.</li><li>Deploying the Networks (OpenTofu)</li><li>Setting up DNS names (or /etc/hosts if it fits better for you).</li><li>Deploying the Servers (OpenTofu)</li><li>Installation of Openstack with ansible-kola</li></ul><p>This article won’t show how to do anything with Openstack. I’ll drop another introductory article soon.</p><h3>Part 1. Hints Before you begin</h3><p>I’ll consider the basics exposed in my previous introductory article for OpenTofu/Terraform on a libvirt provider:</p><p><a href="https://medium.com/@joseignacio.carretero/opentofu-terraform-and-libvirt-4c653192e16f">OpenTofu (Terraform) and libvirt</a></p><p>I will also use, in order to be able to access the servers by name, a local DNS Server I which I have deployed for demos and articles:</p><p><a href="https://medium.com/@joseignacio.carretero/local-dns-server-for-demos-523afad32e7c">Local DNS Server for Demos</a></p><p>All the configuration files can be found in the repository: <a href="https://github.com/jicarretero/deploy-openstack-tofu">https://github.com/jicarretero/deploy-openstack-tofu</a></p><h4>A note on the terraform driver used.</h4><p>In my introductory article for <a href="https://medium.com/@joseignacio.carretero/opentofu-terraform-and-libvirt-4c653192e16f">OpenTofu (Terraform) and libvirt</a> I recieved one nice comment from <a href="https://medium.com/u/a2244405ced9">MozeBaltyk</a> (thank you): The driver I am using in these articles is 5 years old. Yes, it is. However, it works nicely and it is very well documented. It works as expected and it is very stable.</p><p>The “new” driver is dmacvicar/libvirt — And I think it is much more difficult to use. When it was in its version 0.7.6, this driver was really nice to use and it was forked to multani/libvirt driver. However, dmacvicar/libvirt mutated and now it goes with a really different philosophy, somehow translating the libvirt-xml complexity to the driver.</p><p>Does this dmacvicar/libvirt driver work? Absolutely yes!. Does this driver works as you can expect? — Not in my experiece.</p><p>I also wrote my .tf files form this dmacvicar/libvirt driver in my git repo, in the branch dmacvicar (<a href="https://github.com/jicarretero/deploy-openstack-tofu/tree/dmacvicar">https://github.com/jicarretero/deploy-openstack-tofu/tree/dmacvicar</a>). Please, feel free to use this one. Please, feel free to compare both styles.</p><p>From a didactic point of view, the option is clear to me: Use multani/libvirt.</p><h4>Hardware used for the demo/article</h4><p>This article and the demo has been implemented using a Laptop Intel i7, 20 Th</p><ul><li>Intel i7, 20 cpus</li><li>64 Gb Ram memory</li><li>4Tb nvme disk (of course we won’t use that much).</li></ul><h3>Part 2. Architecture overview</h3><p>Using OpenTofu we’ll deploy this architecture, we’ll install later install Openstack on the os-admin and os-compute nodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hZSvZP836iqa9RMJ.png" /><figcaption>Architecture to be achieved.</figcaption></figure><p>The features of the different virtual servers are described as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-xKEYKFArwSQXfpv.png" /><figcaption>Virtual servers configurations and roles</figcaption></figure><h4>The “os-routing” host</h4><p>This host is connected to the default libvirt network. It is accesible from outside the network. It is also connected to the <strong>openstack</strong> servers using the vlan 30. It has a double fold purpose:</p><ul><li>Allow connections to Openstack APIs and Dashboard from outside the Openstack networks.</li><li>Allow Openstack servers connect to the internet.</li></ul><h4>The Openstack servers hosts</h4><p>These hosts are connected to vlan 30 in order to be able to access to the internet (updates, download things, etc) using os-routing host. They are connected in the same LAN to send and receive messages between them.</p><p>These hosts are also connected to vlan 40 so tunneled data from Openstack VMs can reach the servers and they can get their configurations, communicate between them, etc.</p><p>Last, these are also connected to vlan 50 which is connected to a different virtual bridge. This one is to connect the VMs to the internet using a public IP. Of course, in this case is simulated, but I will be able to connect the VMs in this Public IP.</p><h3>Part 3. Deploying the Networks with OpenTofu</h3><p>First, I have to create the networks osnet and os-external - The default network is already created by default in the installation of libvirt. Once the networks are created, I&#39;ll let it be there instead of letting any terraform destroy remove my network infrastructure. So, this is my configuration file for my networks:</p><pre>terraform {<br>  required_version = &quot;&gt;= 1.0&quot;<br>  required_providers {<br>    libvirt = {<br>      source  = &quot;multani/libvirt&quot;<br>      version = &quot;0.6.3-1+4&quot;<br>    }<br>  }<br>}<br><br>provider &quot;libvirt&quot; {<br>  uri = &quot;qemu:///system&quot;<br>}<br># Create &quot;osnet&quot; network. Type bridge<br>resource &quot;libvirt_network&quot; &quot;osnet&quot; {<br>  name   = &quot;osnet&quot;<br>  mode   = &quot;bridge&quot;<br>  bridge = &quot;br-os&quot;<br>}<br># Create &quot;os-external&quot; network. Type nat<br>resource &quot;libvirt_network&quot; &quot;os-external&quot; {<br>  name      = &quot;os-external&quot;<br>  mode      = &quot;nat&quot;<br>  bridge    = &quot;br-os-ext&quot;<br>  addresses = [&quot;10.202.254.0/24&quot;]<br>}</pre><p>As always:</p><pre># Init terraform with the required providers:<br>terraform init<br><br># Create the networks<br>terraform apply</pre><p>In this case, terraform apply will create the Networks in libvirt, unfortunately I can&#39;t do everything here using OpenTofu in this case. There&#39;s a limitation in the configuration I can do. So, I need to do a couple things manually:</p><ul><li>Create bridge br-os so I can attach bridged networks to it (br-os-ext will be created as the NAT network is created).</li><li>Set “vlan” awareness in both bridges.</li></ul><pre># Create the bridge for the bridged &quot;osnet&quot; Network<br>sudo ip link add br-os type bridge<br><br># Let br-os and br-os-ext be aware of vlans<br>sudo ip link add br-os type bridge vlan_filtering 1<br>sudo ip link add br-os-ext type bridge vlan_filtering 1<br>sudo ip link set br-os up<br>sudo ip link set br-os-ext up</pre><h3>Part 4. Setting up Names for the servers.</h3><h3>Option 1. Using DNS server</h3><p>I strongly think that having a DNS is very useful. So, I created a new zone for this demo in my named server (consider visiting my article “<a href="https://jicg.eu/local-dns-server-for-demos/">Local DNS Server for Demos</a>”).</p><p>So, in /etc/named/named.conf.local I added a new zone:</p><pre>....<br>zone &quot;openstack.mine&quot; {<br>    type master;<br>    file &quot;/etc/named/zones/db.openstack.mine&quot;; # zone file path<br>};<br>....</pre><p>And I created the file /etc/named/zones/db.openstack.mine:</p><pre>$TTL    604801<br>@       IN      SOA     services.openstack.mine. admin.openstack.mine. (<br>         2025112100     ; Serial<br>             604800     ; Refresh<br>              86400     ; Retry<br>            2419200     ; Expire<br>             604800     ; Negative Cache TTL<br>)<br><br>; name servers - NS records<br>    IN      NS      services<br>services.openstack.mine.          IN      A       192.168.3.253<br>admin.openstack.mine.             IN      A       192.168.3.253<br>; name servers - A records<br>os-routing              IN      A       172.27.30.1<br>os-admin                IN      A       172.27.30.2<br>os-compute-0            IN      A       172.27.30.20<br>os-compute-1            IN      A       172.27.30.21<br>os-compute-2            IN      A       172.27.30.22</pre><h3>Option 2: modify /etc/hosts</h3><p>As a workaround, if you don’t want to have these DNSs entries or a local DNS server, you can simply add in every virtual server and in the host laptop the entries in /etc/hosts the entries:</p><pre># Add these entries to every /etc/hosts if you don&#39;t have a dns<br>172.27.30.1      os-routing.openstack.mine<br>172.27.30.2      os-admin.openstack.mine<br>172.27.30.1      os-compute-0.openstack.mine<br>172.27.30.1      os-compute-1.openstack.mine<br>172.27.30.1      os-compute-2.openstack.mine</pre><h3>Part 5. Deploying the Servers (OpenTofu)</h3><p>We’ll be using Ubuntu 24.04 for this deployment, and, as I explained in my “<a href="https://jicg.eu/opentofu-terraform-and-libvirt/">OpenTofu (Terraform) and libvirt</a>” article, I created a set of pools for my Virtual Servers deployment.</p><pre># Download Ubuntu 24.04 latest image. I do that to my &quot;base-image-pool&quot; directory.<br>sudo wget -O /data/libvirt/base-image-pool/ubuntu-24.04-base-x.qcow2 \<br>  https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img<br><br># Refresh your pool, specially the newly downloaded image to the pool<br>virsh pool-refresh base-image-pool</pre><h4>OS-ROUTING Host</h4><p>This host is, as previously described, a router from the “outside” of the Openstack cluster to the APIs and to the dashboard of Openstack. And it also serves for Openstack cluster servers to be able to access internet in order to download the packages and all the things it might need.</p><p>Its network definition will be (/etc/netplan/50-netpan) - It will have 2 network interfaces, one in default network and the other one in the bridged network with vlan 30.</p><pre>network:<br> version: 2<br> renderer: networkd<br> ethernets:<br>   ens3:<br>     dhcp4: true<br>   ens4:<br>     dhcp4: false<br>     dhcp6: false<br>     optional: true<br> vlans:<br>   ens4.30:<br>     id: 30<br>     link: ens4<br>     addresses: [172.27.30.1/24]<br>     optional: true</pre><p>The cloud-init for this server will install some useful packages and configure the server to NAT traffic from 172.27.30.0/24 (the openstack servers) to the default 192.168.122.1 ip. It does this by installing a new systemctl service.</p><p>It will also allows traffic to be forwarded, so the Openstack APIs can be reached.</p><p>This is done in the file cloud-init-routing.cfg:</p><pre>#cloud-config<br># vim: syntax=yaml<br>#<br>...<br>package_update: true<br>package_upgrade: true<br>packages:<br>  - qemu-guest-agent<br>  - curl<br>  - wget<br>  - tmux<br>  - jq<br>  - vlan<br>  - ripgrep<br>  - fzf<br>  - python3<br>  - python3-virtualenv<br>  - python3-pip<br>  - python3-dev<br>write_files:<br>  - path: /etc/systemd/system/krtroute.service<br>    content: |<br>      [Unit]<br>      Description=MASQUERADE FOR ROUTING<br>      <br>      [Service]<br>      Type=oneshot<br>      ExecStart=/usr/sbin/iptables -t nat -A POSTROUTING -s 172.27.30.0/24 -j MASQUERADE<br>      RemainAfterExit=yes<br>      TimeoutSec=0<br>      WorkingDirectory=/<br>      RestartSec=10<br>      [Install]<br>      WantedBy=multi-user.target<br>    permissions: &#39;0644&#39;<br>runcmd:<br>  - systemctl daemon-reload<br>  - systemctl enable krtroute.service<br>  - systemctl start krtroute.service<br>  - systemctl enable qemu-guest-agent<br>  - systemctl start qemu-guest-agent<br>  - sed -i &#39;s/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g&#39; /etc/sysctl.conf<br>  - sysctl -p</pre><h4>Other Openstack servers</h4><p>The network configuration will be slightly different since they don`t have a network interface in the default network but they have 2 interfaces in the bridged osnet network, and an interface (without IP) in the nated os-external network - This network interface is only for &quot;internet&quot; networking in the VMs. Their netplan configuration is something like this:</p><pre>network:<br>  version: 2<br>  ethernets:<br>    ens3:<br>      optional: true<br>    ens4:<br>      optional: true<br>    ens5:<br>      optional: true<br>  vlans:<br>    ens3.30:<br>      addresses: [172.27.30.${n_ip}/24]<br>      id: 30<br>      gateway4: 172.27.30.1<br>      link: ens3<br>      nameservers:<br>        addresses: [ 192.168.3.253 ]<br>      optional: true<br>    ens4.40:<br>      addresses: [172.27.40.${n_ip}/24]<br>      id: 40<br>      link: ens4<br>      optional: true<br>    ens5.50:<br>      id: 50<br>      link: ens5<br>      optional: true</pre><p>where:</p><ul><li>n_ip = 2 for the os-admin server.</li><li>n_ip = 20, 21, 22 … for the os-compute servers.</li></ul><p>In this case, you can see that the “nameservers” is set to 192.168.3.253 which is my local DNS installation. For sure you&#39;ll want to change that.</p><p>These servers install the same packages than the os-routing server. And they also configure IP forwarding. However, the os-admin server will initaliza a new physical volume and will add it a new volume group for cinder – We&#39;ll explain this later:</p><pre>....<br>runcmd:<br>  - systemctl daemon-reload<br>  - systemctl enable qemu-guest-agent<br>  - systemctl start qemu-guest-agent<br>  - sed -i &#39;s/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/g&#39; /etc/sysctl.conf<br>  - sysctl -p<br>%{ if is_admin }<br>  - pvcreate /dev/vdb<br>  - vgcreate cinder-volumes /dev/vdb<br>%{ endif }<br>...</pre><p><strong>differences between os-admin and compute nodes</strong></p><ul><li>Compute nodes have 16Gb RAM and 250Gb SSD</li><li>os-admin node have 12Gb RAM and 60Gb+300Gb SSD — The 300Gb ssd is for volume storage (that’s pvcreate, and vgcreate).</li></ul><h4>Time to run it!</h4><pre>terraform init<br>terraform apply</pre><p>Once the VMs are started, we can run virsh list to see the vms created (5 vms as expected) and the leases in the default network. In my case 192.168.122.246:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qA02eLSUa43K4WG2.png" /><figcaption>Virtual servers running. And leases in the Default network — Need to route there.</figcaption></figure><p>So, now, I have to route my traffic to 172.27.30.x through 192.168.122.246:</p><pre>sudo ip route add 172.27.30.0/24 via 192.168.122.246</pre><p>The user I configured is named kolla – So, maybe I can ping and login to other servers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*z3lbx9PbfjI4PUrS.png" /><figcaption>I can connect to the Openstack servers from my laptop (through os-router)</figcaption></figure><h3>Part 6. Install Openstack using Kolla-ansible</h3><p>As a prerequisite we need python3 and virtual-env installed. So, I create my virtual-env</p><pre>python3 -m venv ~/.venv/kolla-ansible<br>source ~/.venv/kolla-ansible/bin/activate<br><br>pip install -U pip<br><br>pip install kolla-ansible==20.3.0 ansible-core==2.18.0<br><br>## Create /etc/kolla and let the current user (jicg in my case) own it:<br>sudo mkdir -p /etc/kolla<br>sudo chown jicg:jicg /etc/kolla<br><br>## Install dependencies<br>kolla-ansible install-deps</pre><p>In a new kolla folder, I will create some files, among them ansible.cfg:</p><pre>[defaults]<br>host_key_checking=False<br>pipelining=True<br>forks=20</pre><p>And multinode, my inventory file – Here are the first 40 lines (the most important ones), however, this file has more than 600 lines:</p><pre># These initial groups are the only groups required to be modified. The<br># additional groups are for more control of the environment.<br>[control]<br># These hostname must be resolvable from your deployment host<br>os-admin.openstack.mine ansible_user=kolla ansible_become=true<br><br># The above can also be specified as follows:<br>#control[01:03]     ansible_user=kolla<br><br># The network nodes are where your l3-agent and loadbalancers will run<br># This can be the same as a host in the control group<br>[network]<br>os-admin.openstack.mine ansible_user=kolla ansible_become=true<br><br>[compute]<br>os-compute-0.openstack.mine ansible_user=kolla ansible_become=true<br>os-compute-1.openstack.mine ansible_user=kolla ansible_become=true<br>os-compute-2.openstack.mine ansible_user=kolla ansible_become=true<br><br>[monitoring]<br>os-admin.openstack.mine ansible_user=kolla ansible_become=true<br><br># When compute nodes and control nodes use different interfaces,<br># you need to comment out &quot;api_interface&quot; and other interfaces from the globals.yml<br># and specify like below:<br>#compute01 neutron_external_interface=eth0 api_interface=em1 tunnel_interface=em1<br>[storage]<br>os-admin.openstack.mine ansible_user=kolla ansible_become=true<br><br>[deployment]<br>localhost       ansible_connection=local<br><br>[baremetal:children]<br>control<br>network<br>compute<br>storage<br>monitoring<br>....</pre><h4>Preliminary configurations generation.</h4><p><strong>Create the password:</strong></p><pre># Copy the password template file to /etc/kolla<br>cp  ~/.venv/kolla-ansible/share/kolla-ansible/etc_examples/kolla/passwords.yml /etc/kolla<br><br># Set new passwords in the template<br>kolla-genpwd</pre><p><strong>Create globals.yml configuration</strong>:</p><p>Copy and modify the configuration file (globals.yml), so the networks and components to be installed are the ones that we need.</p><pre>cp ~/.venv/kolla-ansible/share/kolla-ansible/etc_examples/kolla/globals.yml /etc/kolla</pre><h3>Installation</h3><p>Before continuing, I must admit that I had to do a “small” patch to make the installation “fluently” work. I added 3 lines at the end of the file ~/.venv/kolla-ansible/share/kolla-ansible/ansible/roles/prechecks/vars/main.yml – It simply complained in the &quot;prechecks&quot; that Archlinux is not supported... now it is. BTW I use Arch.</p><pre>...<br># jicg<br>  Archlinux:<br>    - &quot;rolling&quot;</pre><p>Ok, the installation:</p><pre># Bootstrap servers -- ~3 minutes<br>kolla-ansible bootstrap-servers -i ./multinode<br><br># Prechecks --  ~1 minute<br>kolla-ansible prechecks -i ./multinode<br><br><br># Pull the images - It is specially recommended when upgrading too ~7 minutes<br>kolla-ansible pull -i multinode<br><br># Finally... deploy -- ~11 minutes<br>kolla-ansible deploy -i ./multinode</pre><p>And that’s it — I’ve got my Openstack up and running -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1006/0*Z8HYuwmrPd5Ysvqq.png" /><figcaption>Login screen for Opentstack dashboard.</figcaption></figure><p>I wrote another article that shows how to use this openstack installation (the first steps) —</p><p><a href="https://medium.com/@joseignacio.carretero/openstack-some-first-steps-c97d6784edb1">Openstack: Some first steps</a></p><p>Using the command grep keystone_admin_password /etc/kolla/passwords.yml I can get the password for user *admin*:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Cj9niOTtn3-_Xq5_.png" /><figcaption>Openstack dashboard after login.</figcaption></figure><p>It is out of the scope to make an introduction on how we can start operating our Openstack deploymen, but It will come soon an introductory article on how to use this Openstack deployment.</p><h3>Conclusion</h3><p>This article is intended to understand how to use more complex deployments using OpenTofu/Terraform. All the basics of HCL language are used in this article: Create Virtualized Network infrastructures, servers with different sizes and purposes and different configurations, adding different size and number of disks to each servers, etc.</p><p>And in another part, one the infrastructure is deployed, I showed how we could use tools based in ansible to deploy our own services. In this case, our own private cloud.</p><p>How can I use this Openstack private cloud deployment? — This will come soon in future articles.</p><p>Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a4a6e4c2690f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[OpenTofu (Terraform) and libvirt]]></title>
            <link>https://medium.com/@joseignacio.carretero/opentofu-terraform-and-libvirt-4c653192e16f?source=rss-9f463fd493bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c653192e16f</guid>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[cloud]]></category>
            <category><![CDATA[libvirt]]></category>
            <dc:creator><![CDATA[Joseignacio Carretero]]></dc:creator>
            <pubDate>Tue, 06 Jan 2026 10:33:27 GMT</pubDate>
            <atom:updated>2026-03-06T11:38:34.160Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*iqR6_apc11r3wGERnDjfjw.png" /></figure><p>A month ago I wrote an article about <a href="https://medium.com/@joseignacio.carretero/vagrant-libvirt-plugin-57f253887308">Vagrant and libvirt</a> where it was described how to deploy and use new servers quickly and how to install the software needed just as the server starts using both bash scripts and Ansible. It is quite easy and useful, however, Vagrant is only intended for development and demos. In this article it is also described how to install libvirt and qemu-kvm.</p><p>Another great tool for keeping your infrastructure as code is <a href="https://opentofu.org/?ref=jicg.eu">OpenTofu</a>, which is a drop-in replacement for <a href="https://developer.hashicorp.com/terraform?ref=jicg.eu">Terraform</a>. I will use the words Terraform, OpenTofu, tofu as synonyms.</p><p>You can also read this article <a href="https://jicg.eu/opentofu-terraform-and-libvirt/">here</a>.</p><p><strong>What will do in this tutorial?</strong></p><p>Basically the idea is creating a new local Ubuntu 24.04 virtual server using OpenTofu and the HCL (Hashicorp Configuration Language) to describe our virtual server.</p><ul><li>We’ll use the libvirt provider to deploy it in qemu-kvm.</li><li>We’ll configure our server using cloud-init to have</li><li>A hostname</li><li>An user (named ubuntu) accessible by ssh</li><li>Install some software packages and a small and lightweight k3s kubernetes distribution to demonstrate that we can do some setups here.</li></ul><p><strong>A note on the terraform driver used.</strong></p><p>I recieved one nice comment from <a href="https://medium.com/u/a2244405ced9">MozeBaltyk</a> (thank you) to this article, the first comment: The driver I am using in these article and in my continuation article (<a href="https://medium.com/@joseignacio.carretero/provisioning-infrastructure-with-terraform-and-libvirt-for-openstack-deployment-a4a6e4c2690f">Provisioning Infrastructure with Terraform and Libvirt for OpenStack Deployment</a>) is 5 years old. Yes, it is. However, it works nicely and it is very well documented. It works as expected and it is very stable.</p><p>The “new” driver is dmacvicar/libvirt — And I think it is much more difficult to use. When it was in its version 0.7.6, this driver was really nice to use and it was forked to multani/libvirt driver. However, dmacvicar/libvirt mutated and now it goes with a really different philosophy, somehow translating the libvirt-xml complexity to the driver.</p><p>Does this dmacvicar/libvirt driver work? Absolutely yes!. Does this driver works as you can expect? — Not in my experiece.</p><p>From a didactic point of view, the option is clear to me: Use multani/libvirt.</p><h3>Installation</h3><h4>qemu+livirt</h4><p>Installation description of libvirt and qemu are everywhere in the internet for your Linux distribution, a simple search and you’ll find tons of installations, anyway, I show here how to install it for Arch Linux and Ubuntu, but for sure you can find better installation tutorials around.</p><pre># <br>source /etc/lsb-release<br><br>case $DISTRIB_ID in<br>&quot;Arch&quot;)<br>  sudo pacman -Suy --noconfirm qemu libvirt virt-manager ebtables dnsmasq bridge-utils<br>  ;;<br>&quot;Ubuntu&quot;)<br>  sudo apt update -y<br>  sudo apt install -y qemu-system libvirt-daemon virt-manager<br>  ;;<br>esac<br># Yes... we should check that it installed... but it is irrelevant here.<br>sudo systemctl enable libvirtd<br>sudo systemctl start libvirtd</pre><h4>OpenTofu</h4><p>In Ubuntu 24.04 we can install OpenTofu using the following commands:</p><pre># Install packages needed to reliable add new repos to the system<br>sudo apt update<br>sudo apt install -y curl gnupg apt-transport-https ca-certificates<br><br># Add tofu repo<br>curl -fsSL https://get.opentofu.org/opentofu.gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/opentofu.gpg<br>curl -fsSL https://packages.opentofu.org/opentofu/tofu/gpgkey | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/tofu.gpg<br>echo &quot;deb https://packages.opentofu.org/opentofu/tofu/any/ any main&quot;|sudo tee /etc/apt/sources.list.d/opentofu.list<br># Install OpenTofu<br>sudo apt update<br>sudo apt install tofu</pre><p>I use Arch Linux btw, so the installation goes as folows</p><pre>sudo pacman -S opentofu</pre><h3>My libvirt pool configuration.</h3><p>A libvirt pool is a storage location (directories in this case) where libvirt stores virtual machine disk images, ISO files, and other storage volumes.</p><p>I usually have (at least) 3 different pools in order to use my libvirt installation:</p><ul><li>The <strong>default</strong> pool (the one I have by default for libvirt).</li><li>The <strong>cloud-init-pool</strong> pool, where I put all the cloud-init generated ISO images (we’ll talk about this later).</li><li>The <strong>base-image-pool</strong> pool, where I put my base images, this means the qcow2 base images that I will use for different purposes. In this article I will only show about Ubuntu-24.04, but I have some others like Almalinux-9.</li></ul><h4>Defining the pools (using HCL)</h4><p>I don’t create my “stable” pools this way, I always do it using <strong>libvirt</strong>. But only for my “stable” pools. We could create a separate directory to work with pools:</p><pre>mkdir my-pools<br>cd my-pools</pre><p>Then write a main.tf file:</p><pre>## main.tf file (only for pools)<br>terraform {<br>  required_providers {<br>    libvirt = {<br>      source  = &quot;multani/libvirt&quot;<br>      version = &quot;0.6.3-1+4&quot;<br>    }<br>  }<br>}<br><br>// Connect to local libvirt<br>provider &quot;libvirt&quot; {<br>  uri = &quot;qemu:///system&quot;<br>}<br><br>resource &quot;libvirt_pool&quot; &quot;base-image-pool&quot; {<br>  name = &quot;base-image-pool&quot;<br>  type = &quot;dir&quot;<br>  path = &quot;/data/libvirt/base-image-pool&quot;<br>}<br>resource &quot;libvirt_pool&quot; &quot;cloud-init-pool&quot; {<br>  name = &quot;cloud-init-pool&quot;<br>  type = &quot;dir&quot;<br>  path = &quot;/data/libvirt/cloud-init-pool&quot;<br>}</pre><p>And run the commands:</p><pre>tofu init<br>tofu apply</pre><p>I do this in separate directory (different OpenTofu) deployments because I don’t want the pools (and its data) to be removed when I run tofu destroy. I want these pools to be there forever, for all my installations. This is basically the reason why I&#39;ve defined my pools using the libvirt-way.</p><h3>Defining the pools (the libvirt-way)</h3><p>I did this once and forever in my installation using libvirt. You can skip this section if you only care about Terraform things.</p><pre># Create the directories<br>mkdir -p /data/libvirt/base-image-pool<br>mkdir -p /data/libvirt/cloud-init-pool</pre><p>Once I have the directories, I create both xml files, one to describe the base-image-pool and the other one to define the base-image-pool:</p><pre># base-image-pool.xml file<br>&lt;pool type=&#39;dir&#39;&gt;<br>&lt;name&gt;base-image-pool&lt;/name&gt;<br>&lt;target&gt;<br>&lt;path&gt;/data/libvirt/base-image-pool&lt;/path&gt;<br>&lt;permissions&gt;<br>      &lt;mode&gt;0755&lt;/mode&gt;<br>      &lt;owner&gt;0&lt;/owner&gt;<br>      &lt;group&gt;0&lt;/group&gt;<br>    &lt;/permissions&gt;<br>&lt;/target&gt;<br>&lt;/pool&gt;</pre><p>And other one to define the cloud-init-pool:</p><pre># cloud-init-pool.xml file<br>&lt;pool type=&#39;dir&#39;&gt;<br>&lt;name&gt;base-image-pool&lt;/name&gt;<br>&lt;target&gt;<br>&lt;path&gt;/data/libvirt/cloud-init-pool&lt;/path&gt;<br>&lt;permissions&gt;<br>      &lt;mode&gt;0755&lt;/mode&gt;<br>      &lt;owner&gt;0&lt;/owner&gt;<br>      &lt;group&gt;0&lt;/group&gt;<br>    &lt;/permissions&gt;<br>&lt;/target&gt;<br>&lt;/pool&gt;</pre><p>And I create the pools:</p><pre># Define pools according to the files:<br>virsh pool-define cloud-init-pool.xml<br>virsh pool-define base-image-pool.xml<br><br># Start the new defined pools<br>virsh pool-start cloud-init-pool<br>virsh pool-start base-image-pool<br># Set the autostart flag to the pools, so they start after rebooting.<br>virsh pool-autostart cloud-init-pool<br>virsh pool-autostart base-image-pool</pre><h4>Download a base image to work with</h4><p>The next step (it doesn’t matter how you created the pools) will be downloading an Ubuntu 24.04 image from the official Ubuntu cloud-image repository:</p><pre>wget -O /data/libvirt/base-image-pool/ubuntu-24.04-base-x.qcow2 \<br>  https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img<br><br># Refresh, specially the newly downloaded image to the pool<br>virsh pool-refresh base-image-pool</pre><p>Now we can check the files in the pool virsh vol-list --pool base-image-pool:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/0*pA-_EM90DlXqFT1y.png" /></figure><h3>First VM</h3><p>I always have the alias terraform because I was used to type that instead of tofu so I prevent mistakes this way (shame on me).</p><pre>alias terraform=&#39;tofu&#39;</pre><p>The provider that we’ll use is <a href="https://registry.terraform.io/providers/multani/libvirt/latest/docs?ref=jicg.eu">multani/libvirt</a>. It is well documented, so it is always the best reference.</p><h4>Setting the provider</h4><p>To set up the libvirt provider, we’ll write in the file main.tf as the first few lines the following ones:</p><pre>## main.tf<br>/*<br>The following lines sets the provider to use for our HCL deployment<br><br>This defines a connection to libvirt<br>*/<br>terraform {<br>  required_providers {<br>    libvirt = {<br>      source  = &quot;multani/libvirt&quot;<br>      version = &quot;0.6.3-1+4&quot;<br>    }<br>  }<br>}<br><br>// Connect to local libvirt<br>provider &quot;libvirt&quot; {<br>  uri = &quot;qemu:///system&quot;<br>  <br>  // We could also connect to a remote libvirt server:<br>  // uri = qemu+ssh://user@remote_host/system<br>}</pre><p>If we run tofu init , we download the provider (this step can be done later, at any time before running tofu plan or tofu apply):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OD1Hmx27niZuzDuj.png" /></figure><h4>Setup the VM (domain) features</h4><p>In libvirt, we call the VMs domains. So, in this case we’ll define a domain. Basically, and as always, we’ll have to define:</p><ul><li>Volume base image and the size of the volume (how may HD gigabytes do we need?)</li><li>RAM size</li><li>Network to use (in this first example, I’ll use the default network).</li><li>Initial configuration of the VM (cloud-init).</li></ul><p>The full main.tf goes as follows:</p><pre>## main.tf - full file<br>/*<br>The following lines sets the provider to use for our HCL deployment<br><br>This defines a connection to libvirt<br>*/<br>terraform {<br>  required_providers {<br>    libvirt = {<br>      source  = &quot;multani/libvirt&quot;<br>      version = &quot;0.6.3-1+4&quot;<br>    }<br>  }<br>}<br><br>// Connect to local libvirt<br>provider &quot;libvirt&quot; {<br>  uri = &quot;qemu:///system&quot;<br>}<br><br>/****************<br> *<br> * Defining VM Volume<br> *<br> ****************/<br>resource &quot;libvirt_volume&quot; &quot;first_vm_volume&quot; {<br>  // The next 2 lines defines the name of the volume and its pool<br>  name = &quot;first_vm_volume&quot;<br>  pool = &quot;default&quot;<br><br>  // The base image name and its pool<br>  base_volume_name = &quot;ubuntu-24.04-base.qcow2&quot;<br>  base_volume_pool = &quot;base-image-pool&quot;<br><br>  // Disk format qcow2<br>  format = &quot;qcow2&quot;<br><br>  // Size - ~ 25Gb<br>  size = 25 * 1024 * 1024 * 1024<br>}<br><br>/****************<br> *<br> * Defining cloud-init file<br> *<br> ****************/<br>locals {<br>  // I will use my id_rsa.pub file. I will load it from the file<br>  ssh_public_key = file(pathexpand(&quot;~/.ssh/id_rsa.pub&quot;))<br>}<br><br>data &quot;template_file&quot; &quot;cloud_init_data&quot; {<br>  // I define 2 variables:<br>  // - hostname - which sets the hostname in the VM<br>  // - ssh_public_key - Loaded in previous step from file<br>  vars = {<br>    hostname       = &quot;firstvm&quot;<br>    ssh_public_key = local.ssh_public_key<br>  }<br><br>  // I define the template file cloud_init.cfg<br>  template = file(&quot;${path.module}/cloud_init.cfg&quot;)<br>}<br><br>resource &quot;libvirt_cloudinit_disk&quot; &quot;first_vm_cloudinit&quot; {<br>  // Finally create the cloud-init iso file. With its name in the<br>  // cloud-init-pool<br>  name = &quot;first_vm_cloudinit.iso&quot;<br>  pool = &quot;cloud-init-pool&quot;<br><br>  // The data to create the iso file is the data rendered from<br>  // from what it is defined in the previous step<br>  user_data = data.template_file.cloud_init_data.rendered<br>}<br><br><br>/****************<br> *<br> * Defining the domain<br> *<br> ****************/<br>resource &quot;libvirt_domain&quot; &quot;ubuntu-2404-first-vm&quot; {<br>  name   = &quot;first-vm&quot; //  name of the server<br>  memory = &quot;2048&quot;     // RAM memory in MB<br>  vcpu   = 2          // Number of virtual CPUs<br><br>  // The network in the default network<br>  network_interface {<br>    network_name = &quot;default&quot;<br>  }<br><br>  // The disk, defined above<br>  disk {<br>    volume_id = libvirt_volume.first_vm_volume.id<br>  }<br><br>  // The CPU mode - I like host-passthrough<br>  cpu = {<br>    mode = &quot;host-passthrough&quot;<br>  }<br><br>  // The cloud-init defined above<br>  cloudinit = libvirt_cloudinit_disk.first_vm_cloudinit.id<br><br>  // A couple of configurations related to the console<br>  console {<br>    type        = &quot;pty&quot;<br>    target_type = &quot;serial&quot;<br>    target_port = &quot;0&quot;<br>  }<br><br>  // And the graphics - Although in this case I will not install any<br>  // graphical interface<br>  graphics {<br>    type        = &quot;spice&quot;<br>    listen_type = &quot;address&quot;<br>    autoport    = true<br>  }<br>}</pre><p>Regarding the file cloud_init.cfg, in this first case I will have this file:</p><pre>#cloud-config<br># vim: syntax=yaml<br>#<br><br>hostname: ${hostname}<br>manage_etc_hosts: false<br>ssh_pwauth: true<br>users:<br>  - name: ubuntu<br>    ssh_authorized_keys:<br>      - ${ssh_public_key}<br>    lock_passwd: false<br>    # the password is generated using: mkpasswd -m sha-512 &quot;1234&quot;<br>    passwd: $6$.NO1xMXBmPli7nXx$flG9pE.4yUaMKSIQ8w7yXWpxkXxXrfW7eUH7zTIT4wYR2WeVuV/14NEfPJqfwnI/BSjRt/DEn6eDcU8laaZ6Q1<br>    sudo: [&#39;ALL=(ALL) NOPASSWD:ALL&#39;]<br>    shell: /bin/bash<br>    groups: wheel<br><br>disable_root: false<br>package_update: true<br>package_upgrade: true<br># Install some packages I like to have<br>packages:<br>  - curl<br>  - wget<br>  - jq<br>  - tmux<br>  - ripgrep<br># Run sume commands. For example, installing k3s<br>runcmd:<br>  - curl -sfL https://get.k3s.io | sh -<br>  - systemctl daemon-reload<br>  - systemctl restart k3s<br>  - chgrp ubuntu /etc/rancher/k3s/k3s.yaml<br>  - chmod g+r /etc/rancher/k3s/k3s.yaml</pre><p>When the VM boots, it will install a few packages (curl, jq, ripgrep, etc.) and it will run some commands that will install k3s in the system. It will also create the user ubuntu accessible using my id_rsa key.</p><h4>Starting the VM</h4><p>Having previously run tofu init, we can run the command</p><pre>tofu apply</pre><p>And after confirming the plan that OpenTofu will use, (typing yes), the VM will be deployed. We can run virsh list to see that it is running, and we can run virsh net-dhcp-leases default to find our VM IPv4.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*qUAByCcsfvY1wxpj.png" /></figure><p>Of course, we can ssh our VM and check that the installation of k3s has been done with the commands in our `cloud_init.cfg` file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/0*Ny63TDbLm_wDSJor.png" /></figure><h3>Last word</h3><p>If we see in virt-manager the VM we have deployed using OpenTofu and libvirt we have a quite good configuration by default:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/0*UWmWQc43daezIR3s.png" /></figure><p>Virtio Hardware by default for both the network interfaces and the disks (we don’t need to tweak anything at all), providing the best performance we can get thanks to para-virtualisation instead of full-virtualisation.</p><p>Regarding the CPU, I set the host-passthroughmode in the configuration file. It is the most performant and fastest mode. We won&#39;t be able to do live migrations of the server in the cloud. This is another long story to discuss somewhere else. In any case, I don&#39;t need live migrations today.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c653192e16f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vagrant: libvirt plugin]]></title>
            <link>https://medium.com/@joseignacio.carretero/vagrant-libvirt-plugin-57f253887308?source=rss-9f463fd493bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/57f253887308</guid>
            <category><![CDATA[vagrant]]></category>
            <category><![CDATA[libvirt]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[Joseignacio Carretero]]></dc:creator>
            <pubDate>Mon, 01 Dec 2025 11:15:37 GMT</pubDate>
            <atom:updated>2025-12-01T11:15:37.702Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/0*UWD4vipVJ4_WVSas.png" /></figure><p>Vagrant is a developer tool created by <a href="https://developer.hashicorp.com/vagrant">HashiCorp</a>. And I mean it is a developer tool not intended for production. However, It is a fantastic tool which teaches a lot about deploying virtual servers, configuring them, using virtual infrastructure, etc.</p><p>I’ve been using Vagrant since quite a few years ago. It is that easy to create new virtual machines and configuring them the way I need. And yes, I admit that once I was stuck to Virtual Box for some years, even if I knew that KVM is much more performant than Virtual Box. However, I’ve been using the Vagrant libvirt plugin for over a year and I rid off Virtual Box.</p><p>You can also read this story here: <a href="https://jicg.eu/vagrant-libvirt-plugin/">https://jicg.eu/vagrant-libvirt-plugin/</a></p><p>The main reason to use Virtual box instead of libvirt is that Virtual box is really portable. It will work on Linux but it will also work in Windows or Mac OS. This is my best reason to use this default provider for Vagrant. However, I don’t work with windows, nor with MacOS so let’s check go with libvirt and KVM.</p><p>The vagrant-libvirt plugin has a very nice and complete documentation: <a href="https://vagrant-libvirt.github.io/vagrant-libvirt/configuration.html">https://vagrant-libvirt.github.io/vagrant-libvirt/configuration.html</a></p><h3>Installation pre-requisites</h3><p>Of course, we’ll need to install vagrant and all the needed virtualization stuff in our Linux system. It will be easy for you to find a good installation guide for qemu-kvm, libvirt and vagrant for your operating system.</p><p>In Ubuntu we could simple run (… and yes, I know it could be more fine grained):</p><pre># libvirt + qemu installation Ubuntu<br>sudo apt install -y qemu-system libvirt-daemon virt-manager <br><br>sudo systemctl enable libvirtd<br>sudo systemctl start libvirtd</pre><p>And the Vagrant installation goes this way</p><pre># Vagrant Installation Ubuntu<br><br>wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor | sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg &gt; /dev/null<br><br>echo &quot;deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main&quot; | sudo tee /etc/apt/sources.list.d/hashicorp.list<br><br>sudo apt update &amp;&amp; sudo apt install vagrant</pre><p>In arch-linux we’ll need to install vagrant using <strong>yay</strong> or <strong>paru</strong> or something simular after installing libvirt and qemu</p><pre># Vagrant with libvirt and qemu installation in Arch Linux<br><br>sudo pacman -S qemu libvirt virt-manager ebtables dnsmasq bridge-utils<br><br>sudo systemctl enable libvirtd<br>sudo systemctl start libvirtd<br><br># Install Vagrant<br>yay -S vagrant</pre><p>In both systems it is interesting to add your user to the libvirt and kvm groups:</p><pre>sudo usermod -aG libvirt $(whoami)<br>sudo usermod -aG kvm $(whoami)</pre><p>And log out and in again.</p><h3>Install the vagrant-libvirt plugin</h3><h4>Using Ubuntu 24.04</h4><p>If you are using Ubuntu, installing the Vagrant plugin in Ubuntu is really easy, you simple run:</p><pre># Well, we&#39;ll need to compile some things.<br>sudo apt install -y build-essential libvirt-dev<br><br># And install the plugin itself.<br>vagrant plugin install vagrant-libvirt</pre><h4>Using Arch Linux</h4><p>However, I am using Arch Linux and, after running that command, our plugin sadly won’t work. The installation process was a bit more complicated. I need to manually install the gem:</p><pre># Install libvirt-vagrant plugin Archlinux</pre><pre>cd vagrant-libvirt<br>vim lib/vagrant-libvirt/driver.rb</pre><pre>## In a text editor (nano or vim), we remove the lines:<br># &gt; ip_command = %q( awk &quot;/$mac/ {print \$1}&quot; /proc/net/arp )<br># &gt; ...<br># &gt; libvirt_ip_command: ip_command,</pre><pre>## We build the Gem:<br>gem build</pre><pre>## And install it in Vagrant:<br>VAGRANT_DISABLE_STRICT_DEPENDENCY_ENFORCEMENT=1 vagrant plugin install ./vagrant-libvirt-*.gem</pre><p>If those 2 lines are not removed, well get this error:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/0*FxistPMDtAymJdEm.png" /></figure><p>So, the changes in the driver.rbfile are as follow:</p><pre># Get config options for Libvirt provider.<br>        config = @machine.provider_config<br>        uri = config.uri</pre><pre>-       # Setup command for retrieving IP address for newly created machine<br>-       # with some MAC address. Get it from dnsmasq leases table<br>-       ip_command = %q( awk &quot;/$mac/ {print \$1}&quot; /proc/net/arp )<br>-<br>        conn_attr = {<br>          provider: &#39;libvirt&#39;,<br>          libvirt_uri: uri,<br>-         libvirt_ip_command: ip_command,<br>        }<br>        conn_attr[:libvirt_username] = config.username if config.username<br>        conn_attr[:libvirt_password] = config.password if config.password</pre><h3>Using it: Creating a first VM.</h3><p>In qemu-kvm there are 3 important things (to me) which are 1. setting up a virtio-network driver, 2. setting a virtio-disk driver and 3. host-passthrough the CPU (passing the host CPU model features, model, stepping, exactly to the guest). The first 2 ones are that way by default by vagrant-libvirt, and we&#39;ll only have to care for the last one.</p><pre>$ cd vagrant/01-basic-ub2404-vm</pre><pre>$ # We need to be in the directory where the Vagranfile is:<br>$ ls<br>Vagrantfile</pre><pre>$</pre><p>This first Vagrantfile goes like this:</p><pre>Vagrant.configure(&quot;2&quot;) do |config|<br>  # The Vagrant-box to use (operating system image) - an ubuntu24.0 here.<br>  config.vm.box = &quot;cloud-image/ubuntu-24.04&quot;</pre><pre>  # Configurations for the Virtual server:<br>  config.vm.provider &quot;libvirt&quot; do |vb|</pre><pre>    # Disk 50 Gb<br>    vb.machine_virtual_size = 50</pre><pre>    # Memory 8Gb RAM<br>    vb.memory = &quot;8192&quot;</pre><pre>    # 4 cores/cpus<br>    vb.cpus = 4 </pre><pre>    # Nested virtualization - yes (virtualization inside virtualization)<br>    vb.nested = true</pre><pre>    # We need to care this: host-passtrhough -- Copy our CPU Configurations<br>    vb.cpu_mode = &quot;host-passthrough&quot;<br>  end<br>end</pre><p>Once defined, we can run vagrant up to start the new server and when the server is running, we can connect via ssh to the server with vagrant ssh.</p><p>We can stop the server with vagrant halt (we could start again the same server using again vagrant up) or we can destroy the server forever using vagrant destroy.</p><p>0:00</p><p>/1:46</p><p>1×</p><p>Simple and quick demo.</p><h3>Configure the server as it boots.</h3><p>Despite of configuring the server, we could take advantage and add the hostname configuration in the Vagrantfile.</p><p>We can add one line after config.vm.box = ... and before config.vm.provider like this:</p><pre># Set the hostname in the server:<br>config.vm.hostname = &quot;demo-bootstrap&quot;</pre><h3>Script to configure the Server</h3><p>We can add a script which can be run as root when the VM starts. This script will install all the needed packages and make all the needed configurations to our virtual server as soon as it starts.</p><p>In my case, let’s say I could be interested in booting a server running a small <strong>k3s</strong> kubernetes instance, which is quite easy to install. I also want the the default user <strong>vagrant</strong> to be able to use the k3s instance — I need to change the group of the configration file to <strong>vagrant</strong>.</p><p>So, the slightly modified Vagrantfile is:</p><pre>Vagrant.configure(&quot;2&quot;) do |config|<br>  # The Vagrant-box to use (operating system image) - an ubuntu24.0 here.<br>  config.vm.box = &quot;cloud-image/ubuntu-24.04&quot;</pre><pre>  # Provision script: The script will be run as root when the VM starts<br>  config.vm.provision :shell, path:&quot;bootstrap.sh&quot;</pre><pre>  # Set the hostname in the server:<br>  config.vm.hostname = &quot;demo-bootstrap&quot;</pre><pre>  # Configurations for the Virtual server:<br>  config.vm.provider &quot;libvirt&quot; do |vb|</pre><pre>    # Disk 50 Gb<br>    vb.machine_virtual_size = 50</pre><pre>    # Memory 8Gb RAM<br>    vb.memory = &quot;8192&quot;</pre><pre>    # 4 cores/cpus<br>    vb.cpus = 4 </pre><pre>    # Nested virtualization - yes (virtualization inside virtualization)<br>    vb.nested = true</pre><pre>    # We need to care this: host-passtrhough -- Copy our CPU Configurations<br>    vb.cpu_mode = &quot;host-passthrough&quot;<br>  end<br>end</pre><p>And this is the bootstrap file</p><p>#!/bin/bash # Install some desired packages sudo apt update -y sudo apt -y install git curl wget ripgrep fzf jq build-essential # Install K3S curl -sfL https://get.k3s.io | sh - # I want the user Vagrant to use the shell bash (sh is the default one) chsh -s /bin/bash vagrant # Set the apropriate permissions to k3s configuration file for Vagrant user chgrp vagrant /etc/rancher/k3s/k3s.yaml chmod g+r /etc/rancher/k3s/k3s.yaml</p><p>bootstrap.sh configuration script</p><p>After configuring, we simply run the server with vagrant up — As we always do. It is installing software from Internet, so yes... we can expect long boot times. But when the VM is ready, we&#39;ll have a running version of kubernetes in a server named <strong>demo-bootstrap</strong>, as we can see in the picture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/0*4YfKXsuuBgkWtq6O.png" /></figure><h3>Ansible to configure the server</h3><p>Of course, you need to have <strong>ansible</strong> installed before trying to configure your virtual server with ansible. Vagrant makes use of a previously installed ansible in the system, but it doesn’t install it.</p><p>The file tree configuration is as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/0*WaYwFn6Ww_cY5sdj.png" /></figure><p>Of course, bear in mind that this is the simplest configuration we can do. Only 1 virtual server with a very simple playbook. We’ll go deeper in this some day. In this case, even we don’t have an inventory, vagrant is clever enough to know that our ansible inventory is all the servers.</p><p>The configuration of the Vagrantfile is basically the same as it was, the git diff with the previous one is shown here:</p><pre>diff --git a/../02-install-script-vm/Vagrantfile b/Vagrantfile<br>index 72f7ac5..dbf08c2 100644<br>--- a/../02-install-script-vm/Vagrantfile<br>+++ b/Vagrantfile<br>@@ -2,12 +2,16 @@ Vagrant.configure(&quot;2&quot;) do |config|<br>   # The Vagrant-box to use (operating system image) - an ubuntu24.0 here.<br>   config.vm.box = &quot;cloud-image/ubuntu-24.04&quot;<br> <br>-  # Provision script<br>-  config.vm.provision :shell, path:&quot;bootstrap.sh&quot;<br>-<br>     # Set the hostname in the server:<br>   config.vm.hostname = &quot;demo-bootstrap&quot;<br> <br>+  # Provision ansible<br>+  config.vm.provision &quot;ansible&quot; do |ansible|<br>+    ansible.playbook = &quot;playbook.yml&quot;<br>+<br>+    # Just to have a lot of information on screen<br>+    ansible.verbose = &quot;v&quot;<br>+  end<br> <br>   # Configurations for the Virtual server:<br>   config.vm.provider &quot;libvirt&quot; do |vb|</pre><p>And the equivalent file playbook.yml to our bootstrap.sh is:</p><pre>- name: Configure environment<br>  hosts: all<br>  become: true<br>  vars:<br>    package_list:<br>      - git <br>      - curl<br>      - wget<br>      - ripgrep<br>      - fzf<br>      - jq<br>  tasks:<br>  - name: Update apt package cache<br>    apt:<br>      update_cache: yes</pre><pre>  - name: Install the packages in vars<br>    apt:<br>      name: &quot;{{ package_list }}&quot;<br>      state: present</pre><pre>  - name: Install K3S script<br>    shell: |<br>        curl -sfL <a href="https://get.k3s.io/">https://get.k3s.io</a> | sh -</pre><pre>  - name: Change the group of the k3s.yaml file to vagrant<br>    file:<br>      path: /etc/rancher/k3s/k3s.yaml<br>      group: vagrant<br>      mode: g+r</pre><pre>  - name: Change vagrant shell to bash instead of sh<br>    user:<br>      name: vagrant<br>      shell: /bin/bash</pre><p>We can ran, as always vagrant up to start our VM. When it is ready, ansible will have its magic done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sPOVij0Cqlk_yaF-.png" /></figure><h3>Networking</h3><p>By default in libvirt, we have the network <strong>default</strong>. Usually bound to the bridge <strong>virbr0</strong> and with CIDR 192.168.122.0/24. When we create a new server, it will be attached to this bridge device. However, if we use vagrant to create a new server, as we’ve explained in previous examples, a new network will be usually created — It will typically have CIDR 192.168.121.0/24 and it will be usually bound to the bridge <strong>virbr1</strong>.</p><p>After starting any of our Vagrant servers from our previous example, we will have these network configurations in libvirt. In the next example, we can see the networks defined in libvirt as well as the dhcp-leases that network has assigned. In my case, I have 2 virtual servers and each of the leases correspond to each one of the servers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SGs8XXL9FREYvCyK.png" /></figure><p>After destroying all servers, the network <strong>vagrant-libvirt</strong> will be destroyed.</p><h3>Using the “default” network</h3><p>We can use any other network we can think of. For example, let’s start a server connected to our “default” network.</p><p><strong>CAVEAT</strong>: When we destroy the last VM in the Vagrant network, it will be removed. So, in order to keep our network (default, vagrant-libvirt or whatever we use), we should add the line vb.management_network_keep = true to the Vagrantfile. For example, let&#39;s start a new VM in our default network.</p><p>vb.management_network_autostart = false So no new network is created.</p><p>vb.management_network_name = default Connect the VM network interface to the network default.</p><pre>Vagrant.configure(&quot;2&quot;) do |config|<br>  # The Vagrant-box to use (operating system image) - an ubuntu24.0 here.<br>  config.vm.box = &quot;cloud-image/ubuntu-24.04&quot;</pre><pre>  # Configurations for the Virtual server:<br>  config.vm.provider &quot;libvirt&quot; do |vb|</pre><pre>    # Disk 50 Gb<br>    vb.machine_virtual_size = 50</pre><pre>    # Memory 8Gb RAM<br>    vb.memory = &quot;8192&quot;</pre><pre>    # 4 cores/cpus<br>    vb.cpus = 4 </pre><pre>    # Nested virtualization - yes (virtualization inside virtualization)<br>    vb.nested = true</pre><pre>    # We need to care this: host-passtrhough -- Copy our CPU Configurations<br>    vb.cpu_mode = &quot;host-passthrough&quot;</pre><pre>    # Do not create any network automatically<br>    vb.management_network_autostart = false</pre><pre>    # Use specific existing network<br>    vb.management_network_name = &quot;default&quot;<br>    <br>    # Keep the network after exiting. By default, it will be removed<br>    vb.management_network_keep = true<br>  end<br>end</pre><p>After vagrant up, we can see that we&#39;ve got a dhcp-lease in our default interface corresponding to our server:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NuZQNmRHHxAV4hjE.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=57f253887308" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Local DNS Server for Demos]]></title>
            <link>https://medium.com/@joseignacio.carretero/local-dns-server-for-demos-523afad32e7c?source=rss-9f463fd493bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/523afad32e7c</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[dns-servers]]></category>
            <category><![CDATA[vagrant]]></category>
            <dc:creator><![CDATA[Joseignacio Carretero]]></dc:creator>
            <pubDate>Sat, 22 Nov 2025 10:42:55 GMT</pubDate>
            <atom:updated>2025-11-22T10:46:06.180Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z81thh_cKix8zM0QnYBA6A.png" /></figure><p>I’ve been making a lot of Demos and proofs of concept with my laptop over the last years using virtualised servers, kubernetes, LXC, dockers and other different environments. Some time ago, I used to set the appropriate name/ip entries in /etc/hosts, but that isn&#39;t a good solution.</p><p>You can also read this article <a href="https://jicg.eu/local-dns-server-for-demos/">here — https://jicg.eu</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/846/0*34JF-4pkUJdzxi4n.png" /><figcaption>Too may lines in /etc/hosts</figcaption></figure><p>I realised my /etc/hostsfile was growing bigger an bigger every day and I realised that entries were growing older and older everyday. When I created a new Virtual server, I had to replicate the hosts entries in my Virtual servers so that they could talk each other using the name instead of the IP.</p><p>I had to get this fixed once and forever: I needed a DNS Server.</p><p>In this article, I’ll explain the reasons to deploy a DNS server, which one I like to use, how to install and configure it and, at the end, I’ll a very easy demo where I’ll deploy a new Ubuntu Virtual server which can be accessed using its name by ssh (using DNS) and I’ll will deploy a mini-kubernetes (K3S). In the K3S I’ll deploy a nginx web server as a service and I’ll set an Ingress entry, so it can be clear that we can access the web server content using our local web browser.</p><h3>Advantages of local DNS server</h3><h4>Consistent local names for my local testing domains</h4><p>Yes, I want to have a domain name like “jicg.mine” and as many subdomains in that domain as I can imagine. So, I configure my own domain name and… voilà, I can set up all the names I need, all I the names I want. If I use my own DNS server as the default one in my laptop, then all my virtual servers, my kubernetes clusters or my dockers will use this DNS and the server names will be consistent among them all. No more extra work than setting the names in the DNS server once.</p><p>Please, note that the top level domain name <strong>.local</strong> is a special-use domain reserved and it might not be a good idea to use it. You can check all the <a href="https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains#Special-use_domains">special-use domains in wikipedia</a>.</p><h4>Centralised Hosts File Management</h4><p>As I described in the introduction, the /etc/hostsfile easily becomes messy and unmaintainable. The host file, even though seems to be an easy solution at first, has its limitations. Imagine you need a set of names like *.demo.jicg.mine (I&#39;ll use it late since it is very comfortable for defining Ingress entries), you&#39;ll struggle writing entries in all the /etc/hostsfiles across your demo infrastructure. It is simply a bad idea. To sum up, modifying /etc/hosts is both a messy and a poor solution.</p><p>Using a DNS nameserver is really a nice solution to keep things under control for the demo or all your infrastructure.</p><h3>What interface should DNS server should listen?</h3><p>It is easy to see that, in my laptop I have several DNS servers installed since I use LXC or Libvirt:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/977/0*LvcjSPCIl832M7mP.png" /></figure><p>So, 0.0.0.0 is not the right place to set the DNS listening socket. And as I want it to be accessible from virtual servers, kubernetes deployments, and from everywhere, I can&#39;t set it up in 127.0.0.1.</p><p>My solution was creating a new local interface. I created a new <strong>bridge</strong>.</p><p>Most Linux desktop systems today use NetworkManager, which has a graphical configuration tool. So, in order to create a new persistent network interface in my laptop which I will use from every virtual server<em> </em>I created a new <strong>bridge</strong> interface using my graphical interface:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/0*lbqodOmGXYEr47bb.png" /><figcaption>Network Manager: Press “+” button to add a new network interface</figcaption></figure><p>You’ll be requested to select the type of interface you want to create, and in my case I used bridge, I pressed <strong>create</strong> and filled the info required under <em>bridge</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/0*y_Nt0_k_wPBM3aQ5.png" /><figcaption>Create a new Bridge: Basic settings</figcaption></figure><p>In this bridge you won’t probably need to enable the stp. We won&#39;t have so many bridges nor such a complex network deployment that might cause loops in the simulated network. However it won&#39;t harm if you activate it.</p><p>Under “<strong>IPv4 Settings</strong>” I wrote the IPv4 configuration (no IPv6 used in my case):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*ar3JgtjjQfNkZO-B.png" /><figcaption>My IPv4 configuration (No IPv6 needed here)</figcaption></figure><p>And yes, a /32 is enough. I don&#39;t need a full network here. I simply need an IP. Nothing else. So, yes it is not a mistake it is only what I need.</p><h3>Bind installation and configuration</h3><p>I feel comfortable with bind9and named. So I decided to install it in my laptop. You can probably do it with a local corednsor dnsmasq (although dnsmasq has also its limitations). Anyway, I am comfortable with bind, so that will be my choice.</p><p>If you don’t know much about bind, you’ll realise I write about named and bind mixing them as I didn&#39;t know what software to use. To clarify, bindis the software suite and namedis the name server provided (the daemon). After the clarification, Let&#39;s install it:</p><pre># In Archlinux:<br>sudo pacman -S bind<br><br># Enable named for next restarts<br>sudo systemctl enable named<br><br># Start named<br>sudo systemctl start named</pre><p>It can also be deployed in Ubuntu. There will be no need to enable and start manually the service. Ubuntu does this for you. So, the installation is simply like this:</p><pre>sudo apt install -y bind9</pre><p>Not it is time for the <strong>configuration</strong> of the named server, And yes, they have different places to configure things, but they are basically the same. In Arch Linux it is /etc/named.conf and all the files in the directory /etc/named, whilst in Ubuntu it is /etc/bind/named.conf and the files in /etc/bind. The configuration is basically done the same way. Let&#39;s start setting the options. The following is a excerpt from the corresponding named.conf — The configuration of “options”:</p><pre>options {<br> listen-on-v6 port 53 {  none;  };<br><br>    # Listen on... the bridge we previously created.<br> listen-on port 53 { 192.168.3.253; };<br> directory  &quot;/var/named&quot;;<br> dump-file  &quot;/var/named/data/cache_dump.db&quot;;<br> statistics-file &quot;/var/named/data/named_stats.txt&quot;;<br> memstatistics-file &quot;/var/named/data/named_mem_stats.txt&quot;;<br> recursing-file  &quot;/var/named/data/named.recursing&quot;;<br> secroots-file   &quot;/var/named/data/named.secroots&quot;;<br><br> recursion yes;<br><br>    # If the name is not local, query other DNSs<br> forwarders {<br>                8.8.8.8;<br>                8.8.4.4;<br>        };<br><br> # dnssec-enable yes;<br> dnssec-validation yes;<br><br> /* Path to ISC DLV key */<br> bindkeys-file &quot;/etc/named.root.key&quot;;<br><br> managed-keys-directory &quot;/var/named/dynamic&quot;;<br><br> pid-file &quot;/run/named/named.pid&quot;;<br> session-keyfile &quot;/run/named/session.key&quot;;<br>};</pre><p>This way, we’ll configure the namedserver to listen</p><ul><li>Only in the IP 192.168.3.253</li><li>Try to resolve the name for the clients by doing all the needed work, for example, querying other namerservers.</li><li>forwarders — Use the nameservers (in this case google’s nameservers) to find the right answer for the name.</li></ul><p>Now, we’ll need to change with the NetworkManager our network configurations in order to use our local DNS server as show in the following picture instead of those provided by our ISP provider:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/0*Kr8dxIYm6d9prtZg.png" /><figcaption>local DNS server for our wired connection in our Network Manager</figcaption></figure><h3>Configure a new Zone (domain) in our server</h3><p>Once everything is up and running, we can configure our 1st zone. I’ll show the example for jicg.mine domain</p><ol><li>We have to edit /etc/named/named.conf.local and add the following lines to define a new local zone:</li></ol><pre>zone &quot;jicg.mine&quot; {<br>    type master;<br>    file &quot;/etc/named/zones/db.jicg.mine&quot;; # zone file path<br>};</pre><p>Create the file defined in the zone /etc/named/zones/db.jicg.mine</p><pre>$TTL    604801<br>@       IN      SOA     services.jicg.mine. admin.jicg.mine. (<br>         2025112100     ; Serial<br>             604800     ; Refresh<br>              86400     ; Retry<br>            2419200     ; Expire<br>             604800     ; Negative Cache TTL<br>)<br>; name servers - NS records<br>    IN      NS      services<br>services.jicg.mine.          IN      A       192.168.3.253<br>admin.jicg.mine.             IN      A       192.168.3.253<br>; name servers - A records<br>demo.jicg.mine.              IN      A       192.168.122.100<br>*.demo.jicg.mine.            IN      A       192.168.122.100</pre><ol><li>Let’s check if everything is correct using the named-checkconf to test the configuration of named and named-checkzone to check if the zone configuration is correct:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/0*qDMS60IAuav0aNy1.png" /><figcaption>Check named configuration (both the config and the zone jicg.min)</figcaption></figure><p>2. Finally I could reload the daemon named</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/847/0*yjO7yRTNo9ckQIeZ.png" /><figcaption>Reload named and check it is working</figcaption></figure><h3>Test Use Case: Demo with Kubernetes Ingress</h3><p>I want to access a server in Kubernetes using a name, for example, <a href="http://my-nginx.demo.jicg.mine./">http://my-nginx.demo.jicg.mine.</a></p><p>I’ll do all the process from the deployment of the Virtual server using vagrant with the libvirt provider — basically this consist deploying a new virtual server with a defined IP and I’ll show the Vagrantfile used and the shell script used to install K3S when the server starts, no need to type almost anything but vagrant up.</p><p>In the second part, we’ll deploy the kubernetes service and the Ingress entries that will configure the web site (yes, I know, it will be empty) to demonstrate how the DNS server helps us in this example scenario.</p><h4>Deploy a new VM and install k3s kubernetes.</h4><p>Vagrant is a very good tool to deploy new Virtual servers. It is not the fastest tool and it is not for production but it is very nice to create disposable virtual servers. I usually work with Vagrant and its libvirt plugin. So, in order to start my virtual server using Vagrant, I create a new directory named demo and I create 2 files: Vagrantfile and bootstrap.sh. The first one is to configure the new virtual server to boot and the other one is a bash-script to configure and install things. Here it is the example of Vagrantfile to configure the newly created VM</p><pre>Vagrant.configure(&quot;2&quot;) do |config|<br>  config.vm.box = &quot;cloud-image/ubuntu-24.04&quot;<br>  config.vm.provision :shell, path:&quot;bootstrap.sh&quot;<br><br>  config.vm.hostname = &quot;demo.jicg.mine&quot;<br>  config.vm.synced_folder &#39;./&#39;, &#39;/vagrant&#39;, type: &#39;nfs&#39;, nfs_version: 4<br><br>  config.vm.provider &quot;libvirt&quot; do |vb|<br>    vb.machine_virtual_size = 50<br>    vb.memory = &quot;8192&quot;<br>    vb.cpus = 4 <br>    vb.nested = true<br>  end<br><br>  config.vm.network :private_network,<br>    libvirt__mac: &quot;52:54:00:bd:ba:98&quot;,<br>    ip: &quot;192.168.122.100&quot;,<br>    type: &quot;dhcp&quot;,<br>    libvirt__network_name: &quot;default&quot;<br><br>end</pre><p>And here it is the Vagrantfile to bootstrap the server installing k3s and some tools I like to have:</p><pre>#!/bin/bash<br><br>## Update the VM and install some tools I like to have<br>sudo apt update -y<br>sudo apt -y install git curl wget ripgrep fzf jq<br><br>## Install k3s<br>curl -sfL https://get.k3s.io | sh -<br><br># Configure the k3s service, setting the node ip to the one used in our demo<br>sed -i &#39;s/server \\/server --node-ip 192.168.122.100 --node-external-ip 192.168.122.100/g&#39; /etc/systemd/system/k3s.service<br><br># Reload systenctl after the changes in the service<br>systemctl daemon-reload<br><br># Reload the service itself<br>systemctl restart k3s<br><br># Set the bash shell for vagrant (by default it is /bin/sh)<br>chsh -s /bin/bash vagrant<br><br># Add the corresponding permissions to vagrant so it can use kubernetes<br>chgrp vagrant /etc/rancher/k3s/k3s.yaml<br>chmod g+r /etc/rancher/k3s/k3s.yaml</pre><p>We run the vagrant upcommand in order to start the installation of our Virtual Server and after some time, everything would have ended and we&#39;ll be able to ssh the VM and use Kubernetes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/0*nwKIQktUju0Rcdst.png" /><figcaption>After finishing the installation, we can ssh the vm</figcaption></figure><p>Or even we could login using the name in the DNS. Of course, we’ll have to use the private key generated by Vagrant, but it is absolutely feasible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/0*cKc2kjR7BkVGJuCC.png" /><figcaption>Login using a DNS Name. Checking the DNS Inside the Vagrant VM.</figcaption></figure><h3>Deploy the Ingress service</h3><p>One of the easiest thing we can deploy in kubernetes to demonstrate Ingress is an nginx service. I’ll create both a Service for an nginx deployment and its Ingress.</p><pre># Apply the Deployment and the Service for the nginx<br>kubectl apply -f https://raw.githubusercontent.com/jicarretero/jicgeu-support/refs/heads/master/OtherExamples/2025/11/localDnsDemo/nginx.yaml<br><br># Apply Ingress for nginx<br>kubectl apply -f https://raw.githubusercontent.com/jicarretero/jicgeu-support/refs/heads/master/OtherExamples/2025/11/localDnsDemo/nginx-ingress.yaml</pre><p>Apply yaml files to create an nginx service and its ingress.</p><p>If you read carefully the ingress file, you can see these 2 lines:</p><pre>rules:<br>    - host: my-nginx.demo.jicg.mine</pre><p>It means that when an HTTP request comes for <a href="http://my-nginx.demo.jicg.mine,/">http://my-nginx.demo.jicg.mine,</a> it will be mapped to the nginx service:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/939/0*0RPQKYeJbbpEagMv.png" /></figure><p>And the valid name is that one and no other name. It is the only one that will be mapped to the service:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/923/0*psPgnaMmyzXnNfRQ.png" /></figure><p>If we wanted this last name to work, I need to apply a manifest to create an Ingress entry for that new name, mapping it to the nginx service — Here it is the new ingress file to apply to add the ingress so we can use this new name (notice that it is already defined in the DNS server as *.demo.jicg.mine):</p><pre>apiVersion: networking.k8s.io/v1<br>kind: Ingress<br>metadata:<br>  name: nginx<br>spec:<br>  rules:<br>    - host: other-name.demo.jicg.mine<br>      http:<br>        paths:<br>          - path: /<br>            pathType: Prefix<br>            backend:<br>              service:<br>                name: nginx<br>                port:<br>                  number: 80</pre><p>Then run kubectl apply -f other-name-ingress.yaml and... we&#39;ve go it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/0*uClDebhA4tFcueyV.png" /></figure><p>Again, in this case, we’ll have 2 names and only these 2 names. I don’t think I need more. Well, I think I have one too many.</p><h3>Last Word</h3><p>In production environments, there is no doubt that a DNS name server is really important, but for local demos and learning I really value what a DNS server can do for my demos. I will be able to create new entries to map our desired names to IPs to different services. I will be able to access the different servers with ssh using names instead of remembering IP addresses, create web services accessible by their names, etc.</p><p>Yes, it might be a bit difficult at first, however, the effort is quickly paid off.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=523afad32e7c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>