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:
- Start and verify a Minikube cluster.
- Create and verify various Kubernetes resources using
kubectl create.
Start Minikube and Verify the Cluster
Before working with Kubernetes, you need a running cluster. Minikube provides a lightweight local Kubernetes cluster.
Navigate to your project directory:
Open your terminal and navigate to the default working directory:
cd /home/labex/projectStart Minikube:
Start Minikube to initialize the cluster:
minikube start- Minikube creates a single-node Kubernetes cluster. This step may take a few minutes.
Verify Minikube status:
Check if Minikube started successfully:
minikube statusLook for components like
apiserverandkubeletlisted asRunning.Confirm Kubernetes configuration:
Ensure
kubectlis connected to the Minikube cluster:kubectl cluster-infoThis 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.
Create a namespace definition file:
Open a new file named
namespace.yaml:nano namespace.yamlDefine the namespace:
Add the following content:
apiVersion: v1 kind: Namespace metadata: name: mynamespaceSave the file by pressing
Ctrl+X, thenY, andEnter.Apply the namespace:
Create the namespace:
kubectl create -f namespace.yamlVerify the namespace:
Check the list of namespaces:
kubectl get namespacesConfirm that
mynamespaceappears in the output.
Create a Deployment
Deployments manage and maintain the desired state of pods.
Create a deployment definition file:
Open a file named
deployment.yaml:nano deployment.yamlDefine 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: nginxSave the file.
Apply the deployment:
Create the deployment:
kubectl create -f deployment.yamlVerify the deployment:
Check the deployment and its pods:
kubectl get deployments -n mynamespace kubectl get pods -n mynamespaceEnsure three pods are running.
Create a Service
A service provides stable network access to a set of pods.
Create a service definition file:
Open a file named
service.yaml:nano service.yamlDefine 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: 80Save the file.
Apply the service:
Create the service:
kubectl create -f service.yamlVerify the service:
Check the list of services:
kubectl get services -n mynamespaceConfirm that
myserviceis listed.
Create a Secret
Secrets securely store sensitive information like passwords or API keys.
Create a secret definition file:
Open a file named
secret.yaml:nano secret.yamlDefine 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.
Apply the secret:
Create the secret:
kubectl create -f secret.yamlVerify the secret:
Check the list of secrets:
kubectl get secrets -n mynamespaceConfirm that
mysecretappears in the output.
Create a ConfigMap
ConfigMaps store configuration data in key-value pairs.
Create a ConfigMap definition file:
Open a file named
configmap.yaml:nano configmap.yamlDefine 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.
Apply the ConfigMap:
Create the ConfigMap:
kubectl create -f configmap.yamlVerify the ConfigMap:
Check the list of ConfigMaps:
kubectl get configmaps -n mynamespaceConfirm that
myconfigmapappears in the output.
Summary
In this lab, you learned how to:
- Start and verify a Minikube cluster.
- Create Kubernetes resources like namespaces, deployments, services, secrets, and ConfigMaps.
- 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!


