您好我正在尝试获取服务的外部 IP。我可以在 azure 控制台中看到 externalIp,但数据对象不包含它。
resource "kubernetes_manifest" "haproxy_controller_svc" {
manifest = yamldecode(file("${path.root}/k8s/ingress/controller.svc.yaml"))
}
output "ingress_external_ip" {
value = kubernetes_manifest.haproxy_controller_svc.object.spec.externalIPs[0]
}
$ tf apply
Error: Attempt to index null value
│
│ on ingress.tf line 61, in output "ingress_external_ip":
│ 61: value = kubernetes_manifest.haproxy_controller_svc.object.spec.externalIPs[0]
│ ├────────────────
│ │ kubernetes_manifest.haproxy_controller_svc.object.spec.externalIPs is null
│
│ This value is null, so it does not have any indices.
由于入口已经存在并且有一个 IP(来自之前的 tf apply)。在这一点上,我不认为这是因为尚未在 Azure 中配置负载均衡器。我再次可以在仪表板上看到它。
如果我打印完整的对象。它看起来像这样。
ingress_svc_object = {
"apiVersion" = "v1"
"kind" = "Service"
"metadata" = {
"annotations" = tomap(null) /* of string */
"clusterName" = tostring(null)
"creationTimestamp" = tostring(null)
"deletionGracePeriodSeconds" = tonumber(null)
"deletionTimestamp" = tostring(null)
"finalizers" = tolist([
"service.kubernetes.io/load-balancer-cleanup",
])
"generateName" = tostring(null)
"generation" = tonumber(null)
"labels" = tomap({
"run" = "haproxy-ingress"
})
"managedFields" = tolist(null) /* of object */
"name" = "haproxy-ingress"
"namespace" = "ingress"
"ownerReferences" = tolist(null) /* of object */
"resourceVersion" = tostring(null)
"selfLink" = tostring(null)
"uid" = tostring(null)
}
"spec" = {
"allocateLoadBalancerNodePorts" = tobool(null)
"clusterIP" = "10.0.147.131"
"clusterIPs" = tolist([
"10.0.147.131",
])
"externalIPs" = tolist(null) /* of string */
"externalName" = tostring(null)
"externalTrafficPolicy" = "Local"
"healthCheckNodePort" = 31208
"internalTrafficPolicy" = tostring(null)
"ipFamilies" = tolist([
"IPv4",
])
"ipFamilyPolicy" = "SingleStack"
"loadBalancerClass" = tostring(null)
"loadBalancerIP" = tostring(null)
"loadBalancerSourceRanges" = tolist(null) /* of string */
"ports" = tolist([
{
"appProtocol" = tostring(null)
"name" = "http"
"nodePort" = 30281
"port" = 80
"protocol" = "TCP"
"targetPort" = 80
},
{
"appProtocol" = tostring(null)
"name" = "https"
"nodePort" = 30705
"port" = 443
"protocol" = "TCP"
"targetPort" = 443
},
{
"appProtocol" = tostring(null)
"name" = "stat"
"nodePort" = 30711
"port" = 1024
"protocol" = "TCP"
"targetPort" = 1024
},
])
"publishNotReadyAddresses" = tobool(null)
"selector" = tomap({
"run" = "haproxy-ingress"
})
"sessionAffinity" = "None"
"sessionAffinityConfig" = {
"clientIP" = {
"timeoutSeconds" = tonumber(null)
}
}
"topologyKeys" = tolist(null) /* of string */
"type" = "LoadBalancer"
}
}
编辑:
kubectl 的状态描述:
$ k get -o yaml service/haproxy-ingress -n ingress
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2021-08-13T07:28:11Z"
finalizers:
- service.kubernetes.io/load-balancer-cleanup
labels:
run: haproxy-ingress
name: haproxy-ingress
namespace: ingress
resourceVersion: "471823"
uid: 837281bd-3cb5-4454-93fb-b3d36605444a
spec:
clusterIP: 10.0.147.131
clusterIPs:
- 10.0.147.131
externalTrafficPolicy: Local
healthCheckNodePort: 31208
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- name: http
nodePort: 30281
port: 80
protocol: TCP
targetPort: 80
- name: https
nodePort: 30705
port: 443
protocol: TCP
targetPort: 443
- name: stat
nodePort: 30711
port: 1024
protocol: TCP
targetPort: 1024
selector:
run: haproxy-ingress
sessionAffinity: None
type: LoadBalancer
status:
loadBalancer:
ingress:
- ip: 1.2.3.4
编辑2:
为了测试,我手动获取服务数据。
data "kubernetes_service" "test" {
metadata {
name = "haproxy-ingress"
namespace = "ingress"
}
}
output "test_svc_state" {
value = data.kubernetes_service.test.status
}
test_svc_state = tolist([
{
"load_balancer" = tolist([
{
"ingress" = tolist([
{
"hostname" = ""
"ip" = "1.2.3.4"
},
])
},
])
},
])