Working with Services in Kubernetes

Working with Services in Kubernetes

What are Services in K8s

In Kubernetes (K8s), a service is an abstraction that defines a logical set of pods and a policy by which to access them. It provides a stable endpoint (IP address and port) that can be used to interact with the pods, abstracting away the complexity of managing individual pod IPs and dynamic changes in the cluster.

Key points about Kubernetes services

  1. Stable Network Endpoint:A service is assigned a stable IP address and port, allowing clients to connect to it without needing to know the specific IP addresses of the individual pods.
  2. Pod Selection:A service selects pods based on labels. Pods with matching labels are part of the service.
  3. Types of Services:Kubernetes supports various types of services, including ClusterIP, NodePort, LoadBalancer, and ExternalName. Each type has its own use case and characteristics.ClusterIP: Exposes the service on a cluster-internal IP. This type makes the service only accessible from within the cluster.NodePort: Exposes the service on each node's IP at a static port. This makes the service accessible externally, not just within the cluster.LoadBalancer: Creates an external load balancer that forwards traffic to the service. This is useful when you need to expose the service to the internet.ExternalName: Maps the service to the contents of the externalName field (e.g., a DNS name).
  4. Service Discovery:Services enable service discovery within the Kubernetes cluster. Other applications or services can discover and communicate with each other using the service's stable IP and port.
  5. Load Balancing:For services with multiple pods, Kubernetes automatically load-balances the traffic among the selected pods.
  6. Session Affinity:Services can be configured to use session affinity, ensuring that requests from a client are always directed to the same pod, which can be useful for maintaining state.

Headless Service:

In addition to the common service types, Kubernetes also supports headless services. A headless service is used when you don't need a stable IP and you want direct communication with the individual pods.

Service for your todo-app Deployment

apiVersion: v1
kind: Service
metadata:
  name: todo-app-service
spec:
  selector:
    app: todo-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  type: ClusterIP        

  • metadata.name: Specifies the name of the service, in this case, todo-app-service.
  • spec.selector: Defines the labels that the service will use to select pods. In this example, it selects pods with the label app: todo-app. Ensure that your todo-app deployment pods have this label.
  • spec.ports: Specifies the ports configuration. In this case, it exposes port 80 on the service, which will be internally forwarded to port 8080 on the pods.
  • spec.type: Defines the type of service. Here, it's set to ClusterIP, which means the service will get an internal IP within the cluster.

kubectl apply -f todo-app-service.yaml        

要查看或添加评论,请登录

Daniel Gurus的更多文章

  • Why prefer Kubernetes ?

    Why prefer Kubernetes ?

    Kubernetes has become a cornerstone in modern container orchestration and management for a variety of reasons. Its…

    3 条评论
  • AWS EC2 Automation

    AWS EC2 Automation

    Instance Types General Purpose Instances (e.g.

  • AWS and IAM Basics

    AWS and IAM Basics

    AWS Identity and Access Management (IAM) is a web service provided by Amazon Web Services (AWS) that enables you to…

    2 条评论
  • Managing Persistent Volumes in Your Deployment

    Managing Persistent Volumes in Your Deployment

    What are Persistent Volumes in k8s In Kubernetes (k8s), a Persistent Volume (PV) is a cluster-wide piece of storage in…

  • Mastering ConfigMaps and Secrets in Kubernetes

    Mastering ConfigMaps and Secrets in Kubernetes

    What are ConfigMaps and Secrets in k8s ConfigMaps: ConfigMaps are Kubernetes resources that allow you to decouple…

  • Mastering Docker Best Practices: A DevOps Engineer's Guide

    Mastering Docker Best Practices: A DevOps Engineer's Guide

    Introduction: In the ever-evolving landscape of software development and deployment, Docker has emerged as a…

  • Devops Best Practices for Seamless Integration

    Devops Best Practices for Seamless Integration

    Introduction: In today's fast-paced tech world, the need for efficient collaboration between development and operations…

    1 条评论
  • Working with Namespaces and Services in Kubernetes

    Working with Namespaces and Services in Kubernetes

    What are Namespaces and Services in k8s A Namespace in Kubernetes is a way to partition cluster resources. It allows…

  • Basic networking concepts for Devops engineer

    Basic networking concepts for Devops engineer

    Here are some fundamental networking concepts that are important for a DevOps engineer: IP Addressing: IPv4 and IPv6:…

    3 条评论
  • Launching your Kubernetes Cluster with Deployment

    Launching your Kubernetes Cluster with Deployment

    What is Deployment in k8s? A Kubernetes Deployment is an API resource that provides a declarative way to define the…

社区洞察

其他会员也浏览了