Kubernetes 部署workerman socket.io 应用

https://github.com/walkor/phpsocket.io
此教程是使用workerman架设websocket服务器。

1. 在你的project root directory 内创建 shell script , 命名为entrypoint.sh

#!/bin/sh
php /var/www/html/start.php start 

2. 创建dockerfile, 并且暴露2021端口,这端口为你的socket.io 端口

FROM php:7.3-cli-stretch
WORKDIR /var/www/html

COPY ./ ./

RUN chmod +x /var/www/html/entrypoint.sh
RUN uname -a && apt update
RUN docker-php-ext-install sockets pcntl
RUN apt-get install libevent-dev libssl-dev -y && pecl install event
RUN echo extension=event.so > /usr/local/etc/php/conf.d/30-event.ini
EXPOSE 2021

ENTRYPOINT [ "/var/www/html/entrypoint.sh" ]

3. 接下来的步骤就是把应用部署进kubernetes集群 , 首先是创建namespace

apiVersion: v1
kind: Namespace
metadata:
    name: workerman

4. 设置deployment, 我是使用阿里云的私有container registry,所以需要提供imagePullSecrets

apiVersion: apps/v1
kind: Deployment
metadata:
    name: websocket-deployment
    namespace: workerman
spec:
    replicas: 1
    selector:
        matchLabels:
            app: websocket_app
    template:
        metadata:
            labels:
                app: websocket_app
        spec:
            containers:
                - name: websocket-container
                  image: registry-intl-vpc.ap-southeast-1.aliyuncs.com/pangzai/workerman
                  imagePullPolicy: Always
                  ports:
                      - containerPort: 2021
                        name: port80test
            imagePullSecrets:
                - name: alicloud-websocket-key

5. 设置service

apiVersion: v1
kind: Service
metadata:
    name: websocket-service
    namespace: workerman
spec:
    selector:
        app: websocket_app
    ports:
        - name: "2021test"
          protocol: TCP
          port: 2021
          targetPort: 2021
    clusterIP: None

6. 设置cert-manager的certificate , 如果你不打算设置https可以省略这步骤

apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: websocket-selfsigned-cert-tls
  namespace: workerman
spec:
  dnsNames:
    - websocket.pangzai.win
  secretName: websocket-selfsigned-cert-tls
  issuerRef:
    name: letsencrypt-dns01

7. 设置ingress后就完成了

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
    name: websocket-ingress
    namespace: workerman
    annotations:
      kubernetes.io/ingress.class: "nginx"
      cert-manager.io/issuer: "workerman/letsencrypt-dns01"
      nginx.ingress.kubernetes.io/ssl-redirect: "true"
      nginx.ingress.kubernetes.io/proxy-read-timeout: '7200'
      nginx.ingress.kubernetes.io/proxy-send-timeout: '7200'
      nginx.ingress.kubernetes.io/rewrite-target: /$1
  
      # 这必须加入service的名
      nginx.org/websocket-services: "websocket-service"
spec:
    tls:
    - hosts:
      - websocket.pangzai.win
      secretName: websocket-selfsigned-cert-tls
    rules:
        - host: websocket.pangzai.win
          http:
              paths:
                  - path: /test1/?(.*) 
                    backend:
                          serviceName: websocket-service
                          servicePort: 2021

总结

https://websocket.pangzai.win/test1 才能映射到websocket-service , 主域名是没映射的(https://websocket.pangzai.win),这样的话同一个域名可以映射到多个服务

Loading

Facebook评论