This document describes the configurations for deploying a Websocket service on Kubernetes built with Laravel 7 that uses the pusher/pusher-php-server, beyondcode/laravel-websockets, laravel-echo and pusher-js libraries
This deployment and service will run the artisan websockets:serve
command and the websocket will be served on default port 6001
apiVersion: apps/v1
kind: Deployment
metadata:
name: YOUR-NAMESPACE-websocket
namespace: YOUR-NAMESPACE
annotations:
keel.sh/policy: force
labels:
app: YOUR-NAMESPACE-websocket
spec:
replicas: 1
selector:
matchLabels:
app: YOUR-NAMESPACE-websocket
template:
metadata:
labels:
app: YOUR-NAMESPACE-websocket
spec:
containers:
- name: YOUR-NAMESPACE-websocket-container
image: baimages/YOUR-IMAGE:unstable
command:
- php
args:
- '-d variables_order=EGPCS'
- artisan
- websockets:serve
imagePullPolicy: Always
envFrom:
- configMapRef:
name: YOUR-NAMESPACE-cm
---
apiVersion: v1
kind: Service
metadata:
name: YOUR-NAMESPACE-websocket-service
namespace: wordpresspro
spec:
selector:
app: YOUR-NAMESPACE-websocket
ports:
- protocol: TCP
port: 6001
type: ClusterIP
You will need a public path in the ingress so that Laravel Echo can access the service externally. Add the following path to your existing ingress (similar to this https://prnt.sc/In3x0bwfNhnq)
- pathType: Prefix
path: "/app"
backend:
service:
name: YOUR-NAMESPACE-websocket-service
port:
number: 6001
Important: your websocket will be accessed externally from port 443 and not port 6001
BROADCAST_DRIVER: pusher
PUSHER_APP_ID: "YOUR_APP_ID"
PUSHER_APP_KEY: "YOUR_APP_KEY"
PUSHER_APP_SECRET: "YOUR_APP_SECRET"
PUSHER_APP_CLUSTER: mt1
PUSHER_APP_HOST: YOUR_APP-websocket-service # name of your service
PUSHER_APP_PORT: "6001"
PUSHER_APP_SCHEMA: http
LARAVEL_WEBSOCKETS_PORT: "6001"
MIX_PUSHER_APP_KEY: "YOUR_APP_KEY"
MIX_PUSHER_APP_CLUSTER: "mt1"
MIX_PUSHER_APP_HOST: "YOUR_APP.kub.app.builderall.io" # your app host
MIX_PUSHER_APP_PORT: "443"
MIX_PUSHER_FORCE_TLS: "false"
If your app doesn't have a development key, you can create one at https://pusher.com/
Check your Laravel Echo instance if you are using variables from the Node environment https://prnt.sc/COXaJThZ7T3s. If so, you must manually replace them because when the image is generated in Github Action, the container does not have access to the environment variables. To make the replacement, create the file entrypoint.sh
with the following lines (you can commit this file in your repository):
sed -i -e "s|TEMP_PUSHER_APP_KEY|$MIX_PUSHER_APP_KEY|g" /var/www/public/js/app.js
sed -i -e "s|TEMP_PUSHER_APP_CLUSTER|$MIX_PUSHER_APP_CLUSTER|g" /var/www/public/js/app.js
sed -i -e "s|TEMP_PUSHER_APP_HOST|$MIX_PUSHER_APP_HOST|g" /var/www/public/js/app.js
sed -i -e "s|TEMP_PUSHER_APP_PORT|$MIX_PUSHER_APP_PORT|g" /var/www/public/js/app.js
sh /entrypoint.sh
And add to your dockerfile:
- Before the
npm run
command
# sed envs
ENV MIX_PUSHER_APP_KEY="TEMP_PUSHER_APP_KEY"
ENV MIX_PUSHER_APP_CLUSTER="TEMP_PUSHER_APP_CLUSTER"
ENV MIX_PUSHER_APP_HOST="TEMP_PUSHER_APP_HOST"
ENV MIX_PUSHER_APP_PORT="TEMP_PUSHER_APP_PORT"
ENV MIX_PUSHER_FORCE_TLS=false
- Before your "EXPOSE" command
COPY PATH-OF-FILE/entrypoint.sh entrypoint.sh
CMD ["sh", "entrypoint.sh"]
Just apply your Yaml files and push the changes. In the new version of the image, the websocket will already be working and you can view it at YOUR-APP-URL/websockets (or another url defined in the websockets.php path variable)