Kubernetes Deployment Tutorial For Beginners - Understanding YAML (2024)

This Kubernetes deployment tutorial guide will explain the key concepts in a Kubernetes YAML specification with an Nginx example deployment.

Introduction:

In Kubernetes, pods are the basic units that get deployed in the cluster. Kubernetes deployment is an abstraction layer forthe pods. The main purpose of the deployment object is to maintain the resources declared in the deployment configuration in its desired state. A deployment configuration can be of YAML or JSON format.

Key Things To Understand

  1. A Deployment can schedule multiple pods. A pod as a unit cannot scale by itself.
  2. A Deployment represents a single purpose with a group of PODs.
  3. A single POD can have multiple containers and these containers inside a single POD shares the same IP and can talk to each other using localhost address.
  4. To access a Deployment with one or many PODs, you need a Kubernetes Service endpoint mapped to the deployment using labels and selectors.
  5. A deployment should have only stateless services. Any application that requires state management should be deployed as a Kubernetes StatefulSet.

Deployment YAML:

Kubernetes deployment Yaml contains the following main specifications.

  1. apiVersion
  2. Kind
  3. metadata
  4. spec

Now let’s look at each specification in detail.

Note: In Kubernetes, everything persistent is defined as an object. Example: Deployments, services, Replica Set, Configmap, Jobs etc

apiVersion

This specifies the API version of the Kubernetes deployment object. It varies between each Kubernetes version.

How To Use the Right API version:Kubernetes contains three API versions.

  1. Alpha: This is the early release candidate. It might contain bugs and there is no guarantee that it will work in the future. Example:scalingpolicy.kope.io/v1alpha1
  2. Beta: The API’s become beta once its alpha tested. It will be in continuous development & testing until it becomes stable. Beta versions will most likely go into the Kubernetes main APIs.Example:batch/v1beta1
  3. Stable: The APIs which does not contain alpha and beta goes into the stable category. Only stableversions are recommended to be used in production systems. Example: apps/v1

These APIs could belong to different API groups.

An example list of Kubernetes APIs from different API groups from Kubernetes version1.10.6 is shown below. Deployment object belongs toapps API group. You can list these API on http://localhost:8001/ using the kubectl proxy.

{ "paths": [ "/api", "/api/v1", "/apis", "/apis/", "/apis/admissionregistration.k8s.io", "/apis/admissionregistration.k8s.io/v1beta1", "/apis/apiextensions.k8s.io", "/apis/apiextensions.k8s.io/v1beta1", "/apis/apiregistration.k8s.io", "/apis/apiregistration.k8s.io/v1", "/apis/apiregistration.k8s.io/v1beta1", "/apis/apps", "/apis/apps/v1", "/apis/apps/v1beta1", "/apis/apps/v1beta2", "/apis/authentication.k8s.io", "/apis/authentication.k8s.io/v1", "/apis/authentication.k8s.io/v1beta1", "/apis/authorization.k8s.io", "/apis/authorization.k8s.io/v1", "/apis/authorization.k8s.io/v1beta1", "/apis/autoscaling", "/apis/autoscaling/v1", "/apis/autoscaling/v2beta1", "/apis/batch", "/apis/batch/v1", "/apis/batch/v1beta1", "/apis/certificates.k8s.io", "/apis/certificates.k8s.io/v1beta1", "/apis/cloud.google.com", "/apis/cloud.google.com/v1beta1", "/apis/extensions", "/apis/extensions/v1beta1", "/apis/metrics.k8s.io", "/apis/metrics.k8s.io/v1beta1", "/apis/networking.k8s.io", "/apis/networking.k8s.io/v1", "/apis/policy", "/apis/policy/v1beta1", "/apis/rbac.authorization.k8s.io", "/apis/rbac.authorization.k8s.io/v1", "/apis/rbac.authorization.k8s.io/v1beta1", "/apis/scalingpolicy.kope.io", "/apis/scalingpolicy.kope.io/v1alpha1", "/apis/storage.k8s.io", "/apis/storage.k8s.io/v1", "/apis/storage.k8s.io/v1beta1" ]}

Kind

Kind describes the type of the object/resource to be created. In our case its a deployment object. Following are the main list of objects/resources supported by Kubernetes.

