Create a cluster on GKE:
gcloud container clusters create backend --num-nodes=2 --machine-type n1-standard-4
Get credentials:
gcloud container clusters get-credentials backend
Build the docker image:
docker build -t gcr.io/your-company/image:v1 .
Push the image to GCR:
docker push gcr.io/your-company/image:v1
Deploy the application:
kubectl apply -f moleculer-k8.yml
Basic deployment script:
#!/bin/bash
** replace gcr.io/you-company/image:v1 in the k8 yml with $DEPLOYMENT_IMAGE **
HASH=$(git rev-parse HEAD)
IMAGE=gcr.io/your-company/image:$HASH
echo "Hash: $HASH"
echo "Image: $IMAGE"
read -p "Continue? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
docker build --no-cache -t $IMAGE .
docker push $IMAGE
export DEPLOYMENT_IMAGE=$IMAGE
envsubst < k8.yml | kubectl apply -f -
fi
Below is an example of the k8 yml file...
apiVersion: v1
kind: Service
metadata:
name: mongo
labels:
name: mongo
spec:
ports:
- port: 27017
targetPort: 27017
selector:
name: mongo
---
apiVersion: v1
kind: Service
metadata:
name: nats
labels:
name: nats
spec:
ports:
- port: 4222
targetPort: 4222
selector:
name: nats
---
apiVersion: v1
kind: Service
metadata:
name: api-service
spec:
type: NodePort
ports:
- port: 80
name: http
targetPort: 3000
protocol: TCP
- port: 443
name: https
targetPort: 3000
protocol: TCP
selector:
app: api
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: api-ingress
annotations:
kubernetes.io/ingress.global-static-ip-name: "name-of-static-ip"
kubernetes.io/ingress.class: "gce"
spec:
rules:
- http:
paths:
- path: /*
backend:
serviceName: api-service
servicePort: 80
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: mongo
labels:
name: mongo
spec:
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
name: mongo
spec:
containers:
- image: mongo
name: mongo
ports:
- containerPort: 27017
resources: {}
volumeMounts:
- mountPath: /data/db
name: mongo-data
restartPolicy: Always
volumes:
- name: mongo-data
persistentVolumeClaim:
claimName: mongo-data
status: {}
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mongo-data
labels:
name: mongo-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
status: {}
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nats
labels:
name: nats
spec:
replicas: 1
strategy: {}
template:
metadata:
labels:
name: nats
spec:
containers:
- image: nats
name: nats
ports:
- containerPort: 4222
resources: {}
restartPolicy: Always
status: {}
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: api
labels:
name: api
spec:
replicas: 1
template:
metadata:
name: api
labels:
app: api
name: api
spec:
containers:
- name: api
image: gcr.io/you-company/image:v1
imagePullPolicy: Always
ports:
- name: api
containerPort: 3000
env:
- name: LOGGER
value: "true"
- name: LOGLEVEL
value: info
- name: MONGO_URI
value: mongodb://mongo/dbname
- name: PORT
value: "3000"
- name: SERVICES
value: services/api.service.js
- name: TRANSPORTER
value: nats://nats:4222
restartPolicy: Always
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: someotherservice
labels:
name: someotherservice
spec:
replicas: 1
template:
metadata:
labels:
name: someotherservice
spec:
containers:
- name: someotherservice
image: gcr.io/your-company/image:v1
imagePullPolicy: Always
env:
- name: NODEID
value: 'node-someotherservice'
- name: LOGGER
value: "true"
- name: LOGLEVEL
value: info
- name: MONGO_URI
value: mongodb://mongo/dbname
- name: SERVICES
value: services/someother.service.js
- name: TRANSPORTER
value: nats://nats:4222
restartPolicy: Always