×
Community Blog Creating and Scaling Kubernetes Pods on Alibaba Cloud

Creating and Scaling Kubernetes Pods on Alibaba Cloud

In this article, we will be learning about Kubernetes Pods and relevant concepts by configuring them on an Alibaba Cloud Elastic Compute Service instance.

In this article, we will be learning the core concept of a Kubernetes Pod by configuring one on an Alibaba Cloud Elastic Compute Service instance. We will also be briefly looking at Clusters, Deployments, and ReplicaSets.

Kubernetes on Alibaba Cloud

There are two ways to get Kubernetes set up on Alibaba Cloud: through Container Service (built-in) and the other through an Elastic Compute Service (ECS) instance (self-built). You can to Alibaba Cloud Kubernetes vs. self-built Kubernetes to learn more about the differences of the two options.

For this article, we will choose the self-built Kubernetes approach. We have setup one master node and minion node which is running on the Alibaba Cloud ECS cluster.

The following reference deployment shows one ECS instance set up as kube-master and two ECS instances named minion1 and minion 2 up and running in an Alibaba cloud environment.  

Kubernetes Concepts

Before talking about Pods, let's take a step back and look at the cluster we just created.

1

From the architecture diagram above, what can we say about Kubernetes Clusters? Kubernetes coordinates a highly available cluster of computers that are connected to work as a single unit. A Kubernetes cluster consists of two types of resources:

  1. The Master coordinates the cluster
  2. Nodes are the workers that run applications

A Pod on the other hand, is the basic building block of Kubernetes. According to Kubernetes, Pods are the smallest and simplest unit in the Kubernetes object model that you create or deploy, which represents a running process on your cluster.

A Pod always runs on a Node. A Node is a worker machine in Kubernetes and may be either a virtual or a physical machine, depending on the cluster. Each Node is managed by the Master. A Node can have multiple pods, and the Kubernetes master automatically handles scheduling the pods across the Nodes in the cluster.

Exploring Kubernetes Cluster

Let's first run kubectl, the main command line interface (CLI) tool for running commands and managing Kubernetes clusters. We can find out the information of our cluster as follows:

root@kube-master:$ kubectl cluster-info
Kubernetes master is running at https://172.16.9.12:6443
KubeDNS is running at https://172.16.9.12:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

We can also verify nodes that have joined the cluster

root@kube-master:$ kubectl get nodes 
NAME          STATUS    ROLES     AGE       VERSION
kube-master   Ready     master    18h       v1.11.0
kube-minion   Ready     <none>    18h       v1.11.0

A resource is an endpoint in the Kubernetes API that stores a collection of API objects of a certain kind. For example, the built-in pods resource contains a collection of Pod objects. We can list all resources in the name space with the following command:

        kubectl get all

To List all supported resource types along with their shortnames and API group, run the kubectl api-resources command:

root@kube-master:$ kubectl api-resources
NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND
bindings                                                                      true         Binding
pods                              po                                          true         Pod
services                          svc                                         true         Service
deployments                       deploy       apps                           true         Deployment
replicasets                       rs           extensions                     true         ReplicaSet
clusterroles                                   rbac.authorization.k8s.io      false        ClusterRole
rolebindings                                   rbac.authorization.k8s.io      true         RoleBinding
.......
.......
________________________________________
 

Exploring ReplicaSets

ReplicaSets ensure that a specified number of pod replicas are running at any given time. Start a single Nginx instance with one ReplicaSet.

root@kube-master:$ kubectl run nginx --image=nginx --replicas=1

You can see if we actually have a Kubernetes pods up and running:

root@kube-master:$ kubectl get pods
NAME                     READY     STATUS              RESTARTS   AGE
nginx-64f497f8fd-sqmjk   0/1       ContainerCreating   0          12s

The output shows that the Nginx pod is being created. If you run the same command again, you can see that the pods are up and running

root@kube-master:$ kubectl get pods 
NAME                     READY     STATUS    RESTARTS   AGE
nginx-64f497f8fd-sqmjk   1/1       Running   0          46s

We can retrieve a lot more information about each of these pods using the kubectl describe pod command:

root@kube-master:$ kubectl describe pod nginx-64f497f8fd-sqmjk
......
 ......
    Containers:
      nginx:
        Container ID:   docker://a7bc2921ca62187778c5f65da4e139516f2701caf32e325cbeef2a1ee082da0b
        Image:          nginx
        Image ID:       docker-pullable://nginx@sha256:a65beb8c90a08b22a9ff6a219c2f363e16c477b6d610da28fe9cba37c2c3a2ac
        Port:           <none>
        Host Port:      <none>
        State:          Running
          Started:      Mon, 16 Jul 2018 18:48:53 +0530
        Ready:          True
        Restart Count:  0
        Environment:    <none>
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from default-token-8wxrj (ro)

