Imperative commands directly create, modify, and remove Kubernetes objects. This is a quick and helpful way to get things going. Today, we’re going to break down these “must” commands and use them.

It is best to use declarative YAML settings for more stable deployments in production.

During the exam try to use imperative commands as much as possible to be fast and avoid mistakes, but you need to copy from the documentation PVs, PVCs and NETPOLs, they cannot be created imperatively, get used to find the examples fast in the documentation.

I used a lot the -h option to get the help and examples from the command line on the imperative commands that is much faster than looking in the documentation. If you need to check the documentation you have them all here, in the [kubectl reference] (https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands), be sure you review them many times to know what you can do imperatively, some that are specially interesting are set.

Kubernetes Command Types

Imperative commands, declarative object configuration, and imperative object configuration are the main approaches to control resources and applications in Kubernetes. Each strategy has advantages and drawbacks. Let’s compare these types.

  1. Imperative Commands: Kubernetes imperative commands directly manipulate live objects. For novices, they are the simplest. They aren’t good for complex procedures and don’t make tracking system changes easier.

  2. Imperative Object Configuration: Command-line parameters or local configuration files generate and modify Kubernetes objects. This method gives you more control than imperative instructions and simplifies tracking changes. It’s complicated for complex systems and doesn’t automatically manage live system changes.

    Example: bash kubectl create -f nginx.yaml This command generates a Nginx.yaml defined Kubernetes object.

  3. Declarative Object Configuration: This method uses version-controlled object configuration files. Modifying an object’s configuration and forcing the changes are required. This is the most flexible way, allowing complex procedures and easy change tracking. It demands extensive knowledge of Kubernetes object structures.

    Example: bash kubectl apply -f nginx.yaml This command updates the Kubernetes object in the nginx.yaml file with any modifications.

Commonly Used Imperative Commands

Here are some of the most commonly used Kubernetes imperative commands:

  1. kubectl run: This command is used to create a new Deployment.

    kubectl run nginx --image=nginx
    
  2. kubectl expose: This command is used to expose Kubernetes objects, such as Deployments, as new Services.

    kubectl expose deployment nginx --port=80 --target-port=8080
    
  3. kubectl delete: This command is used to delete resources, either by filenames, stdin, resources, names, or by resources and label selector.

    kubectl delete deployment nginx
    
  4. kubectl get: This command is used to display one or many resources.

    kubectl get pods
    

Create and Expose a Custom Nginx Pod from Alpine in Kubernetes

1. Create the Pod

First, let’s create a pod named custom-nginx using the nginx image.

kubectl run custom-nginx --image=nginx:alpine
2. Modify the Pod to use port 8080

After creating the pod, we’ll edit it to specify that it should use port 8080.

kubectl edit pod custom-nginx

In the editor that appears, find the containers: section. Under the line specifying image: nginx, add the following YAML to set the container port to 8080:

ports:
- containerPort: 8080

Save and exit the editor.

This step only annotates the desired ports in the pod definition. The standard nginx image serves on port 80 by default. You’ll need a custom configuration or a custom Docker image to make nginx actually serve on 8080.

3. Expose the Pod

To expose the custom-nginx pod so that it can be accessed externally, you can create a service. Here, we’ll use the NodePort type for demonstration:

kubectl expose pod custom-nginx --port=8080 --target-port=8080 --type=NodePort

This assumes that your nginx is configured to serve on port 8080. If you are using the standard nginx image, it serves on port 80, so you’ll need to adjust the target-port accordingly.

You can use --name=custom-nginx-service to specify the name of the service.

4. Verify the Pod and Service

Finally, verify that the pod and service are created successfully:

Check the pod:

kubectl get pods custom-nginx

Check the service:

kubectl get svc custom-nginx
5. Create a Deployment

Create a deployment named custom-nginx-deployment with 3 replicas using the nginx:alpine image:

kubectl create deployment custom-nginx-deployment --image=nginx:alpine --replicas=3

You can use --namespace=custom-namespace to specify the name of the namespace you want to use. (You can create a new namespace with kubectl create namespace custom-namespace)

Now, update the deployment to use port 8080:

kubectl patch deployment custom-nginx-deployment --type='json' -p='[{"op": "add", "path": "/spec/template/spec/containers/0/ports", "value": [{"containerPort": 8080}]}]'

Now, your deployment custom-nginx-deployment should be running 3 replicas of the nginx:alpine image, each configured to use port 8080.

In this example:

  1. The kubectl create deployment command is used to create a deployment named custom-nginx-deployment with 3 replicas using the nginx:alpine image.
  2. The kubectl patch command is used to update the deployment to use port 8080.

You can edit the deployment with kubectl edit deployment custom-nginx-deployment

6. Verify the Deployment

Verify the status of the deployment:

kubectl describe deployment custom-nginx-deployment

Status of the deployment

Exercises and Questions

Execute those instructions and examine the results:

  1. Exercise: Create a deployment using kubectl run, expose it with kubectl expose, list it with kubectl get, and delete it with kubectl delete.
  2. Question 1: What happens if you run kubectl run command without specifying the --image flag?
  3. Question 2: How can you delete multiple deployments at once using kubectl delete?

Solutions and Explanations

Now, let’s discuss the solutions and explanations to the exercises and questions we proposed earlier.

  1. Exercise Solution:

    Step 1: Create a deployment using kubectl run

    kubectl run nginx --image=nginx
    

    This command creates a new Deployment named nginx, running the nginx image.

    Step 2: Expose it with kubectl expose

    kubectl expose deployment nginx --port=80 --target-port=8080
    

    This command exposes the nginx Deployment as a new Service that listens on port 80 and forwards requests to port 8080 of the pods selected by the deployment.

    Step 3: List it with kubectl get

    kubectl get services
    

    This command displays all the Services in the current namespace, including the newly created Service.

    Step 4: Delete it with kubectl delete

    kubectl delete service nginx
    kubectl delete deployment nginx
    

    These commands delete the nginx Service and the nginx Deployment.

  2. Question 1 Solution: If you run the kubectl run command without specifying the --image flag, it will throw an error. The --image flag is required to specify the Docker image to run in the containers.

  3. Question 2 Solution: You can delete multiple deployments at once using kubectl delete by separating the deployment names with a space. For example:

    kubectl delete deployments deployment1 deployment2 deployment3
    

    This command deletes the deployments named deployment1, deployment2, and deployment3.