为 CDK 中的 EKS 构造检索 LoadBalancer URL
Retrieve the LoadBalancer URL for EKS construct in CDK
我正在使用 EKS Construct to create an EKS cluster in CDK. I'm adding the NGINX helm chart to the cluster and I want to export the ingress LoadBalancer URL. The EKS Construct exposes a .getServiceLoadBalancer()
方法,但它需要一个服务名称。我不确定如何获取 LoadBalancer 服务的名称以将其传递给该方法。感觉就像我错过了什么。示例:
export class EksClusterStack extends cdk.NestedStack {
elbUrl: string;
constructor(scope: cdk.Construct, id: string, props?: cdk.NestedStackProps) {
super(scope, id, props);
const clusterAdmin = new iam.Role(this, 'AdminRole', {
assumedBy: new iam.AccountRootPrincipal()
});
const cluster = new eks.Cluster(this, 'Cluster', {
mastersRole: clusterAdmin,
version: eks.KubernetesVersion.V1_18,
defaultCapacity: 2,
});
const nginx = cluster.addHelmChart('NginxIngress', {
chart: 'nginx-ingress',
repository: 'https://helm.nginx.com/stable',
});
this.elbUrl = cluster.getServiceLoadBalancerAddress('{Where do I get the service name?}') //<- This is what I can't figure out
}
我查看了 helm chart 上的属性,它似乎没有暴露任何符合要求的东西。感谢任何见解。谢谢!
您部署的是入口控制器控制图,它本身不公开任何内容,而是扫描任何类型为 Ingress 的 K8S 对象。在这种情况下,您应该在部署入口控制器之后部署入口,并且您不能使用预建函数为您获取 LoadBalancerAddress。
示例:
self.alb_domain_name = eks.KubernetesObjectValue(
self, 'Query',
cluster=cluster,
object_type='Ingress',
object_name='cluster-ingress', # this is your ingress name
object_namespace='my-ingress-controller', # in which namespace your ingress is deployed
json_path='.status.loadBalancer.ingress[0].hostname' # this json path will get you the hostname for the deployed AWS ELB/ALB
)
在我的特定情况下,我只需要一种方法来定义入口控制器的名称,这样它就不会自动生成。这样做会让我查询入口控制器的 ELB 地址。解决方法是为入口控制器掌舵图提供一个发布名称。部署后,CDK 将 -nginx-ingress 附加到发布名称的末尾,但根据发布名称,您可以计算出 k8s 服务名称。这是一个工作版本:
import * as cdk from '@aws-cdk/core';
import * as eks from '@aws-cdk/aws-eks';
import * as iam from '@aws-cdk/aws-iam';
export class SimpleEks extends cdk.NestedStack {
elbUrl: string;
constructor(scope: cdk.Construct, id: string, props?: cdk.NestedStackProps) {
super(scope, id, props);
const ingressControllerReleaseName = 'ingress-controller'
const clusterAdmin = new iam.Role(this, 'AdminRole', {
assumedBy: new iam.AccountRootPrincipal()
});
const cluster = new eks.Cluster(this, 'cluster', {
clusterName: 'cluster',
mastersRole: clusterAdmin,
version: eks.KubernetesVersion.V1_18,
defaultCapacity: 2,
});
const ingressControllerChart = cluster.addHelmChart('IngressController', {
chart: 'nginx-ingress',
repository: 'https://helm.nginx.com/stable',
release: ingressControllerReleaseName, //This fixes the service name so it's predictable and not auto-generated
});
const albAddress = new eks.KubernetesObjectValue(this, 'elbAddress', {
cluster,
objectType: 'Service',
objectName: `${ingressControllerReleaseName}-nginx-ingress`, //This is what I was missing
jsonPath: '.status.loadBalancer.ingress[0].hostname',
});
// I haven't tried the below code, but I suspect it might work as well as getSvcLBAddress is just a convenience method over `eks.KubernetesObjectValue()`
//const elb = cluster.getServiceLoadBalancerAddress(`${ingressControllerReleaseName}-nginx-ingress`);
const elb = albAddress.value; //This is what I needed to get.
}
}
我正在使用 EKS Construct to create an EKS cluster in CDK. I'm adding the NGINX helm chart to the cluster and I want to export the ingress LoadBalancer URL. The EKS Construct exposes a .getServiceLoadBalancer()
方法,但它需要一个服务名称。我不确定如何获取 LoadBalancer 服务的名称以将其传递给该方法。感觉就像我错过了什么。示例:
export class EksClusterStack extends cdk.NestedStack {
elbUrl: string;
constructor(scope: cdk.Construct, id: string, props?: cdk.NestedStackProps) {
super(scope, id, props);
const clusterAdmin = new iam.Role(this, 'AdminRole', {
assumedBy: new iam.AccountRootPrincipal()
});
const cluster = new eks.Cluster(this, 'Cluster', {
mastersRole: clusterAdmin,
version: eks.KubernetesVersion.V1_18,
defaultCapacity: 2,
});
const nginx = cluster.addHelmChart('NginxIngress', {
chart: 'nginx-ingress',
repository: 'https://helm.nginx.com/stable',
});
this.elbUrl = cluster.getServiceLoadBalancerAddress('{Where do I get the service name?}') //<- This is what I can't figure out
}
我查看了 helm chart 上的属性,它似乎没有暴露任何符合要求的东西。感谢任何见解。谢谢!
您部署的是入口控制器控制图,它本身不公开任何内容,而是扫描任何类型为 Ingress 的 K8S 对象。在这种情况下,您应该在部署入口控制器之后部署入口,并且您不能使用预建函数为您获取 LoadBalancerAddress。
示例:
self.alb_domain_name = eks.KubernetesObjectValue(
self, 'Query',
cluster=cluster,
object_type='Ingress',
object_name='cluster-ingress', # this is your ingress name
object_namespace='my-ingress-controller', # in which namespace your ingress is deployed
json_path='.status.loadBalancer.ingress[0].hostname' # this json path will get you the hostname for the deployed AWS ELB/ALB
)
在我的特定情况下,我只需要一种方法来定义入口控制器的名称,这样它就不会自动生成。这样做会让我查询入口控制器的 ELB 地址。解决方法是为入口控制器掌舵图提供一个发布名称。部署后,CDK 将 -nginx-ingress 附加到发布名称的末尾,但根据发布名称,您可以计算出 k8s 服务名称。这是一个工作版本:
import * as cdk from '@aws-cdk/core';
import * as eks from '@aws-cdk/aws-eks';
import * as iam from '@aws-cdk/aws-iam';
export class SimpleEks extends cdk.NestedStack {
elbUrl: string;
constructor(scope: cdk.Construct, id: string, props?: cdk.NestedStackProps) {
super(scope, id, props);
const ingressControllerReleaseName = 'ingress-controller'
const clusterAdmin = new iam.Role(this, 'AdminRole', {
assumedBy: new iam.AccountRootPrincipal()
});
const cluster = new eks.Cluster(this, 'cluster', {
clusterName: 'cluster',
mastersRole: clusterAdmin,
version: eks.KubernetesVersion.V1_18,
defaultCapacity: 2,
});
const ingressControllerChart = cluster.addHelmChart('IngressController', {
chart: 'nginx-ingress',
repository: 'https://helm.nginx.com/stable',
release: ingressControllerReleaseName, //This fixes the service name so it's predictable and not auto-generated
});
const albAddress = new eks.KubernetesObjectValue(this, 'elbAddress', {
cluster,
objectType: 'Service',
objectName: `${ingressControllerReleaseName}-nginx-ingress`, //This is what I was missing
jsonPath: '.status.loadBalancer.ingress[0].hostname',
});
// I haven't tried the below code, but I suspect it might work as well as getSvcLBAddress is just a convenience method over `eks.KubernetesObjectValue()`
//const elb = cluster.getServiceLoadBalancerAddress(`${ingressControllerReleaseName}-nginx-ingress`);
const elb = albAddress.value; //This is what I needed to get.
}
}