Get a DNS name called Programmatically

I have a state with three members. They are accessible from within the cluster with something like:

podname-{0..n}.service.default.svc.cluster.local

I am using the Kubernetes API from the controller. I just created a Statefulset with:

import (
    "k8s.io/client-go/kubernetes"
    appsv1 "k8s.io/api/apps/v1"
)
...

s := appsv1.StatefulSet{ /* some data */ }
kubernetes.AppsV1().StatefulSets(nameSpace).Create(s)

Suppose everything works correctly, and after a few minutes I have 3 sub-sets. How can I get Pod DNS records for newly created Pods?

I know that I can create DNS using:

fmt.Sprintf("%s-%d.%s.%s.svc.cluster.local", s.Name, idx, service, S.Namespace)

But I find some problems:

  • I will have to calculate the index based on the number of replicas
  • I need to know the service name used for this StatefulSet
  • I assume the cluster domain cluster.local, which is not necessarily true

, ( , ), API, , StatefulSet, DNS Replicas. API?

+4
2

, , , , KubeDNS.

. podWatchFunc . ,

func podWatchFunc(c *kubernetes.Clientset, ns string, s labels.Selector) func(options meta.ListOptions) (watch.Interface, error) {
    return func(options meta.ListOptions) (watch.Interface, error) {
        if s != nil {
            options.LabelSelector = s.String()
        }
        w, err := c.CoreV1().Pods(ns).Watch(options)
        if err != nil {
            return nil, err
        }
        return w, nil
    }
}

 import (
   "fmt"
   "k8s.io/client-go/1.4/kubernetes"
   "k8s.io/client-go/1.4/pkg/api/v1"
   "k8s.io/client-go/1.4/tools/clientcmd"
)
...
   // uses the current context in kubeconfig
   config, _ := clientcmd.BuildConfigFromFlags("", "path to kubeconfig")
   // creates the clientset
   clientset, _:= kubernetes.NewForConfig(config)
   // access the API to list pods
   pods, _:= clientset.CoreV1().Pods("").List(v1.ListOptions{})
   fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
... 

, dns k8s, . ip/host dns ... coredsn dns-masq. pod

    metadata:
  creationTimestamp: 2018-04-01T13:45:01Z
  generateName: client-deployment-f8454db47-
  labels:
    app: client
    pod-template-hash: "940108603"
  name: client-deployment-f8454db47-5gk64
  namespace: novaposhta
  ownerReferences:
  - apiVersion: extensions/v1beta1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: client-deployment-f8454db47
    uid: d833ef0f-35b2-11e8-9278-42010a840112
  resourceVersion: "73550"
  selfLink: /api/v1/namespaces/novaposhta/pods/client-deployment-f8454db47-5gk64
+1

Service StatefulSet, DNS SRV. , , Service, . , Python, :

>>> import dns
>>> for item in dns.resolver.query('your-app-name-headless', 'SRV'): item
...
<DNS IN SRV rdata: 10 25 0 2555bb89.your-app-name-headless.your-project.svc.cluster.local.>
<DNS IN SRV rdata: 10 25 0 830bdb18.your-app-name-headless.your-project.svc.cluster.local.>
<DNS IN SRV rdata: 10 25 0 aeb532de.your-app-name-headless.your-project.svc.cluster.local.>
<DNS IN SRV rdata: 10 25 0 a432c21f.your-app-name-headless.your-project.svc.cluster.local.>

:

+1

Source: https://habr.com/ru/post/1695825/


All Articles