代码之家  ›  专栏  ›  技术社区  ›  martins

带有GCP Cloud SQL的Kubernetes应用程序不接受任何连接。Github上提供了完整的源代码

  •  0
  • martins  · 技术社区  · 7 年前

    我创建了一个使用谷歌云PostgreSQL的Rails 5应用示例。 我可以在本地用 docker-compose up ,但当我将其部署到GCP时,无法远程连接到它。 我试着复制 https://cloud.google.com/ruby/tutorials/bookshelf-on-kubernetes-engine 他们使用的地方 targetPort: http-server

    rails应用程序是 published on Github . 我做错什么了吗-|

    在本地运行该应用程序很有效

    git clone git@github.com:stabenfeldt/k8s-colors.git
    docker-compose up -d
    docker-compose run colors rake db:create db:migrate
    open http://localhost:3000
    

    创建一个GKE集群

    gcloud container clusters create color-cluster --num-nodes=2
    

    设置PostgreSQL云SQL

    我听从了老师的指示 https://cloud.google.com/sql/docs/postgres/connect-kubernetes-engine?authuser=1 并更新了我的配置/数据库。yml和k8s/颜色。yml使用这些值。

    已部署但仍停留在ContainerCreating上

    kubectl apply -f k8s/colors.yml
    kubectl get pods
    
    NAME                    READY     STATUS              RESTARTS   AGE
    colors-d9f744dc-d5l5v   0/2       ContainerCreating   0          5m
    colors-d9f744dc-spmws   0/2       ContainerCreating   0          5m
    
    
    kubectl logs d9f744dc-d5l5v -c colors  # => Nothing logged
    

    kubectl得到部署

    NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
    colors    2         2         2            0           7m
    

    但无法连接到应用程序

    kubectl get svc
    NAME         TYPE           CLUSTER-IP      EXTERNAL-IP      PORT(S)        AGE
    colors       LoadBalancer   10.55.245.192   35.228.111.217   80:30746/TCP   1h
    kubernetes   ClusterIP      10.55.240.1     <none>           443/TCP        1h
    

    卷曲35.228.111.217#=>没有回应!:-/

    kubectl描述svc颜色

    Name:                     colors
    Namespace:                default
    Labels:                   <none>
    Annotations:              kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"colors","namespace":"default"},"spec":{"ports":[{"port":80,"targetPort":3000}]...
    Selector:                 app=colors
    Type:                     LoadBalancer
    IP:                       10.55.252.91
    LoadBalancer Ingress:     35.228.203.46
    Port:                     <unset>  80/TCP
    TargetPort:               3000/TCP
    NodePort:                 <unset>  30964/TCP
    Endpoints:                <none>
    Session Affinity:         None
    External Traffic Policy:  Cluster
    Events:
      Type    Reason                Age   From                Message
      ----    ------                ----  ----                -------
      Normal  Type                  1m    service-controller  ClusterIP -> LoadBalancer
      Normal  EnsuringLoadBalancer  1m    service-controller  Ensuring load balancer
      Normal  EnsuredLoadBalancer   30s   service-controller  Ensured load balancer
    

    k8s/服务。yml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: colors
      labels:
        app: colors
    
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: colors
      template:
        metadata:
          labels:
            app: colors
        spec:
          containers:
            - name: colors
              image: docker.io/stabenfeldt/colors:latest
              ports:
                - name: http-server
                  containerPort: 3000
    
    
              env:
                - name: POSTGRES_HOST
                  value: 127.0.0.1:5432
                - name: POSTGRES_USER
                  valueFrom:
                    secretKeyRef:
                      name: cloudsql-db-credentials
                      key: username
                - name: POSTGRES_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: cloudsql-db-credentials
                      key: password
    
            - name: cloudsql-proxy
              image: gcr.io/cloudsql-docker/gce-proxy:1.11
              command: ["/cloud_sql_proxy",
                        "-instances=PROJECT_ID:europe-west1:staging=tcp:5432",
                        "-credential_file=/secrets/cloudsql/credentials.json"]
              volumeMounts:
                - name: cloudsql-instance-credentials
                  mountPath: /secrets/cloudsql
                  readOnly: true
    
          volumes:
            - name: cloudsql-instance-credentials
              secret:
                secretName: cloudsql-instance-credentials
    
    ---
    
    
    apiVersion: v1
    kind: Service
    metadata:
      name: colors
    spec:
      type: LoadBalancer
      ports:
      - port: 80
        targetPort: 3000
      selector:
        app: colors
    

    kubectl描述部署

    Name:                   colors
    Namespace:              default
    CreationTimestamp:      Fri, 13 Jul 2018 10:37:06 +0200
    Labels:                 app=colors
    Annotations:            deployment.kubernetes.io/revision=1
                            kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"colors"},"name":"colors","namespace":"default"},"spec":{"repl...
    Selector:               app=colors
    Replicas:               2 desired | 2 updated | 2 total | 0 available | 2 unavailable
    StrategyType:           RollingUpdate
    MinReadySeconds:        0
    RollingUpdateStrategy:  25% max unavailable, 25% max surge
    Pod Template:
      Labels:  app=colors
      Containers:
       colors:
        Image:  docker.io/stabenfeldt/colors:latest
        Port:   3000/TCP
        Environment:
          POSTGRES_HOST:      127.0.0.1:5432
          POSTGRES_USER:      <set to the key 'username' in secret 'cloudsql-db-credentials'>  Optional: false
          POSTGRES_PASSWORD:  <set to the key 'password' in secret 'cloudsql-db-credentials'>  Optional: false
        Mounts:               <none>
       cloudsql-proxy:
        Image:  gcr.io/cloudsql-docker/gce-proxy:1.11
        Port:   <none>
        Command:
          /cloud_sql_proxy
          -instances=MY-INSTANCE:europe-west1:staging=tcp:5432
          -credential_file=/secrets/cloudsql/credentials.json
        Environment:  <none>
        Mounts:
          /secrets/cloudsql from cloudsql-instance-credentials (ro)
      Volumes:
       cloudsql-instance-credentials:
        Type:        Secret (a volume populated by a Secret)
        SecretName:  cloudsql-instance-credentials
        Optional:    false
    Conditions:
      Type           Status  Reason
      ----           ------  ------
      Available      False   MinimumReplicasUnavailable
      Progressing    True    ReplicaSetUpdated
    OldReplicaSets:  <none>
    NewReplicaSet:   colors-d9f744dc (2/2 replicas created)
    Events:
      Type    Reason             Age   From                   Message
      ----    ------             ----  ----                   -------
      Normal  ScalingReplicaSet  1m    deployment-controller  Scaled up replica set colors-d9f744dc to 2
    

    kubectl描述服务

    Name:                     colors
    Namespace:                default
    Labels:                   <none>
    Annotations:              kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"colors","namespace":"default"},"spec":{"ports":[{"port":80,"targetPort":3000}]...
    Selector:                 app=colors
    Type:                     LoadBalancer
    IP:                       10.55.252.91
    LoadBalancer Ingress:     35.228.203.46
    Port:                     <unset>  80/TCP
    TargetPort:               3000/TCP
    NodePort:                 <unset>  30964/TCP
    Endpoints:                <none>
    Session Affinity:         None
    External Traffic Policy:  Cluster
    Events:
      Type    Reason                Age   From                Message
      ----    ------                ----  ----                -------
      Normal  Type                  4m    service-controller  ClusterIP -> LoadBalancer
      Normal  EnsuringLoadBalancer  4m    service-controller  Ensuring load balancer
      Normal  EnsuredLoadBalancer   3m    service-controller  Ensured load balancer
    
    
    Name:              kubernetes
    Namespace:         default
    Labels:            component=apiserver
                       provider=kubernetes
    Annotations:       <none>
    Selector:          <none>
    Type:              ClusterIP
    IP:                10.55.240.1
    Port:              https  443/TCP
    TargetPort:        443/TCP
    Endpoints:         35.228.79.249:443
    Session Affinity:  ClientIP
    Events:            <none>
    
    3 回复  |  直到 7 年前
        1
  •  1
  •   kurtisvg    7 年前

    我看不出有什么不对劲的地方,但这里有一些小贴士可以验证Kubernetes对象与YAML相比的外观:

    使用 describe 命令以获取有关对象的更多信息,并确保对象设置正确。

    例如,如果你 kubectl describe deployment <deployment_name> 您应该验证是否存在以下行:

    Port:       3000/TCP
    

    谢谢你的服务- kubectl describe service <service_name> :

    LoadBalancer Ingress:     <PUBLIC_IP>
    Port:                     <unset>  80/TCP
    TargetPort:               3000/TCP
    

    最后,我不确定您是否希望在LoadBalancer中应用以下内容:

      labels:
        app: colors
    

    由于您将此标签用作选择器,它可能会做一些古怪的事情,并试图在自身而不是包含应用程序的容器中实现负载平衡。

    另外,作为术语的补充说明,GCP(谷歌云平台)是谷歌服务的总称,GKE(谷歌Kubernetes引擎)是为您提供托管Kubernetes集群的服务。

    希望这有帮助。

        2
  •  0
  •   Community CDub    5 年前

    工作设置可以在我的 example Rails app at Github .

    k8s/颜色。yml

    # Remember to update MY-INSTANCE
    
    apiVersion: v1
    kind: Service
    metadata:
      name: colors-frontend
      labels:
        app: colors
        tier: frontend
    spec:
      type: LoadBalancer
      ports:
      - port: 80
        targetPort: http-server
      selector:
        app: colors
        tier: frontend
    
    ---
    
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: colors-frontend
      labels:
        app: colors
        tier: frontend
    spec:
      replicas: 3
      template:
        metadata:
          labels:
            app: colors
            tier: frontend
        spec:
          volumes:
            - name: cloudsql-instance-credentials
              secret:
                secretName: cloudsql-instance-credentials
    
          containers:
            - name: cloudsql-proxy
              image: gcr.io/cloudsql-docker/gce-proxy:1.11
              command: ["/cloud_sql_proxy",
                        "-instances=MY-INSTANCE:europe-west1:development=tcp:5432",
                        "-credential_file=/secrets/cloudsql/credentials.json"]
              volumeMounts:
                  - name: cloudsql-instance-credentials
                    mountPath: /secrets/cloudsql
                    readOnly: true
    
    
            - name: colors-app
              image: docker.io/stabenfeldt/colors:1
              imagePullPolicy: Always
    
              env:
                - name: RAILS_LOG_TO_STDOUT
                  value: "true"
                - name: RAILS_ENV
                  value: development
                - name: POSTGRES_HOST
                  value: 127.0.0.1
                - name: POSTGRES_USERNAME
                  valueFrom:
                    secretKeyRef:
                      name: cloudsql-db-credentials
                      key: username
                - name: POSTGRES_PASSWORD
                  valueFrom:
                    secretKeyRef:
                      name: cloudsql-db-credentials
                      key: password
    
              ports:
              - name: http-server
                containerPort: 3000
    
        3
  •  0
  •   Narendra    7 年前

    您的POSTGRES_主机环境变量需要 本地服务器 而不是 127.0.0.01:5432 .您不需要在POSTGRES_主机中添加端口

    推荐文章