Skip to content

Instantly share code, notes, and snippets.

@JoelSpeed
Created June 23, 2020 14:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoelSpeed/a4e160a18402b04f13d799b0d51ad465 to your computer and use it in GitHub Desktop.
Save JoelSpeed/a4e160a18402b04f13d799b0d51ad465 to your computer and use it in GitHub Desktop.
Simulate spot termination event
apiVersion: batch/v1
kind: Job
metadata:
name: simulate-termination
spec:
template:
spec:
containers:
- name: iptables
image: alpine
command: ["/bin/sh", "-c"]
args:
- >
apk update && apk add iptables bind-tools;
export SERVICE_IP=$(dig +short spot-termination-simulator.${NAMESPACE}.svc.cluster.local);
if [ -z ${SERVICE_IP} ]; then echo "No service IP"; exit 1; fi;
iptables-nft -t nat -A OUTPUT -p tcp -d 169.254.169.254 -j DNAT --to-destination ${SERVICE_IP}:8082;
iptables-nft -t nat -A POSTROUTING -j MASQUERADE;
echo "Redirected metadata service to ${SERVICE_IP}:8082";
env:
- name: NAMESPACE
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
securityContext:
privileged: true
capabilities:
add:
- NET_ADMIN
- NET_RAW
volumeMounts:
- mountPath: /run/xtables.lock
name: xtables-lock
readOnly: false
- mountPath: /lib/modules
name: lib-modules
readOnly: true
restartPolicy: OnFailure
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet
nodeSelector:
machine.openshift.io/interruptible-instance: ""
serviceAccountName: simulate-termination
volumes:
- name: xtables-lock
hostPath:
path: /run/xtables.lock
type: FileOrCreate
- name: lib-modules
hostPath:
path: /lib/modules
backoffLimit: 4
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: simulate-termination
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: simulate-termination
subjects:
- kind: ServiceAccount
name: simulate-termination
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: simulate-termination
rules:
- apiGroups:
- security.openshift.io
resourceNames:
- privileged
resources:
- securitycontextconstraints
verbs:
- use
---
apiVersion: "v1"
kind: ServiceAccount
metadata:
name: simulate-termination
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: spot-termination-simulator
name: spot-termination-simulator
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: spot-termination-simulator
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: spot-termination-simulator
spec:
containers:
- image: shoganator/ec2-spot-termination-simulator:1.0.1
imagePullPolicy: Always
name: spot-termination-simulator
env:
- name: PORT
value: "8082"
dnsPolicy: ClusterFirst
restartPolicy: Always
terminationGracePeriodSeconds: 1
---
apiVersion: v1
kind: Service
metadata:
labels:
app: spot-termination-simulator
name: spot-termination-simulator
spec:
ports:
- name: http
port: 8082
protocol: TCP
targetPort: 8082
selector:
app: spot-termination-simulator
sessionAffinity: None
clusterIP: None
type: ClusterIP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment