arrow

Basic Usage Of Kubernetes

1. Experiment

1.1 Knowledge points

This experiment primarily uses Container Service of Alibaba Cloud. It shows how to create a Kubernetes cluster on this service, to help you to understand the basic concepts and functions of Kubernetes.Container Service of Alibaba Cloud offers high-performance and scalable containerized application management services. It supports the lifecycle management of applications containerized using Docker and Kubernetes.Container Service simplifies the deployment of containerized application management clusters and integrates Alibaba Cloud’s virtualization, storage, network, and security capabilities, providing an optimal container running environment in the cloud.

1.2 Experiment process

  • Log onto Kubernetes
  • pod
  • service

1.3 Cloud resources required

  • ECS
  • CS

1.4 Prerequisites

  • If you’re using your own Alibaba Cloud account instead of the account provided by this lab to operate the experiment, please note that you’ll need to choose the same Ubuntu 16.04 operating system for your ECS in order to run the experiment smoothly.
  • Before starting the experiment, please confirm that the previous experiment has been closed normally and exited.

2. Start the experiment environment

Click Start Lab in the upper right corner of the page to start the experiment.

image desc.

After the experiment environment is successfully started, the system has deployed resources required by this experiment in the background, including the ECS instance, RDS instance, Server Load Balancer instance, and OSS bucket. An account consisting of the username and password for logging on to the Web console of Alibaba Cloud is also provided.

image desc

After the experiment environment is started and related resources are properly deployed, the experiment starts a countdown. You have two hours to perform experimental operations. After the countdown ends, the experiment stops, and related resources are released. During the experiment, pay attention to the remaining time and arrange your time wisely. Next, use the username and password provided by the system to log on to the Web console of Alibaba Cloud and view related resources:

openCole

Go to the logon page of Alibaba Cloud console.

image desc

Fill in the sub-user account and click Next.

image desc

Fill in the sub-user password and click Log on.

image desc

After you successfully log on to the console, the following page is displayed.

image desc

3. Basic objects of Kubernetes

The following figure shows a typical Kubernetes architecture.

image desc

3.1 pod

A Pod is the basic operating unit of Kubernetes and is also the carrier for running applications. The entire Kubernetes cluster is centered around pods, including pod creation, maintenance, and access.A pod can contain one or more associated containers. Pods are isolated from each other. However, the containers inside each pod share data with each other, such as data volumes and network addresses.

3.2 RC/RS

ReplicationController (RC) is a core concept of Kubernetes.It defines the expected number of replicas of each type of pod at any time.RC consists of three parts, namely, the expected number of pod replicas, Label Selector for filtering the target pod, and the template for creating pods when the number of pods is less than the expected value.

ReplicaSet (RS) is an upgrade to RC. In contrast to RC, RS supports the set-based Label Selector.

3.3 Service

Service is also a core resource object of Kubernetes.Service defines the access address for a service. This address allows the front-end application (pod) to access its back-end cluster instance composed by replicas of this pod. Service seamlessly interworks with the pod replicas through Label Selector.

3.4 Deployment

Deployment is designed to better resolve pod orchestration problems by using RS internally. This can be regarded as an upgrade to RC.

3.5 node

Kubernetes classifies the machines in a cluster into master and worker nodes. These nodes can be physical machines or virtual machines.

The master node runs a series of cluster management processes, including key processes such as kube-apiserver, kube-controller-manage, and kube-scheduler. These processes are responsible for resource management, pod scheduling, auto scaling, security control, and system control in the entire cluster.

The worker node runs applications. Typically, one worker node runs hundreds of pods.

4. Create a cluster

Refer to the following figure and select Container Service to enter the container service console.

image desc

Refer to the figure below to create a cluster first.

image desc

Select the Standard Managed Kubernetes.

image desc

Refer to the figure below, set the cluster name, select the US (Silicon Valley) area, and check the VPC network and switch to which it belongs. Click Next.

image desc

Start the worker node configuration.

Refer to the figure below and select the instance type of the Worker node.

image desc

The number of worker nodes is set to 3.

image desc

Set password, and click Next.

image desc

Refer to the following figure to set. Click Next.

image desc

Click Create Cluster.

image desc

It takes about 10 minutes to create a cluster. Please wait patiently.

image desc

image desc

5. Log onto Kubernetes

Click Elastic Computer Service, as shown in the following picture.

image desc

