Deploy a WebSocket service in Google Kubernetes

Posted by

Goals : We want to deploy a WebSocket service inside the Google Kubernetes.

Requirements: Google Cloud, Docker, Kubernetes cluster.

Step 1 : Prepare the app deployment depoyment.yaml.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: notebook
spec:
  replicas: 2
  selector:
    matchLabels:
      app: notebook
  template:
    metadata:
      labels:
        app: notebook
    spec:
      containers:
      - name: notebook
        image: gcr.io/<google_project_id>/notebook:1.0.0
        imagePullPolicy: Always
        readinessProbe:
           httpGet:
             path: /api
             port: 8888
           initialDelaySeconds: 30
           periodSeconds: 10
        ports:
        - containerPort: 8888

Step 2: Define backend-config and service

apiVersion: v1
kind: Service
metadata:
  annotations:
    beta.cloud.google.com/backend-config: '{"ports": {"8888":"notebook-backendconfig"}}'
  name: notebook
  labels:
    app: notebook
spec:
  ports:
    - port: 8888
  type: NodePort
  selector:
    app: notebook

---

apiVersion: cloud.google.com/v1beta1
kind: BackendConfig
metadata:
  name: notebook-backendconfig
spec:
  timeoutSec: 1800
  connectionDraining:
    drainingTimeoutSec: 1800

Step 3: Deploy Ingress

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: dispatcher
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.org/websocket-services: "notebook"
    nginx.ingress.kubernetes.io/proxy-connect-timeout: "3600"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "3600"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "3600"
spec:
  tls:
  - secretName: gopulse-secret-tls
    hosts:
      - app.gopulse.io
  backend:
    serviceName: notebook
    servicePort: 8888
  rules:
  - host: app.gopulse.io
    http:
      paths:
        - path: /*
          backend:
            serviceName: notebook
            servicePort: 8888

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml

Notes: the notebook-backendconfig will help increase the timeout for websocket client, so we will not get the problem with auto disconnection.

All above steps is applied for our production https://app.gopulse.io

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.