Creating a simple Helm Chart
A Helm chart is a package that contains all the resources needed to run an application on a Kubernetes cluster. Helm charts are stored in a repository, which is a collection of charts. In this section, we will create a simple Helm chart for a basic web application.
Here are the steps to create a simple Helm chart:
- Create a new directory for your chart. For example:
shellCopy code$ mkdir mychart $ cd mychart
- Initialize the chart by running the following command:
luaCopy code$ helm create .
This command generates the basic directory structure for a Helm chart and creates a file named Chart.yaml that contains the chart's metadata.
- Next, we need to specify the resources that the chart needs to run our application. To do this, we will create a YAML file for each resource we want to deploy. For example, let's create a file called
templates/deployment.yamlfor a Kubernetes Deployment resource:
yamlCopy codeapiVersion: apps/v1 kind: Deployment metadata: name: {{ .Release.Name }}-deployment labels: app: {{ .Release.Name }} spec: replicas: 1 selector: matchLabels: app: {{ .Release.Name }} template: metadata: labels: app: {{ .Release.Name }} spec: containers: - name: {{ .Release.Name }} image: nginx:alpine ports: - containerPort: 80
This YAML file specifies a Deployment resource that will run a single replica of an nginx container on port 80.
- Next, let's create a Service resource that will expose the deployment. Create a file called
templates/service.yaml:
yamlCopy codeapiVersion: v1 kind: Service metadata: name: {{ .Release.Name }}-service labels: app: {{ .Release.Name }} spec: selector: app: {{ .Release.Name }} ports: - name: http port: 80 targetPort: 80 type: ClusterIP
This YAML file creates a Service resource that will expose the Deployment on port 80 with a ClusterIP type, which makes the service only accessible within the cluster.
- Finally, we need to tell Helm to use these templates. Open the
Chart.yamlfile and add the following to thetemplatessection:
yamlCopy codetemplates: - deployment.yaml - service.yaml
- That's it! You have created a simple Helm chart that can be used to deploy a basic web application to a Kubernetes cluster. To test the chart, run the following command:
cssCopy code$ helm install --name myrelease .
This command installs the chart in your cluster with the name myrelease.
You can use the helm list command to see the releases installed in your cluster and the helm delete command to delete a release.
Leave a Comment