Basavraj Devpuje:
Hello,
Recently I have started the course and I have one question regarding the ClusterIP service.
Could you please provide me an example for “How to use service name inside pod spec”.
Thank you in advance.
Mohamed Ayman:
Hello @Basavraj Devpuje,
The service name isn’t used inside the Pod spec.
we call the label of the pod inside the spec of the service itself. check the below example
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
<http://app.kubernetes.io/name|app.kubernetes.io/name>: proxy
spec:
containers:
- name: nginx
image: nginx:11.14.2
ports:
- containerPort: 80
name: http-web-svc
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
<http://app.kubernetes.io/name|app.kubernetes.io/name>: proxy
ports:
- name: name-of-service-port
protocol: TCP
port: 80
targetPort: http-web-svc
Basavraj Devpuje:
Thank you. Now its clear . But still a small question. For e.g. If the backend svc have the name “backend-svc”. Then how the front-end pods can access the svc “backend-svc”? like in yaml file
unnivkn:
Hi @Basavraj Devpuje fyr:
unnivkn:
k run frontend --image=nginx --expose --port=80
k run backend --image=nginx --expose --port=80
k get po,svc
root@controlplane ~ ➜ k get po,svc -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/backend 1/1 Running 0 91s 10.244.0.5 controlplane <none> <none>
pod/frontend 1/1 Running 0 9m34s 10.244.0.4 controlplane <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/backend-svc ClusterIP 10.97.58.3 <none> 80/TCP 36s run=backend
service/frontend-svc ClusterIP 10.111.234.60 <none> 80/TCP 4m27s run=frontend
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 20m <none>
root@controlplane ~ ➜
root@controlplane ~ ➜ k describe svc frontend-svc backend-svc
Name: frontend-svc
Namespace: default
Labels: <none>
Annotations: <none>
Selector: run=frontend
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.111.234.60
IPs: 10.111.234.60
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.0.4:80
Session Affinity: None
Events: <none>
Name: backend-svc
Namespace: default
Labels: <none>
Annotations: <none>
Selector: run=backend
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.97.58.3
IPs: 10.97.58.3
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.0.5:80
Session Affinity: None
Events: <none>
root@controlplane ~ ➜
root@controlplane ~ ➜
root@controlplane ~ ➜ k exec frontend – curl backend-svc
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 615 100 615 0 0 300k 0 --:–:-- --:–:-- --:–:-- 300k
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href=“http://nginx.org/”>http://nginx.org|nginx.org</a>.<br/>
Commercial support is available at
<a href=“http://nginx.com/”>http://nginx.com|nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@controlplane ~ ➜
root@controlplane ~ ➜ k exec backend – curl frontend-svc
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href=“http://nginx.org/”>http://nginx.org|nginx.org</a>.<br/>
Commercial support is available at
<a href=“http://nginx.com/”>http://nginx.com|nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
100 615 100 615 0 0 600k 0 --:–:-- --:–:-- --:–:-- 600k
root@controlplane ~ ➜