Creating a kubernetes web application from scratch

Time:2021-3-3

preface

From how to create aPHP WebStart with the application and bring you into theKubernetesMy world.

Basics

Environmental Science

  • CentOS 7.5 (Kernel 3.10)
  • Minikube (Kubernetes 1.10.0)

What’s required of you

I assume that you have mastered the following basic skills:

  • Docker & can write dockerfile
  • How to Google
  • Have a dockerhub account
  • Manually compiled LNMP or lamp

Building basic image

Creating a kubernetes web application from scratch

The figure above depicts the image we need to createContainersOf whichPause ContaineryesKubernetesWe don’t need to care about it, but it’s very important. There will be an article to describe it in the futurePause ContainerWhat are you doing.
In fact, the basic image is usually ready-made, but it is recommended to compile it manually during the learning process to find out what the official default configuration has.DockerfileI’ll put the code inGitHubBecause it’s too long to show here.

Create nginx image

Nginx: Nginx For K8S GitHub Repo

Compile nginx image

    docker build . -t motecshine/nginx1.12-for-k8s:v0.1.0
    docker push motecshine/nginx1.12-for-k8s:v0.1.0

Creating php-fpm image

FPM: FPM For K8S GitHub Repo

Compile FPM image

    docker build . -t motecshine/php71-for-k8s:v0.1.0
    docker push motecshine/php71-for-k8s:v0.1.0

matters needing attention:Dockerfile CMDNeed to closeNginxandFPMOfdaemonThe specific features of repoDockerfileThis is to ensure thatContainerLife cycle and developmentPODThe life cycle is consistent.

Building business image

We will create our business image based on the above image

Create code image

We are based onLaravelTo create a mirror.

Code: Code For K8S GitHub Repo

Compile code image

    docker build . -t motecshine/code-for-k8s:v0.1.1
    docker push motecshine/code-for-k8s:v0.1.1

Create nginx image

laravel-nginx-for-k8s: Laravel For K8S GitHub Repo

Compile nginx image

    docker build . -t  motecshine/laravel-nginx-for-k8s:v0.1.1
    docker push  motecshine/laravel-nginx-for-k8s:v0.1.1

Creating php-fpm image

laravel-fpm-for-k8s: Laravel-FPM For K8S GitHub Repo

Compile FPM image

    docker build . -t  motecshine/laravel-fpm-for-k8s:v0.1.0
    docker push  motecshine/laravel-fpm-for-k8s:v0.1.0

Building kubernetes application

Creating a kubernetes web application from scratch

The overall architecture is shown in the figure above

Building a minimal operating unit (POD)

Creating a kubernetes web application from scratch

Create deployment

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: laravel
  namespace: default
spec:
  Replicas: 1 # expected number of copies
  template:
    metadata:
      labels:
        app: laravel # app label
        version: testing
    spec:
      containers:
      - name: code
        image: motecshine/code-for-k8s:v0.1.1
        Volumemounts: # mount directory
        - mountPath: /data2
          name: code
      - name: fpm
        image: motecshine/laravel-fpm-for-k8s:v0.1.0
        imagePullPolicy: IfNotPresent
        Resources: resource constraints
           limits:
             cpu: 350m
             memory: 350Mi
           requests:
             cpu: 50m
             memory: 50Mi
        ports:
        - name: fpm
          containerPort: 9000
        volumeMounts:
        -Mountpath: / data / code # mount code
          name: code
        -Mountpath: / var / log # mount log
          name: log  
      - name: laravel-nginx
        image: motecshine/laravel-nginx-for-k8s:v0.1.0
        imagePullPolicy: IfNotPresent
        resources:
          limits:
            cpu: 350m
            memory: 350Mi
          requests:
            cpu: 50m
            memory: 50Mi
        ports:
        - name: ivp-nginx
          Containerport: 80? Exposure endpoint
        volumeMounts:
        - mountPath: /data/code
          name: code
        - mountPath: /var/log
          name: log  
      volumes:
      - name: code
        emptyDir: {}
      - name: log
        hostPath:
          path: /var/log
          type: Directory

Build service

apiVersion: v1
kind: Service
metadata:
  name: laravel-service
  namespace: default
  labels:
    app: laravel-service
    version: testing-service
spec:
  type: ClusterIP
  selector:
    app: laravel
    version: testing
  ports:
    - name: http
      port: 80

Building ingress


apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: laravel-ingress
  namespace: default
  labels:
    app: laravel-ingress
spec:
  rules:
  - host: laravel.test
    http:
      paths:
      - path: /
        backend:
          serviceName: laravel-service
          servicePort: 80

Installing minikube

curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube && sudo cp minikube /usr/local/bin/ && rm minikube

Installing traefik

We use open sourceIngressComponent installationSee here

Launch web application

The above configuration file is here

git clone [email protected]:motecshine/laravel-k8s-config.git
cd laravel-k8s-config && kubectl create -f .

effect

Creating a kubernetes web application from scratch

epilogue

A brief introduction to how to create a web application is just the beginning,KubernetesBehind it is a huge ecological environment,CI,CD,ELK(EFK), APMLet’s uncover its mystery a little bit.

The next one will beKubenetesbe based onEFKLog collection platform.

This work adoptsCC agreementReprint must indicate the author and the link of this article