Skip to content

Lab3 Service Solution

Service ClusterIP

image

Créer un service nommé s1 de type ClusterIP rassemblant des pods ayant comme label app1=web

Terminal window
vi s1.yml
apiVersion: v1
kind: Service
metadata:
name: s1
spec:
selector:
app1: web
type: ClusterIP
ports:
- port: 80
targetPort: 80

Créer un pod web1 basé sur l’image nginx avec comme label app1=web

Terminal window
vi web1.yml
apiVersion: v1
kind: Pod
metadata:
name: web1
labels:
app1: web
spec:
containers:
- name: nginx
image: nginx:1.21-alpine

Créer un pod web2 basé sur l’image httpd avec comme label app1=web

Terminal window
vi web2.yml
apiVersion: v1
kind: Pod
metadata:
name: web2
labels:
app1: web
spec:
containers:
- name: nginx
image: httpd:2.4-alpine

Créer un pod debug basé sur l’image alpine

Terminal window
vi debug.yml
apiVersion: v1
kind: Pod
metadata:
name: debug
spec:
containers:
- name: debug
image: alpine:3.13
command: ["sleep","3600"]

Se connecter sur le pod debug et exécuter plusieurs fois la commande “curl s1”

Appliquer les spécifications

Terminal window
kubectl apply -f .
pod/debug created
service/s1 created
pod/web1 created
pod/web2 created

Visualiser les pods et le service

Terminal window
kubectl get svc,pods
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/s1 ClusterIP 10.96.245.226 <none> 80/TCP 63s
NAME READY STATUS RESTARTS AGE
pod/debug 1/1 Running 0 63s
pod/web1 1/1 Running 0 63s
pod/web2 1/1 Running 0 63s

Se connecter sur le pod debug et exécuter plusieurs fois la commande “curl s1”

Terminal window
kubectl exec -it debug -- sh
Terminal window
apk add curl
while true
do
curl s1
done

Service NodePort

image

Créer un service nommé s2 de type NodePort rassemblant des pods ayant comme label app2=web

Terminal window
vi s2.yml
apiVersion: v1
kind: Service
metadata:
name: s2
spec:
selector:
app2: web
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30000

Créer un pod web3 basé sur l’image nginx avec comme label app2=web

Terminal window
vi web3.yml
apiVersion: v1
kind: Pod
metadata:
name: web3
labels:
app2: web
spec:
containers:
- name: nginx
image: nginx:1.21-alpine

Créer un pod web4 basé sur l’image httpd avec comme label app2=web

Terminal window
vi web4.yml
apiVersion: v1
kind: Pod
metadata:
name: web4
labels:
app2: web
spec:
containers:
- name: nginx
image: httpd:2.4-alpine

Depuis le poste de travail, exécuter plusieurs fois la commande “curl node1:30000”

Appliquer les spécifications

Terminal window
kubectl apply -f .
service/s2 created
pod/web3 created
pod/web4 created

Visualiser les pods et le service

Terminal window
kubectl get svc,pods
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/s2 NodePort 10.96.34.252 <none> 80:30000/TCP 57s
NAME READY STATUS RESTARTS AGE
pod/web3 1/1 Running 0 57s
pod/web4 1/1 Running 0 57s

Depuis le poste de travail, exécuter plusieurs fois la commande “curl node1:30000”

Terminal window
while true
do
curl node1:30000
done