23

How to develop PHP applications on Local Kubernetes with Helm

Dont Worry! It is Very Easy.. πŸ™‚ Install the Helm CLI and k3s(/or minikube) as kubernetes before the begin. now…

Dont Worry! It is Very Easy.. πŸ™‚

Install the Helm CLI and k3s(/or minikube) as kubernetes before the begin.

  1. Create Project Tree with empty File Content like so:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
.
β”œβ”€β”€ Chart.yaml
β”œβ”€β”€ project
β”‚ └── index.php
β”œβ”€β”€ templates
β”‚ β”œβ”€β”€ deployment.yaml
β”‚ β”œβ”€β”€ ingress.yaml
β”‚ β”œβ”€β”€ pvc.yaml
β”‚ β”œβ”€β”€ pv.yaml
β”‚ └── service.yaml
└── values.yaml
. β”œβ”€β”€ Chart.yaml β”œβ”€β”€ project β”‚ └── index.php β”œβ”€β”€ templates β”‚ β”œβ”€β”€ deployment.yaml β”‚ β”œβ”€β”€ ingress.yaml β”‚ β”œβ”€β”€ pvc.yaml β”‚ β”œβ”€β”€ pv.yaml β”‚ └── service.yaml └── values.yaml
.
β”œβ”€β”€ Chart.yaml
β”œβ”€β”€ project
β”‚   └── index.php
β”œβ”€β”€ templates
β”‚   β”œβ”€β”€ deployment.yaml
β”‚   β”œβ”€β”€ ingress.yaml
β”‚   β”œβ”€β”€ pvc.yaml
β”‚   β”œβ”€β”€ pv.yaml
β”‚   └── service.yaml
└── values.yaml

now let’s fill in the files.

Read more: How to develop PHP applications on Local Kubernetes with Helm

Chart.yaml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apiVersion: v2
name: myfirstlocalphpwithkubernetes
description: A Helm chart for local PHP Development on local Kubernetes
# A chart can be either an 'application' or a 'library' chart.
type: application
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
version: 0.1.0
# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application.
appVersion: "1.16.0"
apiVersion: v2 name: myfirstlocalphpwithkubernetes description: A Helm chart for local PHP Development on local Kubernetes # A chart can be either an 'application' or a 'library' chart. type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. version: 0.1.0 # This is the version number of the application being deployed. This version number should be # incremented each time you make changes to the application. appVersion: "1.16.0"
apiVersion: v2
name: myfirstlocalphpwithkubernetes
description: A Helm chart for local PHP Development on local Kubernetes

# A chart can be either an 'application' or a 'library' chart.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
version: 0.1.0
# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. 
appVersion: "1.16.0"

project/index.php

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
echo "My First local PHP with Kubernetes via Helm";
<?php echo "My First local PHP with Kubernetes via Helm";
<?php

echo "My First local PHP with Kubernetes via Helm";

templates/deployment.yaml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}-deployment
labels:
app: {{ .Release.Name }}-php
spec:
selector:
matchLabels:
app: {{ .Release.Name }}-php
template:
metadata:
labels:
app: {{ .Release.Name }}-php
spec:
containers:
- image: {{ .Values.image.name }}:{{ .Values.image.tag }}
name: {{ .Release.Name }}-php
imagePullPolicy: Always
ports:
- containerPort: 80
volumeMounts:
- name: {{ .Release.Name }}-php-storage
mountPath: {{ .Values.mountPath }}
volumes:
- name: {{ .Release.Name }}-php-storage
persistentVolumeClaim:
claimName: {{ .Release.Name }}-pvc-php
apiVersion: apps/v1 kind: Deployment metadata: name: {{ .Release.Name }}-deployment labels: app: {{ .Release.Name }}-php spec: selector: matchLabels: app: {{ .Release.Name }}-php template: metadata: labels: app: {{ .Release.Name }}-php spec: containers: - image: {{ .Values.image.name }}:{{ .Values.image.tag }} name: {{ .Release.Name }}-php imagePullPolicy: Always ports: - containerPort: 80 volumeMounts: - name: {{ .Release.Name }}-php-storage mountPath: {{ .Values.mountPath }} volumes: - name: {{ .Release.Name }}-php-storage persistentVolumeClaim: claimName: {{ .Release.Name }}-pvc-php
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}-deployment
  labels:
        app: {{ .Release.Name }}-php
