skip to Main Content

I am new at kubernetes and I have a cluster with docker desktop with a pod which has my back end in a container and so far I know that a service attaches to a pod (pods) and provides a static IP which allows for external traffic (I want to hit my backend with my browser). I am unsure how to do this and nothing online really makes sense.

I think I need to make my service a load balancer type to allow a static IP to pass traffic into my pods (I have only one but I will up the replicas when this works).

I can’t seem to get it to work, here are the config files that are relevant:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: my-app
  name: my-app
spec:
  selector:
    matchLabels:
      app: my-app
  replicas: 1
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app/my-app
        imagePullPolicy: IfNotPresent
        env:
        - name: GET_HOSTS_FROM
          value: dns
        - name: DATABASE_URL
          value: postgres://postgres:postgres@database/postgres
        ports:
        - containerPort: 80

--------------------------------------------

apiVersion: v1
kind: Service
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  ports:
  - port: 80
    targetPort: 8080 
  selector:
    app: my-app

I ultimately want to put this on aws or gcp but I am a bit lost on where to go from here, if anyone has any advice or material that can help a brainlet like me understand this I would really appreciate it.

EDIT: I have added an ingress here and I am not too sure what should be happening when hooked up to the service:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-srv
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  ingressClassName: nginx
  rules:
    - host: lesser-bank.com
      http:
        paths:
          - pathType: Prefix
            path: "/test"
            backend:
              service:
                name: lesser-bank-api
                port:
                  number: 8080

2

Answers


  1. Services are of type ClusterIp by default which means that your pods can only be accessed from within the cluster.

    An object of kind Ingress can provide external access to your service, something like this:

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-srv
      annotations:
        kubernetes.io/ingress.class: nginx
        nginx.ingress.kubernetes.io/use-regex: "true"
    spec:
      rules:
        - host: acme.com
          http:
            paths:
              - pathType: Prefix
                path: "/myapp"
                backend:
                  service:
                    name: my-app
                    port:
                      number: 80
    

    This is a very common question and can be found easily in the Kubernetes docs.

    Login or Signup to reply.
  2. If you want to expose just one deployment then it will be ideal to just expose your service via a Loadbalancer type. Refer to this link for detailed explanation.

    However, if you want to use Nginx Ingress controller with Let’s Encrypt and Cert Manager, please refer to the following link for a detailed explanation and instructions:

    https://github.com/digitalocean/Kubernetes-Starter-Kit-Developers/blob/main/03-setup-ingress-controller/nginx.md

    ---
    
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      labels:
        app: my-app
      name: my-app
    spec:
      selector:
        matchLabels:
          app: my-app
      replicas: 1
      template:
        metadata:
          labels:
            app: my-app
        spec:
          containers:
          - name: my-app
            image: my-app/my-app
            imagePullPolicy: IfNotPresent
            env:
            - name: GET_HOSTS_FROM
              value: dns
            - name: DATABASE_URL
              value: postgres://postgres:postgres@database/postgres
            ports:
            - containerPort: 80
    ---
    
    apiVersion: v1
    kind: Service
    metadata:
      name: my-app
      labels:
        app: my-app
    spec:
      type: LoadBalancer #add this line to expose your deployment
      ports:
      - port: 80
        targetPort: 8080 
      selector:
        app: my-app
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search