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?
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
- First, log in to the DigitalOcean account.
- 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.
- 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
Copy Code - 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
Copy Code
Create a Namespace
Now, we can create a new namespace in our Kubernetes cluster with kubectl:
kubectl create namespace mynamespace
Copy Code
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
Copy Code
Then, apply the deployment as seen here:
kubectl apply -f deployment.yaml
Copy Code
Configure Ingress to Use Subdomain
- 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
Copy Code - 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
Copy Code - Now, apply the ingress as seen here:
kubectl apply -f ingress.yaml
Copy Code
Update DNS Settings
- In the DigitalOcean control panel, go to the Networking section and select the domain.
- 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
Copy Code
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