<p>I am testing gke ingress to route traffic to two different services. My deployment consists of a basic web container that deploys a blue web page default and a green web page. I am able to get a response back Essentially the &quot;/<em>&quot; works with either blue or green deployment. But when i go to http:///green i get a 404 response. I have tested the same with &quot;/</em>&quot; as green deployment and it displays a green web page. But if i go http:///blue it results in a 404 response,</p>
<p>I have verified my containers are working properly by attaching a load balancer directly to them. I am following this how to guide in gke to setup a similar environment. <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/load-balance-ingress" rel="nofollow noreferrer">GKE Ingress How to  guide</a></p>
<p>Any help on what i am missing would greatly help me understand better what is going on and why my gke load balancer is unable to route traffic.</p>
<p>green deployment file</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata: 
  name: myapp-green
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
      version: green
  template:
    metadata:
      name: myapp
      labels:
        app: myapp
        version: green
    spec:
      containers:
        - name: myapp
          image: gcr.io/ultra-welder-300122/myapp:green
          imagePullPolicy: Always
          resources:
            requests:
              cpu: &quot;100m&quot;
              memory: &quot;100Mi&quot;
          ports:
          - containerPort: 8080
</code></pre>
<p>blue  Deployment file</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata: 
  name: myapp-blue
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
      version: blue
  template:
    metadata:
      name: myapp
      labels:
        app: myapp
        version: blue
    spec:
      containers:
        - name: myapp
          image: gcr.io/ultra-welder-300122/myapp:blue
          imagePullPolicy: Always
          resources:
            requests:
              cpu: &quot;100m&quot;
              memory: &quot;100Mi&quot;
          ports:
          - containerPort: 8888
</code></pre>
<p>My Service yaml</p>
<pre><code>apiVersion: v1
kind: Service
metadata: 
  name: myapp-blue-service
  labels:
    app: myapp
    version: blue
spec:
  type: NodePort
  selector:
    app: myapp
    version: blue
  ports: 
  - protocol: TCP
    port: 80
    targetPort: 8888
---
apiVersion: v1
kind: Service
metadata: 
  name: myapp-green-service
  labels:
    app: myapp
    version: green
spec:
  type: NodePort
  selector:
    app: myapp
    version: green
  ports: 
  - protocol: TCP
    port: 80
    targetPort: 8080
</code></pre>
<p>Gke ingress yaml</p>
<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: myapp-ingress
  annotations:
    # If the class annotation is not specified it defaults to &quot;gce&quot;.
    kubernetes.io/ingress.class: &quot;gce&quot;
spec:
  rules:
  - http:
      paths:
      - path: /*
        backend:
          serviceName: myapp-blue-service
          servicePort: 80
      - path: /green
        backend:
          serviceName: myapp-green-service
          servicePort: 80
</code></pre>
<p><strong>Service Status</strong></p>
<pre><code>NAME                  TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes            ClusterIP   10.42.0.1      &lt;none&gt;        443/TCP        6h18m
myapp-blue-service    NodePort    10.42.81.106   &lt;none&gt;        80:31664/TCP   4h35m
myapp-green-service   NodePort    10.42.74.168   &lt;none&gt;        80:30246/TCP   4h35m
</code></pre>
<p><strong>Ingress Status</strong></p>
<pre><code>kubectl describe ing myapp-ingress
Name:             myapp-ingress
Namespace:        default
Address:          34.95.121.24
Default backend:  default-http-backend:80 (10.41.0.9:8080)
Rules:
  Host        Path  Backends
  ----        ----  --------
  *
              /*        myapp-blue-service:80 (10.41.0.24:8888,10.41.1.16:8888)
              /green    myapp-green-service:80 (10.41.0.27:8080,10.41.1.19:8080)
Annotations:  ingress.kubernetes.io/backends:
                {&quot;k8s-be-30192--a4913825f2ae16d4&quot;:&quot;HEALTHY&quot;,&quot;k8s-be-30246--a4913825f2ae16d4&quot;:&quot;HEALTHY&quot;,&quot;k8s-be-31664--a4913825f2ae16d4&quot;:&quot;HEALTHY&quot;}
              ingress.kubernetes.io/forwarding-rule: k8s2-fr-dkti1gqp-default-myapp-ingress-yd16xk65
              ingress.kubernetes.io/target-proxy: k8s2-tp-dkti1gqp-default-myapp-ingress-yd16xk65
              ingress.kubernetes.io/url-map: k8s2-um-dkti1gqp-default-myapp-ingress-yd16xk65
              kubernetes.io/ingress.class: gce
</code></pre>
<p><strong>Deployment and pods Status</strong></p>
<pre><code>NAME                                          READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/myapp-blue                    2/2     2            2           4h18m
deployment.apps/myapp-green                   2/2     2            2           38m
NAME                                               READY   STATUS    RESTARTS   AGE
pod/myapp-blue-9c9cbf5b-c7v7c                      1/1     Running   0          4h18m
pod/myapp-blue-9c9cbf5b-cpvgl                      1/1     Running   0          4h18m
pod/myapp-green-7f56cc9496-hnfkz                   1/1     Running   0          38m
pod/myapp-green-7f56cc9496-v8bb6                   1/1     Running   0          38m
</code></pre>
<p><strong>Service status</strong></p>
<pre><code>NAME                  TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes            ClusterIP   10.42.0.1      &lt;none&gt;        443/TCP        6h24m
myapp-blue-service    NodePort    10.42.81.106   &lt;none&gt;        80:31664/TCP   4h40m
myapp-green-service   NodePort    10.42.74.168   &lt;none&gt;        80:30246/TCP   4h40m
</code></pre>

Ramesh Rajan
<p>I found the problem is with gke ingress controller. Gke ingress controller doesn't provide rewrite-target to /. I am serving my green and blue web pages from the webroot. GKE ingress controller is forwarding the requested URL <a href="http://34.95.121.24/green" rel="nofollow noreferrer">http://34.95.121.24/green</a> to http://{backend
}/green. There is no page hosted on http://{backend
}/green as my default page is hosted on http://{backend}/.</p>
<p>There are two solutions to the problem.</p>
<ol>
<li>Use nginx-ingress controller with annotation to rewrite-target set to /.</li>
</ol>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myapp-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /*
        backend:
          serviceName: myapp-blue-service
          servicePort: 80
      - path: /green
        backend:
          serviceName: myapp-green-service
          servicePort: 80

</code></pre>
<ol start="2">
<li>Handle rewrite or redirect non-existing pages in the docker container. In my case it was a nginx webserver.</li>
</ol>
<p>I created a default server configuration section to route any traffic hitting /green to index.html.</p>
<pre><code>server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;
        server_name _;
        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                index index.html
                try_files $uri $uri/ =404;
        }
        location /green {
                try_files $uri $uri/ /index.html;
        }

</code></pre>
<p>I copied default site configuration to /etc/nginx/sites-avaiable in my docker configuration file.</p>
<p>My sample Dockerfile</p>
<pre><code>FROM ubuntu
RUN apt-get update
RUN apt-get install nginx -y
COPY index.html /var/www/html/
COPY default /etc/nginx/sites-available/
EXPOSE 80
CMD [&quot;nginx&quot;,&quot;-g&quot;,&quot;daemon off;&quot;]
</code></pre>
