Deploying to GKE  |  Cloud Build Documentation  |  Google Cloud (2024)

Stay organized with collections Save and categorize content based on your preferences.

This page explains how to deploy an application to Kubernetes usingCloud Build.

Cloud Build provides a gke-deploy builder that enables you to deploy a containerized application to aGKE cluster.

gke-deploy is a wrapper around kubectl, the command-line interface forKubernetes. It applies Google's recommended practices for deploying applicationsto Kubernetes by:

  • Updating the application's Kubernetes resource file to use the containerimage's digest instead of a tag.

  • Adding recommended labels to the Kubernetes resource file.

  • Retrieving credentials for the GKE clustersto which you're deploying the image.

  • Waiting for the Kubernetes resource file that was submitted to be ready.

If you want to deploy your applications using kubectl directly and do not needadditional functionality, Cloud Build also provides a kubectl builder that you can use to deploy your application to aGKE cluster.

Before you begin

  • Create a GKE cluster, if you do not have one yet.

  • Have your containerized application in the form of source code and aDockerfile ready. Your source code needs to be stored in a repository, such asCloud Source Repositories, GitHub, or Bitbucket.

  • You'll need at least one Kubernetes resource file describing the Kubernetesresources used to run your application. If you do not have Kubernetesresource files, use the following steps to generate one for yourapplication:

    1. Open the Kubernetes Engine clusters page in the Google Cloud console.
    2. On the Kubernetes Engine clusters page, click Deploy.
    3. Select your container and click Continue. You will see the Configurationsection.
    4. Under Configuration YAML, click View YAML to get a sample Kubernetesresource file.

Required IAM permissions

Add the Google Kubernetes Engine Developer role to your account:

  1. Open the Cloud Build Settings page:

    Open the Cloud Build Settings page

    You'll see the Service account permissions page:

    Deploying to GKE | Cloud Build Documentation | Google Cloud (3)

  2. Set the status of the Kubernetes Engine Developer role to Enabled.

Deploying a pre-built container image

To deploy a particular version of your application with gke-deploy:

  1. Make sure your Kubernetes resource file is referring to thecorrect container image tag or digest.

  2. Add the gke-deploy step in your build configuration file:

    YAML

    steps:...# deploy container image to GKE- name: "gcr.io/cloud-builders/gke-deploy" args: - run - --filename=kubernetes-resource-file - --location=location - --cluster=cluster

    JSON

    { "steps": [ { "name": "gcr.io/cloud-builders/gke-deploy", "args": [ "run", "--filename=kubernetes-resource-file", "--location=location", "--cluster=cluster" ] } ]}

    Where:

    • kubernetes-resource-file is the file path of your Kubernetes resourcefile or the directory path containing your Kubernetes resource files.
    • cluster is the name of the GKE cluster that the applicationwill be deployed to.
    • location is the region/zone of the cluster.

    For more information on available flags, see gke-deploy run flags.

  3. Start your build:

    gcloud builds submit --region=REGION --project=project-id --config build-config

    Where:

    • project-id is the ID for your project.
    • build-config is the name of your build configuration file.
    • REGION is one of the supported build regions.

Building and deploying a new container image

To build a new container image and deploy the new container image:

  1. Update your Kubernetes resource file with the new containerimage using the --image attribute:

    YAML

    steps: # build the container image- name: "gcr.io/cloud-builders/docker" args: ["build", "-t", "gcr.io/project-id/image:tag", "."] # push container image- name: "gcr.io/cloud-builders/docker" args: ["push", "gcr.io/project-id/image:tag"] # deploy container image to GKE- name: "gcr.io/cloud-builders/gke-deploy" args: - run - --filename=kubernetes-resource-file - --image=gcr.io/project-id/image:tag - --location=location - --cluster=cluster

    JSON

    { "steps": [ { "name": "gcr.io/cloud-builders/docker", "args": [ "build", "-t", "gcr.io/project-id/image:tag", "." ] }, { "name": "gcr.io/cloud-builders/docker", "args": [ "push", "gcr.io/project-id/image:tag" ] }, { "name": "gcr.io/cloud-builders/gke-deploy", "args": [ "run", "--filename=kubernetes-resource-file", "--image=gcr.io/project-id/image:tag", "--location=location", "--cluster=cluster" ] } ]}

    Where:

    • project-id is the ID for your project.
    • image is the desired name of the container image, usually the applicationname.
    • tag is the tag of the container image.
      • If you are building a new container image with each commit, a good practiceis to use the commit short SHA as a tag. Cloud Build makes thisavailable as a default substitution, $SHORT_SHA.
    • kubernetes-resource-file is the file path of your Kubernetes resourcefile or the directory path containing your Kubernetes resource files.
    • cluster is the name of the GKE cluster that the applicationwill be deployed to.
    • location is the region/zone of the cluster will be deployed to.
  2. Start your build:

    gcloud builds submit --region=REGION --project=project-id --config build-config

    Where:

    • project-id is the ID for your project.
    • build-config is the name of your build configuration file.
    • REGION is one of the supported build regions.

