/kubernetes-sidecar-injector

A Kubernetes mutating admission webhook server to inject busybox sidecar container.

Primary LanguageTypeScript

Kubernetes Sidecar Injector

This repo contains the complete code for my Kubernetes Sidecar Container Injection Medium article to create a Kubernetes mutating admission controller that injects a busybox-curl sidecar container into pods.

Requirments

Deploying

  1. Install the kubernetes-sidecar-injector chart
helm install kubernetes-sidecar-injector charts/kubernetes-sidecar-injector/ \
--values charts/kubernetes-sidecar-injector/values.yaml \
--namespace default
  1. Install the httpbin chart
helm install httpbin charts/httpbin/ \
--values charts/httpbin/values.yaml \
--namespace default
  1. Listing all containers in the httpbin Deployment's Pod, you can notice that a new container is running in it named curl.
export POD_NAME=$(kubectl get pods \
--namespace default \
-l "app.kubernetes.io/name=httpbin,app.kubernetes.io/instance=httpbin" \
-o jsonpath="{.items[0].metadata.name}")

kubectl get pods $POD_NAME \
--namespace default \
-o jsonpath='{.spec.containers[*].name}'
  1. Accessing the httpbin HTTP server from inside the curl container.
export POD_NAME=$(kubectl get pods \
--namespace default \
-l "app.kubernetes.io/name=httpbin,app.kubernetes.io/instance=httpbin" \
-o jsonpath="{.items[0].metadata.name}")

kubectl exec $POD_NAME \
--namespace default \
-c curl \
-- curl http://localhost/anything