Kubernetes -Services, Ingress, and ConfigurationsDay 9 of 50 days DevOps Tools Series
Tags: kubernetes, devops, development, containers
Published: 2024-07-17 12:59:30

## **Introduction**

Welcome to Day 9 of our 50 Days DevOps Tools series. Over the past two days, we have covered the fundamental and advanced concepts of Kubernetes, including its architecture, basic commands, Deployments, StatefulSets, and persistent storage. Today, we will delve into more advanced Kubernetes concepts such as Services, Ingress, and Configurations. These concepts are essential for managing network traffic and configuring applications within Kubernetes.

**Services**
In Kubernetes, a Service is an abstraction that defines a logical set of pods and a policy by which to access them. Services enable loose coupling between dependent pods. Kubernetes supports several types of services, such as ClusterIP, NodePort, and LoadBalancer.

**Key Features of Services:**

**Stable Network Endpoint:** Services provide a stable IP address and DNS name.
**Load Balancing:** Distributes traffic across multiple pods.
**Service Discovery:** Kubernetes automatically discovers services and endpoints.
**Isolation:** Services can isolate internal and external traffic.

**Types of Services**

**ClusterIP:** Exposes the service on an internal IP in the cluster. This is the default service type.
**NodePort:** Exposes the service on the same port of each selected node in the cluster.
**LoadBalancer:** Exposes the service using a cloud provider's load balancer.
**ExternalName:** Maps the service to the contents of the externalName field (e.g., foo.bar.example.com).

**Creating a Service:**
Here’s how to create a Service using a YAML configuration file.

**ClusterIP Service (default)**

```
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
```
**NodePort Service**

```
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
      nodePort: 30007

```
**LoadBalancer Service**

```
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: LoadBalancer
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
```
**Explanation:**

**apiVersion:** Specifies the API version.
**kind:** Specifies the type of Kubernetes object (Service).
**metadata:** Contains metadata about the Service, including the name.
**spec:** Defines the desired state of the Service.
**type:** Specifies the type of Service (ClusterIP, NodePort, LoadBalancer).
**selector:** Selects pods based on labels.
**ports:** Defines the port configurations for the Service.

**Commands:**

kubectl apply -f service.yaml #Create a service
kubectl get services #Get services
kubectl describe service my-service #Describe a service
kubectl delete service my-service #Delete a service

## **Ingress**

Ingress is a Kubernetes object that manages external access to services within a cluster, typically HTTP. Ingress can provide load balancing, SSL termination, and name-based virtual hosting.

**Key Features of Ingress:**

**Load Balancing:** Distributes traffic across multiple backend services.
**SSL/TLS Termination:** Terminate SSL/TLS at the ingress point.
**Name-Based Virtual Hosting:** Route traffic based on the host name.
**Creating an Ingress Resource:**

Here’s how to create an Ingress resource using a YAML configuration file.

```
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - host: my-app.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-service
            port:
              number: 80
```
**Explanation:**

**apiVersion:** Specifies the API version.
**kind:** Specifies the type of Kubernetes object (Ingress).
**metadata:** Contains metadata about the Ingress, including the name.
**spec:** Defines the desired state of the Ingress.
**rules:** Specifies the routing rules for the Ingress.
**host:** The host name to match.
**http:** Specifies the HTTP rules.
**paths:** Defines the paths to match and the backend services to route to.
**path:** The path to match.
**pathType:** The type of path matching (Prefix, Exact).
**backend:** The backend service and port to route to.

## Configurations: ConfigMaps and Secrets
Kubernetes provides ConfigMaps and Secrets to manage configuration data and sensitive information, respectively.

**ConfigMaps**
ConfigMaps are used to store non-confidential data in key-value pairs. They can be used to configure applications without hardcoding configuration data in the container images.

**Creating a ConfigMap:**
Here’s how to create a ConfigMap using a YAML configuration file.

```
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
data:
  database_url: mongodb://my-db:27017
  feature_enabled: "true"
```
**Explanation:**

**apiVersion:** Specifies the API version.
**kind:** Specifies the type of Kubernetes object (ConfigMap).
**metadata:** Contains metadata about the ConfigMap, including the name.
**data:** Defines the key-value pairs for the configuration data.

**Secrets**
Secrets are used to store confidential data, such as passwords, OAuth tokens, and SSH keys. Secrets are similar to ConfigMaps but are specifically designed to store sensitive information.

**Creating a Secret:**
Here’s how to create a Secret using a YAML configuration file.

```
apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  username: YWRtaW4=
  password: MWYyZDFlMmU2N2Rm
```
**Conclusion**

Understanding Services, Ingress, and Configurations (ConfigMaps and Secrets) in Kubernetes is crucial for managing network traffic and configuring applications within a cluster. These concepts help in maintaining a stable, secure, and scalable environment for your applications.

With this post, we conclude our three-day deep dive into Kubernetes. From tomorrow, we will explore other exciting DevOps tools that will further enhance your DevOps workflow. Stay tuned!

🔄 **Subscribe to our blog to get notifications on upcoming posts.**