Bobcares

Create Subdomains using Namespaces on DigitalOcean

by | Jul 26, 2024

Learn how to create Subdomains using Namespaces on DigitalOcean. Our DigitalOcean Support team is here to help you with your questions and concerns.

Create Subdomains using Namespaces on DigitalOcean

Did you know that setting up a subdomain using a namespace on DigitalOcean Kubernetes helps manage and deploy our applications?

Create Subdomains using Namespaces on DigitalOcean

Today, we are going to take a look at how to create a Kubernetes cluster, set up a namespace, deploy our application, configure ingress, and update DNS settings.

Set Up Kubernetes Cluster on DigitalOcean

  1. First, log in to the DigitalOcean account.
  2. Then, go to the Kubernetes section and create a new cluster. At this point, we can choose our cluster configuration, including the region and node size.
  3. Next, we have to make sure we have kubectl installed on the local machine. We can use the kubectl tool to interact with Kubernetes clusters.

    # Install kubectl on Ubuntu
    sudo apt-get update
    sudo apt-get install -y kubectl

  4. Then, download the Kubernetes configuration file from the DigitalOcean control panel and set up kubectl to use it.

    # Save the configuration file and set the KUBECONFIG environment variable
    export KUBECONFIG=$HOME/.kube/config

Create a Namespace

Now, we can create a new namespace in our Kubernetes cluster with kubectl:

kubectl create namespace mynamespace

Deploy Application

Now, we have to deploy our application to the newly created namespace. We can use a deployment YAML file to specify the namespace.


apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
namespace: mynamespace
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
ports:
- containerPort: 80

Then, apply the deployment as seen here:

kubectl apply -f deployment.yaml

Configure Ingress to Use Subdomain

  1. Next, install an ingress controller in the cluster to manage external access to our services. DigitalOcean offers an easy way to install the NGINX ingress controller.

    kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/cloud/deploy.yaml

  2. Then, create an ingress resource to route traffic to our application using the subdomain.


    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
    name: myapp-ingress
    namespace: mynamespace
    annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    spec:
    rules:
    - host: sub.example.com
    http:
    paths:
    - path: /
    pathType: Prefix
    backend:
    service:
    name: myapp-service
    port:
    number: 80

  3. Now, apply the ingress as seen here:

    kubectl apply -f ingress.yaml

Update DNS Settings

  1. In the DigitalOcean control panel, go to the Networking section and select the domain.
  2. Then, add a new CNAME record for the subdomain, pointing to the DigitalOcean Kubernetes Load Balancer IP address.

    Type: CNAME
    Name: sub
    Value: <Load Balancer IP>
    TTL: 3600

Benefits of Using Namespace and Subdomain

  • Namespaces offer isolation for different environments or different applications.
  • Using subdomains helps in organizing and routing traffic to different applications or parts of an application.
  • This approach scales well with additional services or applications, each with its own namespace and subdomain.

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

In brief, our Support Experts demonstrated how to create Subdomains using Namespaces on DigitalOcean.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.