Home  >  Article  >  Java  >  Kubernetes service--a brief example of running a single-instance stateful service

Kubernetes service--a brief example of running a single-instance stateful service

巴扎黑
巴扎黑Original
2017-07-22 15:21:212050browse

Goal

Create a PV in your environment
Create a Deployment of MySQl
Start by exposing MySQL to other pods using DNS names in the cluster

Previously

You need aKubernetes cluster and a kubectl command line tool that can connect to the cluster. If you don't have a cluster, you can use Minikube to create one.
We will create a PV (PersistentVolume) for data storage. Click here to view the supported PV types. This guide will use GCEPersistentDisk to demonstrate, but any PV type will work normally. GCEPersistentDisk only works on Google Compute Engine (GCE).

Create the disk in your environment

## In Google Compute Engine, run:

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

Then create a PV pointing to the mysql-disk just created. The following is a configuration file for creating a PV, pointing to the GCE disk mentioned above:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv
spec:
  capacity:
    storage: 20Gi
  accessModes:- ReadWriteOnce
  gcePersistentDisk:
    pdName: mysql-disk
    fsType: ext4

Note that the line pdName: mysql-disk matches the GCE environment creation disk above The name. If you want to create PVs in other environments, you can check out Persistent Volumes for details.
Create PV:

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

Deploy MySQL

You can create a stateful service through Kubernetes Deployment, and then use PVC (PersistentVolumeClaim) to connect to the existing PV. For example, the following YAML file describes a Deployment that runs MySQL and uses PVC. The file defines a volume mounted to /var/lib/mysql and creates a PVC that requires a 20G volume size.
Note: The password is defined in the YAML configuration file, which is not safe. Check out Kubernetes Secrets for more secure solutions.

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:- port: 3306
  selector:
    app: mysql
  clusterIP: None---apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  accessModes:- ReadWriteOnce
  resources:
    requests:
      storage: 20Gi---apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: mysql
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:      - image: mysql:5.6name: 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 content in the YAML file.

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

2. Display Deployment information.

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:       <none>
 NewReplicaSet:        mysql-63082529 (1/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. Display the pods created by Deployment.

kubectl get pods -l app=mysql

 NAME                   READY     STATUS    RESTARTS   AGE
 mysql-63082529-2z3ki   1/1       Running   0          3m

4. Check PV.

 kubectl describe pv mysql-pv

 Name:            mysql-pv
 Labels:          <none>
 Status:          Bound
 Claim:           default/mysql-pv-claim
 Reclaim Policy:  Retain
 Access Modes:    RWO
 Capacity:        20Gi
 Message:    
 Source:
     Type:        GCEPersistentDisk (a Persistent Disk 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:       <none>
 Capacity:     20Gi
 Access Modes: RWO
 No events.

Access MySQL instance

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

kubectl run -it --rm --image=mysql:5.6 mysql-client -- mysql -h <pod-ip> -ppassword

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

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

Update the image or other parts of the Deployment and you can use it as usual kubectl apply command to complete. The following are things to note when using stateful applications:

Do not expand the application. This application is only for singleton applications. The following PV can only be mapped to one Pod. For clustered stateful applications, check out the StatefulSet documentation.
Use strategy: type: Recreate in the Deployment’s YAML configuration document. It will tell Kubernetes not to use rolling update. Because Rolling update won't work, there won't be multiple Pods running at the same time. The strategy Recreate will delete the previous Pod when creating a new Pod with updated configuration.

Delete Deployment

Delete the Deployment object by name:

kubectl delete deployment,svc mysql
kubectl delete pvc mysql-pv-claim
kubectl delete pv mysql-pv

另外,如果你使用的是GCE disk,还需要删除对应的disk:

gcloud compute disks delete mysql-disk

 

文章转自:

The above is the detailed content of Kubernetes service--a brief example of running a single-instance stateful service. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn