Skip to content

Instantly share code, notes, and snippets.

@devops-school
Created May 7, 2025 15:18
Show Gist options
  • Save devops-school/a8084424b7c6e366da8f44b72105eea8 to your computer and use it in GitHub Desktop.
Save devops-school/a8084424b7c6e366da8f44b72105eea8 to your computer and use it in GitHub Desktop.
OpenShift Tutorial – Basic Learning Workflow

To deploy an image in OpenShift using the openshift/httpd image, and configure different Kubernetes resources such as StatefulSets, Secrets, ConfigMaps, CronJobs, Jobs, DaemonSets, and HPA, here are the YAML configurations and relevant commands for each.

1. Deployment (for basic deployment)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
      - name: httpd
        image: openshift/httpd
        ports:
        - containerPort: 80

Command to deploy:

oc apply -f httpd-deployment.yaml

2. StatefulSet (for managing stateful workloads)

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: httpd-statefulset
spec:
  serviceName: "httpd"
  replicas: 3
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
      - name: httpd
        image: openshift/httpd
        ports:
        - containerPort: 80

Command to deploy:

oc apply -f httpd-statefulset.yaml

3. Secrets (for storing sensitive data like passwords)

apiVersion: v1
kind: Secret
metadata:
  name: httpd-secret
type: Opaque
data:
  password: cGFzc3dvcmQ=  # base64 encoded password

Command to create a secret:

oc apply -f httpd-secret.yaml

4. ConfigMap (for configuration files)

apiVersion: v1
kind: ConfigMap
metadata:
  name: httpd-config
data:
  httpd.conf: |
    ServerName localhost
    DocumentRoot /var/www/html

Command to create a config map:

oc apply -f httpd-config.yaml

5. CronJob (for scheduling periodic jobs)

apiVersion: batch/v1
kind: CronJob
metadata:
  name: httpd-cronjob
spec:
  schedule: "*/5 * * * *"  # Every 5 minutes
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: httpd
            image: openshift/httpd
          restartPolicy: OnFailure

Command to deploy a cron job:

oc apply -f httpd-cronjob.yaml

6. Job (for one-time tasks)

apiVersion: batch/v1
kind: Job
metadata:
  name: httpd-job
spec:
  template:
    spec:
      containers:
      - name: httpd
        image: openshift/httpd
      restartPolicy: Never

Command to deploy a job:

oc apply -f httpd-job.yaml

7. DaemonSet (for deploying a pod on every node)

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: httpd-daemonset
spec:
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
      - name: httpd
        image: openshift/httpd
        ports:
        - containerPort: 80

Command to deploy a daemon set:

oc apply -f httpd-daemonset.yaml

8. Horizontal Pod Autoscaler (HPA)

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: httpd-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: httpd-deployment
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50

Command to deploy HPA:

oc apply -f httpd-hpa.yaml

Summary of Commands

To deploy each of the above resources, use the following commands:

  1. Deployment:

    oc apply -f httpd-deployment.yaml
  2. StatefulSet:

    oc apply -f httpd-statefulset.yaml
  3. Secrets:

    oc apply -f httpd-secret.yaml
  4. ConfigMap:

    oc apply -f httpd-config.yaml
  5. CronJob:

    oc apply -f httpd-cronjob.yaml
  6. Job:

    oc apply -f httpd-job.yaml
  7. DaemonSet:

    oc apply -f httpd-daemonset.yaml
  8. Horizontal Pod Autoscaler:

    oc apply -f httpd-hpa.yaml

These YAML files and commands cover the most common Kubernetes resources you may need while deploying an image on OpenShift. Adjust the values (e.g., replicas, resources, image, etc.) as needed for your use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment