⬅️ Back to Kubernetes overview
Create a deployment with nginx
kubectl create deployment --image=nginx nginx Verify the deployment:
kubectl get deployments
kubectl 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.
kubectl expose deployment nginx --name=nginx-clusterip --port=80 --target-port=80 --type=ClusterIPkubectl get service nginx-clusterip
kubectl port-forward services/nginx-clusterip 8080:80
# In another temrinal window
curl localhost:8080 # or open localhost:8080 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 nginx 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:
kubectl scale deployment nginx --replicas 2When portforwarding kubectl 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>.
kubectl expose deployment nginx --name=nginx-nodeport --port=80 --target-port=80 --type=NodePortkubectl get service nginx-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.
kubectl create service externalname nginx-externalname --external-name=example.comkubectl get service nginx-externalnameThe service redirects any requests to nginx-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.