在Kubernetes中部署Laravel

时间:2019-01-31 20:20:25

标签: laravel docker nginx kubernetes google-cloud-platform

我想部署在谷歌云平台在kubernetes一个laravel应用程序。

我遵循了一些教程,并成功在Docker VM上本地尝试了它们。

https://learnk8s.io/blog/kubernetes-deploy-laravel-the-easy-way

https://blog.cloud66.com/deploying-your-laravel-php-applications-with-cloud-66/

但是,当使用的入口分配一个域名到应用程序中kubernetes试图部署。我不断收到502错误的网关页面。

我使用与图像k8s.gcr.io/nginx-ingress-controller:0.8.3一个nginx的入口控制器和我的入口是如下

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  tls:
  - hosts:
    - domainname.com
    secretName: sslcertificate
  rules:
  - host: domain.com
    http:
      paths:
      - backend:
          serviceName: service
          servicePort: 80
        path: /

这是我的应用程序服务

apiVersion: v1
kind: Service
metadata:
  name: service
  labels:
    name: demo
    version: v1
spec:
  ports:
    - port: 80
      targetPort: 8080
      protocol: TCP
  selector:
    name: demo
  type: NodePort

这是我的入口控制器

apiVersion: v1
kind: Service
metadata:
  name: default-http-backend
  labels:
    k8s-app: default-http-backend
spec:
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
    name: http
  selector:
    k8s-app: default-http-backend
---
apiVersion: v1
kind: ReplicationController
metadata:
  name: default-http-backend
spec:
  replicas: 1
  selector:
    k8s-app: default-http-backend
  template:
    metadata:
      labels:
        k8s-app: default-http-backend
    spec:
      terminationGracePeriodSeconds: 60
      containers:
      - name: default-http-backend
        # Any image is permissable as long as:
        # 1. It serves a 404 page at /
        # 2. It serves 200 on a /healthz endpoint
        image: gcr.io/google_containers/defaultbackend:1.0
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 30
          timeoutSeconds: 5
        ports:
        - containerPort: 8080
        resources:
          limits:
            cpu: 10m
            memory: 20Mi
          requests:
            cpu: 10m
            memory: 20Mi
---
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-ingress-controller
  labels:
    k8s-app: nginx-ingress-lb
spec:
  replicas: 1
  selector:
    k8s-app: nginx-ingress-lb
  template:
    metadata:
      labels:
        k8s-app: nginx-ingress-lb
        name: nginx-ingress-lb
    spec:
      terminationGracePeriodSeconds: 60
      containers:
      - image: gcr.io/google_containers/nginx-ingress-controller:0.8.3
        name: nginx-ingress-lb
        imagePullPolicy: Always
        readinessProbe:
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
        livenessProbe:
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
          initialDelaySeconds: 10
          timeoutSeconds: 1
        # use downward API
        env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
            fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
        ports:
        - containerPort: 80
          hostPort: 80
        - containerPort: 443
          hostPort: 443
        # we expose 18080 to access nginx stats in url /nginx-status
        # this is optional
        - containerPort: 18080
          hostPort: 18080
        args:
        - /nginx-ingress-controller
        - --default-backend-service=$(POD_NAMESPACE)/default-http-backend

这是我的laravel应用程序部署

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: demo-rc
  labels:
    name: demo
    version: v1
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        name: demo
        version: v1
    spec:
      containers:
      - image: gcr.io/projectname/laravelapp:vx
        name: app-pod
        ports:
        - containerPort: 8080

我试图域条目添加到主机文件,但没有运气! 在那里我必须添加为nginx的入口控制器configmap.yaml文件中的特定配置?

1 个答案:

答案 0 :(得分:0)

简而言之,为了能够通过外部域名(singapore.smartlabplatform.com)访问您的应用程序,您需要为GCP L4负载均衡器的外部IP地址(即EXTERNAL-IP)创建一个DNS记录默认的nginx-ingress-controller's服务),在这里被视为待处理:

==> v1/Service
NAME                            TYPE          CLUSTER-IP    EXTERNAL-IP  
nginx-ingress-controller        LoadBalancer  10.7.248.226  pending      
nginx-ingress-default-backend   ClusterIP     10.7.245.75   none

该怎么做?在GKE教程页面here中对此进行了说明。

在当前环境中,您只能通过两种方式访问​​应用程序:

  1. 从外部,通过负载均衡器EXTERNAL-IP:

    enter image description here

  2. 在内部,使用 laravel-kubernetes-demo 服务dns名称的Kubernetes集群:

  

$ curl laravel-kubernetes-demo.default.svc.cluster.local                     

<title>Laravel Kubernetes Demo :: LearnK8s</title>

如果您想要所有神奇的事情,例如自动创建DNS记录,并在入口资源规范中出现host: domain.com,则应该使用external-dns(使Kubernetes资源可以通过公共DNS发现。服务器),here是有关如何专门为GKE进行设置的教程。