CDK8s is a software development framework for defining Kubernetes applications and reusable abstractions using familiar programming languages and rich object-oriented APIs.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- image: my-app:1.0
name: my-app
class MyChart(Chart):
def __init__(self, scope: Construct, id: str):
super().__init__(scope, id)
label = {"app": "my-app"}
k8s.KubeDeployment(self,
'my-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='my-app',
image='my-app:1.0'
)
])
)
)
)
app = App()
MyChart(app, "my-app")
app.synth()
export class WebService extends Construct {
constructor(scope: Construct,
id: string,
options: WebServiceOptions) {
super(scope, id);
const port = options.port || 80;
const containerPort = options.containerPort || 8080;
const label = { app: Names.toLabelValue(this) };
const replicas = options.replicas ?? 1;
new KubeService(this, 'service', {
...
});
new KubeDeployment(this, 'deployment', {
...
});
}
}
$ cdk8s init python-app
Initializing a project from the python-app template
...
=========================================================
Your cdk8s Python project is ready!
cat help Prints this message
cdk8s synth Synthesize k8s manifests to dist/
cdk8s import Imports k8s API objects to "imports/k8s"
Deploy:
kubectl apply -f dist/*.k8s.yaml
=========================================================
$ ls
cdk8s.yaml dist help imports main.py Pipfile Pipfile.lock
$ vi main.py
$ cdk8s synth
dist/my-app.k8s.yaml
$ kubectl apply -f dist/my-app.k8s.yaml
$ cdk8s synth -p | kubectl apply -f -
class MyChart extends cdk8s.Chart {
constructor(scope: Construct, id: string) {
super(scope, id);
const redis = new Helm(this, 'redis', {
chart: 'bitnami/redis',
values: {
sentinel: {
enabled: true
}
}
});
}
}
const master = redis.apiObjects
.find(o => o.name === 'my-redis-master');
master.metadata.addAnnotation(
'my.annotation', 'hey-there'
);
const chart = new cdk8s.Chart(app, 'my-chart');
const appData = new kplus.ConfigMap(chart, 'AppData');
appData.addDirectory(path.join(__dirname, 'app'));
const appVolume = kplus.Volume.fromConfigMap(appData);
const deployment = new kplus.Deployment(chart,
'Deployment',
{ replicas: 3 }
);
const appPath = '/var/lib/app';
const port = 80;
const container = deployment.addContainer({
image: 'node:14.4.0-alpine3.12',
command: ['node', 'index.js', `${port}`],
port: port,
workingDir: appPath,
});
container.mount(appPath, appVolume);
deployment.expose(8080,
{serviceType: kplus.ServiceType.LOAD_BALANCER}
)
final Map<String, String> selector = new HashMap<>();
selector.put("app", "hello-k8s");
final List<ServicePort> servicePorts = new ArrayList<>();
final ServicePort servicePort = new ServicePort.Builder()
.port(80)
.targetPort(IntOrString.fromNumber(8080))
.build();
servicePorts.add(servicePort);
final ServiceSpec serviceSpec = new ServiceSpec.Builder()
.type("LoadBalancer")
.selector(selector)
.ports(servicePorts)
.build();
final KubeServiceProps serviceProps =
new KubeServiceProps.Builder()
.spec(serviceSpec)
.build();
new KubeService(this, "service", serviceProps);
$ cdk8s init golang-app
Give your vote!
open questions?
↓