microservices application on k8s

planning:

deployment:

  • voting-app - python
  • redis - redis
  • worker - .net
  • postgres - postgres
  • result-app - nodejs

workflow:

voting-app:80 > redis:6379 > worker > postgres:5432 > result-app:80

goals:

  1. deploy containers
  2. enable connectivity
  3. external access

steps:

  1. deploy pods
  2. create services (ClusterIP)
    1. redis
    2. db
  3. create services (NodePort)
    1. voting-app
    2. result-app

the worker has no service - a service is only required if the app has webservice port that needs to be exposed

deploying:

make a directory called voting-app and change to this

create voting-app-pod.yaml

---
apiVersion: v1
kind: Pod
metadata:
  name: voting-app-pod
  labels:
    name: voting-app-pod
    app: demo-voting-app
spec: 
  containers:
    - name: voting-app
      image: kodekloud/examplevotingapp_vote:v1
      ports:
        - containerPort: 80

create result-app-pod.yaml

---
apiVersion: v1
kind: Pod
metadata:
  name: result-app-pod
  labels:
    name: result-app-pod
    app: demo-voting-app
spec: 
  containers:
    - name: result-app
      image: kodekloud/examplevotingapp_result:v1
      ports:
        - containerPort: 80

create redis-pod.yaml

---
apiVersion: v1
kind: Pod
metadata:
  name: redis-pod
  labels:
    name: redis-pod
    app: demo-voting-app
spec: 
  containers:
    - name: redis
      image: redis
      ports:
        - containerPort: 6379

create postgres-pod.yaml

---
apiVersion: v1
kind: Pod
metadata:
  name: postgres-pod
  labels:
    name: postgres-pod
    app: demo-voting-app
spec: 
  containers:
    - name: postgres
      image: postgres
      ports:
        - containerPort: 5432
      env:
        - name: POSTGRES_USER
          value: "postgres"
        - name: POSTGRES_PASSWORD
          value: "postgres"

create worker-app-pod.yaml

---
apiVersion: v1
kind: Pod
metadata:
  name: worker-app-pod
  labels:
    name: worker-app-pod
    app: demo-voting-app
spec: 
  containers:
    - name: worker-app
      image: kodekloud/examplevotingapp_worker:v1

create redis-service.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: redis
  labels:
    name: redis-service
    app: demo-voting-app
spec:
  ports:
    - port: 6379
      targetPort: 6379
  selector:
    name: redis-pod
    app: demo-voting-app

create postgres-service.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: db
  labels:
    name: postgres-service
    app: demo-voting-app
spec:
  ports:
    - port: 5432
      targetPort: 5432
  selector:
    name: postgres-pod
    app: demo-voting-app

create voting-app-service.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: voting-service
  labels:
    name: voting-service
    app: demo-voting-app
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30004
  selector:
    name: voting-app-pod
    app: demo-voting-app

create result-app-service.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: result-service
  labels:
    name: result-service
    app: demo-voting-app
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30005
  selector:
    name: result-app-pod
    app: demo-voting-app

create voting-app

# create voting app pod
> kubectl create -f voting-app-pod.yaml

# create voting app service
> kubectl create -f voting-app-service.yaml

# get svc and pod status in one command
> kubectl get pods,svc
  ---
  NAME                 READY   STATUS    RESTARTS   AGE
  pod/voting-app-pod   1/1     Running   0          30s

  NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE                                            
  service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        4d23h                                          
  service/voting-app   NodePort    10.100.144.107   <none>        80:30004/TCP   13s

create redis

# create voting app pod
> kubectl create -f redis-pod.yaml

# create voting app service
> kubectl create -f redis-service.yaml

# get svc and pod status in one command
> kubectl get pods,svc

create postgres

# create voting app pod
> kubectl create -f postgres-pod.yaml

# create voting app service
> kubectl create -f postgres-service.yaml

# get svc and pod status in one command
> kubectl get pods,svc

create worker-app

# create voting app pod
> kubectl create -f worker-app-pod.yaml

# get svc and pod status in one command
> kubectl get pods,svc

create result-app

# create voting app pod
> kubectl create -f result-app-pod.yaml

# create voting app service
> kubectl create -f result-app-service.yaml

# get svc and pod status in one command
> kubectl get pods,svc
  ---
  NAME                 READY   STATUS    RESTARTS        AGE
  pod/postgres-pod     1/1     Running   0               7m40s
  pod/redis-pod        1/1     Running   0               109s
  pod/result-app-pod   1/1     Running   0               3m44s
  pod/voting-app-pod   1/1     Running   0               11m
  pod/worker-app-pod   1/1     Running   5 (2m15s ago)   4m42s

  NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
  service/db           ClusterIP   10.101.65.11     <none>        5432/TCP       7m35s
  service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        4d23h
  service/redis        ClusterIP   10.109.242.172   <none>        6379/TCP       104s
  service/result-app   NodePort    10.97.59.103     <none>        80:30005/TCP   3m38s
  service/voting-app   NodePort    10.100.144.107   <none>        80:30004/TCP   11m

get minikube cluster url

# voting-service
> minikube service voting-service --url
  ---
  http://192.168.49.2:30004

# voting-service
> minikube service result-service --url
  ---
  http://192.168.49.2:30005