Last active
March 22, 2022 05:53
-
-
Save thiagotigaz/c60073bb3e9ffbb69d9d1fc39591ae53 to your computer and use it in GitHub Desktop.
NodeJS Microservices with NestJS, Kafka, and Kubernetes - Part 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM node:16 | |
ARG KAFKA_BROKERS=kafka-service:9092 | |
ENV KAFKA_BROKERS ${KAFKA_BROKERS} | |
WORKDIR /usr/src/app | |
COPY package*.json ./ | |
RUN npm install | |
COPY . . | |
RUN npm run build | |
RUN npm run build-microservices | |
ENTRYPOINT ["node"] | |
CMD ["dist/apps/api/main"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: api-deployment | |
labels: | |
app: api | |
spec: | |
replicas: 2 | |
selector: | |
matchLabels: | |
app: api | |
template: | |
metadata: | |
labels: | |
app: api | |
spec: | |
containers: | |
- name: api | |
image: limascloud/node | |
imagePullPolicy: IfNotPresent | |
args: ["dist/apps/api/main"] | |
ports: | |
- containerPort: 3000 | |
env: | |
- name: KAFKA_BROKERS | |
valueFrom: | |
configMapKeyRef: | |
name: config-map | |
key: kafka-url | |
--- | |
apiVersion: v1 | |
kind: Service | |
metadata: | |
name: api-service | |
spec: | |
type: NodePort | |
selector: | |
app: api | |
ports: | |
- protocol: TCP | |
port: 3000 | |
targetPort: 3000 | |
nodePort: 30100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
kubectl delete -f k8-templates/k8-microservices.yaml | |
kubectl delete -f k8-templates/k8-api.yaml | |
kubectl delete -f k8-templates/k8-kafka.yaml | |
kubectl delete -f k8-templates/k8-secret.yaml | |
kubectl delete -f k8-templates/k8-config.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: v1 | |
kind: ConfigMap | |
metadata: | |
name: config-map | |
data: | |
kafka-url: kafka-service:9092 | |
zookeeper-url: zookeeper-service:2181 | |
postgres-user: nestjs-microservices |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: zookeeper-deployment | |
labels: | |
app: zookeeper | |
spec: | |
replicas: 1 | |
selector: | |
matchLabels: | |
app: zookeeper | |
template: | |
metadata: | |
labels: | |
app: zookeeper | |
spec: | |
containers: | |
- name: zookeeper | |
image: confluentinc/cp-zookeeper:latest | |
env: | |
- name: ZOOKEEPER_CLIENT_PORT | |
value: "2181" | |
ports: | |
- containerPort: 2181 | |
name: client | |
--- | |
apiVersion: v1 | |
kind: Service | |
metadata: | |
name: zookeeper-service | |
labels: | |
app: zookeeper | |
spec: | |
selector: | |
app: zookeeper | |
ports: | |
- protocol: TCP | |
port: 2181 | |
targetPort: 2181 | |
--- | |
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: kafka-deployment | |
labels: | |
app: kafka | |
spec: | |
replicas: 1 | |
selector: | |
matchLabels: | |
app: kafka | |
template: | |
metadata: | |
labels: | |
app: kafka | |
spec: | |
containers: | |
- name: kafka | |
image: confluentinc/cp-kafka:latest | |
ports: | |
- containerPort: 9092 | |
name: kafka-in | |
- containerPort: 9094 | |
name: kafka-out | |
env: | |
- name: KAFKA_BROKER_ID | |
value: "1" | |
- name: KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR | |
value: "1" | |
- name: KAFKA_ZOOKEEPER_CONNECT | |
valueFrom: | |
configMapKeyRef: | |
name: config-map | |
key: zookeeper-url | |
- name: KAFKA_INTER_BROKER_LISTENER_NAME | |
value: "INTERNAL" | |
- name: KAFKA_LISTENERS | |
value: "INTERNAL://:9092,OUTSIDE://:30094" | |
- name: KAFKA_ADVERTISED_LISTENERS | |
value: "INTERNAL://kafka-service:9092,OUTSIDE://kubernetes.docker.internal:30094" | |
- name: KAFKA_LISTENER_SECURITY_PROTOCOL_MAP | |
value: "INTERNAL:PLAINTEXT,OUTSIDE:PLAINTEXT" | |
--- | |
apiVersion: v1 | |
kind: Service | |
metadata: | |
name: kafka-service | |
labels: | |
app: kafka | |
spec: | |
type: NodePort | |
selector: | |
app: kafka | |
ports: | |
- name: kafka-in | |
protocol: TCP | |
port: 9092 | |
targetPort: 9092 | |
- name: kafka-out | |
protocol: TCP | |
port: 30094 | |
targetPort: 30094 | |
nodePort: 30094 | |
--- | |
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: control-center-deployment | |
labels: | |
app: control-center | |
spec: | |
replicas: 1 | |
selector: | |
matchLabels: | |
app: control-center | |
template: | |
metadata: | |
labels: | |
app: control-center | |
spec: | |
containers: | |
- name: control-center | |
image: confluentinc/cp-enterprise-control-center:latest | |
ports: | |
- containerPort: 9021 | |
env: | |
- name: CONTROL_CENTER_BOOTSTRAP_SERVERS | |
valueFrom: | |
configMapKeyRef: | |
name: config-map | |
key: kafka-url | |
- name: CONTROL_CENTER_REPLICATION_FACTOR | |
value: "1" | |
- name: PORT | |
value: "9021" | |
--- | |
apiVersion: v1 | |
kind: Service | |
metadata: | |
name: control-center-service | |
labels: | |
app: control-center | |
spec: | |
type: NodePort | |
selector: | |
app: control-center | |
ports: | |
- protocol: TCP | |
port: 9021 | |
targetPort: 9021 | |
nodePort: 30021 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: apps/v1 | |
kind: Deployment | |
metadata: | |
name: microservices-deployment | |
labels: | |
app: microservices | |
spec: | |
replicas: 2 | |
selector: | |
matchLabels: | |
app: microservices | |
template: | |
metadata: | |
labels: | |
app: microservices | |
spec: | |
containers: | |
- name: microservices | |
image: limascloud/node | |
imagePullPolicy: IfNotPresent | |
args: [ "dist/apps/microservices/main" ] | |
env: | |
- name: KAFKA_BROKERS | |
valueFrom: | |
configMapKeyRef: | |
name: config-map | |
key: kafka-url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: v1 | |
kind: Secret | |
metadata: | |
name: secret | |
type: Opaque | |
data: | |
postgres-password: cGFzc3dvcmQK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Build Docker image used by api and microservices. Name of image has to match image declared on templates. | |
docker build -t limascloud/node . | |
kubectl apply -f k8-templates/k8-config.yaml | |
kubectl apply -f k8-templates/k8-secret.yaml | |
kubectl apply -f k8-templates/k8-kafka.yaml | |
kubectl apply -f k8-templates/k8-api.yaml | |
kubectl apply -f k8-templates/k8-microservices.yaml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment