Sobes.tech
Middle

How did you deploy the application using OpenShift?

sobes.tech AI

Answer from AI

When deploying an application in OpenShift, I usually followed these steps:

  1. Preparing the application artifact: Ensured I had a ready Docker image of the application or the source code that OpenShift could build.
  2. Authenticating in the cluster: Used the oc login command to connect to the desired OpenShift environment.
  3. Creating a new project (namespace): If the project did not exist, I created it:
    # Creating a new project
    oc new-project my-application-project
    
  4. Creating a BuildConfig resource: Defined how OpenShift should build the application image. This could be a Dockerfile build or an S2I (Source-to-Image) build from source code.
    # Example BuildConfig for S2I build from a Git repository
    apiVersion: build.openshift.io/v1
    kind: BuildConfig
    metadata:
      name: my-app-build
    spec:
      source:
        git:
          uri: https://github.com/my-org/my-app.git # Repository URL
      strategy:
        sourceStrategy:
          from:
            kind: ImageStreamTag
            name: 'nodejs:latest' # Base image for build
            namespace: openshift
      output:
        to:
          kind: ImageStreamTag
          name: 'my-app:latest' # Tag for the output image
    
    Then I applied it:
    # Applying BuildConfig
    oc apply -f buildconfig.yaml
    
  5. Starting the build: Initiated the image build process.
    # Starting build by BuildConfig name
    oc start-build my-app-build --follow
    
  6. Creating a DeploymentConfig resource: Described how the application should be deployed and managed in the cluster.
    # Example DeploymentConfig
    apiVersion: apps.openshift.io/v1
    kind: DeploymentConfig
    metadata:
      name: my-app
    spec:
      replicas: 3 # Desired number of instances
      selector:
        app: my-app # Selector for Pods
      strategy:
        type: Rolling # Deployment strategy
      template:
        metadata:
          labels:
            app: my-app
        spec:
          containers:
          - name: my-app
            image: 'image-registry.openshift-image-registry.svc:5000/my-application-project/my-app:latest' # Image from ImageStream
            ports:
            - containerPort: 8080 # Application port
      triggers:
      - type: ConfigChange # Trigger on DeploymentConfig change
      - type: ImageChange # Trigger on image change in ImageStream
        imageChangeParams:
          automatic: true
          containerNames:
          - my-app
          from:
            kind: ImageStreamTag
            name: 'my-app:latest'
    
    I applied it:
    # Applying DeploymentConfig
    oc apply -f deploymentconfig.yaml
    
  7. Creating a Service resource: Defined how the application Pods would be accessible within the cluster.
    # Example Service
    apiVersion: v1
    kind: Service
    metadata:
      name: my-app-service
    spec:
      selector:
        app: my-app # Selector for Pods
      ports:
      - protocol: TCP
        port: 80 # External port of the service
        targetPort: 8080 # Target port in the Pod
    
    I applied it:
    # Applying Service
    oc apply -f service.yaml
    
  8. Creating a Route resource: Made the application accessible externally.
    # Example Route
    apiVersion: route.openshift.io/v1
    kind: Route
    metadata:
      name: my-app-route
    spec:
      to:
        kind: Service
        name: my-app-service # Service to route traffic to
      port:
        targetPort: 80 # Target port of the service
    
    I applied it:
    # Applying Route
    oc apply -f route.yaml
    
  9. Monitoring deployment: Watched the deployment and Pod statuses.
    # Viewing DeploymentConfig status
    oc rollout status dc/my-app
    # Viewing list of Pods
    oc get pods
    # Viewing Pod logs
    oc logs <pod_name>
    

Depending on specific requirements, Kubernetes Deployment could be used instead of OpenShift DeploymentConfig, along with other resources like PersistentVolumeClaims for storage management or Secrets and ConfigMaps for configuration injection. These steps are often automated with CI/CD pipelines (e.g., OpenShift Pipelines, Jenkins, GitLab CI).