Tutorial Highlights & Transcript
00:00 - Features of tfk8s
But there’s a Terraform built-in function that does a similar thing called yaml.decode. So if you use that in the Terraform code, you would have to put your YAML code directly in and then just call a function around it. I think that this is a lot neater, because you can also use this for multiple manifests. Where before, with YAML decode, you would have to separate out your different manifests into different committees resources. With this, it’ll do that for you. Also, getting the extra functionality of against directory trees and Helm charts and it’s just a better tool ready.
01:44 - Installation
02:03 - Usage
Then the other ways we are able to use it, you can use it against the directory, so you just do a find command for the directory name. It’s quite long so I’ll probably copy and paste that one but using that is really helpful. You can just go through a directory tree and look for any YAML files and convert those to the Terraform files. You can also use it for running Kubernetes resources and output those as maps instead. Also, there’s a way to use a flag to strip those server side fields. It’s a lot neater.
The Helm charts are quite straightforward using the Helm template command. You can input that value file, and it will convert that to Kubernetes manifest resources.
03:26 - Demo
If I go here, so I’ve just made a simple nginx app file. It’s got a service in it and a deployment but you can see there’s two different manifests here. It’s exactly the same, it’s so simple. So I can do this to, again, print to the console. Or if I want to output that to a file, that -O flag. You can now separate it into two different Kubernetes manifest resources. You’ll notice for the naming, it takes the kind and then there’s like an underscore and the name. So all of your resources will be named in that kind of convention here.
Then I can show a Kubernetes running example. Using this, I could get my namespace and instead of getting that as YAML, I can directly convert the output YAML into map, which is really useful. I could do that for other resources. For example, if I just create a nginx file with a pod in it, I can just do an apply on that. Get pods. Normally, where I could do GitHub web, I could do an output of YAML with that. I can pipe that to tfk8s to get that as map instead. Then if I use -S, I can strip out the server side fields as well, which is really nice and neat. I can grab just the map and server side fields, as well. You can tie them together and it just saves a lot of legwork trying to do that manually.
I’ve got a couple more examples I can show. One is as the directory tree. In my directory example, over here, I’ve got four different directories. Leave that and I just took back some different Kubernetes resources here. I’ve got a daemon set there, got some deployments there, a couple of services in here, and a web app. If I run this from here, go through each of those valves, we got a Terraform file for each of those, which is pretty powerful, yet something so simple. With each of those, I got also some that had multiple manifests in the directory for a moment. I debriefed a web app, a cat map that will serve the YAML because I got two manifests in the same file here. Again, it will just separate those out into separate Kubernetes manifests, which is really cool.
Then I’ve got one more example for the Helm chart. I will create a Helm chart right now. This is just a more related default. So we’ve got all the template files here, we’ve got the YAML file here. Where you normally run just the Helm template then you can then use case two. As you can see, just convert that into the entire file with all the manifests. I think the most powerful thing I could do because of that. Here, the deployments were converted into different Kubernetes manifest resources.
This isn’t being used in my specific project at the moment, but I know that we have some different ones where we need to be doing this kind of stuff. I would absolutely use this tool to do that tather than doing that by hand. Anytime that you can automate something just takes out a huge error, which is so easily done when you look at the syntax of these files. It’s a really handy tool, so easy to install and so easy to use.
Grace is a DevOps Engineer at nClouds and an AWS Certified Developer - Associate and AWS Certified Solutions Architect - Associate.