Docker & K8s

Docker

注意

Yearning安装包内已含有Dockerfile文件,可直接进行build打包成镜像

容器启动时可通过环境变量的方式传入数据库地址以及秘钥。

单独使用docker时请先创建数据库并设置字符集为utf8mb4

如下所示

docker run -d -it -p8000:8000 -e SECRET_KEY=dbcjqheupqjsuwsm -e MYSQL_USER=root -e MYSQL_ADDR=10.0.0.3:3306 -e MYSQL_PASSWORD=123123 -e MYSQL_DB=Yearning chaiyd/yearning

Docker-compose(推荐安装)

安装包/源码仓库内docker目录下执行 docker-compose up -d 即可

K8s

k8s部署来自知乎open in new window 以下为适配3.0版本的部署方式。

Secret

apiVersion: v1
kind: Secret
metadata:
  name: db-conf
  namespace: yearning
type: Opaque  # 使用的是generic类型
data:   # 这里配置的是数据库的相关信息,使用base64加密输入: # echo -n 'xxxx' | base64
  addr: cm9vdAweqw==
  user: cm9vdAweqw==
  pass: cm9vdAweqw==
  data: WWVhcm5pbmc=    
  sk: W3213123Vhcm5pbmc=

Service

apiVersion: v1
kind: Service
metadata:
  labels:
    app: yearning
  name: yearning
  namespace: yearning
spec:
  ports:
    - port: 80  # svc内部端口,通过clusterIP访问
      protocol: TCP
      targetPort: 8000  # 镜像内服务的端口
  selector: # 标签选择器,与deployment中的标签保持一致
    app: yearning
  type: NodePort  # Service类型

Ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: yearning
  namespace: yearning
spec:
  rules:
    - host: yearning.io
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: yearning
                port:
                  number: 80

Deployment

apiVersion: apps/v1 # API版泵
kind: Deployment  # 资源类型
metadata: # 元数据
  labels: # 标签
    app: yearning
  name: yearning  # deployment的名字
  namespace: yearning  # 所属命名空间
spec: 
  replicas: 3 # 副本数
  selector: # 选择器,选择针对谁做
    matchLabels:
      app: yearning
  template: # 镜像的模板
    metadata: # 元数据
      labels: # 标签
        app: yearning
    spec:
      containers: # 容器信息
        - image: chaiyd/yearning  # 容器镜像
          name: yearning # 容器的名字
          imagePullPolicy: IfNotPresent # 镜像的下载策略
          env:  # 容器中的变量
            - name: MYSQL_ADDR
              valueFrom:
                secretKeyRef: # 存储的变量信息
                  name: db-conf
                  key: addr
            - name: MYSQL_USER
              valueFrom:
                secretKeyRef:
                  name: db-conf
                  key: user
            - name: MYSQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-conf
                  key: pass
            - name: MYSQL_DB
              valueFrom:
                secretKeyRef:
                  name: db-conf
                  key: data
            - name: SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: db-conf
                  key: sk
          ports:    # 定义容器中的端口信息
            - containerPort: 8000
              name: web
              protocol: TCP
          readinessProbe:   # 就绪检查
            httpGet:
              path: /
              port: web
              scheme: HTTP
            initialDelaySeconds: 25
            periodSeconds: 2
          livenessProbe:    # 存活检查
            httpGet:
              path: /
              port: web
              scheme: HTTP
            initialDelaySeconds: 30
            periodSeconds: 2
          resources:    # 资源限制
            requests:
              cpu: 200m
              memory: 1Gi
            limits:
              cpu: 250m
              memory: 2Gi