Here you can see configuration information about the container and Pod, as well as status information about the container and Pod.

The container state is either Waiting, Running, or Terminated. Here you can see that for a container in Running state, the system tells you when the container started.

"Ready" tells you whether the container passed its last readiness probe.

Replicas help us protect your application from being lost due to misoperations or disasters. We can get the deployment configuration of Nginx with the command:

root@kube-master:$ kubectl get deployment nginx
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx     1         1         1            1           6m

During the initializing state we have told the Kubernetes to maintain one replica only by setting the flag --replicas=1

Let's try deleting our pod.

root@kube-master:$ kubectl delete pod nginx-64f497f8fd-sqmjk

Query on pod still shows one pod is running with different ID. This is because we have set up our Kubernetes to keep one running replica. This can be useful to protect your production server from accidently being destroyed.

root@kube-master:$ kubectl get pods 
NAME                     READY     STATUS    RESTARTS   AGE
nginx-64f497f8fd-fg9q7   1/1       Running   0          1m

Scaling the Pods

Scaling Up the Pods

To scale up the pods, tell kubectl how many current replicas are there (current-replicas) and how many needs to be scaled (replicas)

DESIRED =3, CURRENT =1

root@kube-master:$ kubectl scale --current-replicas=1 --replicas=3 deployment/nginx 

When you run this code, two new pods will be created and deployed.

root@kube-master:$ kubectl get pods
NAME                     READY     STATUS              RESTARTS   AGE
nginx-64f497f8fd-brn22   0/1       ContainerCreating   0          17s
nginx-64f497f8fd-fg9q7   1/1       Running             0          2h
nginx-64f497f8fd-z2vbb   0/1       ContainerCreating   0          17s

After creating the containers check the status of these pods

root@kube-master:$ kubectl get pods
NAME                     READY     STATUS    RESTARTS   AGE
nginx-64f497f8fd-brn22   1/1       Running   0          30s
nginx-64f497f8fd-fg9q7   1/1       Running   0          2h
nginx-64f497f8fd-z2vbb   1/1       Running   0          30s

You can also follow a similar step to scale down the Pods:

root@kube-master:$ kubectl scale --current-replicas=3 --replicas=1 deployment/nginx 
deployment.extensions/nginx scaled

In the output below, we can see that two pods are terminating.

root@kube-master:$ kubectl get pods 
NAME                     READY     STATUS        RESTARTS   AGE
nginx-64f497f8fd-fg9q7   1/1       Running       0          2h
nginx-64f497f8fd-fpjk9   0/1       Terminating   0          35s
nginx-64f497f8fd-lk2pw   0/1       Terminating   0          35s

After scaling down only one Nginx pod is running.

root@kube-master:$ kubectl get pods
NAME                     READY     STATUS    RESTARTS   AGE
nginx-64f497f8fd-fg9q7   1/1       Running   0          2h

Adding a New Node to a Cluster

To add new node to the Kubernetes cluster, you need a token and discovery-token-ca-cert-hash.

First, create a token using the kubeadm command in the kube-master setup,

root@kube-master:$ kubeadm token create 
I0717 10:32:47.753179   22047 feature_gate.go:230] feature gates: &{map[]}
yy8zho.n3w5inti3twy7v0y

Get rootCA cert fingerprint

root@kube-master:$ openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'

The produced discovery-token-ca-cert-hash value:

30e3baf5cb4474b23d5d2500836f6b4da19fa629b64339b1301d3e04892e08aa

Once the Token is created, join the new node, kube-minion2 using the token and the discovery-token-ca-cert-hash value:

root@kube-minion2:$  kubeadm join 172.16.9.12:6443 --token yy8zho.n3w5inti3twy7v0y --discovery-token-ca-cert-hash sha256:30e3baf5cb4474b23d5d2500836f6b4da19fa629b64339b1301d3e04892e08aa

Now in this cluster we have two minion nodes and one master node

root@kube-master:$ kubectl get nodes
NAME           STATUS    ROLES     AGE       VERSION
kube-master    Ready     master    18h       v1.11.0
kube-minion    Ready     <none>    18h       v1.11.0
kube-minion2   Ready     <none>    2m        v1.11.0

Even after adding nodes, you can still scale up your Pods.

kubectl scale --current-replicas=1 --replicas=5 deployment/nginx 

Look for the Events to verify on the new node pods are created and Nginx are deployed

kubectl describe pods | grep kube-minion
0 0 0
Share on

Alibaba Clouder

2,599 posts | 764 followers

You may also like

Comments