20220819实习进度汇报

实习进度汇报

这几天在做什么:

  • 数据持久化 pvc pv sc
  • 学习 secret、configmap 用法并应用;

这些事情的进度:

1.使用hugo更新实习日记至github主页

github主页 (linjianshu.github.io)

https://github.com/linjianshu/e-book-Gin

https://github.com/linjianshu/client-go

2.数据持久化
  • 使用hostpath(不推荐 但是简单)

statefulset.yaml文件

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-mysql-statefulset
spec:
  serviceName: my-mysql-service
  replicas: 3
  selector:
    matchLabels:
      app: my-mysql
  template:
    metadata:
      labels:
        app: my-mysql
    spec:
      affinity:
        #节点的亲和性  好像是没有节点反亲和的
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: node   #要求pod不允许运行在标签node=master的节点上
                    operator: NotIn
                    values:
                      - master
        #pod的反亲和性
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"  #要求pod不允许运行在已有pod标签为webapp的节点上
                    operator: In
                    values:
                      - "my-mysql"
              topologyKey: "kubernetes.io/hostname"
      containers:
        - name: my-mysql
          image: mysql:latest
          imagePullPolicy: IfNotPresent  #IfNotPresent 仅本地没有镜像时才远程拉取 , Always永远都是远程拉取 , Never永远只是用本地镜像 本地没有则报错
          args:
            - "--character-set-server=utf8"     #  指定字符编码
            - "--collation-server=utf8_general_ci"    #  指定字符编码
          env:
            - name: MYSQL_ROOT_PASSWORD    #  指定root用户的用户名
              value: "123456"
            - name: MYSQL_DATABASE    # 新建的数据库
              value: "datashare"
          volumeMounts:
            - name: data #与65行对应
              mountPath: /var/lib/mysql   #容器里面挂载的路径
      volumes:
        - name: data  #卷名字
          hostPath:
            path: /data/mysql-data  #节点上的路径
            type: DirectoryOrCreate  #指向一个目录 不存在自动创建

---
apiVersion: v1
kind: Service
metadata:
  name: my-mysql-service
spec:
  selector:
    app: my-mysql
  type: ClusterIP
  clusterIP: None
  ports:
    - port: 3306
      targetPort: 3306
  • 运行截图

image-20220820154026704
image-20220820154026704

进入容器创建student数据库

image-20220820154138057
image-20220820154138057

进入节点minikube-m03查看是否挂载成功

image-20220820154356207
image-20220820154356207

删除pod查看是否数据会丢失

image-20220820154606129
image-20220820154606129

image-20220820154621624
image-20220820154621624

  • 使用pvc-pv-sc

SC storage class:将存储卷划分为不同的种类 例如SSD 普通磁盘 本地磁盘 按需使用

云服务商会提供

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer

Persistent Volume PV:描述数据卷的具体信息 , 例如磁盘大小 访问模式 文档 类型

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysqldata
spec:
  capacity:
    storage: 2Gi
  volumeMode: Filesystem  #Filesystem(文件系统) Block(块)
  accessModes:
    - ReadWriteOnce  #卷可以被一个节点以读写方式挂载
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-storage
  local:
    path: /root/data
  nodeAffinity:
    required:
      #通过hostname 限定在某个节点创建存储卷
      nodeSelectorTerms:
        - matchExpressions:
            - key: node
              operator: In
              values:
                - worker02

PVC persistent volume claim:对存储需求的一个申明 可以理解为一个申请单 系统根据这个申请单去找一个合适的PV 还可以根据PVC自动创建PV

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysqldata
spec:
  accessModes: ["ReadWriteOnce"]
  storageClassName: "local-storage"
  resources:
    requests:
      storage: 2Gi
为什么要这么多层的抽象
  • 更好的分工 运维人员负责提供好存储 开发人员不需要关注磁盘细节 只需要写一个申请单
  • 方便云服务商提供不同类型 配置细节不需要开发者关注 只需要写一个申请单
  • 动态创建 开发人员写好申请单后 供应商可以根据需求自动创建所需存储卷
