# Deploy Nginx Service in Kubernetes

## Nginx service Deployment

## Prerequisites

You have to set the Kubernetes cluster to follow these [links](https://docs.neevcloud.com/neevcloud-products/kubernetes/launch-kubernetes-cluster).

### Access the Kubernetes Master Node

To access the master node in a Kubernetes cluster, follow these [LINK:](https://docs.neevcloud.com/neevcloud-products/kubernetes/launch-kubernetes-cluster#to-ssh-into-the-master-node)

Open a terminal session.

Use the SSH protocol to connect:

```
ssh -i access_key user@master-node-address
```

{% hint style="info" %}
Replace `user` with your username and `master-node-address` with the IP address or hostname of the master node.
{% endhint %}

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 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&#x20;

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2FAVuzGSQSCui9Fp23haR1%2Fimage.png?alt=media&#x26;token=5012eea9-9620-42c1-83c6-bbbb82802cec" alt=""><figcaption></figcaption></figure>

### **Create a Deployment YAML File (deployment.yaml)**

```
vi 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)**

```
vi service.yaml
```

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

### Apply the YAML file to create the Nginx Service&#x20;

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 
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2Fksva3Tw1mRVCiibIAd9I%2Fimage.png?alt=media&#x26;token=e7896586-ce87-4cf0-a57b-4a0719f03f50" alt=""><figcaption></figcaption></figure>

* Once the external IP is assigned, you can use it to access your application.
* For detailed information about the service, run:

```
kubectl describe services 
```

<figure><img src="https://1876135298-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FEC5NwtFshv6EATOemuUn%2Fuploads%2F5tCQB4Rgc4M5IezJWo63%2Fimage.png?alt=media&#x26;token=6f16296d-5640-485f-9ead-77100f360634" alt=""><figcaption></figcaption></figure>

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>.
