Skip to content

Commit

Permalink
Use WebService class
Browse files Browse the repository at this point in the history
  • Loading branch information
atrakic committed Apr 24, 2021
1 parent 6b64673 commit ebb9438
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 34 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/cdk8s-k3d.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
- uses: actions/setup-python@v2
with:
python-version: 3.8

- uses: AbsaOSS/[email protected]
id: k3d-cluster-1
with:
Expand All @@ -34,6 +35,14 @@ jobs:
echo
kubectl cluster-info
- name: Nodes
run: |
docker ps -a
kubectl get nodes -o wide
- name: Network
run: docker network inspect k3d-action-bridge-network

- name: Install CDK8s-cli
run: npm install -g cdk8s-cli

Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
node_modules
dist/
imports/

# Generated
dist/*
30 changes: 21 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
MAKEFLAGS += --silent

.PHONY: up down deploy status test
.PHONY: up import synth deploy status test clean

all: up import synth deploy status test
echo "Doing all"

up:
which cdk8s || scripts/install.sh
which cdk8s || npm install -g cdk8s-cli
k3d cluster ls k3s-default || k3d cluster create --config k3d.yaml
kubectl cluster-info

down:
echo -n "Are you sure? (ctrl+C to abort) "
read _
k3d cluster stop k3s-default
k3d cluster delete k3s-default
import:
pipenv install
cdk8s import --language python

deploy:
synth: ## Create k8s manifest
cdk8s synth

deploy:
kubectl apply -f ./dist/hello.k8s.yaml

status:
kubectl get svc,po
kubectl get svc,po -o wide

test:
[ -f ./test/test.sh ] && ./test/test.sh

clean:
echo -n "Are you sure? (Press Enter to continue or Ctrl+C to abort) "
read _
k3d cluster stop k3s-default
k3d cluster delete k3s-default
pipenv clean
rm -rf ./dist ./imports

-include include.mk
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,26 @@ $ cdk8s synth
```console
$ kubectl apply -f dist/hello.k8s.yaml
```

## Demo


```console
make status
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 2m40s <none>
service/hello-service-c8685ad9 LoadBalancer 10.43.174.4 192.168.240.2,192.168.240.3 80:32694/TCP 2m8s app=hello

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/svclb-hello-service-c8685ad9-mvrxw 1/1 Running 0 2m8s 10.42.1.4 k3d-k3s-default-agent-0 <none> <none>
pod/svclb-hello-service-c8685ad9-kg2pk 1/1 Running 0 2m8s 10.42.0.5 k3d-k3s-default-server-0 <none> <none>
pod/hello-deployment-c8aab50d-586bd8cf75-rs827 1/1 Running 0 2m8s 10.42.0.3 k3d-k3s-default-server-0 <none> <none>
pod/hello-deployment-c8aab50d-586bd8cf75-6d967 1/1 Running 0 2m8s 10.42.0.4 k3d-k3s-default-server-0 <none> <none>
```

For more details see: [Demo](https://github.com/atrakic/k3d-cdk8s-demo/actions),
[Source gh-action](./.github/workflows/cdk8s-k3d.yml), [K3d config](./k3d.yaml)


## Credits
https://github.com/cdk8s-team/cdk8s/tree/master/examples/python/web-service
23 changes: 4 additions & 19 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,15 @@
from cdk8s import App, Chart

from imports import k8s
from webservice import WebService

class MyChart(Chart):
def __init__(self, scope: Construct, ns: str):
super().__init__(scope, ns)

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

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

k8s.KubeDeployment(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)])]))))
WebService(self, "hello",
image="paulbouwer/hello-kubernetes:1.7",
replicas=2)

app = App()
MyChart(app, "hello")
Expand Down
3 changes: 0 additions & 3 deletions scripts/install.sh

This file was deleted.

29 changes: 29 additions & 0 deletions webservice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3
from constructs import Construct, Node

from imports import k8s

class WebService(Construct):

def __init__(self, scope: Construct, id: str, *, image: str, replicas=1, port=80, containerPort=8080, **kwargs):
super().__init__(scope, id)

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

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

k8s.KubeDeployment(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="web",
image=image,
ports=[k8s.ContainerPort(container_port=containerPort)])]))))

0 comments on commit ebb9438

Please sign in to comment.