Kubernetes Create Command

KubernetesKubernetesBeginner
Practice Now

Introduction

The kubectl create command is an essential tool for creating Kubernetes resources. It uses YAML or JSON files to define resources like namespaces, deployments, services, secrets, and ConfigMaps. In this lab, you will learn how to create these resources step by step and understand their roles in Kubernetes.

By the end of this lab, you will:

  1. Start and verify a Minikube cluster.
  2. Create and verify various Kubernetes resources using kubectl create.

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/AdvancedCommandsGroup(["`Advanced Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ClusterInformationGroup(["`Cluster Information`"]) kubernetes/BasicCommandsGroup -.-> kubernetes/create("`Create`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/AdvancedCommandsGroup -.-> kubernetes/apply("`Apply`") kubernetes/ClusterInformationGroup -.-> kubernetes/cluster_info("`Cluster Info`") subgraph Lab Skills kubernetes/create -.-> lab-8506{{"`Kubernetes Create Command`"}} kubernetes/get -.-> lab-8506{{"`Kubernetes Create Command`"}} kubernetes/apply -.-> lab-8506{{"`Kubernetes Create Command`"}} kubernetes/cluster_info -.-> lab-8506{{"`Kubernetes Create Command`"}} end

Start Minikube and Verify the Cluster

Before working with Kubernetes, you need a running cluster. Minikube provides a lightweight local Kubernetes cluster.

  1. Navigate to your project directory:

    Open your terminal and navigate to the default working directory:

    cd /home/labex/project
  2. Start Minikube:

    Start Minikube to initialize the cluster:

    minikube start
    • Minikube creates a single-node Kubernetes cluster. This step may take a few minutes.
  3. Verify Minikube status:

    Check if Minikube started successfully:

    minikube status

    Look for components like apiserver and kubelet listed as Running.

  4. Confirm Kubernetes configuration:

    Ensure kubectl is connected to the Minikube cluster:

    kubectl cluster-info

    This displays details about the API server and other components.

If Minikube fails to start. Use minikube delete to reset and try again.

Explore the kubectl create Command

The kubectl create command provides multiple subcommands to create Kubernetes resources. It helps manage the creation of resources like namespaces, deployments, services, secrets, and ConfigMaps.

Run the following command to view the available kubectl create subcommands:

kubectl create -h

You will see the following output:

Create a resource from a file or from stdin.

JSON and YAML formats are accepted.

Examples:
  ## Create a pod using the data in pod.json
  kubectl create -f ./pod.json

  ## Create a pod based on the JSON passed into stdin
  cat pod.json | kubectl create -f -

  ## Edit the data in registry.yaml in JSON then create the resource using the edited data
  kubectl create -f registry.yaml --edit -o json

Available Commands:
  clusterrole           Create a cluster role
  clusterrolebinding    Create a cluster role binding for a particular cluster role
  configmap             Create a config map from a local file, directory or literal value
  cronjob               Create a cron job with the specified name
  deployment            Create a deployment with the specified name
  ingress               Create an ingress with the specified name
  job                   Create a job with the specified name
  namespace             Create a namespace with the specified name
  poddisruptionbudget   Create a pod disruption budget with the specified name
  priorityclass         Create a priority class with the specified name
  quota                 Create a quota with the specified name
  role                  Create a role with single rule
  rolebinding           Create a role binding for a particular role or cluster role
  secret                Create a secret using specified subcommand
  service               Create a service using a specified subcommand
  serviceaccount        Create a service account with the specified name
  token                 Request a service account token

Review the available subcommands and their descriptions to understand how kubectl create can be used.

Create a Namespace

Namespaces allow you to organize and isolate resources in Kubernetes.

  1. Create a namespace definition file:

    Open a new file named namespace.yaml:

    nano namespace.yaml
  2. Define the namespace:

    Add the following content:

    apiVersion: v1
    kind: Namespace
    metadata:
      name: mynamespace

    Save the file by pressing Ctrl+X, then Y, and Enter.

  3. Apply the namespace:

    Create the namespace:

    kubectl create -f namespace.yaml
  4. Verify the namespace:

    Check the list of namespaces:

    kubectl get namespaces

    Confirm that mynamespace appears in the output.

Create a Deployment

Deployments manage and maintain the desired state of pods.

  1. Create a deployment definition file:

    Open a file named deployment.yaml:

    nano deployment.yaml
  2. Define the deployment:

    Add the following content to deploy three replicas of an Nginx container:

    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: nginx-container
              image: nginx

    Save the file.

  3. Apply the deployment:

    Create the deployment:

    kubectl create -f deployment.yaml
  4. Verify the deployment:

    Check the deployment and its pods:

    kubectl get deployments -n mynamespace
    kubectl get pods -n mynamespace

    Ensure three pods are running.

Create a Service

A service provides stable network access to a set of pods.

  1. Create a service definition file:

    Open a file named service.yaml:

    nano service.yaml
  2. Define the service:

    Add the following content:

    apiVersion: v1
    kind: Service
    metadata:
      name: myservice
      namespace: mynamespace
    spec:
      selector:
        app: myapp
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80

    Save the file.

  3. Apply the service:

    Create the service:

    kubectl create -f service.yaml
  4. Verify the service:

    Check the list of services:

    kubectl get services -n mynamespace

    Confirm that myservice is listed.

Create a Secret

Secrets securely store sensitive information like passwords or API keys.

  1. Create a secret definition file:

    Open a file named secret.yaml:

    nano secret.yaml
  2. Define the secret:

    Add the following content with Base64-encoded values:

    apiVersion: v1
    kind: Secret
    metadata:
      name: mysecret
      namespace: mynamespace
    type: Opaque
    data:
      username: dXNlcm5hbWU= ## Base64 for "username"
      password: cGFzc3dvcmQ= ## Base64 for "password"

    Save the file.

  3. Apply the secret:

    Create the secret:

    kubectl create -f secret.yaml
  4. Verify the secret:

    Check the list of secrets:

    kubectl get secrets -n mynamespace

    Confirm that mysecret appears in the output.

Create a ConfigMap

ConfigMaps store configuration data in key-value pairs.

  1. Create a ConfigMap definition file:

    Open a file named configmap.yaml:

    nano configmap.yaml
  2. Define the ConfigMap:

    Add the following content:

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: myconfigmap
      namespace: mynamespace
    data:
      database.host: "example.com"
      database.port: "5432"

    Save the file.

  3. Apply the ConfigMap:

    Create the ConfigMap:

    kubectl create -f configmap.yaml
  4. Verify the ConfigMap:

    Check the list of ConfigMaps:

    kubectl get configmaps -n mynamespace

    Confirm that myconfigmap appears in the output.

Summary

In this lab, you learned how to:

  1. Start and verify a Minikube cluster.
  2. Create Kubernetes resources like namespaces, deployments, services, secrets, and ConfigMaps.
  3. Verify the status of these resources.

By mastering these steps, you will be able to manage and deploy applications effectively in Kubernetes. Practice these steps to strengthen your understanding!

Other Kubernetes Tutorials you may like