Overview of OpenShift
OpenShift is a powerful Platform as a Service (PaaS) that enables developers to build, deploy, and manage applications quickly and efficiently. It integrates with Docker and Kubernetes to provide a comprehensive container orchestration and management solution, offering several variations depending on your needs:
- OpenShift Origin: The community-driven upstream project that is the foundation for OpenShift.
- OpenShift Online: A public cloud service where you can develop, build, and deploy applications in a hosted environment.
- OpenShift Dedicated: A private cloud offering managed by Red Hat.
- OpenShift Enterprise: A robust and scalable enterprise version for on-premises deployment.
OpenShift Origin: A Deep Dive
OpenShift Origin builds on top of Docker and Kubernetes, adding tools that cater to both developers and operations teams. It accelerates the application lifecycle by providing:
- Docker: Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers are lightweight, portable, and run the same regardless of where they are deployed. They package up the application with all its dependencies, ensuring consistency across multiple development and production environments.
- Kubernetes (K8s): Kubernetes is an open-source platform designed for automating deployment, scaling, and operations of application containers. It organizes Docker containers into pods, manages their lifecycle, and scales them across a cluster of machines.
- Developer and Operational Tools: OpenShift provides tools like Source Code Management (SCM), pipelines for continuous integration/continuous deployment (CI/CD), a container registry, software-defined networking, APIs, and governance features that streamline the development process.
Installing and Starting OpenShift
To get started with OpenShift, you can follow the official installation guide available at OpenShift Documentation.
Steps to Install and Start OpenShift:
- Download the
minishift
zip file from the official website. - Extract the contents and copy them to your
C:
drive. - Open a Command Prompt (cmd) and run the following command to start OpenShift with a VirtualBox driver:
minishift.exe start --vm-driver virtualbox
Accessing OpenShift
OpenShift can be accessed in three different ways:
- REST API: For programmatic access, allowing you to interact with OpenShift programmatically via HTTP requests.
- CLI (Command Line Interface): Provides a powerful way to interact with OpenShift using the
oc
command. - Web Console: A user-friendly graphical interface to manage your OpenShift resources.
Working with OpenShift CLI (oc
)
The oc
command-line client is a powerful tool for interacting with OpenShift. Below are some common commands and explanations:
- Set Up the Environment for
oc
:
oc-env
This command configures your shell environment for the oc
client.
- Login as an Admin:
oc login -u admin -p password
This logs you in as an administrator.
- Login as a Developer:
oc login -u developer -p password
This logs you in as a developer user.
- Logout:
oc logout
This logs you out of your current OpenShift session.
- Check Current User:
oc whoami -t
This command returns the current user and authentication token being used.
- Manage Projects:
oc get projects
oc project dev-2
The first command lists all projects you have access to. The second command switches to a specific project (dev-2
).
- Rollout Management:
oc rollout latest dc/simple-webapp-docker
oc rollout history dc/simple-webapp-docker
oc rollout describe dc/simple-webapp-docker
oc rollout undo dc/simple-webapp-docker
These commands are used for managing deployments, including initiating a rollout, viewing the rollout history, describing the current state, and undoing the last deployment.
- Persistent Volume Claims (PVC):
oc get pvc
oc delete pvc db
These commands allow you to manage persistent storage claims, which are used to store data persistently across pod restarts.
- Resource Creation and Management:
oc create -f app.yml -n project-1
oc export service db
These commands create resources from a YAML configuration file and export services for easier replication.
Example: Starting and Managing Minishift
To work with Minishift, the following commands are useful:
- Start Minishift:
minishift.exe start --vm-driver virtualbox
This command starts the Minishift VM using VirtualBox as the driver.
- Set Up Environment for Minishift:
minishift oc-env
Configures your shell environment for the Minishift oc
client.
- Delete Minishift VM:
minishift delete
Removes the Minishift VM and any associated data.
- Check Minishift Status:
minishift status
Displays the current status of the Minishift VM.
- Restart OpenShift:
minishift openshift restart
Restarts the OpenShift cluster running within Minishift.
Using REST API with OpenShift
The REST API is a powerful way to interact with OpenShift programmatically. For example, to get user information or list all projects:
- Get Current User:
oc whoami -t
curl https://locahost:8443/oapi/v1/users -H "Authorization: Bearer <Token>"
- List All Projects:
curl https://192.168.99.100:8443/oapi/v1/projects -k -H "Authorization: Bearer fbegqvmGfxVH-1yC2d74FStFXotypp6FO8o0sWtGOyI"
Building and Deploying Applications
OpenShift supports multiple deployment strategies and provides robust CI/CD capabilities. Here’s how a typical deployment pipeline might look:
- Source Code Repository: Code is stored in a version control system like Git.
- Source to Image (S2I): OpenShift builds Docker images directly from the source code.
- Docker Image Creation: The S2I process creates a Docker image.
- Docker Registry: The image is pushed to a Docker registry.
- Deployment: The application is deployed to the OpenShift cluster.
Deployment Strategies:
- Recreate: Stops the old version of the application before starting the new one.
- Blue/Green: Deploys a new version alongside the old one, and then switches traffic to the new version.
- Rolling: Gradually replaces old instances with new ones.