Deploying a stateful application  |  Google Kubernetes Engine (GKE)  |  Google Cloud (2024)

Autopilot Standard

This page explains how to deploy a stateful application usingGoogle Kubernetes Engine (GKE).

Overview

Stateful applications save data topersistent disk storagefor use by the server, by clients, and by other applications. An example of astateful application is a database or key-value store to which data is saved andretrieved by other applications.

Persistent storage can bedynamically provisioned,so that the underlying volumes are created on demand. In Kubernetes, youconfigure dynamic provisioning by creating aStorageClass.In GKE, a default StorageClass allows you to dynamicallyprovision Compute Engine persistent disks.

Kubernetes uses the StatefulSetcontroller to deploy stateful applications as StatefulSet objects. Pods inStatefulSets are not interchangeable: each Pod has a unique identifier that ismaintained no matter where it is scheduled.

Stateful applications are different fromstateless applications, in whichclient data is not saved to the server between sessions.

You can learn more aboutpersistent storage in multi-zonal and regional clusters.

Before you begin

Before you start, make sure you have performed the following tasks:

  • Enable the Google Kubernetes Engine API.
  • Enable Google Kubernetes Engine API
  • If you want to use the Google Cloud CLI for this task, install and then initialize the gcloud CLI. If you previously installed the gcloud CLI, get the latest version by running gcloud components update.
  • Ensure your containerized application is stored in an image registry, such asArtifact Registry.

You can follow thequickstart,to enable the GKE API, create a cluster, and learn more aboutGKE.

Requesting persistent storage in a StatefulSet

Applications can request persistent storage with aPersistentVolumeClaim.

Typically, you must create PersistentVolumeClaim objects in addition to creatingthe Pod. However, StatefulSet objects include a volumeClaimTemplates array,which automatically generates the PersistentVolumeClaim objects. EachStatefulSet replica gets its own PersistentVolumeClaim object.

You can also use a preexisting disk in a StatefulSet.

Creating a StatefulSet

To create a StatefulSet resource, use the kubectl apply command.

The kubectl apply command uses manifest files to create, update, and deleteresources in your cluster. This is a declarativemethod of object configuration. This method retains writes made to live objects withoutmerging the changes back into the object configuration files.

Linux

The following manifest file is a simple example of a StatefulSet governed by aService that has been created separately:

apiVersion: apps/v1kind: StatefulSetmetadata: name: STATEFULSET_NAMEspec: selector: matchLabels: app: APP_NAME serviceName: "SERVICE_NAME" replicas: 3 updateStrategy: type: RollingUpdate template: metadata: labels: app: APP_NAME spec: containers: - name: CONTAINER_NAME image: ... ports: - containerPort: 80 name: PORT_NAME volumeMounts: - name: PVC_NAME mountPath: ... volumeClaimTemplates: - metadata: name: PVC_NAME annotations: ... spec: accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 1Gi

Replace the following:

  • STATEFULSET_NAME: the name of the StatefulSet.
  • SERVICE_NAME: the name of the Service.
  • APP_NAME: the name of the application run in the Pods.
  • CONTAINER_NAME: the name of the containers in the Pods.
  • PORT_NAME: the name of the port opened by the StatefulSet.
  • PVC_NAME: the name of the PersistentVolumeClaim.

In this file, the kind field specifies that a StatefulSet object should becreated with the specifications defined in the file. This example StatefulSetproduces three replicated Pods, and opens port 80 for exposing theStatefulSet to the internet.

Windows

When using clusters with Windows Server node pools,you must create a StorageClass because the default StorageClass uses ext4 asthe file system type, which only works for Linux containers. If you are using aCompute Engine persistent disk, you must use NTFS as the file storage typeas shown in the following example:

apiVersion: storage.k8s.io/v1kind: StorageClassmetadata: name: STORAGECLASS_NAMEparameters: type: pd-standard fstype: NTFSprovisioner: kubernetes.io/gce-pdreclaimPolicy: DeletevolumeBindingMode: WaitForFirstConsumer

The following StatefulSet manifest uses the StorageClass defined above. Itcreates four PersistentVolume and PersistentVolumeClaim pairs to representfour Compute Engine persistent disks. Each Pod in the StatefulSet consumesone persistent disk.

To ensure your Pods are correctly scheduled onto Windows Server nodes, youmust add a node selector to your Pod specification.

apiVersion: apps/v1kind: StatefulSetmetadata: name: STATEFULSET_NAMEspec: replicas: 4 selector: matchLabels: app: APP_NAME template: metadata: labels: app: APP_NAME name: CONTAINER_NAME spec: nodeSelector: kubernetes.io/os: windows containers: - name: CONTAINER_NAME image: ... ports: - containerPort: 80 name: PORT_NAME volumeMounts: - name: PVC_NAME mountPath: C:\mnt\state volumeClaimTemplates: - metadata: name: PVC_NAME spec: storageClassName: STORAGECLASS_NAME accessModes: [ "ReadWriteOnce" ] resources: requests: storage: 1Gi

