Batch 4: Cloud Ansible & OpenShift AWS & Azure Docker & Podman Apache & NGINX ← Full TOC

← Ansible Index

☸️ OpenShift & Kubernetes — oc CLI, pods, deployments, services, routes, builds

OpenShift is Red Hat's enterprise Kubernetes distribution. It adds developer tools, built-in CI/CD (Source-to-Image), stricter security defaults, and the Route resource for external access. The oc CLI extends kubectl.

📋 Reference

ItemSyntax / ValueDescription
oc loginoc login https://api.cluster:6443 -u admin -p passAuthenticate to OpenShift cluster
oc projectoc project myapp / oc new-project myappSwitch to or create a namespace/project
oc new-appoc new-app python~https://github.com/user/repoSource-to-Image build from git repo
oc new-buildoc new-build --strategy=dockerfile --binaryCreate a build config
oc start-buildoc start-build myapp --from-dir=. --followTrigger a build from local directory
oc getoc get pods / deployments / services / routesList resources
oc describeoc describe pod mypod-abc123Detailed info about a resource
oc logsoc logs mypod-abc123 -fStream pod logs
oc execoc exec -it mypod-abc123 -- /bin/bashShell into a running pod
oc rshoc rsh mypod-abc123Remote shell (OpenShift convenience)
oc applyoc apply -f manifest.yamlApply YAML manifest
oc deleteoc delete pod mypod-abc123Delete a resource
oc scaleoc scale deployment myapp --replicas=5Scale deployment
oc rolloutoc rollout status deployment/myappCheck rollout progress
oc rollout undooc rollout undo deployment/myappRoll back to previous version
oc exposeoc expose service myappCreate a Route from a Service
RouteOpenShift-specific resource for external HTTP/S accessLike k8s Ingress but with TLS termination built-in
oc set imageoc set image deployment/myapp myapp=image:tagUpdate container image
oc set envoc set env deployment/myapp KEY=valueSet environment variable
oc admoc adm policy add-scc-to-user anyuid -z defaultAdmin commands (cluster admin required)
SCCSecurity Context Constraint — stricter than k8s PodSecurityPolicyOpenShift blocks containers running as root by default
DeploymentConfigOpenShift-specific deployment resource (legacy)dc/myapp — prefer Deployment in OCP 4.x
BuildConfigOpenShift build pipeline resourcebc/myapp — S2I, Docker, or custom strategy
ImageStreamOpenShift image registry abstractionis/myapp — triggers redeploy on new image
oc processoc process -f template.yaml -p PARAM=val | oc apply -f -Process OpenShift template
Helm on OCPhelm install myapp ./chart -n myprojectHelm works on OpenShift 4.x

💡 Example

# OpenShift / Kubernetes — deployment workflow

# ── Connect to cluster ────────────────────────────────────────
# OpenShift
oc login https://api.myocp.example.com:6443 -u developer -p password
oc project mywebuniversity

# Kubernetes (kubectl)
kubectl config use-context my-k8s-cluster
kubectl config set-context --current --namespace=mywebuniversity

# ── Deploy application ────────────────────────────────────────
# Option 1: From Docker image
oc new-app --image=myregistry.io/mywebuniversity/api:latest \
  --name=mywebuniversity-api

# Option 2: Source-to-Image from git
oc new-app python~https://github.com/myorg/mywebuniversity-api \
  --name=mywebuniversity-api \
  --env=FLASK_ENV=production

# Option 3: From YAML manifest
cat > deployment.yaml << 'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mywebuniversity-api
  namespace: mywebuniversity
spec:
  replicas: 3
  selector:
    matchLabels:
      app: mywebuniversity-api
  template:
    metadata:
      labels:
        app: mywebuniversity-api
    spec:
      containers:
        - name: api
          image: myregistry.io/mywebuniversity/api:2.0
          ports:
            - containerPort: 8080
          env:
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: url
          resources:
            requests: {cpu: "100m", memory: "128Mi"}
            limits:   {cpu: "500m", memory: "512Mi"}
          readinessProbe:
            httpGet: {path: /health, port: 8080}
            initialDelaySeconds: 10
---
apiVersion: v1
kind: Service
metadata:
  name: mywebuniversity-api
spec:
  selector:
    app: mywebuniversity-api
  ports:
    - port: 80
      targetPort: 8080
EOF

oc apply -f deployment.yaml    # OpenShift
kubectl apply -f deployment.yaml  # Kubernetes

# ── Expose externally ─────────────────────────────────────────
# OpenShift Route (auto TLS with cluster CA)
oc expose service mywebuniversity-api
oc get routes

# Custom hostname with TLS
oc create route edge mywebuniversity-api \
  --service=mywebuniversity-api \
  --hostname=api.mywebuniversity.com \
  --cert=tls.crt --key=tls.key

# Kubernetes Ingress equivalent
kubectl apply -f - << 'EOF'
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: mywebuniversity-api
  annotations:
    cert-manager.io/cluster-issuer: letsencrypt-prod
spec:
  rules:
    - host: api.mywebuniversity.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service: {name: mywebuniversity-api, port: {number: 80}}
  tls:
    - hosts: [api.mywebuniversity.com]
      secretName: api-tls
EOF

# ── Monitor and manage ────────────────────────────────────────
oc get pods -w                           # watch pods
oc logs -f deployment/mywebuniversity-api  # stream logs
oc describe pod mywebuniversity-api-xxx  # debug pod issues
oc exec -it mywebuniversity-api-xxx -- bash  # shell in

oc scale deployment mywebuniversity-api --replicas=5
oc set image deployment/mywebuniversity-api api=myregistry.io/mywebuniversity/api:2.1
oc rollout status deployment/mywebuniversity-api
oc rollout undo deployment/mywebuniversity-api  # if needed

# ── Secrets and ConfigMaps ────────────────────────────────────
oc create secret generic db-credentials \
  --from-literal=url="postgresql://admin:pass@db:5432/mywebuniversity"

oc create configmap app-config \
  --from-literal=ENVIRONMENT=production \
  --from-literal=LOG_LEVEL=info

# ── Build from source (OpenShift S2I) ────────────────────────
oc start-build mywebuniversity-api --from-dir=. --follow
oc get builds
oc logs build/mywebuniversity-api-2

← Ansible Inventory & Roles  |  🏠 Index  |  Ansible Ad-hoc & Modules →