Problem Statement:-
In this lab we will create services on top of our deployed microservices so that they can talk to each other and making our webserver MS publically accessible:
Expose Internally
- MySQL
- Attendance
- Gateway
- Frontend
Expose Publically
- Webserver
By the end of this lab, you should be able to have the attendance functionality fully functional.
What you will not have is the automated deployment of your MS. To do a new deployment you have to create a new replica set and delete previous one.
Let's try to set up frontend with replicaset with name frontend-replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: empms-frontend
labels:
app: empms-frontend
spec:
replicas: 1
selector:
matchLabels:
app: empms-frontend
template:
metadata:
labels:
app: empms-frontend
spec:
containers:
- name: empms-frontend
image: opstree/empms-frontend:1.0
ports:
- containerPort: 5000Let's create the resource in Kubernetes
kubectl apply -f frontend-replicaset.yamlLet's try to set up a webserver with replicaset with name webserver-replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: empms-webserver
labels:
app: empms-webserver
spec:
replicas: 1
selector:
matchLabels:
app: empms-webserver
template:
metadata:
labels:
app: empms-webserver
spec:
containers:
- name: empms-webserver
image: opstree/empms-webserver:1.0
ports:
- containerPort: 80Let's create the resource in Kubernetes
kubectl apply -f webserver-replicaset.yamlNote:- If you see webserver pod will be in an error state because it cannot resolve the name of frontend service. It will be fixed once we create the service for Frontend application.
Let's create the service for all applications.
Create a manifest for MySQL service with name mysql-service.yaml
---
kind: Service
apiVersion: v1
metadata:
name: empms-db
spec:
type: ClusterIP
selector:
app: empms-db
ports:
- protocol: TCP
port: 3306Create the service from this manifest.
kubectl apply -f mysql-service.yamlNow let's verify the service
kubectl get svcCreate a manifest for attendance service with name attendance-service.yaml
---
kind: Service
apiVersion: v1
metadata:
name: empms-attendance
spec:
type: ClusterIP
selector:
app: empms-attendance
ports:
- protocol: TCP
port: 8081Create the service from this manifest.
kubectl apply -f attendance-service.yamlNow let's verify the service
kubectl get svcCreate a manifest for gateway service with name gateway-service.yaml
---
kind: Service
apiVersion: v1
metadata:
name: empms-gateway
spec:
type: ClusterIP
selector:
app: empms-gateway
ports:
- protocol: TCP
port: 8080Create the service from this manifest.
kubectl apply -f gateway-service.yamlNow let's verify the service
kubectl get svcCreate a manifest for frontend service with name frontend-service.yaml
---
kind: Service
apiVersion: v1
metadata:
name: empms-frontend
spec:
type: ClusterIP
selector:
app: empms-frontend
ports:
- protocol: TCP
port: 5000Create the service from this manifest.
kubectl apply -f frontend-service.yamlNow let's verify the service
kubectl get svcCreate a manifest for webserver service with name webserver-service.yaml
---
kind: Service
apiVersion: v1
metadata:
name: empms-webserver
spec:
type: NodePort
selector:
app: empms-webserver
ports:
- protocol: TCP
port: 80
nodePort: 30085Here we are using the NodePort to expose the service, but it's not a recommended way to expose service in Production.
Create the service from this manifest.
kubectl apply -f webserver-service.yamlkubectl get svcWe can validate the service by using the NodePort which we have defined above.
kubectl delete -f webserver-service.yamlThe definition of load balancer type service would be:-
---
kind: Service
apiVersion: v1
metadata:
name: empms-webserver
spec:
type: LoadBalancer
selector:
app: empms-webserver
ports:
- protocol: TCP
port: 80
targetPort: 80
name: httpHere we are using the LoadBalancer to expose the service.
Now let's verify the service
kubectl get svcminikube service empms-webserverWe can use the LoadBalancer public IP to access the application from the web browser.
Now try to create the new replicaset with version 2 of the image.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: empms-frontend-version2
labels:
app: empms-frontend-version2
spec:
replicas: 1
selector:
matchLabels:
app: empms-frontend-version2
template:
metadata:
labels:
app: empms-frontend-version2
spec:
containers:
- name: empms-frontend
image: opstree/empms-frontend:2.0
ports:
- containerPort: 5000Now for a new version of deployment, we have to delete the first replicaset.
kubectl delete rs emps-frontendAnd then create again
kubectl apply -f frontend-replicaset.yaml