编写OpenShift的yaml以定义容器

本文介绍了如何在OpenShift中创建和应用YAML文件,详细讲解了从创建secret开始,到ConfigMap、Deployment、Service和Route的配置过程,帮助读者掌握容器化应用的部署技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一、创建secret

在OpenShift左侧Resources->Secret->Create Secret里选择Image Secret后创建。
在这里插入图片描述

二、编写yaml

1. ConfigMap

apiVersion: V1
kind: ConfigMap
metadata:
  name: app-config-map
data:
  application.properties: |
  spring.profiles.active=dev
  
  server.xml: >-
  <?xml version='1.0' encoding='utf-8'?>

2. Deployment

apiVersion: V1
kind: Deployment
metadata:
  name: app
spec:
  #定义副本数量
  replicas: 2
  selector:
    matchLabels:
      app: app
  template:
    metadata:
      labels:
        app: app
    spec:
      imagePullSecrets:
        - name: ${secret_name}
      containers:
        - name: app
          image: ${artifactory_url}/${dev_project_name}/${app_name}:'BUILD_ID'
          imagePullPolicy: Always
          #设置资源配额
          resources:
            limits:
              cpu: 2000m
              memory: 4Gi
            env:
              - name: TZ
                value: Asia/Shanghai
            ports:
              - containerPort: 8080
            #挂载到容器目录
            volumeMounts:
              - mountPath: /opt/tomcat/conf/server.xml
                name: [volume-app]
                readOnly: true
                subPath: server.xml
        #定义容器就绪探针
        readinessProbe:
      	  tcpSocket:
            port: 8080
          periodSeconds: 10
        #定义容器探活探针
        livenessProbe:
          periodSeconds: 5
          httpGet:
            path: /health
            port: 8080
      volumes:
        - configMap:
            defalutMode: 420
            #对应1.ConfigMap的名称
            name: app-config-map
          name: [volume-app]

3. Service

apiVersion: V1
kind: Service
metadata:
  name: app
spec:
  ports:
    - port: 8080
      targetPort: 8080
      name: app
  selector: 
    app: app

4. Route

apiVersion: V1
kind: Route
metadata:
  name: app
spec:
  host: app.test.com
  #path: /test
  to:
    kind: Service
    #对应3.Service
    name: app
    weight: 100
  wildcardPolicy: None

三、应用yaml

在OpenShift右侧Add to Project->Import YAML/JSON里导入上述4个yaml文件。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值