Skip to content

Instantly share code, notes, and snippets.

@miguel-amaral
Last active March 4, 2020 18:14
Show Gist options
  • Save miguel-amaral/8b7912256d05c6627a6f1b6753689b43 to your computer and use it in GitHub Desktop.
Save miguel-amaral/8b7912256d05c6627a6f1b6753689b43 to your computer and use it in GitHub Desktop.
{%- set mandatory_vars = [prometheus_metrics, application_port, probe_port, env, force_pod_disruption_budget, args, command, app_url] -%}
---
apiVersion: v1
kind: Service
metadata:
name: "{{ app_name }}-service"
namespace: "{{ namespace }}"
labels:
app: "{{ app_name }}"
spec:
selector:
app: "{{ app_name }}"
ports:
- name: http
protocol: TCP
port: {{ application_port }}
targetPort: {{ application_port }}
type: ClusterIP
---
apiVersion: vaultproject.io/v1
kind: SecretClaim
metadata:
name: "{{ app_name }}-{{ config_version }}-envvars"
namespace: "{{ namespace }}"
spec:
type: Opaque
path: "secret/app/appconfig/{{ vault_namespace }}/{{ app_name }}/{{ config_version }}"
renew: 3600
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: "{{ app_name }}"
namespace: "{{ namespace }}"
labels:
app: "{{ app_name }}"
annotations:
kubernetes.io/change-cause: "{{ change_cause }}"
spec:
selector:
matchLabels:
app: "{{ app_name }}"
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25
maxUnavailable: 0
template:
metadata:
name: "{{ app_name }}"
namespace: "{{ namespace }}"
labels:
app: "{{ app_name }}"
{%- if prometheus_metrics == "true" %}
metrics: "true"
metrics_port: "{{ metrics_port | default(666) }}"
{%- endif %}
spec:
affinity:
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
podAffinityTerm:
topologyKey: kubernetes.io/hostname
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- {{ app_name }}
terminationGracePeriodSeconds: 60
containers:
- name: "{{ app_name }}"
image: "{{ docker_image }}"
lifecycle:
preStop:
exec:
command: [
"sh", "-c",
# Introduce a delay to the shutdown sequence to wait for the
# pod eviction event to propagate. Then, gracefully shutdown
# pod.
"sleep 30",
]
imagePullPolicy: IfNotPresent
ports:
- name: service
containerPort: {{ application_port }}
{%- if application_port != probe_port %}
- name: probe
containerPort: {{ probe_port }}
{%- endif %}
env:
- name: DYNO
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
envFrom:
- secretRef:
name: "{{ app_name }}-{{ config_version }}-envvars"
# this should be reviewed as we are forcing JAVA_OPTS in JVM templates
command: {{ command }}
args: {{ args }}
livenessProbe:
httpGet:
path: {{ liveness_uri | default("/live") }}
{%- if application_port != probe_port %}
port: probe
{%- else %}
port: service
{%- endif %}
initialDelaySeconds: 60
periodSeconds: 5
timeoutSeconds: 3
successThreshold: 1
failureThreshold: 3
readinessProbe:
httpGet:
path: {{ readiness_uri | default("/ready") }}
{%- if application_port != probe_port %}
port: probe
{%- else %}
port: service
{%- endif %}
initialDelaySeconds: 50
periodSeconds: 5
timeoutSeconds: 3
successThreshold: 1
failureThreshold: 3
resources:
requests:
cpu: "{{ cpu_resources_requests | default(250) }}m"
memory: "{{ memory_resources_requests | default(1024) }}Mi"
limits:
cpu: "{{ cpu_resources_limits | default(1000) }}m"
memory: "{{ memory_resources_limits | default(1024) }}Mi"
{%- if env == "prd" or force_pod_disruption_budget == "true" %}
---
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
name: "{{ app_name }}"
namespace: "{{ namespace }}"
spec:
minAvailable: 1
selector:
matchLabels:
app: "{{ app_name }}"
{%- endif %}
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: "{{ app_name }}"
namespace: "{{ namespace }}"
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: "{{ app_name }}"
minReplicas: {{ min_replicas | default(1) }}
maxReplicas: {{ max_replicas | default(1) }}
targetCPUUtilizationPercentage: {{ hpa_cpu_utilization | default(70) }}
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "nginx"
name: "{{ app_name }}"
namespace: "{{ namespace }}"
spec:
rules:
- host: "{{ app_url }}.{{ cluster_hostname }}"
http:
paths:
- backend:
serviceName: "{{ app_name }}-service"
servicePort: {{ application_port }}
path: /
- host: "{{ app_url }}{{ app_url_suffix }}"
http:
paths:
- backend:
serviceName: "{{ app_name }}-service"
servicePort: {{ application_port }}
path: /
tls:
- hosts:
- "{{ app_url }}.{{ cluster_hostname }}"
secretName: "wildcard-{{ env }}-cert-tls"
- hosts:
- "{{ app_url }}{{ app_url_suffix }}"
secretName: "wildcard-{{ env }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment