Deploy Nginx Service in Kubernetes

To deploy Nginx in Kubernetes and make it accessible from external sources, you need to follow a few steps:

Accessing the Kubernetes Master Node

To access the master node in a Kubernetes cluster, follow these LINK:

  1. Open a terminal session.

  2. Use the SSH protocol to connect:

    ssh -i access_keuser@master-node-address

    Replace user with your username and master-node-address with the IP address or hostname of the master node.

  3. Enter your password or authenticate with your SSH key when prompted.

Once connected, you can perform administrative tasks on the Kubernetes master node.

Create a Deployment: Define a Kubernetes Deployment manifest to specify the desired state for the Nginx deployment.

Expose the Deployment: Expose the Nginx Deployment either through a Kubernetes Service of type NodePort or LoadBalancer.

Access from External Sources: Access the Nginx service using the assigned NodePort or the external IP if you're using a LoadBalancer.

Here is a step-by-step guide :-

First, check your nodes Check the status of your nodes

  • Create a Deployment YAML File (deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80
  • Create a Service YAML File (service.yaml)

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Once you have created this manifest (deployment.yaml , service.yaml for example ), you can apply it to your Kubernetes cluster using the following command:

kubectl apply -f deployment.yaml

kubectl apply -f service.yaml

  • Check the status of your service using:

kubectl get services

  • Once the external IP is assigned, you can use it to access your application.

  • For detailed information about the service, run:

kubectl describe services

When you create a LoadBalancer Service in Kubernetes for your NGINX deployment, an external IP address is automatically allocated. This allows you to access NGINX from any location using the given public IP.

After deploying the application and service, find the public IP of a node in the Kubernetes cluster.

To verify the service is operational, access it through the load balancer by visiting: http://load_balancer_IP

Understanding YAML

YAML, which stands for YAML Ain't Markup Language, is a human-readable data serialization standard. It is commonly used for configuration files and data exchange between languages with different data structures. Its straightforward structure, utilizing indents and simple syntax, makes it accessible for defining hierarchical data.

YAML files typically use the .yaml or .yml extension. They support data structures such as objects (hashes/maps), arrays, and scalars (strings, integers, and floats), making it versatile for various applications. A key principle in YAML is its emphasis on being easy to read and write, not only for machines but also for humans.

Key Features: Indentation-based structure: Uses spaces for nesting, which visually groups data.

  • Scalar styles: Supports block (using | for maintaining line breaks) and flow (using > for folding line breaks).

  • Comments: Begins with, allowing for explanation or exclusion of elements.

  • Support for complex data types: Allows for representing objects, arrays, and primitives.

Use Cases:

  • Configuration files: Widely utilized in software projects for setting up applications or services.

  • Data exchange: Facilitates data sharing between programming languages with minimal parsing overhead.

In summary, YAML's human-friendly design makes it an excellent choice for configuration and data representation tasks in software development and beyond.

apiVersion:- Specific to the version of the Kubernetes API being used. it defines the schema of the object that you're creating or modifying.

kind:- Specifics the type of Kubernetes resource being defined. Common values include 'Pod', 'Deployment', 'Service', 'PersistenVloumeClaim' etc.

metadata:- Contains metadata about the resource, such as the name, namespace, labels, and annotations.

*name:- The <name> field specifies the unique name of the Kubernetes object. It serves as an identifier and must be unique within its namespace.

*Namespace:- The namespace field(optional) refers to the Kubernetes namespace where the object will be created. Namespaces are used to organize and isolate resources within a cluster. if omitted, the default namespace is used.

*Labels:- Labels are key-value pairs that allow you to arbitrary metadata to Kubernetes objects. They are used for selecting and grouping objects based on specific criteria labels are not meant for identifying objects but rather for categorization.

*Annotations: -Annotations are also key-value pairs, similar to labels. However, they serve a different purpose. Annotations, allow you to attach additional non-identifying metadata to objects Unlike labels, annotations can include more complex data and characters not allowed by labels, they are often used for recording information related to configuration release, details, debugging, and more.

spec:- Defines the desired state of the resources. This section varies depending on the kind of resource being defined.

*For a Pod- it includes the containers to run, volumes to mount, and other pod-level configurations.

*For a Deployment- it includes the desired number of replicas, the template for creating pods, and deployment-specific settings.

*For a Service- it includes the type of service ( ClusterIP, NodePort, LoadBalancer ), the selector to route traffic to pods, and other service-specific configurations.

status:- This section is not included in the YAML files that you create manually. it is automatically generated by Kubernetes and provides information about the current status of the resource.

Last updated