Replace the following:

  • APP_NAME: the name of the application run in the Pods.
  • STATEFULSET_NAME: the name of the StatefulSet.
  • CONTAINER_NAME: the name of the containers in the Pods.
  • PORT_NAME: the name of the port opened by the StatefulSet.
  • PVC_NAME: the name of the PersistentVolumeClaim.
  • STORAGECLASS_NAME: the name of the StorageClass.

To create a StatefulSet resource, run the following command replacingSTATEFULSET_FILE with the manifest file name:

kubectl apply -f STATEFULSET_FILE

You can also use kubectl apply -f DIRECTORY/ to createall objects (except existing ones) defined in configuration files stored in adirectory.

Inspecting a StatefulSet

kubectl

To inspect the StatefulSet, run the following command:

kubectl get statefulset STATEFULSET_NAME -o yaml

This command displays the live configuration of the StatefulSet resource inYAML format.

To list the Pods created by the StatefulSet, run the following command:

kubectl get pods -l app=APP_NAME

In this command, the -l flag instructs kubectl to get all Pods labeledfor the APP_NAME.

The output is similar to the following:

NAME READY STATUS RESTARTS AGEpod-name 1/1 Running 0 1mpod-name 1/1 Running 0 1m

To get detailed information about the StatefulSet, run the following command:

kubectl describe statefulset STATEFULSET_NAME

To get information about a specific Pod, run the following command:

kubectl describe pod POD_NAME

To list the PersistentVolumeClaim objects that were created, run thefollowing command:

kubectl get pvc

The output is similar to the following:

NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGESTATEFULSET_NAME-PVC_NAME-0 Bound pvc-bdff4e1e-183e-11e8-bf6d-42010a800002 1G RWO standard 9sSTATEFULSET_NAME-PVC_NAME-1 Bound pvc-bdff4e1e-183e-11e8-bf6d-42010a800003 1G RWO standard 9sSTATEFULSET_NAME-PVC_NAME-2 Bound pvc-bdff4e1e-183e-11e8-bf6d-42010a800004 1G RWO standard 9s

To get information about a specific PersistentVolumeClaim, run the following command:

kubectl describe pvc STATEFULSET_NAME-PVC_NAME-0

To get information about a specific PersistentVolume, run the following command:

kubectl describe pv PV_NAME

Console

To inspect a StatefulSet, perform the following steps:

  1. Go to the Workloads page in the Google Cloud console.

    Go to Workloads

  2. In the workloads list, click the name of the StatefulSet you want to inspect.

  3. On the Stateful Set details page, do any of the following:

    • Click the Revision History tab to see the StatefulSet's revision history.
    • Click the Events tab to see all events related to the StatefulSet.
    • Click the Logs tab to see container logs for the StatefulSet.
    • Click the YAML tab to see, copy, or download the configurationYAML for the StatefulSet.

Updating a StatefulSet

There are multiple ways of updating StatefulSets. The common, declarative methodis kubectl apply. To update the StatefulSet directly from your shell or in apreferred editor, you can use kubectl edit. You can also use the YAML editorfrom the GKE Workloads menu in the Google Cloud console.

You can roll out updates to thePods specification for a StatefulSet resource, such as its image, resource usage/requests, or configuration.

kubectl apply

You can update the StatefulSet by applying a new or updated manifest file.This is useful for making various changes to your StatefulSet, such as whenscaling or for specifying a new version of your application.

To update a StatefulSet, run the following command:

kubectl apply -f STATEFULSET_FILE

Replace STATEFULSET_FILE with the updated manifest file.

The kubectl apply command applies a manifest file to a resource. If thespecified resource does not exist, it is created by the command.

For more information about kubectl apply, see thekubectl reference documentation.

Console

To edit the live configuration of a StatefulSet, perform the followingsteps:

  1. Go to the Workloads page in the Google Cloud console.

    Go to Workloads

  2. In the workloads list, click the name of the StatefulSet you want to modify.

  3. Click edit Edit.

  4. Change the configuration YAML as desired.

  5. Click Save.

Inspecting update rollout

kubectl

To inspect the rollout of the StatefulSet, run the following command:

kubectl rollout status statefulset STATEFULSET_NAME

To see the StatefulSet's rollout history, run the following command:

kubectl rollout history statefulset STATEFULSET_NAME

To undo a rollout, run the following command:

kubectl rollout undo statefulset STATEFULSET_NAME

Console

To see the revision history of a StatefulSet, perform the following steps:

  1. Go to the Workloads page in the Google Cloud console.

    Go to Workloads

  2. In the workloads list, click the name of the StatefulSet you want to inspect.

  3. Click the Revision History tab.

  4. Select the desired revision.

