Example #1
0
    def __init__(self, scope: Construct, ns: str):
        super().__init__(scope, ns)

        label = {"app": "guestbook-ui"}

        k8s.Service(self,
                    'service',
                    spec=k8s.ServiceSpec(
                        type='LoadBalancer',
                        ports=[
                            k8s.ServicePort(
                                port=80,
                                target_port=k8s.IntOrString.from_number(80))
                        ],
                        selector=label))

        k8s.Deployment(
            self,
            'deployment',
            spec=k8s.DeploymentSpec(
                replicas=1,
                selector=k8s.LabelSelector(match_labels=label),
                template=k8s.PodTemplateSpec(
                    metadata=k8s.ObjectMeta(labels=label),
                    spec=k8s.PodSpec(containers=[
                        k8s.Container(
                            name='guestbook-ui',
                            image='gcr.io/heptio-images/ks-guestbook-demo:0.2',
                            ports=[k8s.ContainerPort(container_port=80)])
                    ]))))
Example #2
0
    def __init__(self, scope: Construct, ns: str):
        super().__init__(scope, ns)

        label = {"app": "hello-k8s"}

        k8s.Service(self,
                    'service',
                    spec=k8s.ServiceSpec(
                        type='LoadBalancer',
                        ports=[
                            k8s.ServicePort(
                                port=80,
                                target_port=k8s.IntOrString.from_number(8080))
                        ],
                        selector=label))

        k8s.Deployment(
            self,
            'deployment',
            spec=k8s.DeploymentSpec(
                replicas=2,
                selector=k8s.LabelSelector(match_labels=label),
                template=k8s.PodTemplateSpec(
                    metadata=k8s.ObjectMeta(labels=label),
                    spec=k8s.PodSpec(containers=[
                        k8s.Container(
                            name='hello-kubernetes',
                            image='paulbouwer/hello-kubernetes:1.7',
                            ports=[k8s.ContainerPort(container_port=8080)])
                    ]))))
Example #3
0
    def __init__(self, scope: Construct, ns: str, *,
                 image: str,
                 replicas: typing.Optional[int] = 1,
                 port: typing.Optional[int] = 80,
                 container_port: typing.Optional[int] = 8000):
        super().__init__(scope, ns)

        label = {'app': Node.of(self).unique_id}

        k8s.Service(self, 'service',
                    spec=k8s.ServiceSpec(
                        type='NodePort',
                        ports=[k8s.ServicePort(port=port, target_port=k8s.IntOrString.from_number(container_port))],
                        selector=label))

        k8s.Deployment(self, 'deployment',
                       spec=k8s.DeploymentSpec(
                           replicas=replicas,
                           selector=k8s.LabelSelector(match_labels=label),
                           template=k8s.PodTemplateSpec(
                               metadata=k8s.ObjectMeta(labels=label),
                               spec=k8s.PodSpec(
                                   containers=[
                                       k8s.Container(
                                           name='app',
                                           image=image,
                                            ports=[k8s.ContainerPort(container_port=container_port)])]))))
Example #4
0
    def __init__(self, scope: Construct, id: str):
        super().__init__(scope, id, namespace='dev')

        app_name = 'aws-test-sa-default'
        app_label = {'app': app_name}
        k8s.KubeDeployment(
            self,
            f"Aws-TestDeployment-SA-Default",
            metadata=k8s.ObjectMeta(labels=app_label, name=app_name),
            spec=k8s.DeploymentSpec(
                replicas=1,
                selector=k8s.LabelSelector(match_labels=app_label),
                template=k8s.PodTemplateSpec(
                    metadata=k8s.ObjectMeta(labels=app_label),
                    spec=k8s.PodSpec(containers=[
                        k8s.Container(name=app_name,
                                      image="mikesir87/aws-cli:v1",
                                      command=["sleep 60"])
                    ]))))

        app_name = 'aws-test-iam-sa'
        app_label = {'app': app_name}
        k8s.KubeDeployment(
            self,
            f"Aws-TestDeployment-IAM-SA",
            metadata=k8s.ObjectMeta(labels=app_label, name=app_name),
            spec=k8s.DeploymentSpec(
                replicas=1,
                selector=k8s.LabelSelector(match_labels=app_label),
                template=k8s.PodTemplateSpec(
                    metadata=k8s.ObjectMeta(labels=app_label),
                    spec=k8s.PodSpec(service_account_name='sel-eks-sa',
                                     containers=[
                                         k8s.Container(
                                             name=app_name,
                                             image="mikesir87/aws-cli:v1",
                                             command=["sleep 60"])
                                     ]))))
