# Expose a shop through the Istio ingress gateway

> A Gateway and a VirtualService should serve shop.example.com through Istio's ingress gateway. You get connection refused, then 404s. Fix each layer.

Level: Intermediate · Topic: Networking · About 15 minutes
Try it in the browser: https://k8wiz.com/playground/expose-a-service-through-the-istio-ingress-gateway

The playground is a simulated Kubernetes cluster that runs in the browser. kubectl output and errors follow the real ones.

## The situation

The `shop` namespace runs `storefront`, the web front end on port 80, and `orders-api` on port 8080, which serves `/orders`. Both should be public at `http://shop.example.com`: the site at `/`, and the API under `/api/`, so `/api/orders` reaches `/orders` on orders-api.

A teammate applied a Gateway and a VirtualService, both named `storefront`, for Istio's ingress gateway in `istio-system`. DNS for `shop.example.com` already points at the gateway's load balancer, but `curl http://shop.example.com/` from the terminal can't even connect.

Get both URLs working through the gateway. Don't expose the Services another way.

## Objectives

1. Ask Istio what's wrong (`istioctl analyze -n shop`)
2. http://shop.example.com/ shows the shop
3. http://shop.example.com/api/orders returns the orders
4. Traffic still comes in through the Istio gateway only

## Hints

1. `curl -v http://shop.example.com/` ends in `Couldn't connect to server`: nothing listens on port 80 of the gateway yet. The ingress gateway's Envoy only opens a port when a Gateway that selects its Pod declares it.
2. `istioctl analyze -n shop` says `Referenced selector not found: "istio=ingress"`. That label comes from the Helm chart's examples; this cluster's gateway Pods carry `istio=ingressgateway` (`kubectl get pods -n istio-system --show-labels`). Fix the Gateway's selector.
3. Now the gateway answers `404` with `server: istio-envoy`, and `kubectl logs -n istio-system deploy/istio-ingressgateway --tail=2` shows `404 NR route_not_found`. A VirtualService without `gateways` only applies to sidecars inside the mesh. Add `gateways: [storefront]` (or `shop/storefront`).
4. `/` works now, but `/api/orders` returns `{"error":"not found"}`. That 404 is the app's own (`via_upstream` in the gateway log): orders-api got `/api/orders` and only knows `/orders`. Add `rewrite: {uri: /}` to the `api` route, which replaces the matched `/api/` prefix.

## Solution

```bash
curl -s http://shop.example.com/
```
```bash
istioctl analyze -n shop
```
```bash
kubectl get pods -n istio-system --show-labels
```
```bash
cat <<'EOF' | kubectl apply -f -
apiVersion: networking.istio.io/v1
kind: Gateway
metadata:
  name: storefront
  namespace: shop
spec:
  selector:
    istio: ingressgateway
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - shop.example.com
EOF
```
```bash
curl -s -i http://shop.example.com/
```
```bash
kubectl logs -n istio-system deploy/istio-ingressgateway --tail=2
```
```bash
cat <<'EOF' | kubectl apply -f -
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
  name: storefront
  namespace: shop
spec:
  hosts:
  - shop.example.com
  gateways:
  - storefront
  http:
  - name: api
    match:
    - uri:
        prefix: /api/
    rewrite:
      uri: /
    route:
    - destination:
        host: orders-api
        port:
          number: 8080
  - name: web
    route:
    - destination:
        host: storefront
        port:
          number: 80
EOF
```
```bash
curl -s http://shop.example.com/
```
```bash
curl -s http://shop.example.com/api/orders
```

## What it teaches

- Istio's ingress gateway is an Envoy Deployment behind a LoadBalancer Service. A Gateway resource configures it: its `selector` picks the gateway Pods by label, and its `servers` open ports for a list of hosts.
- A VirtualService binds to a Gateway through `gateways`. A name without a namespace means the VirtualService's own namespace. Without the field it applies only to sidecars (`mesh`), and listing gateways turns `mesh` off unless you list it too.
- The gateway answers `404 NR` when no route matches the host and path. A 404 with `via_upstream` in the log came from the app.
- `rewrite.uri` replaces the part of the path a `prefix` match matched, so `/api/orders` reaches the backend as `/orders`.
- `istioctl analyze` catches most of these before any traffic: a selector that matches no Pod, a gateway that doesn't exist, or a host the Gateway doesn't serve.
