很简单的问题,如何获取当前运行代码的 Pod?
我需要它,因为出于某种原因,我需要直接 ping Pod 的代码,而不是使用我的常规端点(即负载均衡器)。
我当前的配置:
apiVersion: v1
kind: Service
metadata:
name: web-socket-service-api
spec:
ports:
# Port that accepts gRPC and JSON/HTTP2 requests over HTTP.
- port: 8080
targetPort: 8080
protocol: TCP
name: grpc
# Port that accepts gRPC and JSON/HTTP2 requests over HTTP.
- port: 8081
targetPort: 8081
protocol: TCP
name: rest
# Port that accepts WebSockets.
- port: 8082
targetPort: 8082
protocol: TCP
name: websocket
selector:
app: web-socket-service-api
type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: web-socket-service-api
spec:
replicas: 3
template:
metadata:
labels:
app: web-socket-service-api
spec:
containers:
- name: web-socket-service-api
image: gcr.io/[PROJECT]/web-socket-service-api:latest
ports:
- containerPort: 8080
- containerPort: 8081
- containerPort: 8082
我试过
/*
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
*/
func GetContainerIP() string {
// creates the in-cluster config
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
log.Println("checking pods")
pods, err := clientset.CoreV1().Pods("default").List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
但我悲伤地得到了
恐慌:pods 被禁止:用户“system:serviceaccount:default:default”无法在命名空间“default”中的 API 组“”中列出资源“pods”
而且我找不到修复它的方法或任何有关它的教程..
跃然一笑
慕容3067478
慕工程0101907
倚天杖
相关分类