Skip to content

Commit

Permalink
chore(infra): add k8s manifests
Browse files Browse the repository at this point in the history
  • Loading branch information
ds2 committed Jul 18, 2024
1 parent 88e2326 commit 2901de2
Show file tree
Hide file tree
Showing 10 changed files with 348 additions and 0 deletions.
26 changes: 26 additions & 0 deletions infra/k8s/api-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: snack-bar-api
namespace: snack-bar
spec:
replicas: 1
selector:
matchLabels:
app: snack-bar-api
template:
metadata:
labels:
app: snack-bar-api
spec:
containers:
- name: snack-bar-api
image: deborasilveira/snack-bar-api:latest
ports:
- containerPort: 3000
env:
- name: DATABASE_URL
value: "postgresql://root:root@snack-bar-db:5432/snack-api-db"
resources:
requests:
cpu: 100m
12 changes: 12 additions & 0 deletions infra/k8s/api-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Service
metadata:
name: snack-bar-api
namespace: snack-bar
spec:
selector:
app: snack-bar-api
ports:
- port: 3000
targetPort: 3000
type: LoadBalancer
34 changes: 34 additions & 0 deletions infra/k8s/db-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: snack-bar-db
namespace: snack-bar
spec:
replicas: 1
selector:
matchLabels:
app: snack-bar-db
template:
metadata:
labels:
app: snack-bar-db
spec:
containers:
- env:
- name: POSTGRES_DB
value: "snack-api-db"
- name: POSTGRES_USER
value: "root"
- name: POSTGRES_PASSWORD
value: "root"
name: snack-bar-db
image: postgres:12.19
ports:
- containerPort: 5432
volumeMounts:
- name: snack-bar-db-storage
mountPath: /var/lib/postgresql/data
volumes:
- name: snack-bar-db-storage
persistentVolumeClaim:
claimName: snack-bar-db-pvc
13 changes: 13 additions & 0 deletions infra/k8s/db-pv.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: PersistentVolume
metadata:
name: snack-bar-db-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: standard
hostPath:
path: /mnt/data/snack-bar-db
12 changes: 12 additions & 0 deletions infra/k8s/db-pvc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: snack-bar-db-pvc
namespace: snack-bar
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: standard
11 changes: 11 additions & 0 deletions infra/k8s/db-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: snack-bar-db
namespace: snack-bar
spec:
selector:
app: snack-bar-db
ports:
- port: 5432
targetPort: 5432
19 changes: 19 additions & 0 deletions infra/k8s/hpa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: snack-bar-api-hpa
namespace: snack-bar
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: snack-bar-api
minReplicas: 1
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
201 changes: 201 additions & 0 deletions infra/k8s/metrics.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
k8s-app: metrics-server
rbac.authorization.k8s.io/aggregate-to-admin: "true"
rbac.authorization.k8s.io/aggregate-to-edit: "true"
rbac.authorization.k8s.io/aggregate-to-view: "true"
name: system:aggregated-metrics-reader
rules:
- apiGroups:
- metrics.k8s.io
resources:
- pods
- nodes
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
k8s-app: metrics-server
name: system:metrics-server
rules:
- apiGroups:
- ""
resources:
- nodes/metrics
verbs:
- get
- apiGroups:
- ""
resources:
- pods
- nodes
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
labels:
k8s-app: metrics-server
name: metrics-server-auth-reader
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: extension-apiserver-authentication-reader
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: metrics-server
name: metrics-server:system:auth-delegator
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:auth-delegator
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: metrics-server
name: system:metrics-server
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: system:metrics-server
subjects:
- kind: ServiceAccount
name: metrics-server
namespace: kube-system
---
apiVersion: v1
kind: Service
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
spec:
ports:
- name: https
port: 443
protocol: TCP
targetPort: https
selector:
k8s-app: metrics-server
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s-app: metrics-server
name: metrics-server
namespace: kube-system
spec:
selector:
matchLabels:
k8s-app: metrics-server
strategy:
rollingUpdate:
maxUnavailable: 0
template:
metadata:
labels:
k8s-app: metrics-server
spec:
containers:
- args:
- --cert-dir=/tmp
- --secure-port=10250
- --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
- --kubelet-use-node-status-port
- --metric-resolution=15s
image: registry.k8s.io/metrics-server/metrics-server:v0.7.1
imagePullPolicy: IfNotPresent
livenessProbe:
failureThreshold: 3
httpGet:
path: /livez
port: https
scheme: HTTPS
periodSeconds: 10
name: metrics-server
ports:
- containerPort: 10250
name: https
protocol: TCP
readinessProbe:
failureThreshold: 3
httpGet:
path: /readyz
port: https
scheme: HTTPS
initialDelaySeconds: 20
periodSeconds: 10
resources:
requests:
cpu: 100m
memory: 200Mi
securityContext:
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 1000
seccompProfile:
type: RuntimeDefault
volumeMounts:
- mountPath: /tmp
name: tmp-dir
nodeSelector:
kubernetes.io/os: linux
priorityClassName: system-cluster-critical
serviceAccountName: metrics-server
volumes:
- emptyDir: {}
name: tmp-dir
---
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
labels:
k8s-app: metrics-server
name: v1beta1.metrics.k8s.io
spec:
group: metrics.k8s.io
groupPriorityMinimum: 100
insecureSkipTLSVerify: true
service:
name: metrics-server
namespace: kube-system
version: v1beta1
versionPriority: 100
16 changes: 16 additions & 0 deletions infra/k8s/migration-job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: batch/v1
kind: Job
metadata:
name: migration-job
namespace: snack-bar
spec:
template:
spec:
containers:
- name: migration-job
image: deborasilveira/snack-bar-api:latest
command: ["npx", "prisma", "migrate", "deploy"]
env:
- name: DATABASE_URL
value: "postgresql://root:root@snack-bar-db:5432/snack-api-db"
restartPolicy: OnFailure
4 changes: 4 additions & 0 deletions infra/k8s/namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: snack-bar

0 comments on commit 2901de2

Please sign in to comment.