Kubernetes: Creating deployments and service with a yaml file (2024)

In this article, I will be showing you step-by-step how to create two Kubernetes deployments and a service using a yaml file.

  • Spin up two deployments. One deployment contains 2 pods running the nginx image. Include a ConfigMap that points to a custom index.html page that contains the line “This is Deployment One”. The other deployment contains 2 pods running the nginx image. Include a ConfigMap that points to a custom index.html page that contains the line “This is Deployment Two”.
  • Create a service that points to both deployments. You should be able to both deployments using the same ip address and port number.
  • Use the curl command to validate that you eventually see the index.html pages from both Deployment 1 and Deployment 2.

Be sure that you have kubectl installed on your linux os by running the following command:

kubectl version --client

If you get an error message, use this link for instructions on how to install kubectl (instructions). Also, be sure to have your Docker Desktop running.

To begin, use the vim command to open a new file that will become our custom index.html file using ConfigMap for our nginx images in the Kubernetes deployments (we will create two files, one for each deployment). Save the files as “index-html-deployment1.yaml”, and enter the code below:

apiVersion: v1
kind: ConfigMap
metadata:
name: index-html-configmap1
namespace: default
data:
index.html: |
<html>
<h1>Hi! This is Deployment 1! </h1>
</html

We then need to create the ConfigMap by using this file with the kubectl apply command. Run the following:

kubectl apply -f index-html-deployment1.yaml

Here’s how your output should look:

output: configmap/index-html-configmap created

Now repeat the process and change the code to Deployment 2.

apiVersion: v1
kind: ConfigMap
metadata:
name: index-html-configmap2
namespace: default
data:
index.html: |
<html>
<h1>Hi! This is Deployment 2! </h1>
</html
kubectl apply -f index-html-deployment2.yaml

Great! We’ve created our ConfigMap’s for our custom index.html pages for our nginx servers that will be in our Kubernetes deployments.

To begin, use the vim command to open a new file that will become our yaml file for our Kubernetes deployment and let’s give the file this name, nginx-2deployments-service.yaml. In Kubernetes, this yaml file is called a “manifest”. We will use this manifest to create the 2 deployments and the service. Use the code below in your manifest. Each of the deployments and service will be separated by 3 dashes ( — — — ).

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment1
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: nginx-index-file
mountPath: /usr/share/nginx/html/index.html
subPath: index.html
volumes:
- name: nginx-index-file
configMap:
name: index-html-configmap1

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment2
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: nginx-index-file
mountPath: /usr/share/nginx/html/index.html
subPath: index.html
volumes:
- name: nginx-index-file
configMap:
name: index-html-configmap2

---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 80
nodePort: 30052

Let’s breakdown the code for our deployments and our service. Both of our deployments will be running the latest nginx image and we are adding a volume mount named nginx-index-file, including the mountPath, and a volume named nginx-index-file and mapping the configmap we created earlier. For the service, we are using a NodePort, which builds on top of the ClusterIP Service and provides a way to expose a group of Pods to the outside world. NodePorts can range from 30000–32767. We have set the NodePort to 30052.

Now that we have our manifest completed with both deployments and our service, let’s run it by using the following command:

kubebctl apply -f nginx-2deployments-service.yaml

Here is the output of this command:

output:
deployment.apps/nginx-deployment1 created
deployment.apps/nginx-deployment2 created
service/nginx-service created

And we can verify the creation of by running:

kubectl get all

Output:

Kubernetes: Creating deployments and service with a yaml file (1)

You can see we have 2 deployments with 2 pods each and we have our service. Now let’s check to see if our custom index file worked and that both deployments are reached from the nodeport. We will need to run the following command a few times to see it switch between deployments. Run:

curl localhost:30052

Output:

Kubernetes: Creating deployments and service with a yaml file (2)

We can also go to our browser and see the same thing:

Congratulations! We have successfully created a manifest that created 2 deployments with custom nginx index.html files and a service that points to those deployments. To clean up run the following commands:

kubectl delete deployment nginx-deployment1 nginx-deployment2
kubectl delete service nginx-service
Kubernetes: Creating deployments and service with a yaml file (2024)
Top Articles
Latest Posts
Article information

Author: Mrs. Angelic Larkin

Last Updated:

Views: 6238

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Mrs. Angelic Larkin

Birthday: 1992-06-28

Address: Apt. 413 8275 Mueller Overpass, South Magnolia, IA 99527-6023

Phone: +6824704719725

Job: District Real-Estate Facilitator

Hobby: Letterboxing, Vacation, Poi, Homebrewing, Mountain biking, Slacklining, Cabaret

Introduction: My name is Mrs. Angelic Larkin, I am a cute, charming, funny, determined, inexpensive, joyous, cheerful person who loves writing and wants to share my knowledge and understanding with you.