Inject custom scripts to k8s pod

Inject custom scripts to k8s pod

Injecting a script to a pod to debug issues is a common need of Kubernetes administrators. Configmaps can help here. ConfigMaps are Kubernetes objects that allow you to store configuration data separately from the application code. This separation of concerns makes it easier to manage and update configuration data without modifying the application code.

In this article, we will walk you through the steps required to inject a script into a pod using ConfigMap.

Step 1: Create a ConfigMap

The first step is to create a ConfigMap that contains the script you want to inject. You can create a ConfigMap by creating a YAML file or by using the kubectl command-line tool. Here's an example YAML file that creates a ConfigMap with a script:

apiVersion: v1
kind: ConfigMap
metadata:
? name: my-script-configmap
data:
? my-script.sh: |
? ? #!/bin/bash
? ? echo "Hello, World!"        

The above YAML file creates a ConfigMap called "my-script-configmap" with a single key-value pair. The key is "my-script.sh" and the value is a Bash script that prints "Hello, World!" to the console.

To create the ConfigMap using the YAML file, run the following command:

kubectl apply -f my-script-configmap.yaml        

Step 2: Define a Volume Mount

The next step is to define a Volume Mount for the ConfigMap in the pod specification. You can do this by adding a "volumeMounts" section to the container specification in the pod YAML file. Here's an example YAML file that defines a Volume Mount for the ConfigMap:

apiVersion: v1
kind: Pod
metadata:
? name: my-pod
spec:
? containers:
? - name: my-container
? ? image: alpine
? ? volumeMounts:
? ? - name: my-script-volume
? ? ? mountPath: /script
    command: ["/script/my-script.sh"]
? volumes:
? - name: my-script-volume
? ? configMap:
? ? ? name: my-script-configmap
? ?   defaultMode: 0744        

The above YAML file creates a pod called "my-pod" with a single container. The container runs the "alpine" image and has a Volume Mount called "my-script-volume" that mounts the ConfigMap at the path "/script". The pod specification also defines a Volume called "my-script-volume" that is backed by the ConfigMap "my-script-configmap" which includes a single key-value pair called "my-script.sh".

This script can also be stored as an env variable and call that variable to run the script.

apiVersion: v1
kind: Pod
metadata:
? name: my-pod
spec:
? containers:
? - name: my-container
? ? image: alpine
? ? env:
? ? - name: MY_SCRIPT
? ? ? valueFrom:
? ? ? ? configMapKeyRef:
? ? ? ? ? name: my-script-configmap
? ? ? ? ? key: my-script.sh
    command: ["/bin/sh"]
    args: ["-c", "$(MY_SCRIPT)"]        

Step 3: Test the Injection

The final step is to test the injection of the script into the pod. You can do this by running the pod and checking the logs of the container. Here's an example command that creates the pod and streams the logs of the container:

kubectl logs -f my-pod -c my-container        

After running the command, you should see the message "Hello, World!" in the console output.

Conclusion

Injecting a script to a pod using ConfigMap is a straightforward process in Kubernetes deployments. By storing configuration data separately, you can debug issues using custom scripts without modifying the Dockerfile for the image, build, push and use a new image.

Anish Nagaraj Dhandayuthapani

Tech Lead, Platform Architect and Solution Architect at HCL Technologies

1 年

I can see the practical use cases for this! Well written Shafeeque.

回复
Albin Antony

Devops || Docker || Kubernetes || AWS || CI/CD || Gitlab ci || Jenkins || Helm || Shell script || Terraform || DataDog || Prometheus and Grafana || ArgoCD || SonarQube || Nexus

1 年

Superb. ??

要查看或添加评论,请登录

社区洞察

其他会员也浏览了