spec:
  selector:
    matchLabels:
      app: {{ .Release.Name }}-php
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}-php
    spec:
      containers:
      - image: {{ .Values.image.name }}:{{ .Values.image.tag }}
        name: {{ .Release.Name }}-php
        imagePullPolicy: Always
        ports:
          - containerPort: 80
        volumeMounts:
          - name: {{ .Release.Name }}-php-storage
            mountPath: {{ .Values.mountPath }}
      volumes:
        - name: {{ .Release.Name }}-php-storage
          persistentVolumeClaim:
            claimName: {{ .Release.Name }}-pvc-php

templates/ingress.yaml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: {{ .Release.Name }}-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: {{ .Values.hostname }}
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: {{ .Release.Name }}-php-service
port:
number: 80
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: {{ .Release.Name }}-ingress annotations: nginx.ingress.kubernetes.io/rewrite-target: / spec: rules: - host: {{ .Values.hostname }} http: paths: - path: / pathType: Prefix backend: service: name: {{ .Release.Name }}-php-service port: number: 80
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: {{ .Release.Name }}-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: {{ .Values.hostname }}
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: {{ .Release.Name }}-php-service
            port:
              number: 80

template/pv.yaml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-{{ .Release.Name }}-php
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: {{ .Values.hostPath }}
apiVersion: v1 kind: PersistentVolume metadata: name: pv-{{ .Release.Name }}-php labels: type: local spec: storageClassName: manual capacity: storage: 10Gi accessModes: - ReadWriteOnce hostPath: path: {{ .Values.hostPath }}
apiVersion: v1
kind: PersistentVolume
metadata:
  name:  pv-{{ .Release.Name }}-php
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: {{ .Values.hostPath }}

template/pvc.yaml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: {{ .Release.Name }}-pvc-php
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: {{ .Release.Name }}-pvc-php spec: storageClassName: manual accessModes: - ReadWriteOnce resources: requests: storage: 1Gi
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: {{ .Release.Name }}-pvc-php
spec:
    storageClassName: manual
    accessModes:
        - ReadWriteOnce
    resources:
        requests:
            storage: 1Gi

template/service.yaml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
apiVersion: v1
kind: Service
metadata:
name: {{ .Release.Name }}-php-service
spec:
type: LoadBalancer
selector:
app: {{ .Release.Name }}-php
ports:
# By default and for convenience, the `targetPort` is set to the same value as the `port` field.
- port: 80
targetPort: 80
# Optional field
apiVersion: v1 kind: Service metadata: name: {{ .Release.Name }}-php-service spec: type: LoadBalancer selector: app: {{ .Release.Name }}-php ports: # By default and for convenience, the `targetPort` is set to the same value as the `port` field. - port: 80 targetPort: 80 # Optional field
apiVersion: v1
kind: Service
metadata:
  name: {{ .Release.Name }}-php-service
spec:
  type: LoadBalancer
  selector:
    app: {{ .Release.Name }}-php
  ports:
      # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
    - port: 80
      targetPort: 80
      # Optional field

values.yaml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
hostname: 'simple-php.local'
mountPath: 'var/www/html/'
hostPath: '/home/projects/myfirstlocalphpwithkubernetes/project'
image:
name: php
tag: 8.1-apache
hostname: 'simple-php.local' mountPath: 'var/www/html/' hostPath: '/home/projects/myfirstlocalphpwithkubernetes/project' image: name: php tag: 8.1-apache
hostname: 'simple-php.local'
mountPath: 'var/www/html/'
hostPath: '/home/projects/myfirstlocalphpwithkubernetes/project'
image:
  name: php
  tag: 8.1-apache

and let’s install with helm

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
d8dev@d8devs:~/projects/myfirstlocalphpwithkubernetes$ helm install -f values.yaml myfirstlocalphp .
d8dev@d8devs:~/projects/myfirstlocalphpwithkubernetes$ helm install -f values.yaml myfirstlocalphp .
d8dev@d8devs:~/projects/myfirstlocalphpwithkubernetes$ helm install -f values.yaml myfirstlocalphp .

Add hostname (simple-php.local) a.k.a domain in your /etc/hosts with any text editor.

My /etc/hosts looking so;

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
d8dev@d8devs:~/projects/myfirstlocalphpwithkubernetes$ cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 simple-php.local
......
.....
d8dev@d8devs:~/projects/myfirstlocalphpwithkubernetes$ cat /etc/hosts 127.0.0.1 localhost 127.0.1.1 simple-php.local ...... .....
d8dev@d8devs:~/projects/myfirstlocalphpwithkubernetes$ cat /etc/hosts
127.0.0.1       localhost
127.0.1.1       simple-php.local
......
.....

And check the domain in your browser with slash ( http://simple-php.local/ )

Enjoy!

Views: 50

d8devs