Cannot access my service deployed on Azure Kubernetes Service via the Public IP

6/13/2019

I created a web service and embedded it in an Azure Container and used Kubernetes to manage it. Similar to the AKS tutorial published by Microsoft. When I try to access my service via the AKS provided Public IP, the service is not accessible. Pods are working fine and The service logs show that nothing wrong and that the web servers are working fine. What could be the problem? Below are my yaml file and CLI outputs

kubectl get pods
NAME                           READY     STATUS    RESTARTS   AGE
books-back-85c6fd64fd-wjxff    1/1       Running   0          3d
books-front-68448dbfdb-cbmfs   1/1       Running   0          24m

-----------------------------------------------------------
kubectl get logs books-front-68448dbfdb-cbmf
Book {'Title': 'Atomic Habits', 'Author': 'Mark Johnson'} added
Book {'Title': 'If Tomorrow Comes', 'Author': 'Sidney Sheldon'} added
books: [{"Title": "Atomic Habits", "Author": "Mark Johnson"}]
 * Serving Flask app "server" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
-----------------------------------------------------------------------

kubectl get service 
books-back    ClusterIP      10.0.235.217   <none>          6379/TCP       3d
books-front   LoadBalancer   10.0.165.13    52.168.10.232   80:31233/TCP   3d
kubernetes    ClusterIP      10.0.0.1       <none>          443/TCP        3d

-----------------------------------------------------------------------------
My Yaml file


    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      name: books-back
    spec:
      replicas: 1
      template:
        metadata:
          labels:
            app: books-back
        spec:
          nodeSelector:
            "beta.kubernetes.io/os": linux
          containers:
          - name: books-back
            image: redis
            ports:
            - containerPort: 6379
              name: redis
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: books-back
    spec:
      ports:
      - port: 6379
      selector:
        app: books-back
    ---
    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      name: books-front
    spec:
      replicas: 1
      strategy:
        rollingUpdate:
          maxSurge: 1
          maxUnavailable: 1
      minReadySeconds: 5 
      template:
        metadata:
          labels:
            app: books-front
        spec:
          nodeSelector:
            "beta.kubernetes.io/os": linux
          containers:
          - name: books-front
            image: bookservice.azurecr.io/books-front:v1
            ports:
            - containerPort: 80
            resources:
              requests:
                cpu: 250m
              limits:
                cpu: 500m
            env:
            - name: REDIS
              value: "books-back"
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: books-front
    spec:
      type: LoadBalancer
      ports:
      - port: 80
      selector:
        app: books-front

All seems to work well. Yet when I access 52.168.10.232 the service does not respond.

-- Gautam Bajekal
azure
kubernetes

1 Answer

6/14/2019

From the logs of the books-front pod, your application listens to the port 5000, but you expose the port 80 in for your container. It causes unaccessible.

You just need to change to expose the port 5000 in your deployment books-front and service books-front like below:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: books-front
spec:
  replicas: 1
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  minReadySeconds: 5 
  template:
    metadata:
      labels:
        app: books-front
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: books-front
        image: bookservice.azurecr.io/books-front:v1
        ports:
        - containerPort: 5000
        resources:
          requests:
            cpu: 250m
          limits:
            cpu: 500m
        env:
        - name: REDIS
          value: "books-back"
---
apiVersion: v1
kind: Service
metadata:
  name: books-front
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 5000
  selector:
    app: books-front
-- Charles Xu
Source: StackOverflow