lic*_*rna 5 kubernetes kubernetes-go-client
我有一个有 3 名成员的状态集。可以通过以下方式从集群内部访问它们:
podname-{0..n}.service.default.svc.cluster.local
Run Code Online (Sandbox Code Playgroud)
我正在使用控制器中的 Kubernetes API。我刚刚创建了 Statefulset:
podname-{0..n}.service.default.svc.cluster.local
Run Code Online (Sandbox Code Playgroud)
假设一切正常,几分钟后我就有了 3 个正在运行的 Pod。如何获取新创建的 Pod 的 Pod DNS 条目?
我知道我可以使用以下方法构建 DNS:
fmt.Sprintf("%s-%d.%s.%s.svc.cluster.local", s.Name, idx, service, S.Namespace)
Run Code Online (Sandbox Code Playgroud)
但我发现一些问题:
StatefulSet
cluster.local
这不一定是真的我认为它应该存在(但我不确定它是否真的存在),是一个 API,给定 StatefulSet 将允许我知道所创建的副本的 DNS 名称。有这样的API吗?
您所需要的事件嗅探几乎正是 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
}
}
Run Code Online (Sandbox Code Playgroud)
官方文档中另一个好的推荐方法
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))
...
Run Code Online (Sandbox Code Playgroud)
您还可能了解到,dns 是独立的 k8s 服务,而不是集群服务的一部分。 因此,如果您使用默认命名策略和 coredsn 或 dns-masq 服务,请找到 ip/host 并调用您的 dns 端点 了解详细信息。您还可以从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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4628 次 |
最近记录: |