jenkins 设置pod模板安全上下文时,预填充的mysql docker映像未启动

piztneat  于 2022-11-21  发布在  Jenkins
关注(0)|答案(1)|浏览(144)

我 有 一 个 mysql 图像 与 prepolutated 模式 , 下面 我 分享 安装 文件 .
我 的 停靠 文件 :

FROM mysql:8.0.31 as builder

# That file does the DB initialization but also runs mysql daemon, by removing the last line it will only init
RUN ["sed", "-i", "s/exec \"$@\"/echo \"not running $@\"/", "/usr/local/bin/docker-entrypoint.sh"]

# needed for intialization
ENV MYSQL_ROOT_PASSWORD=test

COPY ./sql-scripts /docker-entrypoint-initdb.d/

# Need to change the datadir to something else that /var/lib/mysql because the parent docker file defines it as a volume.
# https://docs.docker.com/engine/reference/builder/#volume :
#       Changing the volume from within the Dockerfile: If any build steps change the data within the volume after
#       it has been declared, those changes will be discarded.
RUN ["/usr/local/bin/docker-entrypoint.sh", "mysqld", "--datadir", "/initialized-db"]

FROM mysql:8.0.31

COPY --from=builder /initialized-db /var/lib/mysql

中 的 每 一 个
我 的 pod 模板 yaml :

apiVersion: v1
kind: Pod
metadata:
  labels:
    label: 'backend'
spec:
  shareProcessNamespace: true
  containers:
    - name: "maven"
      image: maven:3.6.3-openjdk-11
      resources:
        requests:
          memory: "2Gi"
          cpu: "2"
        limits:
          memory: "10Gi"
          cpu: "10"
      command: [ sleep ]
      args: [ 1h ]
      securityContext:
        capabilities:
          add:
            - SYS_PTRACE
    - name: mysql
      image: myDockerRegistry/mysql8-integration-test:v5
      env:
        - name: MYSQL_USER
          value: test
        - name: MYSQL_PASSWORD
          value: test
        - name: MYSQL_ROOT_PASSWORD
          value: test
      securityContext:
        capabilities:
          add:
            - SYS_PTRACE

格式
我 的 管道 :

pipeline {
    agent {
        kubernetes {
            yaml libraryResource('pod-templates/backend.yaml')
        }
    }
    stages { ... }
}

格式
上面 的 设置 工作 正常 , 但是 我 想 为 工作 区 使用 一 个 动态 PVC , 然后 我 在 pod 模板 之后 将 下面 的 行 添加 到 我 的 管道 中 。

workspaceVolume dynamicPVC(accessModes: 'ReadWriteOnce',requestsSize: "10Gi", storageClassName: 'premium-rwo')

格式
但 我 必须 将 securityContext 添加 到 我 的 pod 模板 中 , 这样 Jenkins 才 能 在 代理 中 挂载 PVC :

spec:
  securityContext:
    runAsUser: 1000
    runAsGroup: 1000
    fsGroup: 1000

格式
通过 这些 更改 , pod 启动 , 卷 正确 挂载 , 但 mysql 容器 不 工作 。 以下 是 错误 日志 :

2022-11-03 09:33:25+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.31-1.el8 started.
'/var/lib/mysql/mysql.sock' -> '/var/run/mysqld/mysqld.sock'
2022-11-03T09:33:25.839933Z 0 [Warning] [MY-011068] [Server] The syntax '--skip-host-cache' is deprecated and will be removed in a future release. Please use SET GLOBAL host_cache_size=0 instead.
2022-11-03T09:33:25.842508Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.31) starting as process 13
2022-11-03T09:33:25.845263Z 0 [Warning] [MY-010122] [Server] One can only use the --user switch if running as root
mysqld: File './binlog.index' not found (OS errno 13 - Permission denied)
2022-11-03T09:33:25.845867Z 0 [ERROR] [MY-010119] [Server] Aborting
2022-11-03T09:33:25.846078Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.31)  MySQL Community Server - GPL.

