How to get a list of Kubernetes resources from Terraform? - terraform-provider-kubernetes

I need to get a list of Kubernetes resources (e.g. services) in a namespace that match a specific pattern. kubernetes_service will select a service with a known name, how do I find a list of services whose name is not known in advance?
A script around kubectl that implements an External Data Source might be able to do it, but was hoping that there's some way to do this with the native Kubernetes provider.

Related

Dockerized Application in Kubernetes for multi-tenancy

I currently have a dockerized application that I'd like to have the deployment model of multi-tenancy. The end goal for me is to be able to deploy the multiple application instances without the need to spin up a VM for every instance.
I've looked at Kubernetes, and I'm looking at pods for now, but would like some practical guidance as I'm inexperienced in Kubernetes.
How would I, if possible, practically do this? And how would the deployment look like?
In Kubernetes, you may isolate your tenants by using namespaces. Each tenant will get its own namespace to spin up pods and other namespace scoped objects. Furthermore, you can define role and rolebinding and implement RBAC to grant access to a particular namespace.
Namespaces
In Kubernetes, namespaces provide a mechanism for isolating
groups of resources within a single cluster. Names of resources need
to be unique within a namespace, but not across namespaces.
Namespace-based scoping is applicable only for namespaced objects
(e.g. Deployments, Services, etc) and not for cluster-wide objects
(e.g. StorageClass, Nodes, PersistentVolumes, etc).

Using the OpenShift API is there a way to get deployments in a project

I am doing some recon work on projects in our openshift cluster and I am looking for an easy way to get all the Projects in a certain group.
I know there is an openshift API that has access to certain openshift artifacts:
For example I could make an API call to the openshift cluster like this:
/oapi/v1/projects/{name}
To get a project of a specific name.
Is there a way to then get all the deployments for that project... Something like this:
/oapi/v1/projects/{name}/deployments
So I could know what deployments are in a certain environment in our openshift cluster.
Any thoughts on this would be great.
OCP"Projects" being a superset or "encapsulation" of k8s"Namespaces", you can list the deployments of a specific"Project/Namespace" with this API:
GET /apis/apps/v1/namespaces/{namespace}/deployments
Of course you need the correct autorizations to be able to list such objects as explained in the following docs
Reference: https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/deployment-v1/#http-request-2
OCP API Reference: https://docs.okd.io/latest/rest_api/index.html
K8S API Reference: https://kubernetes.io/docs/reference/kubernetes-api/

How to collect all ip's of pods by specific name filter

I have some legacy application, which deployed on clustered environment. When one of the application nodes receives call it gets from some configuration file static list of all application nodes where application is deployed.
When all ip's collected it communicates with each app node over jmx.
Current aim is to migrate to k8s, so in this case list of application pods is dynamic and can be just stored as is. Need to implement something like service discovery.
Current thoughts is to implement some simple rest service that will run in separate pod, main aim of which is always return some list of ips (entrypoints) of application pods filtered by some predicate.
So I have few questions:
Is it correct way to work? Any other options? (without changing legacy code)
Is there any ready solution for this? If not, how can I get information about needed pods inside my rest service?
Define a service with a scope selector so all your special pods are included then you can list all your endpoints IP's asking the apiservice.
You can check it's working with the command.
kubectl get endpoints
After that remains how to execute this command inside your pod. That's another story.
This link explain that matter
https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/#accessing-the-api-from-a-pod
Looks you're running a clustered application, so probably you need a Headless Service combined with a StatefulSet.
With this, you will be able to reach your replicas using simple DNS like replicas-[0-9].namespace.svc without need to extract IP addresses from endpoints query.

Kubernetes create private namespace/domain (FQDNs)

I am migrating legacy product(VMs) to kubernetes. This product defines its services inside the product using FQDN style and makes heavy use of /etc/hosts files to simulate DNS and implement services resolution
#service names
service1.customer.domain.com
service2.customer.domain.com
# /etc/hosts
192.22.22.21 service1.customer.domain.com
192.22.22.22 service2.customer.domain.com
Changing this FQDN service naming style e.g. servicename.a.b.c would have significant impact on the product so I am trying to maintain this.I was looking to see how i could achieve this in Kubernetes. I hoped to use Kubernetes Namespaces but they disallow FQDN style enforcing a namespace with a single String with no dots while Kubernetes Services apply similar restrictions.
I might have to just bite the bullet and refactor it fully but I was wondering if there was a simpler way to replicate the service name/resolution behaviour in the legacy product in kubernetes?
We cannot control the root domain, this is defined by our customer e.g. customer1.netwk5.workers.us.com. We would use this to define things like cookie domain, generate SSL certs for web server, configure LDAP and so on

How to create a microservice that replicates itself as load of data increases?

Iam working on a project of big data, where Iam trying to get tweets from Twitter and analyse these tweets and make predictions out of it.
I have followed this tutorial : http://blog.cloudera.com/blog/2012/10/analyzing-twitter-data-with-hadoop-part-2-gathering-data-with-flume/
for getting the tweets. Now Iam planning to build a microservice which can replicate itself as I increase the number of topics on which I want tweets. Now whatever code I have written to gather the tweets with that I want to make a microservice that can take a keyword and create a instance of that code for that keyword and gather tweets, for each keyword an instance should be created.
It will also be helpful if you inform me what tools to use for such application.
Thank you.
I want to make a microservice that can take a keyword and create a instance of that code for that keyword and gather tweets, for each keyword an instance should be created.
You could use kubernetes as an underlying cluster/deployment infrastructure. It has an API that allows you to deploy new services programmatically. So what you would have to do is:
Set up a basic service container for your twitter-service that is available in a container repository.
Then you deploy a first service based on your container. The service configuration will contain the keyword that the service uses as well as information about the kubernetes cluster (how to access the cluster API and where to find the container in the repository).
Now your first service has all the information it needs to automatically create additional service descriptions for kubernetes (with other key words) and deploy those additional services by calling the kubernetes cluster API.
Since the additional services will be passed all the necessary information as well, they themselves can then start even more services and so on.
You probably need to put some effort into figuring out the cluster provisioning, but that can also be done automatically with auto-scaling (available for Google or AWS clouds for example).
A different approach would be to run a horizontally scaled cluster of your basic twitter services that use a self organization algorithm to involve all the keywords put into a database or event queue.

Resources