当前位置:网站首页>K8s-- deploy stand-alone MySQL and persist it

K8s-- deploy stand-alone MySQL and persist it

2022-06-23 14:42:00 It artist rookie

Write it at the front

This deployment uses pv,pvc,deployment,service Four resource objects .
pv It uses local Type of ,localStorage It should be the default .
 Insert picture description here

PV

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mypv1
  namespace: test-for-test
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain   #  Delete manually  
  storageClassName: local-storage # Indicates that the pv Of storageClass
  local:
    path: /k8sVolume/mysqlVolume/mypv1 # The full path of the volume on the node .( Must be an existing path , Otherwise, the container cannot be created ,deployment Will report a mistake ) It can be a directory or a block device ( disk 、 Partition …).
  nodeAffinity: # Node affinity configuration , Restrict which nodes can access this volume from 
    required:  
      nodeSelectorTerms: # An array type 
      - matchExpressions: # An array type 
        - key: kubernetes.io/hostname   # String type . Applied by the tag selector label key
          operator: In   # String type . Represents the relationship between a key and a set of values . The effective operators are In, NotIn, Exists, DoesNotExist.Gt, Lt.
          values: # An array of strings , If the operator is In or NotIn, be values Array must be non empty . If the operator is Exists or DoesNotExist, values Array must be empty . If the operator is Gt or Lt, be values Array must have only one element , This element will be interpreted as an integer .
          - roy-ubuntu # The host name of the node 

pvc

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mypvclocal
  namespace: test-for-test
spec:
  accessModes: # Access pattern   Claim when requesting storage with a specific access mode , Use the same access mode Convention as the volume .
    - ReadWriteMany
  volumeMode: Filesystem # Volume mode 
  resources:
    requests:
      storage: 4Gi
  storageClassName: local-storage #  An empty string or a specified value must be explicitly set here , Otherwise, it will be set as the default  StorageClass

deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment # Appoint deployment Name 
  namespace: test-for-test
  labels:  
    app: mysql-labels-app 
spec: # Statute 
  replicas: 3 #pod Number of copies of , Is how many you want to create pod Copy of . Optional fields . Its default value is 1.
  selector:  #selector  Field definition  Deployment  How to find the  Pods. Must match  .spec.template.metadata.labels, Otherwise the request will be  API  Refuse .
    matchLabels:
      app: mysql-pod
  template: #Deployment Pod  Templates ; It and  Pod  The rules of grammar are exactly the same .  It's just that it's nested here , So you don't need  apiVersion  or  kind.
    metadata:
      labels:
        app: mysql-pod
    spec: #pod Template conventions 
      containers: # Containers 
      - name: mysql # Name of the container 
        image: mysql:5.7 # The image used by the container 
        ports:
        - containerPort: 3306
        volumeMounts: 
        - name: mypv1 #pv Of name
          mountPath: /var/lib/mysqlMount# The corresponding path in the container , The path here is wrong , Should be /var/lib/mysql.MySQL All the data are in this , What needs to be persisted is the content under this path 
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
      volumes:
      - name: mypv1#pv Of name
        persistentVolumeClaim:
          claimName: mypvclocal 
      restartPolicy: Always #Deployment  Medium  Pod  The template must specify the appropriate label and the appropriate restart policy . Only  .spec.template.spec.restartPolicy  be equal to  Always  That's what's allowed , This is also the default setting when it is not specified 

service

apiVersion: v1
kind: Service
metadata:
  name: mysql-service
  namespace: test-for-test
spec:
  ports:
  - port: 3306
    nodePort: 30060
  selector:
    app: mysql-pod #  The  Service  All with tags will be  app: mysql-labels-app Exposed to an abstract  Service  On port (targetPort: The port on which the container receives traffic ;port: Abstract that can take any value  Service  port , other  Pod  Access... Through this port  Service
  type: NodePort

Connect... Using visualization tools MySQL

 Please add a picture description

The host address is the address of the node

Into the container

kubectl exec -it pod Of name -c stay deployment File specifies the container for name -n test-for-test – bash

[email protected]:/k8sVolume/mysqlVolume/mypv1# kubectl exec -it mysql-deployment-59f68f87cd-d4bt8 -c mysql -n test-for-test -- bash
[email protected]:/# ls
bin  boot  dev	docker-entrypoint-initdb.d  entrypoint.sh  etc	home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
原网站

版权声明
本文为[It artist rookie]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231353437648.html