Example #5
0
    def __init__(self,
                 scope: Construct,
                 ns: str,
                 *,
                 image: str,
                 replicas: int = 1,
                 port: int = 80,
                 container_port: int = 8080):
        super().__init__(scope, ns)

        label = {"app": Node.of(self).unique_id}

        k8s.Service(
            self,
            "service",
            spec=k8s.ServiceSpec(
                type="LoadBalancer",
                ports=[
                    k8s.ServicePort(
                        port=port,
                        target_port=k8s.IntOrString.from_number(
                            container_port),
                    )
                ],
                selector=label,
            ),
        )

        k8s.Deployment(
            self,
            "deployment",
            spec=k8s.DeploymentSpec(
                replicas=replicas,
                selector=k8s.LabelSelector(match_labels=label),
                template=k8s.PodTemplateSpec(
                    metadata=k8s.ObjectMeta(labels=label),
                    spec=k8s.PodSpec(containers=[
                        k8s.Container(
                            name="app",
                            image=image,
                            ports=[
                                k8s.ContainerPort(
                                    container_port=container_port)
                            ],
                        )
                    ]),
                ),
            ),
        )
Example #6
0
  def render_k8s_resource(self, chart: Chart) -> Deployment:

    # Create the metadata and label selectors for the deployment
    object_meta = k8s.ObjectMeta(labels=self.labels)
    label_selector = k8s.LabelSelector(match_labels=self.match_labels)

    # Generate the podspec templates for the deployment
    podspec = k8s.PodSpec(init_containers=self.init_containers.render(chart),
                          image_pull_secrets=self.image_pull_secrets,
                          containers=self.containers.render(chart),
                          volumes=self.volumes.render(chart))

    podspec_template = k8s.PodTemplateSpec(metadata=object_meta,
                                           spec=podspec)

    # Use the podspec to create the deployment spec before finally returning the completed K8s Deployment.
    deployment_spec = k8s.DeploymentSpec(replicas=self.replicas, selector=label_selector, template=podspec_template)
    k8s.Deployment(chart, 'deployment', metadata=k8s.ObjectMeta(name=self.name), spec=deployment_spec)
    return self
Example #7
0
    def __init__(self, scope: Construct, ns: str):
        super().__init__(scope, ns)

        labels = {"app": "cdk8s-python"}

        k8s.Service(
            self,
            'service',
            spec=k8s.ServiceSpec(
                type='LoadBalancer',
                ports=[
                    k8s.ServicePort(
                        port=80,
                        target_port=k8s.IntOrString.from_number(80)
                    ),
                ],
                selector=labels,
            ),
        )

        k8s.Deployment(
            self,
            'deployment',
            spec=k8s.DeploymentSpec(
                replicas=2,
                selector=k8s.LabelSelector(match_labels=labels),
                template=k8s.PodTemplateSpec(
                    metadata=k8s.ObjectMeta(labels=labels),
                    spec=k8s.PodSpec(
                        containers=[
                            k8s.Container(
                                name=ns,
                                image='nginxdemos/hello',
                                ports=[
                                    k8s.ContainerPort(container_port=80),
                                ],
                            ),
                        ],
                    ),
                ),
            ),
        )