- Firstly, download and install KCL according to the instructions, and then prepare a Kubernetes environment.
We can run the following command to show the config.
cat main.kThe output is
import .app
app.App {
name = "app"
containers.nginx = {
image = "nginx"
ports = [{containerPort = 80}]
}
service.ports = [{ port = 80 }]
}In the above code, we defined a configuration using the App schema, where we configured an nginx container and configured it with an 80 service port.
Besides, KCL allows developers to define the resources required for their applications in a declarative manner and is tied to a platform such as Docker Compose or Kubernetes manifests and allows to generate a platform-specific configuration file such as docker-compose.yaml or a Kubernetes manifests.yaml file. Next, let's generate the corresponding configuration.
If we want to transform the application config into the docker compose config, we can run the command simply:
kcl main.k docker_compose_render.kThe output is
services:
app:
image: nginx
ports:
- published: 80
target: 80
protocol: TCPIf we want to transform the application config into the Kubernetes manifests, we can run the command simply:
kcl main.k kubernetes_render.kThe output is
apiVersion: apps/v1
kind: Deployment
metadata:
name: app
labels:
app: app
spec:
replicas: 1
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: nginx
image: nginx
ports:
- protocol: TCP
containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: app
labels:
app: app
spec:
selector:
app: app
ports:
- port: 80
protocol: TCP