Advanced Kubernetes concepts and Applications

Posted by northcave on Fri, 04 Feb 2022 18:48:39 +0100

1. K8S Storage Volume Abstract Volume

By default: the temporary storage inside the mysql container is used to store data files when the container starts

If mysql Pod is restarted, the mysql container will also be restarted, mysql will reinitialize the data file, and the original data file will be lost.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:5.7
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: petclinic
            - name: MYSQL_DATABASE
              value: petclinic
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  selector:
    app: mysql
  ports:
    - name: tcp
      port: 3306
      targetPort: 3306
  type: ClusterIP

apiVersion: apps/v1
kind: Deployment
metadata:
  name: petclinic
spec:
  replicas: 1
  selector:
    matchLabels:
      app: petclinic
  template:
    metadata:
      labels:
        app: petclinic
    spec:
      containers:
      - name: petclinic
        image: spring2go/spring-petclinic:1.0.1.RELEASE
        env:
          - name: SPRING_PROFILES_ACTIVE
            value: mysql
          - name: DATASOURCE_URL
            value: jdbc:mysql://mysql/petclinic
          - name: DATASOURCE_PASSWORD
            value: petclinic
          - name: DATASOURCE_USERNAME
            value: root
          - name: DATASOURCE_INIT_MODE
            value: always
---
apiVersion: v1
kind: Service
metadata:
  name: petclinic
spec:
  ports:
    - name: http
      port: 8080
      targetPort: 8080
      nodePort: 31080
  selector:
    app: petclinic
  type: NodePort

Where are the data files stored by default when mysql runs?

From this point of view, when petclinic Pod gets up, it connects to mysql on the back end

That would have a problem. What if the existing mysql Pod is deleted? Is there no data because there is no initialization data

As above: Related data will be lost

To solve the problem of missing startup, volume is introduced

 

As above: The shared local path is/tmp/data01

DirectoryOrCreate: Path recreated if it does not exist

Note: Not all paths can be shared [Docker Desktop]

2. K8S Storage Decoupling PVC and PV

As above: mysqlPod takes the form of a simple static binding. mysql Pod is directly associated with hostPath, path, type. That is, the publisher of mysql needs to know the storage details of volume s [which are tightly coupled]. So this publication cannot be migrated between different environments or public clouds. If you want to migrate, you must modify the publication. This is not what we expected

In order to solve this problem in software, a layer of abstraction is introduced. 

PVC: Specifies storage requirements, storage size, read/write mode, but does not specify details of storage implementation

 

 

3. K8S Resource Requests and Limits (Request and Limit)

Computer applications are resource intensive [including memory and cpu].

Resources in K8S are limited. Request and Limit are used to manage resource requests and restrictions [container level settings are supported]

Request: Indicates a request for estimated Cpu or Memory usage at the time the application is released. K8S is scheduled based on the idleness of nodes in the current cluster

Memory overrun delay:

You can see that the maximum allocatable memory under the current node is 2G

Adjusted: Only one custom was started. Three are needed and two are due to insufficient resources in Pengding

If resources are not sufficient, it will continue to restart and be kill ed

4. Memory Configuration for Java/Spring Applications in K8S

kubectl top po/no can view the corresponding pod or node resource usage in real time [dependent component Metrics Server]

[Use the top command or dashboard to achieve the desired effect, that is, to view it]

Advanced Kubernetes Concepts and Applications_ Bell-Bell-Bell_ bilibili 

Topics: Kubernetes Container Cloud Native