diff --git a/k8s/api-deployment.yaml b/k8s/api-deployment.yaml new file mode 100644 index 0000000..2351b5a --- /dev/null +++ b/k8s/api-deployment.yaml @@ -0,0 +1,50 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: api-deployment + labels: + app: api-deployment +spec: + replicas: 1 + selector: + matchLabels: + app: api-deployment + template: + metadata: + labels: + app: api-deployment + spec: + containers: + - name: app + env: + - name: DATABASE_URL + value: "jdbc:postgresql://postgres-svc:5432/fastfood" + - name: SPRING_PROFILES_ACTIVE + value: "dev" + image: otaviodocker/api-fastfood:latest + imagePullPolicy: Always + ports: + - containerPort: 8080 + livenessProbe: + tcpSocket: + port: 8080 + initialDelaySeconds: 40 + periodSeconds: 30 + timeoutSeconds: 10 + failureThreshold: 2 + readinessProbe: + httpGet: + path: /swagger-ui/index.html + port: 8080 + initialDelaySeconds: 50 + periodSeconds: 30 + timeoutSeconds: 10 + failureThreshold: 2 + resources: + requests: + cpu: "20m" + memory: "500Mi" + limits: + cpu: "500m" + memory: "3Gi" + restartPolicy: Always \ No newline at end of file diff --git a/k8s/api-hpa.yaml b/k8s/api-hpa.yaml new file mode 100644 index 0000000..b449f08 --- /dev/null +++ b/k8s/api-hpa.yaml @@ -0,0 +1,37 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: api-hpa +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: api-deployment + minReplicas: 3 + maxReplicas: 10 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 80 + - type: Resource + resource: + name: memory + target: + type: Utilization + averageUtilization: 80 + behavior: + scaleUp: + stabilizationWindowSeconds: 30 + policies: + - type: Percent + value: 80 + periodSeconds: 10 + scaleDown: + stabilizationWindowSeconds: 30 + policies: + - type: Percent + value: 80 + periodSeconds: 10 \ No newline at end of file diff --git a/k8s/api-svc.yaml b/k8s/api-svc.yaml new file mode 100644 index 0000000..7fe1549 --- /dev/null +++ b/k8s/api-svc.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Service +metadata: + name: api-svc +spec: + selector: + app: api-deployment + ports: + - protocol: TCP + port: 80 + targetPort: 8080 + nodePort: 30007 + type: NodePort \ No newline at end of file diff --git a/k8s/configmap.yaml b/k8s/configmap.yaml new file mode 100644 index 0000000..ffd2438 --- /dev/null +++ b/k8s/configmap.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: db-config + labels: + name: db-config +data: + POSTGRES_URL: "jdbc:postgresql://postgres-svc:5432/fastfood" + POSTGRES_DB: "fastfood" + POSTGRES_USER: "postgres" + POSTGRES_PASSWORD: "123456" \ No newline at end of file diff --git a/k8s/metrics.yaml b/k8s/metrics.yaml new file mode 100644 index 0000000..e892837 --- /dev/null +++ b/k8s/metrics.yaml @@ -0,0 +1,159 @@ +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: + - port: 443 + protocol: TCP + targetPort: 4443 + 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 + template: + metadata: + labels: + k8s-app: metrics-server + spec: + serviceAccountName: metrics-server + containers: + - args: + - --cert-dir=/tmp + - --secure-port=4443 + - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname + - --kubelet-use-node-status-port + - --metric-resolution=15s + - --kubelet-insecure-tls + image: k8s.gcr.io/metrics-server/metrics-server:v0.6.1 + name: metrics-server + ports: + - containerPort: 4443 + name: main-port + protocol: TCP +--- +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 diff --git a/k8s/postgres-deployment.yaml b/k8s/postgres-deployment.yaml new file mode 100644 index 0000000..adbc697 --- /dev/null +++ b/k8s/postgres-deployment.yaml @@ -0,0 +1,67 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: postgres-deployment + labels: + app: postgres-deployment +spec: + replicas: 1 + selector: + matchLabels: + app: postgres-deployment + template: + metadata: + labels: + app: postgres-deployment + spec: + containers: + - name: database + image: postgres:latest + imagePullPolicy: Always + ports: + - containerPort: 5432 + protocol: TCP + livenessProbe: + tcpSocket: + port: 5432 + initialDelaySeconds: 20 + periodSeconds: 20 + timeoutSeconds: 10 + failureThreshold: 2 + env: + - name: POSTGRES_URL + valueFrom: + configMapKeyRef: + name: db-config + key: POSTGRES_URL + - name: POSTGRES_DB + valueFrom: + configMapKeyRef: + name: db-config + key: POSTGRES_DB + - name: POSTGRES_USER + valueFrom: + configMapKeyRef: + name: db-config + key: POSTGRES_USER + - name: POSTGRES_PASSWORD + valueFrom: + configMapKeyRef: + name: db-config + key: POSTGRES_PASSWORD + volumeMounts: + - name: config-volume + mountPath: /docker-entrypoint-initdb.d/ + - name: postgres-storage + mountPath: /var/lib/postgresql/data + volumes: + - name: config-volume + configMap: + name: cm-init + items: + - key: init.sql + path: init.sql + - name: postgres-storage + persistentVolumeClaim: + claimName: postgres-pvc + restartPolicy: Always \ No newline at end of file diff --git a/k8s/postgres-pv.yaml b/k8s/postgres-pv.yaml new file mode 100644 index 0000000..cde7796 --- /dev/null +++ b/k8s/postgres-pv.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: PersistentVolume +metadata: + name: postgres-pv + labels: + name: postgres-pv +spec: + capacity: + storage: 1Gi + accessModes: + - ReadWriteMany + storageClassName: local-storage + persistentVolumeReclaimPolicy: Delete + hostPath: + path: "/mnt/data/postgres" \ No newline at end of file diff --git a/k8s/postgres-pvc.yaml b/k8s/postgres-pvc.yaml new file mode 100644 index 0000000..e7a93d5 --- /dev/null +++ b/k8s/postgres-pvc.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: postgres-pvc + labels: + name: postgres-pvc +spec: + accessModes: + - ReadWriteMany + resources: + requests: + storage: 1Gi + storageClassName: local-storage \ No newline at end of file diff --git a/k8s/postgres-svc.yaml b/k8s/postgres-svc.yaml new file mode 100644 index 0000000..8baef27 --- /dev/null +++ b/k8s/postgres-svc.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: postgres-svc +spec: + selector: + app: postgres-deployment + ports: + - protocol: TCP + port: 5432 + targetPort: 5432 + type: ClusterIP \ No newline at end of file diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 52e59a8..47bc385 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -1,5 +1,5 @@ # DATASOURCE -spring.datasource.url=jdbc:postgresql://localhost:5432/fastfood +spring.datasource.url=jdbc:postgresql://postgres-svc:5432/fastfood spring.datasource.username=postgres spring.datasource.password=123456