Done!

How to deploy PHP and Nginx app using Kubernetes


Deploy PHP and Nginx app using Kubernetes

1. Create a Deployment definition file. (deployment.yml)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-deployment
  labels:
    app: php-app
spec:
  selector:
    matchLabels:
      app: php-app
  template:
    metadata:
      name: app-deployment
      labels:
        app: php-app

    spec:
      containers:
        - name: nginx-container
          image: ...your nginx image
          ports:
           - containerPort: 443

        - name: php-container
          image: ..your php image
          ports:
           - containerPort: 80

2. NodePort Service Definition. (nodeport-service.yml)

apiVersion: v1
kind: Service
metadata:
  name: nginx-app-service
spec:
  type: NodePort
  ports:
    - targetPort: 80
      port: 80
      nodePort: 30008
  selector:
    app: php-app

3. Create deployment and service using kubectl command

 - kubectl create -f deployment.yml
 - kubectl create -f service-nodeport.yml
 - kubectl get services
   NAME                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
   nginx-app-service   NodePort    10.152.183.22                 80:30008/TCP   13m
 - kubectl get node -o wide
   NAME          STATUS   ROLES    AGE   VERSION                     INTERNAL-IP    EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION       CONTAINER-RUNTIME
   microk8s-vm   Ready     none   10d   v1.18.2-41+b5cdb79a4060a3   192.168.64.3           Ubuntu 18.04.4 LTS   4.15.0-101-generic   containerd://1.2.5
You should now be able to access your app via 192.168.64.3:30008

Alternatively, using Load Balancer Service

  apiVersion: v1
  kind: Service
  metadata:
    name: nginx-app-service
  spec:
    type: LoadBalancer
    ports:
      - targetPort: 80
        port: 80
    selector:
      app: php-app
 
 - kubectl get services
   NAME                TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)        AGE
   nginx-app-service   LoadBalancer   .............   ...........   80:32280/TCP   4m26s
You should now be able access your app via the external IP.