Copy this ECS instance’s Internet IP address and remotely log in to this ECS (Ubuntu system) instance. For details of remote login, refer to login

At this time, the console will have multiple ECS nodes. The user selects the ECS with the public IP address to log in. The remaining nodes are automatically created when the cluster is created.

image desc

The default account name and password of the ECS instance:

Account name: root

Password: nkYHG890..

Enter the following command to download the latest version of the kubectl client tool.

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

image desc

Enter the following command to grant execution permission to the downloaded client and move it to the /usr/bin directory.

chmod +x kubectl
mv kubectl /usr/bin/

image desc

Enter the following command to create a “.kube” directory.

mkdir -p .kube

image desc

Go back to the Alibaba Cloud container console, click the cluster name, and go to the cluster details page.

image desc

Click COPY to copy the content of the cluster certification.

image desc

Go back to the command line interface and enter the command vim .kube/config to create a new config file. Copy the authentication content of the previously created Kubernetes cluster into it. Save and exit.

image desc

Enter the following command to view node information.

kubectl get node

image desc

It means that you have successfully connected to our Kubernetes cluster.

6. pod

After you successfully log on, run the following command to view the current Kubernetes cluster information:

kubectl cluster-info

image desc

Run the following command to view the nodes in the Kubernetes cluster:

kubectl get nodes

image desc

Enter the command vim test.yaml, copy the following content to the file, save and exit.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

image desc

Enter the following command to create a Deployment resource.

kubectl create -f test.yaml

image desc

Run the following command to view all the running pods.You can see that the pod you created is running.

kubectl get pods

image desc

Copy the name of the pod in the preceding figure and run the following command. Replace YOUR-POD-NAME with the name of your pod.The configuration and status information of this pod is displayed.

kubectl describe pod YOUR-POD-NAME

image desc

<font color='red'>The user can cut off the above result picture when doing the experiment and send it to the teacher, indicating that the part of the current chapter has been completed.</font>

Run the following command to view the RS object you created. This object maintains the number of pods through the --replicas=1 tag.

kubectl get rs

image desc

Run the following command to delete the pod you created. Replace YOUR-POD-NAME with the name of your pod.

kubectl delete pod YOUR-POD-NAME

image desc

After the pod has been deleted, run the following command. A pod still exists, but with a different name. This indicates that a new pod is created immediately after the original one is deleted.

kubectl get pod

image desc

Run the following command to change the value of --replicas in the RS object to 3:

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

image desc

Run the following command to view the current number of pods.The number has increased to 3.

kubectl get pods

image desc

Run the following command to change the value of --replicas in the RS object to 1:

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

image desc

Run the following command to view the current number of pods.The number has been restored to 1.

kubectl get pods

image desc

To delete the pods, also delete the Deployment object controlling the pods.Run the following command to view the name of the Deployment object:

kubectl get deployment

image desc

Run the following command to delete the Deployment object:

kubectl delete deployment nginx

image desc

Run the following command. The pod is deleted.

kubectl get pods

image desc

7. service

Enter the command vim test2.yaml, copy the following content to the file, save and exit.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

image desc

Run the following command to create a Deployment object.

kubectl create -f test2.yaml

image desc

Run the following command to check that the pods have started:

kubectl get pods

image desc

Access the resources in the two pods.Create a service that allows external access for the Deployment object.This service allows external users to access the resources in the pods under the Deployment object.

Run the following command to create the service on port 8080:

kubectl expose deployment my-nginx --port=8080 --target-port=80 --type=LoadBalancer

image desc

Run the following command to check that the service has been created:

kubectl get services

Creating.

image desc

Success.

image desc

Copy the public IP address of the service in the preceding figure. Enter the following URL in the address bar of your browser. Replace YOUR-PUBLIC-IP with the public IP address of your service.

http://YOUR-PUBLIC-IP:8080

image desc

<font color='red'>Users can cut off the above result picture when they are doing the experiment and send it to the teacher, indicating that the current experiment has been completed.</font>

Reminder:
Before you leave this lab, remember to log out your Alibaba RAM account before you click the ‘stop’ button of your lab. Otherwise you’ll encounter some issue when opening a new lab session in the same browser:

image descimage desc

8. Experiment summary

This experiment describes how to use Pod and Service in Kubernetes by using the Kubernetes cluster automatically created on Container Service of Alibaba Cloud.Kubernetes is an open source container orchestration engine from Google. It supports features such as automatic deployment, large-scale auto scaling, and containerized application management.