Advanced#
Warning
This part of the Helm Chart is still influx and may change significantly. Use with caution.
This chapter covers init containers, sidecar containers, security contexts, scheduling constraints, and other advanced configuration.
Init Containers#
Init containers run before the main container starts. Define them as a dictionary — the keys determine execution order (sorted alphabetically):
initContainers:
01-wait-for-db:
image: busybox:1.36
command:
- sh
- -c
- "until nc -z postgres 5432; do sleep 2; done"
02-run-migrations:
image: my-app:latest
command:
- ./migrate
- up
env:
DATABASE_URL: "postgres://app:changeme@postgres:5432/app"
Features#
- Keys are sorted alphabetically to determine order (
01-prefix pattern recommended) - Helm templates are supported in the container spec
- Environment variables support the same flexible format as the main container
Additional (Sidecar) Containers#
Add sidecar containers that run alongside the main application:
additionalContainers:
log-shipper:
image: fluent/fluent-bit:3.0
volumeMounts:
- name: shared
mountPath: /shared
env:
FLUENT_OUTPUT: stdout
persistence:
shared:
enabled: true # emptyDir shared between all containers
Nginx Reverse Proxy Sidecar#
additionalContainers:
nginx:
image: nginx:1.27
ports:
- containerPort: 80
volumeMounts:
- name: nginx-config
mountPath: /etc/nginx/conf.d
configmap:
nginx:
enabled: true
data:
default.conf: |
server {
listen 80;
location / {
proxy_pass http://localhost:8080;
}
}
persistence:
nginx-config:
enabled: true
type: configMap
name: "{{ include \"names.fullname\" . }}-nginx"
mountPath: /etc/nginx/conf.d
Security Contexts#
Pod-Level Security Context#
Applies to all containers in the pod:
podSecurityContext:
runAsUser: 1000
runAsGroup: 1000
fsGroup: 1000
runAsNonRoot: true
Container-Level Security Context#
Applies to the main container only:
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
Hardened Example#
podSecurityContext:
runAsNonRoot: true
runAsUser: 65534
fsGroup: 65534
seccompProfile:
type: RuntimeDefault
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
# You'll likely need a writable tmp directory
persistence:
tmp:
enabled: true
type: emptyDir
mountPath: /tmp
Service Accounts#
Create a Service Account#
serviceAccount:
create: true
name: my-app-sa
annotations:
eks.amazonaws.com/role-arn: arn:aws:iam::123456789:role/my-app
Disable Token Auto-Mount#
automountServiceAccountToken: false
Scheduling#
Node Selector#
Run pods only on nodes with specific labels:
nodeSelector:
kubernetes.io/os: linux
node-type: compute
Tolerations#
Allow pods to schedule on tainted nodes:
tolerations:
- key: dedicated
operator: Equal
value: gpu
effect: NoSchedule
Affinity#
Pod Anti-Affinity (Spread Across Nodes)#
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app.kubernetes.io/name
operator: In
values:
- my-app
topologyKey: kubernetes.io/hostname
Node Affinity#
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: topology.kubernetes.io/zone
operator: In
values:
- eu-central-1a
- eu-central-1b
Topology Spread Constraints#
Distribute pods evenly across zones:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app.kubernetes.io/name: my-app
Lifecycle Hooks#
lifecycle:
preStop:
exec:
command:
- /bin/sh
- -c
- "sleep 15" # Allow in-flight requests to drain
postStart:
httpGet:
path: /warm-cache
port: 8080
Termination#
termination:
gracePeriodSeconds: 60
messagePath: /dev/termination-log
messagePolicy: FallbackToLogsOnError
DNS Configuration#
Custom DNS Policy#
dnsPolicy: ClusterFirst
When using hostNetwork, the chart automatically sets dnsPolicy: ClusterFirstWithHostNet.
DNS Config#
Fix nslookup issues or optimize DNS resolution:
dnsConfig:
options:
- name: ndots
value: "1"
- name: single-request-reopen
Host Aliases#
Add custom entries to /etc/hosts:
hostAliases:
- ip: "192.168.1.100"
hostnames:
- "legacy-service.internal"
- "old-api.internal"
Host Network#
Run the pod directly on the host's network stack:
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet # Recommended when using hostNetwork
Priority Class#
Set scheduling priority relative to other pods:
priorityClassName: system-cluster-critical
Runtime Class#
Use an alternative container runtime (e.g., for GPU workloads):
runtimeClassName: nvidia
Custom Hostname#
hostname: my-custom-hostname
Disable Service Links#
Prevent Kubernetes from injecting service-related environment variables:
enableServiceLinks: false
This is recommended when you have many services in a namespace, as the injected variables can be excessive.
Putting It All Together#
A production-ready configuration combining multiple advanced features:
image:
repository: my-registry/my-app
tag: "v2.0.0"
application:
replicas: 3
strategy: RollingUpdate
rollingUpdate:
surge: 1
unavailable: 0
resources:
requests:
cpu: 250m
memory: 256Mi
limits:
cpu: "1"
memory: 1Gi
podSecurityContext:
runAsNonRoot: true
runAsUser: 1000
fsGroup: 1000
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: [ALL]
serviceAccount:
create: true
automountServiceAccountToken: false
enableServiceLinks: false
initContainers:
01-migrations:
image: my-registry/my-app:v2.0.0
command: ["./migrate", "up"]
env:
DATABASE_URL:
valueFrom:
secretKeyRef:
name: db-credentials
key: url
probes:
liveness:
custom: true
spec:
httpGet:
path: /healthz
port: 8080
periodSeconds: 10
readiness:
custom: true
spec:
httpGet:
path: /ready
port: 8080
periodSeconds: 5
service:
main:
ports:
http:
port: 8080
monitor:
enabled: true
endpoints:
- port: http
path: /metrics
ingress:
main:
enabled: true
annotations:
cert-manager.io/cluster-issuer: letsencrypt-prod-http
hosts:
- host: app.example.com
paths:
- path: /
tls:
- secretName: app-tls
hosts:
- app.example.com
networkPolicy:
enabled: true
policyTypes: [Ingress]
ingress:
- ports:
- protocol: TCP
port: 8080
from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: ingress-nginx
persistence:
tmp:
enabled: true
type: emptyDir
mountPath: /tmp
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app.kubernetes.io/name: my-app
termination:
gracePeriodSeconds: 30
lifecycle:
preStop:
exec:
command: ["/bin/sh", "-c", "sleep 10"]
autoscaling:
enabled: true
minReplicas: 3
maxReplicas: 15
targetCPUUtilizationPercentage: 70