Update strategies

StatefulSet’s updateStrategy field allows you to configure and disableautomated rolling updates for containers, labels, resource requests, limits, andannotations for the Pods in a StatefulSet.

You can learn more aboutUpdate Strategies for StatefulSets in the Kubernetes documentation.

Scaling a StatefulSet

kubectl

The kubectl scale command can be used at any time to scale your StatefulSet.

To manually scale a StatefulSet, run the following command:

kubectl scale statefulset STATEFULSET_NAME --replicas NUMBER_OF_REPLICAS

Replace NUMBER_OF_REPLICAS with the desired number ofreplicated Pods.

Console

To scale a StatefulSet, perform the following steps:

  1. Go to the Workloads page in the Google Cloud console.

    Go to Workloads

  2. In the workloads list, click the name of the StatefulSet you want to modify.

  3. Click listActions > Scale > Edit replicas.

  4. Enter the new number of Replicas for the StatefulSet.

  5. Click Scale.

Deleting a StatefulSet

kubectl

To delete a StatefulSet, run the following command:

kubectl delete statefulset STATEFULSET_NAME

Console

To delete a StatefulSet, perform the following steps:

  1. Go to the Workloads page in the Google Cloud console.

    Go to Workloads

  2. In the workloads list, select one or more StatefulSets you want to delete.

  3. Click delete Delete.

  4. When prompted to confirm, click Delete.

What's next

  • Learn how to deploy a MySQL cluster on GKE for high availability
  • Learn about deploying stateless applications.
  • Take a tutorial about upgrading a cluster running a stateful workload.
Deploying a stateful application  |  Google Kubernetes Engine (GKE)  |  Google Cloud (2024)

FAQs

Can you use a Deployment for stateful applications? ›

Kubernetes Deployment is usually used for stateless applications. However, we can save the state of Deployment by attaching a Persistent Volume to it and make it stateful. The deployed pods will share the same Volume, and the data will be the same across all of them.

What is the difference between GKE Deployment and StatefulSet? ›

The difference between StatefulSets and Deployments reflects the divide between stateful and stateless systems. As their name suggests, StatefulSets are designed to run your app's stateful components, while Deployments are used for stateless ones.

How to deploy an application in GKE? ›

To deploy a particular version of your application with gke-deploy :
  1. Make sure your Kubernetes resource file is referring to the correct container image tag or digest.
  2. Add the gke-deploy step in your build configuration file: YAML JSON. See more code actions. ...
  3. Start your build: See more code actions. Dismiss View.

What is a stateful application in Kubernetes? ›

Stateful applications save data to persistent disk storage for use by the server, by clients, and by other applications. An example of a stateful application is a database or key-value store to which data is saved and retrieved by other applications.

What are the disadvantages of StatefulSet? ›

The biggest limitation of StatefulSets is that they make certain assumptions about how stateful resources are managed and configured. For example, they're designed only to work with stateful storage that is provisioned manually or via a PersistentVolume provisioner.

Why not always use StatefulSet? ›

Conclusion. Both Deployment and StatefulSet try to maximize the availability - but StatefulSet cannot sacrifice data consistency (e.g. your state), so it cannot act as fast as Deployment (stateless) apps can.

What is the difference between GKE and Kubernetes? ›

Kubernetes clusters have a set of management nodes called the control plane, which run system components such as the Kubernetes API server. In GKE, Google manages the control plane and system components for you.

What is the difference between Deployment and service in GKE? ›

Kubernetes Deployment springs into action, creating the specified number of replicas of your application pods. These pods come to life, running your application and catering to incoming requests. Kubernetes Service steps in, providing a stable endpoint for other services or external users to access your application.

What is the difference between stateful and stateless in Kubernetes? ›

Stateless applications tend to include containerized microservices apps, CDN, print services, or any short term workers. and are easy for both deploying and managing resources. Stateful applications typically involve some database, such as Cassandra, MongoDB, or MySQL and processes a read and/or write to it.

What is GKE in Google Cloud? ›

Deep dive into Google Kubernetes Engine. GKE is a feature-rich, managed Kubernetes platform that enables deployment, configuration, and orchestration of containers using Google Cloud infrastructure. A GKE environment is typically composed of multiple Google Compute Engine instances grouped to form a Kubernetes cluster.

How do I deploy Kubernetes to the cloud? ›

A Cloud Shell session opens inside a new frame at the bottom of the Google Cloud console and displays a command-line prompt.
  1. Create a repository. ...
  2. Building the container image. ...
  3. Running your container locally (optional) ...
  4. Pushing the Docker image to Artifact Registry. ...
  5. Creating a GKE cluster. ...
  6. Deploying the sample app to GKE.