格式
i asume 与 mysql 容器 中 的 root 权限 有关 , 但 很 奇怪 , 因为 官方 映像 运行 得 很 完美 。
最 后 这 是 注入 Jenkins 试剂 后 生成 的 原始 yaml :

apiVersion: v1
kind: Pod
metadata:
  annotations:
    buildUrl: >-
      http://jenkins.jenkins.svc.cluster.local:8080/job/LegacyProjects/job/my-project/job/k8s-test/79/
    runUrl: job/LegacyProjects/job/my-project/job/k8s-test/79/
  labels:
    label: backend
    jenkins/jenkins-jenkins-agent: 'true'
    jenkins/label-digest: 4581eadfdfcb3d0141b8e8727b53b2ff9a3575ec
    jenkins/label: LegacyProjects_my-project_k8s-test_79-xgtxd
  name: my-project-k8s-test-79-xgtxd-2xw2r-8wj64
  namespace: jenkins
spec:
  containers:
    - args:
        - 1h
      command:
        - sleep
      image: 'maven:3.6.3-openjdk-11'
      name: maven
      resources:
        limits:
          memory: 10Gi
          cpu: '10'
        requests:
          memory: 2Gi
          cpu: '2'
      securityContext:
        capabilities:
          add:
            - SYS_PTRACE
      volumeMounts:
        - mountPath: /home/jenkins/agent
          name: workspace-volume
          readOnly: false
    - env:
        - name: MYSQL_USER
          value: test
        - name: MYSQL_PASSWORD
          value: test
        - name: MYSQL_ROOT_PASSWORD
          value: test
      image: 'myDockerRegistry/mysql8-integration-test:v5'
      name: mysql
      securityContext:
        capabilities:
          add:
            - SYS_PTRACE
      volumeMounts:
        - mountPath: /home/jenkins/agent
          name: workspace-volume
          readOnly: false
    - env:
        - name: JENKINS_SECRET
          value: '********'
        - name: JENKINS_TUNNEL
          value: 'jenkins-agent.jenkins.svc.cluster.local:50000'
        - name: JENKINS_AGENT_NAME
          value: my-project-k8s-test-79-xgtxd-2xw2r-8wj64
        - name: JENKINS_NAME
          value: my-project-k8s-test-79-xgtxd-2xw2r-8wj64
        - name: JENKINS_AGENT_WORKDIR
          value: /home/jenkins/agent
        - name: JENKINS_URL
          value: 'http://jenkins.jenkins.svc.cluster.local:8080/'
      image: 'jenkins/inbound-agent:4.11-1-jdk11'
      name: jnlp
      resources:
        limits: {}
        requests:
          memory: 256Mi
          cpu: 100m
      volumeMounts:
        - mountPath: /home/jenkins/agent
          name: workspace-volume
          readOnly: false
  nodeSelector:
    kubernetes.io/os: linux
  restartPolicy: Never
  securityContext:
    fsGroup: 1000
    runAsGroup: 1000
    runAsUser: 1000
  shareProcessNamespace: true
  volumes:
    - name: workspace-volume
      persistentVolumeClaim:
        claimName: pvc-workspace-my-project-test-79-xgtxd-2xw2r-8wj64
        readOnly: false

格式
任何 帮助 都 将 不胜 感激

dsekswqp

dsekswqp1#

默认情况下,COPY命令仅以root用户身份运行,您应该为该命令指定--chown=1000:1000标志以设置正确的用户和组(在您的情况下-它是uid和gid为1000的用户,在securityContext中指定),有关详细信息,请参阅https://stackoverflow.com/a/44766666和https://docs.docker.com/engine/reference/builder/#copy
虽然您的用例可能需要使用数据库预先构建映像,但可以考虑将正式的mysql映像作为db运行,并使用带有liquibase/flyway或其他(甚至内置)数据库迁移工具包的init容器运行您的应用程序,从长远来看,这可能是一个更易于移植的解决方案

相关问题