Professional Documents
Culture Documents
Service Discovery - The Kubernetes Workshop
Service Discovery - The Kubernetes Workshop
8. Service Discovery
Overview
Introduction
In the past few chapters, we learned about Pods
and Deployments, which help us run
containerized applications. Now that we are
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idParaD… 1/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idParaD… 2/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
Service
A Service defines policies by which a logical set
of Pods can be accessed. Kubernetes Services
enable communication between various
components of our application, as well as
between different applications. Services help us
connect the application with other applications
or users. For example, let's say we have a set of
Pods running the frontend of an application, a
set of Pods running the backend, and another set
of Pods connecting the data source. The frontend
is the one that users need to interact with
directly. The frontend then needs to connect to
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idParaD… 3/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idParaD… 4/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
Service Configuration
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idParaD… 5/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
apiVersion: v1
kind: Service
metadata:
name: sample-service
spec:
ports:
- port: 80
targetPort: 80
selector:
key: value
For a Service, apiVersion is v1 and kind will
always be Service. In the metadata field, we
will specify the name of the Service. In addition
to the name, we can also add labels and
annotations in the metadata field.
Types of Services
NodePort Service
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idParaD… 7/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idParaD… 8/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
ports:
- targetPort: 80
port: 80
nodePort: 32023
selector:
app: nginx
environment: production
As we can see, there are three ports involved in
the definition of a NodePort Service. Let's take a
look at these:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idParaD… 9/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 10/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
strategy:
type: Recreate
selector:
matchLabels:
app: nginx
environment: production
template:
metadata:
labels:
app: nginx
environment: production
spec:
containers:
- name: nginx-container
image: nginx
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 11/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 32023
selector:
app: nginx
environment: production
5. Run the following command to create the
Service:
kubectl create -f nginx-service-
nodeport.yaml
You should see the following output:
service/nginx-service-nodeport
created
Alternatively, we can use the kubectl expose
command to expose a Deployment or a Pod
using a Kubernetes Service. The following
command will also create a NodePort Service
named nginx-service-nodeport, with port
and targetPort set to 80. The only difference
is that this command doesn't allow us to
customize the nodePort field. nodePort is
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 13/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 14/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
configuration:
kubectl describe service nginx-
service-nodeport
This should give us the following output:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 15/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 16/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 18/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
ClusterIP Service
Service Configuration
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: ClusterIP
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 20/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
ports:
- targetPort: 80
port: 80
selector:
app: nginx
environment: production
The type of Service is set to ClusterIP. Only
two ports are needed for this type of the Service:
targetPort and port. These represent the port
where the application is exposed on the Pod and
the port where the Service is created on a given
cluster IP, respectively.
Containers
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 22/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
environment: production
template:
metadata:
labels:
app: nginx
environment: production
spec:
containers:
- name: nginx-container
image: nginx
2. Run the following command to create the
Deployment using the kubectl apply
command:
kubectl create -f nginx-
deployment.yaml
You should see the following response:
deployment.apps/nginx-deployment
created
3. Run the following command to verify that the
Deployment has created three replicas:
kubectl get pods
You should see output similar to the following:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 24/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 25/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 26/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 27/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
deployment.apps "nginx-
deployment" deleted
Delete the Service using the following
command:
kubectl delete service nginx-
service-clusterip
You should see the following response:
service "nginx-service-
clusterip" deleted
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 29/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 30/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 31/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 32/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
"--service-cluster-ip-
range=10.96.0.0/12",
We can then use the appropriate IP address for
clusterIP for our Service.
5. Modify the nginx-service-custom-
clusterip.yaml file by changing the value of
clusterIP to 10.96.0.5 since that's one of
the valid values:
apiVersion: v1
kind: Service
metadata:
name: nginx-service-custom-
clusterip
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
clusterIP: 10.96.0.5
selector:
app: nginx
environment: production
6. Run the following command to create the
Service again:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 33/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 34/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 35/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
LoadBalancer Service
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 36/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
apiVersion: v1
kind: Service
metadata:
name: loadbalancer-service
spec:
type: LoadBalancer
clusterIP: 10.90.10.0
ports:
- targetPort: 8080
port: 80
selector:
app: nginx
environment: production
ExternalName Service
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 37/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
apiVersion: v1
kind: Service
metadata:
name: externalname-service
spec:
type: ExternalName
externalName:
my.example.domain.com
The preceding Service template maps
externalname-service to a DNS name; for
example, my.example.domain.com.
Ingress
Ingress is an object that defines rules that are
used to manage external access to the Services in
a Kubernetes cluster. Typically, Ingress acts like a
middleman between the internet and the
Services running inside a cluster:
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 39/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 41/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 42/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 43/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 44/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
Expected Output:
Note
Summary
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 45/46
10/16/22, 9:00 AM 8. Service Discovery | The Kubernetes Workshop
https://learning.oreilly.com/library/view/the-kubernetes-workshop/9781838820756/B14870_08_Final_SZ_ePub.xhtml#_idPara… 46/46