componentstatusesconfigmapsdaemonsetsdeploymentseventsendpointshorizontalpodautoscalersingressjobslimitrangesnamespacesnodespodspersistentvolumespersistentvolumeclaimsresourcequotasreplicasetsreplicationcontrollersserviceaccountsservices

Metadata

It is a set of data to uniquely identify a Kubernetes object. Following are the key metadata that can be added to an object.

labelsnamenamespaceannotations

Let’s have a look at each metadata type

  1. Labels: Key-value pairs primarily used to group and categorize deployment object. It is intended for an object to object grouping and mapping using selectors. For example, kubernetes service uses the pod labels in its selectors to send traffic to the right pods. We will see more about labels and selectors in the service creation section.
  2. Name: It represents the name of the deployment to be created.
  3. Namespace: Name of the namespace where you want to create the deployment.
  4. Annotations: key-value pairs like labels, however, used for different use cases. You can add any information to annotations. For example, you can have an annotation like "monitoring" : "trueand external sources will be able to find all the objects with this annotation to scrape its metrics. Objects without this annotation will be omitted.

There are other system generated metadata such us UUID, timestamp, resource version etc. that gets added to each deployment.

Example metadata

metadata: name: resource-name namespace: deployment-demo labels: app: web platform: java release: 18.0 annotations: monitoring: true prod: true

Spec

Under spec, we declare the desired state and characteristics of the object we want to have. For example, in deployment spec, we would specify the number of replicas, image name etc. Kubernetes will make sure all the declaration under the spec is brought to the desired state.

Spec has three important subfields.

  1. Replicas: It will make sure the numbers of pods running all the time for the deployment. Example,
    spec: replicas: 3
  2. Selector: It defines the labels that match the pods for the deployments to manage. Example,
    selector: matchLabels: app: nginx
  3. Template: It has its own metadata and spec. Spec will have all the container information a pod should have. Container image info, port information, ENV variables, command arguments etc. Example,
    template: metadata: labels: app: nginx spec: containers: - image: nginx name: nginx

Kubernetes Example Deployment

Since we have looked at the basics let start with an example deployment. We will do the following in this section.

  1. Create a namespace
  2. Create a Nginx Deployment
  3. Create a Nginx Service
  4. Expose and access the Nginx Service

Note: Few of the operations we perform in this example can be performed with just kubectland without a YAML Declaration. However, we are using the YAML specifications for all operations to understand it better.

Exercise Folder

To begin the exercise, create a folder names deployment-demo and cd into that folder. Create all the exercise files in this folder.

mkdir deployment-demo && cd deployment-demo

Create a Namespace

Let’s create a YAML named namespace.yaml file for creating the namespace.

apiVersion: v1kind: Namespacemetadata: name: deployment-demo labels: apps: web-based annotations: type: demo

Use kubectl command to create the namespace.

kubectl create -f namespace.yaml

Equivalent kubectl command

kubectl create namespace deployment-demo

Assign Resource Quota To Namespace

Now let’s assign some resource quota limits to our newly created namespace. This will make sure the pods deployed in this namespace will not consume more system resources than mentioned in the resource quota.

Create a file named resourceQuota.yaml. Here is the resource quota YAML contents.

apiVersion: v1kind: ResourceQuotametadata: name: mem-cpu-quota namespace: deployment-demospec: hard: requests.cpu: "4" requests.memory: 8Gi limits.cpu: "8" limits.memory: 16Gi

Create the resource quota using the YAML.

kubectl create -f resourceQuota.yaml

Now, let’s describe the namespace to check if the resource quota has been applied to the deployment-demo namespace.

kubectl describe ns deployment-demo

The output should look like the following.

Name: deployment-demoLabels: apps=web-basedAnnotations: type=demoStatus: ActiveResource Quotas Name: mem-cpu-quota Resource Used Hard -------- --- --- limits.cpu 0 2 limits.memory 0 2Gi requests.cpu 0 1 requests.memory 0 1Gi

Create a Deployment

We will use the public Nginx image for this deployment.

Create a file named deployment.yaml and copy the following YAML to the file.

Note: This deployment YAML has minimal required information we discussed above. You can have more specification in the deployment YAML based on the requirement.

apiVersion: apps/v1kind: Deploymentmetadata: name: nginx labels: app: nginx namespace: deployment-demo annotations: monitoring: "true"spec: replicas: 1 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - image: nginx name: nginx ports: - containerPort: 80 resources: limits: memory: "2Gi" cpu: "1000m" requests: memory: "1Gi" cpu: "500m"

