【发布时间】:2022-06-22 16:16:23
【问题描述】:
所以我已经在 GKE 中部署了这个项目,我正在尝试从 github 操作中制作 CI/CD。所以我添加了包含
的工作流文件name: Build and Deploy to GKE
on:
push:
branches:
- main
env:
PROJECT_ID: ${{ secrets.GKE_PROJECT }}
GKE_CLUSTER: ${{ secrets.GKE_CLUSTER }} # Add your cluster name here.
GKE_ZONE: ${{ secrets.GKE_ZONE }} # Add your cluster zone here.
DEPLOYMENT_NAME: ems-app # Add your deployment name here.
IMAGE: ciputra-ems-backend
jobs:
setup-build-publish-deploy:
name: Setup, Build, Publish, and Deploy
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout
uses: actions/checkout@v2
# Setup gcloud CLI
- uses: google-github-actions/setup-gcloud@94337306dda8180d967a56932ceb4ddcf01edae7
with:
service_account_key: ${{ secrets.GKE_SA_KEY }}
project_id: ${{ secrets.GKE_PROJECT }}
# Configure Docker to use the gcloud command-line tool as a credential
# helper for authentication
- run: |-
gcloud --quiet auth configure-docker
# Get the GKE credentials so we can deploy to the cluster
- uses: google-github-actions/get-gke-credentials@fb08709ba27618c31c09e014e1d8364b02e5042e
with:
cluster_name: ${{ env.GKE_CLUSTER }}
location: ${{ env.GKE_ZONE }}
credentials: ${{ secrets.GKE_SA_KEY }}
# Build the Docker image
- name: Build
run: |-
docker build \
--tag "gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA" \
--build-arg GITHUB_SHA="$GITHUB_SHA" \
--build-arg GITHUB_REF="$GITHUB_REF" \
.
# Push the Docker image to Google Container Registry
- name: Publish
run: |-
docker push "gcr.io/$PROJECT_ID/$IMAGE:$GITHUB_SHA"
# Set up kustomize
- name: Set up Kustomize
run: |-
curl -sfLo kustomize https://github.com/kubernetes-sigs/kustomize/releases/download/v3.1.0/kustomize_3.1.0_linux_amd64
chmod u+x ./kustomize
# Deploy the Docker image to the GKE cluster
- name: Deploy
run: |-
./kustomize edit set image LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/IMAGE:TAG=$GAR_LOCATION-docker.pkg.dev/$PROJECT_ID/$REPOSITORY/$IMAGE:$GITHUB_SHA
./kustomize build . | kubectl apply -k ./
kubectl rollout status deployment/$DEPLOYMENT_NAME
kubectl get services -o wide
但是当工作流到达部署部分时,它会显示错误
The Service "ems-app-service" is invalid: metadata.resourceVersion: Invalid value: "": must be specified for an update
现在我已经搜索到这实际上是不正确的,因为 resourceVersion 应该在每次更新时都会改变,所以我只是删除了它
这是我的 kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- service.yaml
- deployment.yaml
我的部署.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
generation: 1
labels:
app: ems-app
name: ems-app
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 3
revisionHistoryLimit: 10
selector:
matchLabels:
app: ems-app
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: ems-app
spec:
containers:
- image: gcr.io/ciputra-nusantara/ems@sha256:70c34c5122039cb7fa877fa440fc4f98b4f037e06c2e0b4be549c4c992bcc86c
imagePullPolicy: IfNotPresent
name: ems-sha256-1
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
还有我的 service.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
cloud.google.com/neg: '{"ingress":true}'
finalizers:
- service.kubernetes.io/load-balancer-cleanup
labels:
app: ems-app
name: ems-app-service
namespace: default
spec:
clusterIP: 10.88.10.114
clusterIPs:
- 10.88.10.114
externalTrafficPolicy: Cluster
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- nodePort: 30261
port: 80
protocol: TCP
targetPort: 80
selector:
app: ems-app
sessionAffinity: None
type: LoadBalancer
status:
loadBalancer:
ingress:
- ip: 34.143.255.159
【问题讨论】:
-
您使用的是哪个集群版本?
-
我最初是从 dockerfile 部署的,如果你是这个意思,我不会使用 GKE Standard 或自动驾驶仪手动创建集群
-
几个选项:1) 从您的服务规范中删除
clusterIP2) 在应用您的服务更新之前运行kubectl annotate svcems-app-service kubectl.kubernetes.io/last-applied-configuration- -
这是因为 last-applied-configuration annotation 中有一个 resourceVersion 字段,这不是预期的。通过运行以下命令删除 kubectl.kubernetes.io/last-applied-configuration 注释并再次更新服务。 "kubectl annotate svc my-service kubectl.kubernetes.io/last-applied-configuration-" 注解末尾的 - 告诉 Kubernetes 完全删除注解。
-
我尝试了你的两个建议,但它会发出一点警告,但我搜索它可以忽略,但后来我遇到另一个问题,这是我对构建的文件所做的更改不存在,我检查了修订详细信息,我的修订未部署。但后来我发现我将 kustomize 集映像设置为存储库而不是容器映像,所以我将其更改为
./kustomize edit set image gcr.io/PROJECT_ID/IMAGE:TAG=gcr.io/${{ env.PROJECT_ID }}/${{ env.IMAGE }}:${{ github.sha }}但新修订版仍未部署
标签: google-kubernetes-engine github-actions kubectl kustomize