What are the steps for deploying in Kubernetes? ›

Prerequisites for Kubernetes Deployment
  1. STEP 1: Installing of Kubernetes on master and work nodes:
  2. STEP 2: Clone a repository.
  3. STEP 3: Building an image from Dockfile.
  4. STEP 4: Push the Docker image to a Registry.
  5. Login to the Docker hub.
  6. Push the image to register.
  7. Command - Line Interface:
  8. Yaml Manifest.
Apr 12, 2024

What are the disadvantages of stateful application? ›

Cons: Developers have to work harder to keep state info, which makes it challenging to develop and maintain stateful apps. Creating a stateful app is not an easy task. It's hard to keep track of state info across instances.

What is an example of a stateful application? ›

A Stateful application remembers specific details of a user like profile, preferences, and user actions. This information is considered as the 'Status' of a system. For example, your shopping cart while using any website in Cloud.

What is an example of a stateful Deployment? ›

Other examples of stateful applications include MySQL clusters, Redis, Kafka, MongoDB, and others. Given this difference, Deployment is more suited to work with stateless applications. As far as a Deployment is concerned, Pods are interchangeable. While a StatefulSet keeps a unique identity for each Pod it manages.

What is the difference between stateful and Deployment? ›

Deployments are ideal for stateless applications, where each instance is essentially identical and interchangeable. On the other hand, StatefulSets are designed for stateful applications that require stable, unique identities and persistent storage.

Can stateless and stateful services be deployed as part of the same service fabric application? ›

When building applications consisting of microservices you typically have a combination of stateless web apps (ASP.NET, node. js, etc.) calling onto stateless and stateful business middle-tier services, all deployed into the same Service Fabric cluster using the Service Fabric deployment commands.

What are the examples of stateful application? ›

Here are some popular examples of stateful applications:
  • Online shopping carts that keep track of what you put in them.
  • Banking systems that keep track of account information.
  • Social media sites that display information based on user's preferences.

Top Articles
Safaricom Vodafone/Voda Africa Roaming Bundles
Desert Financial — Phoenix Desert Sky: Home Mortgage, Auto Loans, Business Banking in Phoenix, AZ
Warren Ohio Craigslist
Unit 30 Quiz: Idioms And Pronunciation
Compare Foods Wilson Nc
Tmf Saul's Investing Discussions
Research Tome Neltharus
Sissy Hypno Gif
Umn Pay Calendar
Steve Strange - From Punk To New Romantic
Jesus Revolution Showtimes Near Chisholm Trail 8
Simple Steamed Purple Sweet Potatoes
Dallas Cowboys On Sirius Xm Radio
Lazarillo De Tormes Summary and Study Guide | SuperSummary
Air Force Chief Results
Gopher Hockey Forum
Selfservice Bright Lending
Military life insurance and survivor benefits | USAGov
Mtr-18W120S150-Ul
Yosemite Sam Hood Ornament
University Of Michigan Paging System
D2L Brightspace Clc
Cognitive Science Cornell
R/Airforcerecruits
Remnants of Filth: Yuwu (Novel) Vol. 4
Co10 Unr
Www Mydocbill Rada
Www.1Tamilmv.con
Dl.high Stakes Sweeps Download
Isablove
Lininii
How often should you visit your Barber?
Guide to Cost-Benefit Analysis of Investment Projects Economic appraisal tool for Cohesion Policy 2014-2020
Wells Fargo Bank Florida Locations
Broken Gphone X Tarkov
The Ultimate Guide to Obtaining Bark in Conan Exiles: Tips and Tricks for the Best Results
Midsouthshooters Supply
Pinellas Fire Active Calls
Craigslist Pa Altoona
Conan Exiles Armor Flexibility Kit
COVID-19/Coronavirus Assistance Programs | FindHelp.org
Smite Builds Season 9
Brake Pads - The Best Front and Rear Brake Pads for Cars, Trucks & SUVs | AutoZone
Grizzly Expiration Date Chart 2023
Brother Bear Tattoo Ideas
Caphras Calculator
El Patron Menu Bardstown Ky
Diamond Spikes Worth Aj
Pilot Travel Center Portersville Photos
Verilife Williamsport Reviews
Electronics coupons, offers & promotions | The Los Angeles Times
Latest Posts
Article information

Author: Errol Quitzon

Last Updated:

Views: 6209

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Errol Quitzon

Birthday: 1993-04-02

Address: 70604 Haley Lane, Port Weldonside, TN 99233-0942

Phone: +9665282866296

Job: Product Retail Agent

Hobby: Computer programming, Horseback riding, Hooping, Dance, Ice skating, Backpacking, Rafting

Introduction: My name is Errol Quitzon, I am a fair, cute, fancy, clean, attractive, sparkling, kind person who loves writing and wants to share my knowledge and understanding with you.