Automating deployments

You can automate the deployment of your application to GKE bycreating a trigger in Cloud Build. You can configure triggers tobuild and deploy images whenever you push changes to your code.

To create a build trigger:

  1. Open the Triggers page in the Google Cloud console:

    Open the Triggers page

  2. Select your project from the project selector drop-down menu at the top of the page.

  3. Click Open.

  4. Click Create trigger.

    On the Create trigger page, enter the following settings:

    1. Enter a name for your trigger.

    2. Select the repository event to start your trigger.

    3. Select the repository that contains your source code and build config file.

    4. Specify the regex for the branch or tag name that will start your trigger.ee

    5. Choose a Configuration for your trigger.

      If you choose a Cloud Build configuration file, you can specify Substitution variables by providing a variable name and the value you want to associate with that variable. In the example below, the user-defined substitution variable _CLOUDSDK_CONTAINER_CLUSTER specifies the cluster to deploy to, and the user-defined substitution variable _CLOUDSDK_COMPUTE_ZONE specifies its location. If you want to deploy to a different cluster, you can use the same build configuration and only need to change the values of the substitution variables:

      YAML

      steps:...# deploy container image to GKE- name: "gcr.io/cloud-builders/gke-deploy" args: - run - --filename=kubernetes-resource-file - --image=gcr.io/project-id/image:tag - --location=${_CLOUDSDK_COMPUTE_ZONE} - --cluster=${_CLOUDSDK_CONTAINER_CLUSTER}

      JSON

      { "steps": [ { "name": "gcr.io/cloud-builders/gke-deploy", "args": [ "run", "--filename=kubernetes-resource-file", "--image=gcr.io/project-id/image:tag", "--location=${_CLOUDSDK_COMPUTE_ZONE}", "--cluster=${_CLOUDSDK_CONTAINER_CLUSTER}" ] } ]}

      Where:

      • kubernetes-resource-file is the file path ofyour Kubernetes configuration file or the directory path containingyour Kubernetes resource files.
      • project-id is the ID for your project.
      • image is the desired name of the container image, usually the application name.
      • tag is the tag of the container image.

      To learn more about how to define substitutions for build configuration files, see Using user-defined substitutions.

  5. Click Create to save your build trigger.

When you push code to your repository, Cloud Build will automaticallytrigger a build. To learn more about build triggers, see Creating and managingbuild triggers.

What's next

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-09-10 UTC.

Deploying to GKE  |  Cloud Build Documentation  |  Google Cloud (2024)
Top Articles
The 6 D's
Advantages and Disadvantages of Quantum Computing: Explained
No Hard Feelings (2023) Tickets & Showtimes
Lengua With A Tilde Crossword
Elleypoint
Skamania Lodge Groupon
Chatiw.ib
Chambersburg star athlete JJ Kelly makes his college decision, and he’s going DI
Here are all the MTV VMA winners, even the awards they announced during the ads
Truist Park Section 135
Gw2 Legendary Amulet
Over70Dating Login
Zendaya Boob Job
Culvers Tartar Sauce
Sarpian Cat
Pvschools Infinite Campus
Nonne's Italian Restaurant And Sports Bar Port Orange Photos
Abortion Bans Have Delayed Emergency Medical Care. In Georgia, Experts Say This Mother’s Death Was Preventable.
Tvtv.us Duluth Mn
Honda cb750 cbx z1 Kawasaki kz900 h2 kz 900 Harley Davidson BMW Indian - wanted - by dealer - sale - craigslist
Carson Municipal Code
White Pages Corpus Christi
Walgreens Tanque Verde And Catalina Hwy
Jet Ski Rental Conneaut Lake Pa
Dulce
Hannaford Weekly Flyer Manchester Nh
Marilyn Seipt Obituary
Royalfh Obituaries Home
Ordensfrau: Der Tod ist die Geburt in ein Leben bei Gott
Paradise Point Animal Hospital With Veterinarians On-The-Go
Kids and Adult Dinosaur Costume
Sf Bay Area Craigslist Com
Melissa N. Comics
Colin Donnell Lpsg
Pill 44615 Orange
Atlantic Broadband Email Login Pronto
3302577704
Wattengel Funeral Home Meadow Drive
Mckinley rugzak - Mode accessoires kopen? Ruime keuze
Stanley Steemer Johnson City Tn
Culver's of Whitewater, WI - W Main St
Miracle Shoes Ff6
Yakini Q Sj Photos
Cleveland Save 25% - Lighthouse Immersive Studios | Buy Tickets
The Horn Of Plenty Figgerits
Breaking down the Stafford trade
Canvas Elms Umd
Aloha Kitchen Florence Menu
Unblocked Games 6X Snow Rider
Big Brother 23: Wiki, Vote, Cast, Release Date, Contestants, Winner, Elimination
Charlotte North Carolina Craigslist Pets
Palmyra Authentic Mediterranean Cuisine مطعم أبو سمرة
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 6239

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.