0%

k8s-Imperative vs Declarative

Imperative vs Declarative

Certification Tips - Imperative Commands with Kubectl

While you would be working mostly the declarative(声明式) way - using definition files, imperative(命令式) commands can help in getting one time tasks done quickly, as well as generate a definition template easily. This would help save a considerable amount of time during your exams.

Before we begin, familiarize with the two options that can come in handy while working with the below commands:

--dry-run: By default as soon as the command is run, the resource will be created. If you simply want to test your command, use the --dry-run=client option. This will not create the resource, instead, tell you whether the resource can be created and if your command is right.(--dry-run:默认情况下,命令运行后将立即创建资源。如果只想测试命令,请使用该--dry-run=client选项,该选项不会创建资源,而是告诉您是否可以创建资源以及您的命令是否正确。)

-o yaml: This will output the resource definition in YAML format on the screen.

(在屏幕上以YAML格式输出资源定义)

Use the above two in combination to generate a resource definition file quickly, that you can then modify and create resources as required, instead of creating the files from scratch.(结合使用以上两种方法可以快速生成资源定义文件,然后可以根据需要修改和创建资源,而不必从头开始创建文件。)

POD

Create an NGINX Pod

1
kubectl run nginx --image=nginx

Generate POD Manifest YAML file (-o yaml). Don’t create it(–dry-run)

1
kubectl run nginx --image=nginx  --dry-run=client -o yaml

Deployment

Create a deployment

1
kubectl create deployment --image=nginx nginx

Generate Deployment YAML file (-o yaml). Don’t create it(–dry-run)

1
kubectl create deployment --image=nginx nginx --dry-run -o yaml

Generate Deployment with 4 Replicas

1
kubectl create deployment nginx --image=nginx --replicas=4

You can also scale(扩展) a deployment using the kubectl scale command.

1
kubectl scale deployment nginx --replicas=4

Another way to do this is to save the YAML definition to a file.

1
kubectl create deployment nginx --image=nginx--dry-run=client -o yaml > nginx-deployment.yaml

You can then update the YAML file with the replicas or any other field before creating the deployment.

Service

Create a Service named redis-service of type ClusterIP to expose pod redis on port 6379创建一个名为redis-service的服务,其类型为ClusterIP,在端口6379上公开pod redis

1
kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml

(This will automatically use the pod’s labels as selectors)会自动把pod的label作为selectors

Or

kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml (This will not use the pods labels as selectors, instead it will assume selectors as app=redis. You cannot pass in selectors as an option. So it does not work very well if your pod has a different label set. So generate the file and modify the selectors before creating the service)

Create a Service named nginx of type NodePort to expose pod nginx’s port 80 on port 30080 on the nodes:

1
kubectl expose pod nginx --port=80 --name nginx-service --type=NodePort --dry-run=client -o yaml

(This will automatically use the pod’s labels as selectors, but you cannot specify the node port. You have to generate a definition file and then add the node port in manually before creating the service with the pod.)

Or

1
kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml

(This will not use the pods labels as selectors)

Both the above commands have their own challenges. While one of it cannot accept a selector the other cannot accept a node port. I would recommend going with the kubectl expose command. If you need to specify a node port, generate a definition file using the same command and manually input the nodeport before creating the service.

Practise

  • Create a new pod called custom-nginx using the nginx image and expose it on container port 8080

    kubectl run custom-nginx --image=nginx --port=8080

  • Create a new deployment called redis-deploy in the dev-ns namespace with the redis image. It should have 2 replicas.

    kubectl create deployment redis-deploy --image=redis --replicas=2 --namespace=dev-ns

    或者:

    Step 1: Create the deployment YAML file
    kubectl create deployment redis-deploy --image redis --namespace=dev-ns --dry-run=client -o yaml > deploy.yaml
    Step 2: Edit the YAML file and add update the replicas to 2
    Step 3: Run kubectl apply -f deploy.yaml to create the deployment in the dev-ns namespace.
    You can also use kubectl scale deployment or kubectl edit deployment to change the number of replicas once the object has been created.

  • Create a pod called httpd using the image httpd:alpine in the default namespace. Next, create a service of type ClusterIP by the same name (httpd). The target port for the service should be 80.

    kubectl run httpd --image=httpd:alpine

    kubectl expose pod httpd --port=80 --name httpd --type=ClusterIP --dry-run=client -o yaml > httpd-service.yaml

    kubectl create -f httpd-service.yaml

    或者:

    kubectl run httpd --image=httpd:alpine --port 80 --expose

    自动创建service和pod