Under containers, we have defined its resource limits, requests and container port (one exposed in Dockerfile).

Create the deployment using kubectl

kubectl create -f deployment.yaml

Check the deployment

kubectl get deployments -n deployment-demo

Even though we have added minimal information, after deployment, Kubernetes will add more information to the deployment such asresourceVersion,uid,status etc.

You can check it by describing the deployment in YAML format using the kubectl command.

kubectl get deployment nginx -n deployment-demo --output yaml

Create a Service and Expose The Deployment

Now that we have a running deployment, we will create a Kubernetes service of type NodePort ( 30500) pointing to the nginx deployment. Using NodePort you will be able to access the Nginx service on all the kubernetes node on port30500.

Create a file named service.yaml and copy the following contents.

apiVersion: v1kind: Servicemetadata: labels: app: nginx name: nginx namespace: deployment-demospec: ports: - nodePort: 30500 port: 80 protocol: TCP targetPort: 80 selector: app: nginx type: NodePort

Service is the best example for explaining labels and selectors. In this service, we have a selector with “app” = “nginx” label. Using this, the service will be able to match the pods in our nginx deployment as the deployment and the pods have the same label. So automatically all the requests coming to the nginx service will be sent to the nginx deployment.

Let’s create the service using kubectl command.

kubectl create -f service.yaml

You can view the service created using kubectl command.

kubectl get services -n deployment-demo

Now, you will be able to access the nginx service on any one of the kubernetes node IP on port 30500

For example,

http://35.134.110.153:30500/
Kubernetes Deployment Tutorial For Beginners - Understanding YAML (2024)

References

Top Articles
Roast Chicken with Balsamic Bell Peppers Recipe
Best Tourtiere Recipe
Spasa Parish
Rentals for rent in Maastricht
159R Bus Schedule Pdf
Sallisaw Bin Store
Black Adam Showtimes Near Maya Cinemas Delano
Espn Transfer Portal Basketball
Pollen Levels Richmond
11 Best Sites Like The Chive For Funny Pictures and Memes
Things to do in Wichita Falls on weekends 12-15 September
Craigslist Pets Huntsville Alabama
Paulette Goddard | American Actress, Modern Times, Charlie Chaplin
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
What's the Difference Between Halal and Haram Meat & Food?
Tyreek Hill admits some regrets but calls for officer who restrained him to be fired | CNN
Haverhill, MA Obituaries | Driscoll Funeral Home and Cremation Service
Rogers Breece Obituaries
Ems Isd Skyward Family Access
Elektrische Arbeit W (Kilowattstunden kWh Strompreis Berechnen Berechnung)
Omni Id Portal Waconia
Kellifans.com
Banned in NYC: Airbnb One Year Later
Four-Legged Friday: Meet Tuscaloosa's Adoptable All-Stars Cub & Pickle
Model Center Jasmin
Ice Dodo Unblocked 76
Is Slatt Offensive
Labcorp Locations Near Me
Storm Prediction Center Convective Outlook
Experience the Convenience of Po Box 790010 St Louis Mo
Fungal Symbiote Terraria
modelo julia - PLAYBOARD
Poker News Views Gossip
Abby's Caribbean Cafe
Joanna Gaines Reveals Who Bought the 'Fixer Upper' Lake House and Her Favorite Features of the Milestone Project
Tri-State Dog Racing Results
Navy Qrs Supervisor Answers
Trade Chart Dave Richard
Lincoln Financial Field Section 110
Free Stuff Craigslist Roanoke Va
Wi Dept Of Regulation & Licensing
Pick N Pull Near Me [Locator Map + Guide + FAQ]
Crystal Westbrooks Nipple
Ice Hockey Dboard
Über 60 Prozent Rabatt auf E-Bikes: Aldi reduziert sämtliche Pedelecs stark im Preis - nur noch für kurze Zeit
Wie blocke ich einen Bot aus Boardman/USA - sellerforum.de
Infinity Pool Showtimes Near Maya Cinemas Bakersfield
Dermpathdiagnostics Com Pay Invoice
How To Use Price Chopper Points At Quiktrip
Maria Butina Bikini
Busted Newspaper Zapata Tx
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 6790

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.