34 Creating The Split API Pods
34 Creating The Split API Pods
In this lesson, we will create API Pods using ReplicaSet and establish communication by creating Service.
cat svc/go-demo-2-api-rs.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: go-demo-2-api
spec:
replicas: 3
selector:
matchLabels:
type: api
service: go-demo-2
template:
metadata:
labels:
type: api
service: go-demo-2
language: go
spec:
containers:
- name: api
image: vfarcic/go-demo-2
env:
- name: DB
value: go-demo-2-db
readinessProbe:
httpGet:
path: /demo/hello
port: 8080
periodSeconds: 1
livenessProbe:
httpGet:
path: /demo/hello
port: 8080
Just as with the database, this ReplicaSet should be familiar since it’s very
similar to the one we used before. We’ll comment only on the differences.
Line 6: The number of replicas is set to 3 . That solves one of the main
problems we had with the previous ReplicaSets that defined Pods with
both containers. Now the number of replicas can differ, and we have one
Pod for the database, and three for the backend API.
Line 14: In the labels section, type label is set to api so that both the
ReplicaSet and the (soon to come) Service can distinguish the Pods from
those created for the database.
The readinessProbe #
The readinessProbe should be used as an indication that the service is ready
to serve requests. When combined with Services construct, only containers
with the readinessProbe state set to Success will receive requests.
does not mean that the requests are sent to Pods “blindly”. The lack of the
retry mechanism is mitigated with readinessProbe , which we added to the
ReplicaSet.
The readinessProbe has the same fields as the livenessProbe . We used the
same values for both, except for the periodSeconds , where instead of relying
on the default value of 10 , we set it to 1 .
kubectl create \
-f svc/go-demo-2-api-rs.yml
cat svc/go-demo-2-api-svc.yml
apiVersion: v1
kind: Service
metadata:
name: go-demo-2-api
spec:
type: NodePort
ports:
- port: 8080
selector:
type: api
service: go-demo-2
There’s nothing truly new in this definition. The type is set to NodePort since
the API should be accessible from outside the cluster. The selector label type
is set to api so that it matches the labels defined for the Pods.
That is the last object we’ll create (in this section), so let’s move on and do it.
kubectl create \
-f svc/go-demo-2-api-svc.yml
Both ReplicaSets for db and api are there, followed by the three replicas of the
go-demo-2-api Pods and one replica of the go-demo-2-db Pod. Finally, the two
Services are running as well, together with the one created by Kubernetes
itself.
curl -i "http://$IP:$PORT/demo/hello"
We retrieved the port of the service (we still have the Minikube node IP from
before) and used it to send a request.
HTTP/1.1 200 OK
Date: Tue, 12 Dec 2017 21:27:51 GMT
Content-Length: 14
Content-Type: text/plain; charset=utf-8
hello, world!
We got the response 200 and a friendly hello, world! message indicating
that the API is indeed accessible from outside the cluster.
Destroying Services #
Before we move further, we’ll delete the objects we created.