Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize stateful service of running single instance in Kubernetes

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to implement the stateful service of running single instance in Kubernetes. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

target

Create a PV in your environment

Create a Deployment for MySQl

Expose MySQL to other pod in the cluster as DNS name

Before we begin

You need a Kubernetes cluster, a kubectl command line tool that can connect to the cluster. If you don't have a cluster, you can use Minikube to create it.

We will create a PV (PersistentVolume) for data storage. Click here to see the types supported by PV, which will be demonstrated using GCEPersistentDisk, but any PV type works fine. GCEPersistentDisk can only work on Google Compute Engine (GCE).

Create disks in your environment

On Google Compute Engine, run:

Gcloud compute disks create-size=20GB mysql-disk

Then create a PV that points to the mysql-disk you just created. Here is a configuration file to create the PV, pointing to the GCE disk mentioned above:

ApiVersion: v1kind: PersistentVolumemetadata: name: mysql-pvspec: capacity: storage: 20Gi accessModes:-ReadWriteOnce gcePersistentDisk: pdName: mysql-disk fsType: ext4

Notice that the line pdName: mysql-disk matches the name of the disk created by the GCE environment above. If you want to create a PV in another environment, you can check the Persistent Volumes for more information.

Create a PV:

Kubectl create-f https://k8s.io/docs/tasks/run-application/gce-volume.yaml deployment MySQL

You can create a stateful service through Kubernetes Deployment, and then use PVC (PersistentVolumeClaim) to connect to an existing PV. For example, the following YAML file describes a Deployment that runs MySQL and uses PVC. The file defines a mount to / var/lib/mysql volume and creates a PVC that requires a 20G volume size.

Note: the password is defined in the YAML configuration file, which is not secure. Check out Kubernetes Secrets for a more secure solution.

ApiVersion: v1kind: Servicemetadata: name: mysqlspec: ports:-port: 3306 selector: app: mysql clusterIP: None---apiVersion: v1kind: PersistentVolumeClaimmetadata: name: mysql-pv-claimspec: accessModes:-ReadWriteOnce resources: requests: storage: 20Gi---apiVersion: apps/v1beta1kind: Deploymentmetadata: name: mysqlspec: strategy: type: Recreate template: metadata: labels: app: mysqlspec: containers: -image: mysql:5.6 name: mysql env: # Use secret in real usage-name: MYSQL_ROOT_PASSWORD value: password ports:-containerPort: 3306 name: mysql volumeMounts:-name: mysql-persistent-storage mountPath: / var/lib/mysql volumes:-name: mysql-persistent-storage persistentVolumeClaim: claimName: mysql-pv-claim

1. Deploy the contents of the YAML file.

Kubectl create-f https://k8s.io/docs/tasks/run-application/mysql-deployment.yaml

two。 Displays information for Deployment.

Kubectl describe deployment mysql Name: mysql Namespace: default CreationTimestamp: Tue 01 Nov 2016 11:18:45-0700 Labels: app=mysql Selector: app=mysql Replicas: 1 updated | 1 total | 0 available | 1 unavailable StrategyType: Recreate MinReadySeconds: 0 OldReplicaSets: NewReplicaSet: mysql-63082529 (1 replicas created) Events: FirstSeen LastSeen Count From SubobjectPath Type Reason Message- -33s 33s 1 {deployment-controller} Normal ScalingReplicaSet Scaled up replica set mysql-63082529 to 1

3. Displays the pod created by Deployment.

Kubectl get pods-l app=mysql NAME READY STATUS RESTARTS AGE mysql-63082529-2z3ki 1 Compact 1 Running 0 3m

4. Check the PV.

Kubectl describe pv mysql-pv Name: mysql-pv Labels: Status: Bound Claim: default/mysql-pv-claim Reclaim Policy: Retain Access Modes: RWO Capacity: 20Gi Message: Source: Type: GCEPersistentDisk (a PersistentDisk resource in Google Compute Engine) PDName: mysql-disk FSType: ext4 Partition: 0 ReadOnly: false No events.

5. Check the PVC.

Kubectl describe pvc mysql-pv-claim Name: mysql-pv-claim Namespace: default Status: Bound Volume: mysql-pv Labels: Capacity: 20Gi Access Modes: RWO No events. Access MySQL instance

The previous YAML file creates a service that allows other Pod in the cluster to access the database. The service option clusterIP:None causes the DNS name of the service to be resolved directly to the IP address of the Pod. This is the best way to use it when your service has only one Pod and you don't plan to increase the number of Pod.

Run a Mysql client to connect to the Mysql service:

Kubectl run-it-- rm-- image=mysql:5.6 mysql-client-- mysql- h-ppassword

The above command creates a new Pod in the cluster that runs a mysql client connected to the Mysql Server of the above service. If it connects successfully, it means that the stateful MySQL database is up and running successfully.

Waiting for pod default/mysql-client-274442439-zyp6i to be running, status is Pending, pod ready: falseIf you don't see a command prompt, try pressing enter.mysql > Update

Updating the image or other part of the Deployment can also be done as usual using the kubectl apply command. Here are some things to pay attention to when using stateful applications:

Do not expand the application. This application is only for a single application. The following PV can only be mapped to one Pod. For stateful applications of clusters, check the StatefulSet documentation.

Use strategy: type: Recreate in the YAML configuration document for Deployment. It tells Kubernetes not to use rolling update. Because Rolling update will not work, there will not be multiple Pod running at the same time. The policy Recreate deletes the previous Pod when it creates a new Pod with the updated configuration.

Delete Deployment

Delete the Deployment object by name:

Kubectl delete deployment,svc mysqlkubectl delete pvc mysql-pv-claimkubectl delete pv mysql-pv

In addition, if you are using GCE disk, you need to delete the corresponding disk:

This is the end of gcloud compute disks delete mysql-disk 's article on "how to implement stateful service for running a single instance in Kubernetes". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report