<p>Facing this issue if I am connecting to ingress for web socket service
failed: Error during WebSocket handshake: Unexpected response code: 400</p>

<p><strong>Ingress YAML</strong></p>

<pre><code>kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: websocket-producer-cdph
spec:
  rules:
    host: some.domain.com
      http:
        paths:
          path: "/"
          backend:
            serviceName: websocket-producer-cdph
            servicePort: 8183
status:
loadBalancer:
ingress:
{}
</code></pre>

<p><strong>Service YAML</strong></p>

<pre><code>kind: Service
apiVersion: v1
metadata:
  name: websocket-producer-cdph
spec:
  ports:    
    name: ws
    protocol: TCP
    port: 8183
    targetPort: 8183
selector:
  app: websocket-producer-cdph
clusterIP: 10.100.254.99
type: ClusterIP
sessionAffinity: None
status:
loadBalancer:
{}
</code></pre>

<p>When I try to listen ws://some.domain.com/ws it's showing
Error during WebSocket handshake: Unexpected response code: 400</p>

<p><em>/ws is the path</em></p>

<p>But if go and update spec type in service to LoadBalancer, it'll generate an IP 192.168.1.17:8183 and listening to that ws://192.168.1.17:8183/ws its working but I need to expose the URL using ingress so it can be used outside of the network.</p>

<p>I am using the following image for ingress controller:</p>

<p><strong>quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.19.0</strong></p>

<p>How can I create ingress for a web-socket service?</p>

Kartik Purohit
<p>As stated in the <a href="https://github.com/nginxinc/kubernetes-ingress/tree/master/examples/websocket#websocket-support" rel="noreferrer">nginx-ingress documentation</a>, to proxy WebSocket traffic you should use annotation with the name of websocket service. Don't forget to use quotes:</p>

<pre><code>nginx.org/websocket-services: "service1[,service2,...]"
</code></pre>

<p>In this example from the <a href="https://github.com/nginxinc/kubernetes-ingress/tree/master/examples/websocket#websocket-support" rel="noreferrer">documentation</a>, WebSocket is enabled only for one of three services (<code>ws-svc</code>):</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: cafe-ingress
  annotations:
    nginx.org/websocket-services: "ws-svc"
spec:
  rules:
  - host: cafe.example.com
    http:
      paths:
      - path: /tea
        backend:
          serviceName: tea-svc
          servicePort: 80
      - path: /coffee
        backend:
          serviceName: coffee-svc
          servicePort: 80
      - path: /ws
        backend:
          serviceName: ws-svc
          servicePort: 8008
</code></pre>
