Introduction
This article covers the following tech skills:
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 be using YAML format.
Create a Namespace
The first step is to create a namespace. A namespace is a way to divide cluster resources between multiple users. To create a namespace, create a file called namespace.yaml
with the following contents:
apiVersion: v1
kind: Namespace
metadata:
name: mynamespace
Then, run the following command to create the namespace:
kubectl create -f namespace.yaml
This will create a namespace called mynamespace
. You can verify that the namespace was created by running the following command:
kubectl get namespaces
This will display a list of namespaces, including the mynamespace
namespace.
Create a Deployment
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 called deployment.yaml
with the following contents:
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
Then, run the following command to create the deployment:
kubectl create -f deployment.yaml
This will create a deployment called mydeployment
in the mynamespace
namespace. The deployment will manage 3 replicas of a pod template that runs an Nginx container.
You can verify that the deployment was created by running the following command:
kubectl get deployments -n mynamespace
This will display a list of deployments, including the mydeployment
deployment.
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 called service.yaml
with the following contents:
apiVersion: v1
kind: Service
metadata:
name: myservice
namespace: mynamespace
spec:
selector:
app: myapp
ports:
- name: http
port: 80
targetPort: 80
Then, run the following command to create the service:
kubectl create -f service.yaml
This will create a service called myservice
in the mynamespace
namespace. The service will select pods with the label app=myapp
and expose port 80.
You can verify that the service was created by running the following command:
kubectl get services -n mynamespace
This will display a list of services, including the myservice
service.
Create 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 contents:
apiVersion: v1
kind: Secret
metadata:
name: mysecret
namespace: mynamespace
type: Opaque
data:
username: dXNlcm5hbWU=
password: cGFzc3dvcmQ=
In this example, we are creating 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
You can verify that the secret was created by running the following command:
kubectl get secrets -n mynamespace
This will display a list of secrets, including the mysecret
secret.
Create a Configmap
The final step is to create a configMap. A configMap is an object that contains configuration data that can be consumed by pods. To create a configMap, create a file called configmap.yaml
with the following contents:
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfigmap
namespace: mynamespace
data:
config: |
database.host=example.com
database.port=5432
database.user=myuser
database.password=mypassword
In this example, we are creating a configMap called myconfigmap
in the mynamespace
namespace. The configMap contains a configuration file that can be consumed by pods.
To create the configMap, run the following command:
kubectl create -f configmap.yaml
You can verify that the configMap was created by running the following command:
kubectl get configmaps -n mynamespace
This will display a list of configMaps, including the myconfigmap
configMap.
Summary
Congratulations! In this lab, you learned how to use the kubectl create
command to create various Kubernetes resources. You started with simple examples and gradually moved on 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.
🚀 Practice Now: Kubernetes Create Command
Want to Learn More?
- 🌳 Learn the latest Kubernetes Skill Trees
- 📖 Read More Kubernetes Tutorials
- 💬 Join our Discord or tweet us @WeAreLabEx