close
close

first Drop

Com TW NOw News 2024

Kubernetes Create Command: Mastering Resource Creation
news

Kubernetes Create Command: Mastering Resource Creation

Introduction

This article covers the following technical skills:

Skills Graph

The kubectl create command is used to create Kubernetes resources. It takes a resource definition file as input and creates the resource in the Kubernetes cluster. The resource definition file can be in YAML or JSON format. In this lab, we will use YAML format.

Creating a namespace

The first step is to create a namespace. A namespace is a way to distribute cluster resources across multiple users. To create a namespace, create a file called namespace.yaml with the following content:

apiVersion: v1
kind: Namespace
metadata:
  name: mynamespace
Go to full screen mode

Exit full screen

Then run the following command to create the namespace:

kubectl create -f namespace.yaml
Go to full screen mode

Exit full screen

This will create a namespace named mynamespaceYou can verify that the namespace has been created by running the following command:

kubectl get namespaces
Go to full screen mode

Exit full screen

This displays a list of namespaces, including the mynamespace namespace.

Creating an implementation

The next step is to create a deployment. A deployment manages a set of replicas of a pod template. To create a deployment, create a file named deployment.yaml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mydeployment
  namespace: mynamespace
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: mycontainer
          image: nginx
Go to full screen mode

Exit full screen

Then run the following command to create the deployment:

kubectl create -f deployment.yaml
Go to full screen mode

Exit full screen

This will create an implementation named mydeployment in the mynamespace namespace. The deployment manages 3 replicas of a pod template running an Nginx container.

You can verify that the deployment has been created by running the following command:

kubectl get deployments -n mynamespace
Go to full screen mode

Exit full screen

This displays a list of deployments, including the mydeployment stake.

Create a service

The next step is to create a service. A service is an abstract way to expose an application running on a set of pods as a network service. To create a service, create a file named service.yaml with the following content:

apiVersion: v1
kind: Service
metadata:
  name: myservice
  namespace: mynamespace
spec:
  selector:
    app: myapp
  ports:
    - name: http
      port: 80
      targetPort: 80
Go to full screen mode

Exit full screen

Then run the following command to create the service:

kubectl create -f service.yaml
Go to full screen mode

Exit full screen

This will create a service named myservice in the mynamespace namespace. The service selects pods with the label app=myapp and expose port 80.

You can verify that the service has been created by running the following command:

kubectl get services -n mynamespace
Go to full screen mode

Exit full screen

This displays a list of services, including the myservice employ.

Make a secret

The next step is to create a secret. A secret is an object that contains sensitive data, such as passwords or API keys. To create a secret, create a file called secret.yaml with the following content:

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
  namespace: mynamespace
type: Opaque
data:
  username: dXNlcm5hbWU=
  password: cGFzc3dvcmQ=
Go to full screen mode

Exit full screen

In this example we create a secret called mysecret in the mynamespace namespace. The secret contains a username and password encoded in Base64 format.

To create the secret, run the following command:

kubectl create -f secret.yaml
Go to full screen mode

Exit full screen

You can verify that the secret has been created by running the following command:

kubectl get secrets -n mynamespace
Go to full screen mode

Exit full screen

This will display a list of secrets including the mysecret secret.

Creating a configuration folder

The final step is to create a configMap. A configMap is an object that contains configuration data that can be used by pods. To create a configMap, create a file named configmap.yaml with the following content:

apiVersion: v1
kind: ConfigMap
metadata:
  name: myconfigmap
  namespace: mynamespace
data:
  config: |
    database.host=example.com
    database.port=5432
    database.user=myuser
    database.password=mypassword
Go to full screen mode

Exit full screen

In this example we create a configMap named myconfigmap in the mynamespace namespace. The configMap contains a configuration file that can be used by pods.

Run the following command to create the configMap:

kubectl create -f configmap.yaml
Go to full screen mode

Exit full screen

You can verify that the configMap has been created by running the following command:

kubectl get configmaps -n mynamespace
Go to full screen mode

Exit full screen

This will display a list of configMaps, including the myconfigmap configurationMap.

Summary

Congratulations! In this lab you learned how to kubectl create command to create various Kubernetes resources. You started with simple examples and gradually moved to more complex examples including creating a namespace, deployment, service, secret, and configMap. Now you have a good understanding of how to use the kubectl create command to create Kubernetes resources.

Mindmap function


🚀 Practice Now: Kubernetes Create Command


Would you like to know more?