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.
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
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
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
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
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
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
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
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
To deploy each of the above resources, use the following commands:
-
Deployment:
oc apply -f httpd-deployment.yaml
-
StatefulSet:
oc apply -f httpd-statefulset.yaml
-
Secrets:
oc apply -f httpd-secret.yaml
-
ConfigMap:
oc apply -f httpd-config.yaml
-
CronJob:
oc apply -f httpd-cronjob.yaml
-
Job:
oc apply -f httpd-job.yaml
-
DaemonSet:
oc apply -f httpd-daemonset.yaml
-
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.