本地磁盘示例

sta的yaml 使用pvc来挂载

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-mysql-statefulset
spec:
  serviceName: my-mysql-service
  replicas: 3
  selector:
    matchLabels:
      app: my-mysql
  template:
    metadata:
      labels:
        app: my-mysql
    spec:
      affinity:
        #节点的亲和性  好像是没有节点反亲和的
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: node   #要求pod不允许运行在标签node=master的节点上
                    operator: NotIn
                    values:
                      - master
        #pod的反亲和性
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"  #要求pod不允许运行在已有pod标签为webapp的节点上
                    operator: In
                    values:
                      - "my-mysql"
              topologyKey: "kubernetes.io/hostname"
      containers:
        - name: my-mysql
          image: mysql:latest
          imagePullPolicy: IfNotPresent  #IfNotPresent 仅本地没有镜像时才远程拉取 , Always永远都是远程拉取 , Never永远只是用本地镜像 本地没有则报错
          args:
            - "--character-set-server=utf8"     #  指定字符编码
            - "--collation-server=utf8_general_ci"    #  指定字符编码
          env:
            - name: MYSQL_ROOT_PASSWORD    #  指定root用户的用户名
              value: "123456"
            - name: MYSQL_DATABASE    # 新建的数据库
              value: "datashare"
          volumeMounts:
            - name: mysql-data #与71行对应
              mountPath: /var/lib/mysql   #容器里面挂载的路径
      volumes:
        #local-storage 要通过pvc 前提是先创建pvc
        - name: mysql-data
          persistentVolumeClaim:
            claimName: mysqldata
---
apiVersion: v1
kind: Service
metadata:
  name: my-mysql-service
spec:
  selector:
    app: my-mysql
  type: ClusterIP
  clusterIP: None
  ports:
    - port: 3306
      targetPort: 3306

pv指定在worker02上的root/data目录上

所以先进入worker02标签的节点里创建目录

image-20220820160940067
image-20220820160940067

创建sc pv pvc

image-20220820160958134
image-20220820160958134

创建sts 完成绑定

image-20220820161631815
image-20220820161631815

查看worker02的root/data路径下是否有数据挂载

image-20220820161800247
image-20220820161800247

3.学习 secret、configmap 用法并应用;

configMap和secret

数据库连接地址 这种可能根据部署环境变化的 不应该写死在代码里

kubernetes为我们提供了configmap 可以方便的配置一些变量

  • configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  #  name: mongo-config
  name: mysql-config
data:
#  mongoAddress: mongodb-0.mongodb:27017
  mysqlAddress: mysql-0.mysql:3306
  • 运行截图

image-20220820163855550
image-20220820163855550

secret文件

一些重要数据 例如密码 token 可以放到secret中

注意:数据要base64编码

  • yaml文件
apiVersion: v1
kind: Secret
metadata:
#  name: mongo-secret
  name: mysql-secret
# Opaque 用户定义的任意数据类型 更多类型介绍 https://kubernetes.io/zh/docs/concepts/configuration/secret/#secret-types
type: Opaque
data:
  #数据要base64
  mysql-username: cm9vdA==  #root
  mysql-password: MTIzNDU2  #123456
  • 运行截图

image-20220820164742646
image-20220820164742646

  • 作为环境变量使用

statefulset.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-mysql-statefulset
spec:
  serviceName: my-mysql-service
  replicas: 2
  selector:
    matchLabels:
      app: my-mysql
  template:
    metadata:
      labels:
        app: my-mysql
    spec:
      affinity:
        #节点的亲和性  好像是没有节点反亲和的
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: node   #要求pod不允许运行在标签node=master的节点上
                    operator: NotIn
                    values:
                      - master
        #pod的反亲和性
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"  #要求pod不允许运行在已有pod标签为webapp的节点上
                    operator: In
                    values:
                      - "my-mysql"
              topologyKey: "kubernetes.io/hostname"
      containers:
        - name: my-mysql
          image: mysql:latest
          imagePullPolicy: IfNotPresent  #IfNotPresent 仅本地没有镜像时才远程拉取 , Always永远都是远程拉取 , Never永远只是用本地镜像 本地没有则报错
          args:
            - "--character-set-server=utf8"     #  指定字符编码
            - "--collation-server=utf8_general_ci"    #  指定字符编码
          env:
            - name: MYSQL_USER
              valueFrom:
                secretKeyRef:
                  name: mysql-secret
                  key: mysql-username
            - name: MYSQL_ROOT_PASSWORD    #  指定root用户的用户名
              valueFrom:
                secretKeyRef:
                  name: mysql-secret
                  key: mysql-password
            - name: MYSQL_DATABASE    # 新建的数据库
              value: "datashare"
          volumeMounts:
            - name: mysql-data #与71行对应
              mountPath: /var/lib/mysql   #容器里面挂载的路径
      volumes:
        #local-storage 要通过pvc 前提是先创建pvc
        - name: mysql-data
          persistentVolumeClaim:
            claimName: mysqldata


---
apiVersion: v1
kind: Service
metadata:
  name: my-mysql-service
spec:
  selector:
    app: my-mysql
  type: ClusterIP
  clusterIP: None
  ports:
    - port: 3306
      targetPort: 3306
  • 运行截图

image-20220820171448686
image-20220820171448686

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: e-book-gin
  labels:
    app: e-book-gin  #deployment的标签
    version: v1.0.0
spec:
  replicas: 6  #副本数量
  selector:  #pod标签选择器,匹配pod标签,默认使用pods的标签 定义标签选择器,部署需要管理的pod(带有该标签的的会被管理)需在pod 模板中定义
    matchLabels:
      app: webapp
      version: v1.0.0
  strategy:
    type: RollingUpdate   #将现有pod替换为新pod的部署策略 滚动更新
  template: #pod的定义
    metadata:
      labels:
        app: webapp  #与12行对应
        version: v1.0.0
    spec:
      affinity:
        #节点的亲和性  好像是没有节点反亲和的
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
              - matchExpressions:
                  - key: node   #要求pod不允许运行在标签node=master的节点上
                    operator: NotIn
                    values:
                      - master
        #pod的反亲和性
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                - key: "app"  #要求pod不允许运行在已有pod标签为webapp的节点上
                  operator: In
                  values:
                  - "webapp"
              topologyKey: "kubernetes.io/hostname"
      containers:
        - image: ccr.ccs.tencentyun.com/hfut-ie/e-book-gin:v3.0 #容器的镜像地址
          name: e-book-gin  #容器的名字
          ports:
            - containerPort: 7777 #对service暴露端口
              name: http
          env:
            - name: MYSQL_USER
              valueFrom:
                secretKeyRef:
                  name: mysql-secret
                  key: mysql-username
            - name: MYSQL_ROOT_PASSWORD    #  指定root用户的用户名
              valueFrom:
                secretKeyRef:
                  name: mysql-secret
                  key: mysql-password
            - name: MYSQL_ADDRESS
              valueFrom:
                configMapKeyRef:
                  name: mysql-config
                  key: mysqlAddress

运行截图

image-20220820172051739
image-20220820172051739

通过echo判断是否成功注入到环境变量中了

image-20220820180741127
image-20220820180741127

mysql也成功注入到环境变量中了

image-20220820181521581
image-20220820181521581

存在哪些问题:

不太清楚为什么挂载卷的目录会引起冲突

image-20220820171332000
image-20220820171332000

打算接下来做什么:

  • helm和命名空间
  • ingress
kubectl常用指令

kubectl exec -it e-book-gin-69b4df6cd9-mfrxt sh

bin/bash进入不了就用sh

kubectl get configmap

kubectl get configmap mysql-config -o yaml

kubectl get pvc

Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy