0

我正在尝试为我的 nodeJs 项目设置 CI/CD。我正在使用 CircleCI、AWS ECR 和 ECS、Kubernetes。我收到以下错误

无法识别“/home/circleci/patched_k8s.yml”:获取http://localhost:8080/api?timeout=32s:拨打 tcp 127.0.0.1:8080:连接:连接被拒绝退出,代码为 1

.circleci/config.yml

version: 2
jobs:
  build:
    working_directory: ~/deploy-k8s
    docker:
      - image: circleci/node:9
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - run:
          name: Installing Dependencies
          command: npm install
      - save_cache:
          key: dependency-cache-{{ checksum "package.json" }}
          paths:
            - ./node_modules
  deploy:
    docker:
      - image: circleci/python:3.6.1
    environment:
      - AWS_DEFAULT_OUTPUT: json
    steps:
      - checkout
      - setup_remote_docker
      - attach_workspace:
          at: workspace
      - restore_cache:
          key: v1-{{ checksum "requirements.txt" }}
      - run:
          name: Install kops
          command: |
            sudo wget -O kops https://github.com/kubernetes/kops/releases/download/$(curl -s https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name | cut -d '"' -f 4)/kops-linux-amd64
            sudo chmod +x ./kops
            sudo mv ./kops /usr/local/bin/kops
      - run:
          name: Install kubectl from binary
          command: |
            sudo apt-get install -qq -y gettext
            sudo wget -O- https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
            echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" /etc/apt/sources.list.d/kubernetes.list
            sudo curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
            sudo chmod +x ./kubectl
            sudo mv ./kubectl /usr/local/bin/kubectl
      - run:
          name: Install awscli
          command: |
            sudo apt-get install -qq -y gettext
            python3 -m venv venv
            . venv/bin/activate
            pip install --upgrade pip
            pip install awscli
      - run:
          name: Setup aws environment variables
          command: |
            echo 'export ECR_REPOSITORY_NAME="${AWS_RESOURCE_NAME_PREFIX}"' >> $BASH_ENV
            echo 'export ECS_CLUSTER_NAME="${AWS_RESOURCE_NAME_PREFIX}-cluster"' >> $BASH_ENV
            echo 'export ECS_SERVICE_NAME="${AWS_RESOURCE_NAME_PREFIX}-service"' >> $BASH_ENV
      - run:
          name: Docker build and push
          command: |
            docker build -t ${FULL_IMAGE_NAME} .
             . venv/bin/activate
             eval $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
            docker tag ${FULL_IMAGE_NAME} ${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/deploy-k8s:${CIRCLE_SHA1}
            docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/deploy-k8s:$CIRCLE_SHA1
      - run:
          name: Deploy to Kubernetes
          command: |
            cat ${HOME}/project/k8s.yml
            envsubst < ${HOME}/project/k8s.yml > ${HOME}/patched_k8s.yml
            cat ${HOME}/patched_k8s.yml
            sudo kubectl apply -f ${HOME}/patched_k8s.yml
            sudo kubectl rollout status deployment/${PROJECT_NAME}
workflows:
  version: 2
  build-and-deploy:
    jobs:
      - build
      - deploy:
          requires:
            - build
          filters:
            branches:
              only: master

k8s.yml

kind: Service
apiVersion: v1
metadata:
  name: ${PROJECT_NAME}
spec:
  selector:
    app: ${PROJECT_NAME}
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
  type: LoadBalancer
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: ${PROJECT_NAME}
  labels:
    app: ${PROJECT_NAME}
spec:
  replicas: 2
  selector:
    matchLabels:
      app: ${PROJECT_NAME}
  template:
    metadata:
      labels:
        app: ${PROJECT_NAME}
    spec:
      containers:
        - name: ${PROJECT_NAME}
          image: $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$ECR_REPOSITORY_NAME:$CIRCLE_SHA1
          ports:
            - name: http
              containerPort: 3000
protocol: TCP

cat ${HOME}/patched_k8s.yml 给我正确的数据。但我不知道我错在哪里。请有人帮我解决这个问题。提前致谢。

4

0 回答 0