Exposer des applications avec des services
Découvrez les différents types de services Kubernetes (ClusterIP, NodePort, LoadBalancer) permettant de rendre vos applications accessibles.
Exposer des applications avec des services est une leçon Docker & Kubernetes for Developers gratuite sur CoddyKit. Ceci est la leçon 2 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Docker & Kubernetes for Developers, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Docker & Kubernetes for Developers comprend 4 leçons au total.
Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.
What are K8s Services?
When you deploy applications in Kubernetes, your Pods can come and go. They get new IPs, they scale up or down, and they might even crash and restart.
This creates a challenge: how do other parts of your application, or external users, consistently find and communicate with these ever-changing Pods?
Services: Stable Access Point
Kubernetes Services solve this problem by providing a stable network abstraction over a set of Pods. Think of a Service as a single, unchanging IP address and DNS name for your application.
- Services act as load balancers, distributing traffic to healthy Pods.
- They ensure your application remains reachable even if individual Pods change or fail.
How Services Find Pods
Services don't directly manage Pods. Instead, they use labels and selectors to find the Pods they should route traffic to. When a Pod matches a Service's selector, it becomes an endpoint for that Service.
- Labels: Key-value pairs attached to objects (like Pods) for identification.
- Selectors: Rules defined in a Service to match Pod labels.
ClusterIP: Internal Only
The ClusterIP Service type is the default. It exposes the Service on an internal IP address within the cluster. This means the Service is only reachable from inside the cluster.
It's perfect for internal communication between different services in your application, like a frontend talking to a backend database.
ClusterIP Service Example
Here's a YAML definition for a ClusterIP Service. It targets Pods with the label app: my-app and exposes port 80, routing traffic to container port 8080.
Apply it with kubectl apply -f service.yaml.
apiVersion: v1
kind: Service
metadata:
name: my-app-clusterip
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIPNodePort: External via Node IP
A NodePort Service opens a specific port on all worker nodes in your cluster. External traffic can reach your Service by contacting any node's IP address on that specific port (e.g., NodeIP:NodePort).
This is useful for exposing services to the outside world, especially in small setups or for development, without needing a cloud load balancer.
NodePort Service Example
This NodePort Service will expose port 80 of your application on a high port (typically 30000-32767) on every node. You can specify a nodePort or let Kubernetes assign one.
After applying, find the assigned port with kubectl get svc.
apiVersion: v1
kind: Service
metadata:
name: my-app-nodeport
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
nodePort: 30007 # Optional: K8s assigns if omitted
type: NodePortLoadBalancer: Cloud Integration
A LoadBalancer Service is primarily used when running Kubernetes on a cloud provider (like AWS, GCP, Azure). It automatically provisions an external cloud load balancer.
This provides a dedicated, stable external IP address and often integrates with cloud-specific features like SSL termination and advanced routing.
LoadBalancer Service Example
When deployed on a cloud provider, this Service will automatically create and configure an external load balancer, assigning it an external IP address.
The external IP will be visible via kubectl get svc once provisioned.
apiVersion: v1
kind: Service
metadata:
name: my-app-loadbalancer
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancerService Type Overview
Let's quickly compare the primary Service types:
- ClusterIP: Internal access only, within the cluster.
- NodePort: External access via a specific port on each node.
- LoadBalancer: External access via a cloud provider's load balancer, stable external IP.
Choosing the right type depends on whether your application needs to be accessible internally or externally, and your deployment environment.
Service Type Quiz
Which Kubernetes Service types provide a direct mechanism for external traffic to reach applications running inside the cluster?
Recap: Exposing Apps
Great job! You've learned how Kubernetes Services abstract network access to your Pods and make your applications reachable. We explored three main types:
- ClusterIP: For internal-only communication within the cluster.
- NodePort: Exposes services on a static port across all nodes for external access.
- LoadBalancer: Integrates with cloud providers to provision an external load balancer, providing a stable external IP.
Understanding these Service types is fundamental for designing accessible and robust applications in Kubernetes.
Questions Fréquemment Posées
La leçon « Exposer des applications avec des services » est-elle gratuite ?
Oui — le texte complet de « Exposer des applications avec des services » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Docker & Kubernetes for Developers, passe à CoddyKit PRO. Le cours Docker & Kubernetes for Developers comprend 4 leçons au total.
Qu'est-ce que j'apprendrai dans « Exposer des applications avec des services » ?
Découvrez les différents types de services Kubernetes (ClusterIP, NodePort, LoadBalancer) permettant de rendre vos applications accessibles. Tu pratiques Docker & Kubernetes for Developers avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.
Dois-je avoir de l'expérience pour commencer Docker & Kubernetes for Developers ?
Aucune expérience préalable n'est requise. Docker & Kubernetes for Developers sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 2 sur 4.
Combien de temps prend la leçon « Exposer des applications avec des services » ?
La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.
Peux-tu écrire et exécuter du code dans cette leçon Docker & Kubernetes for Developers ?
Oui. Chaque leçon Docker & Kubernetes for Developers inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.
Toutes les leçons de ce cours
- Comprendre les déploiements Kubernetes
- Exposer des applications avec des services
- Mise à l’échelle et auto-rétablissement des applications
- Mises à jour progressives et retours en arrière