⬅️ Back to Kubernetes overview
Create a deployment with httpd
oc create deployment --image=ubi8/httpd-24 httpd Verify the deployment:
oc get deployments
oc get podsThe ClusterIP service is the default type. It exposes the service on a cluster-internal IP, which means the service is accessible only within the cluster.
oc expose deployment httpd --name=httpd-clusterip --port=80 --target-port=8080 --type=ClusterIPoc get service httpd-clusterip
oc port-forward services/httpd-clusterip 12380:80
# In another terminal window
curl localhost:12380 # or open localhost:12380 in your browserYou should see an internal IP assigned to the service. Pods within the cluster can communicate with the service using this IP. When port-forwarding you should be able to access the httpd server.
Now that you've created your service, the deployment which it points to can be scaled without any interuption to the user. The service selects one of the pods to forward to.
Scale your service:
oc scale deployment httpd --replicas 2When portforwarding oc chooses one pod to forward traffic to.
The NodePort service exposes the service on each Node's IP at a static port. This allows external access to the service on <NodeIP>:<NodePort>.
oc expose deployment httpd --name=httpd-nodeport --port=80 --target-port=8080 --type=NodePortoc get service httpd-nodeportYou should see a port in the range of 30000-32767. You can now access the service using <NodeIP>:<NodePort>.
Depending on your local enviromnent or cloud env you should now be able to access your service with the nodes ip address and the nodeport.
when running locally this wont work with all local clusters types
The ExternalName service maps a service to the contents of an external DNS name. This type of service does not create a proxy and is mainly used to redirect internal traffic to an external domain.
oc create service externalname httpd-externalname --external-name=example.comoc get service httpd-externalnameThe service redirects any requests to httpd-externalname to example.com. This service is mainly useful for accessing external services with a DNS name.
- Create a new deployment with the Apache Webserver (httpd) and two replicas.
- Create a service to access the Apache Webserver via the browser. If you are using a managed Kubernetes cluster in the cloud, please use the Service type LoadBalancer.