Implementing Microservices Architecture with Kubernetes and Nginx Ingress on a VPS

Implementing Microservices Architecture with Kubernetes and Nginx Ingress on a VPS

Step 1: Deploy Microservices and Databases

# service1-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: service1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: service1
  template:
    metadata:
      labels:
        app: service1
    spec:
      containers:
        - name: service1
          image: your-service1-image
          ports:
            - containerPort: 80

# service2-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: service2
spec:
  replicas: 3
  selector:
    matchLabels:
      app: service2
  template:
    metadata:
      labels:
        app: service2
    spec:
      containers:
        - name: service2
          image: your-service2-image
          ports:
            - containerPort: 80

# postgresdb1.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgresdb1-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

# postgresdb2.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgresdb2-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Apply the above YAML files using kubectl apply -f <filename>.

Step 2: Create Kubernetes Services

# service1-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: service1
spec:
  selector:
    app: service1
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

# service2-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: service2
spec:
  selector:
    app: service2
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Apply the above YAML files using kubectl apply -f <filename>.

Step 3: Install Nginx Ingress Controller

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml

Step 4: Define Ingress Resources

# ingress-service1.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service1
spec:
  rules:
    - http:
        paths:
          - path: /service1
            pathType: Prefix
            backend:
              service:
                name: service1
                port:
                  number: 80

# ingress-service2.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service2
spec:
  rules:
    - http:
        paths:
          - path: /service2
            pathType: Prefix
            backend:
              service:
                name: service2
                port:
                  number: 80

Apply the above YAML files using kubectl apply -f <filename>.

Step 5: External Access Configuration

Configure external access by exposing the Nginx Ingress Controller to the external network, either by setting up a LoadBalancer service or using a NodePort service.

For example, if using a LoadBalancer:

# ingress-controller-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: ingress-nginx-controller
  namespace: ingress-nginx
spec:
  selector:
    app.kubernetes.io/component: controller
  ports:
    - name: http
      port: 80
      targetPort: http
    - name: https
      port: 443
      targetPort: https
  type: LoadBalancer

Apply the above YAML file using kubectl apply -f <filename>.
Fix text and sentences with AI.