The original article is reprinted from Liu Yue’s Technology Blog https://v3u.cn/a_id_185

In 2021, if you don’t talk about container technology, it will be called Docker. There is no doubt that it is one of the most popular container technologies today. However, when we are faced with massive images and containers, how to manage and arrange massive containers quickly and accurately has become a new topic. Kubernetes, also known in the industry as K8s, was invented by Google, but it’s basically the illiterate version of Kubernetes because there are eight letters between K and S. It is an open source engine for container resource management and application arrangement after multiple hosts are virtualized into a cloud platform. It is committed to making the deployment of containerized applications simple and efficient, and provides the application lifecycle management, such as application deployment, planning, update, maintenance and other mechanisms. This time we try to use Kubernetes to deploy Gunicorn+Flask high availability Web cluster project under Win10/Mac system.

First, Kubernetes Docker – based on the desktop, so download Docker – desktop installation package: https://www.docker.com/produc…

Here we are using the latest version of Docker 3.1.0, and the internal integrated version of Kubernetes is 1.19.3. Before installation, there are two points to note. If you are a Windows user, you need to make sure that the system version is Professional:

Second, turn on the Hyper-V virtualization function of the system based on the Pro version:

So while Windows users who want to give Kubernetes a good kiss, make sure to do both, Mac users have no special requirements.

Double-click the installation package to install, the default installation is in the C disk directory, after success, start Docker-Desktop, under normal circumstances, Docker is very easy to start successfully, but Kubernetes is often stuck in the startup interface, this is because some academic problems caused by the inability to download the dependent image of Kubernetes, At this point, we need a different approach, using some open source third-party libraries circuitous help us to download these images, here recommend this open source project: https://github.com/AliyunCont…

Type the command to pull the item:

git clone https://github.com/AliyunContainerService/k8s-for-docker-desktop.git

Enter the project directory, and then check your own version of Kubernetes, the project default pull is 1.19.3 dependency image, if you install the old version of Kubernetes, you need to switch the version to pull operation:

If Kubernetes is v1.18.8, use the following command to switch the v1.18.8 branch git checkout to v1.18.8


If the Kubernetes version is v1.18.6, switch the v1.18.6 branch git checkout to v1.18.6 using the following command


If Kubernetes is v1.18.3, switch the v1.18.3 branch git checkout to v1.18.3 using the following command


If Kubernetes is v1.16.5, use the following command to switch the v1.16.5 branch git checkout to v1.16.5


If Kubernetes is v1.15.5, switch the v1.15.5 branch git checkout to v1.15.5 using the following command


If Kubernetes is v1.15.4, switch the v1.15.4 branch git checkout to v1.15.4 using the following command


If the Kubernetes version is v1.14.8, switch the v1.14.8 branch git checkout to v1.14.8 using the following command


If Kubernetes is v1.14.7, switch the v1.14.7 branch git checkout to v1.14.7 using the following command


If the Kubernetes version is v1.14.6, use the following command to switch the v1.14.6 branch git checkout to v1.14.6


If Kubernetes is v1.14.3, use the following command to switch the v1.14.3 branch git checkout to v1.14.3


If Kubernetes is v1.14.1, switch the v1.14.1 branch git checkout to v1.14.1 using the following command


If Kubernetes is v1.13.0, switch the v1.13.0 branch git checkout to v1.13.0 using the following command


If the Kubernetes version is v1.10.11, use the following command to switch the v1.10.11 branch git checkout to v1.10.11

Then, if the Mac user executes the shell script directly:

./load_images.sh

If you are a Windows user, you will need to execute the script after executing the set-executionPolicy RemoteSigned command:

set-ExecutionPolicy RemoteSigned  
.\load_images.ps1

The proxy pulls the Kubernetes dependency image:

PS C:\Users\Administrator\ WWW \ K8S-For-Docker-Desktop > Set-ExecutionPolicy Remotesigned ExecutionPolicy This will help you prevent untrusted scripts from executing. Change execution strategy may lead to security risks, such as the about_Execution_Policies https:/go.microsoft.com/fwlink/?LinkID=135170 help topic. Do you want to change the execution policy? [Y] is [A] (Y) are (A) [N] N (N) (L) N (L) [S] to suspend all (S) [?] Help (default value is "N"): y PS C:\Users\Administrator\www\k8s-for-docker-desktop> .\load_images.ps1 K8s.gcr.io/pause:3.2=registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 3.2: Pulling from google_containers/pause Digest: sha256:927d98197ec1141a368550822d18fa1c60bdae27b78b0c004f705f548c07814f Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 Registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 Untagged: Registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/pause@sha256:927d98197ec1141a368550822d18fa1c60bdae27b78b0c004f705f5 48c07814f k8s.gcr.io/kube-controller-manager:v1.19.3=registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v 1.19.3 v1.19.3: Pulling from Google_Containers/Kube-Controller-Manager Digest: sha256:1ad35b623b9123c6aab99306ba5427e2829b36b378b9b80a6e988713ac5bffd4 Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.19.3 Registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.19.3 Untagged: Registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.19.3 Untagged: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager@sha256:1ad35b623b9123c6aab99306ba5427e2829b3 6b378b9b80a6e988713ac5bffd4

When the pull is over, run the command to view the list of images:

docker images

You can see the list of base images for Kubernet 1.19.3:

PS C:\Users\Administrator\www\k8s-for-docker-desktop> docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis latest 621ceef7494a 3 weeks ago 104MB Docker/Desktop - Kubernetes -v1.19.3-cni-v0.8.5- Critools-v1.17.0-Debian 621ceef7494a 3 weeks ago 104MB Docker/Desktop - Kubernetes -v1.19.3-cni-v0.8.5- critools-v1.17.0-Debian IO/kube-apcierver BB2106081D17 2 months ago 215MB k8s.gcr. IO/kube-apcierver v1.19.3cdef763A242 3 months ago 215MB k8s.gcr. IO/kube-apcierver V1.19.3a301be0cd4b 3 months ago 119MB k8s.gcr. IO /kube-controller-manager v1.19.39b60aca1d818 3 months ago 119MB k8s.gcr. IO /kube-controller-manager v1.19.39b60aca1d818 3 months ago 119MB k8s.gcr. IO K8s.gcr. IO /kube scheduler v1.19.3aaefbfa906bd 3 months ago 5.7MB k8s.gcr. IO /etcd 3.4.13-0 0369cf4303ff 5 months ago IO /coredns 1.7.0bfe3a36ebd25 7 months ago 1.2MB docker/ Desktop storage-provisioner v1.1E704287CE753 10 253MB k8s.gcr Docker /desktop-vpnkit-controller v1.0 79da3e5a3aa 11 months ago 1.6mb k8s.gcr. IO /pause 3.2 80D28BEDFE5D 11 months ago 413KB quay. IO /kubernetes-ingress-controller/nginx-ingress-controller 0.26.1 29024C9C6E70 16 80D28BEDFE5D 11 months ago 413KB quay. IO /kubernetes-ingress-controller/nginx-ingress-controller 0.26.1 29024C9C6E70 16 months ago 483MB

At this point, don’t worry, if you are a Mac user, you need to delete the cache separately:

# rm -rf ~/Library/Group\ Containers/group.com.docker/pki/  
# rm -rf ~/.kube

Windows users can restart Docker-Desktop without any extra work and click to start the Kubernetes service:

Then, on the command line, type the command:

kubectl version

If you can return the version number, the installation and configuration were successful:

PS C:\Users\Administrator> kubectl version Client Version: Version. The Info {Major: "1", Minor: "19", GitVersion: "v1.19.3 GitCommit:" 1 e11e4a2108024935ecfcb2912226cedeafd99df." BuildDate GitTreeState: "clean" : "the 2020-10-14 T12: graze Z," GoVersion: "go1.15.2", the Compiler: "gc", Platform:"windows/amd64"} Server Version: Version. The Info {Major: "1", Minor: "19", GitVersion: "v1.19.3 GitCommit:" 1 e11e4a2108024935ecfcb2912226cedeafd99df." BuildDate GitTreeState: "clean" : "the 2020-10-14 T12:41:49 Z," GoVersion: "go1.15.2", the Compiler: "gc", Platform:"linux/amd64"} PS C:\Users\Administrator>

Next, let’s simply deploy the Kubernetes container to see how it is different from Docker deployment. Let’s take a look at the traditional deployment of Docker. Suppose we deploy a Redis application and first pull the image:

docker pull redis

Start the container:

docker run -p 6380:6379 --name redis redis

Use the Redis client to link:

redis-cli -p 6380

Return database link status:

PS C:\Users\Administrator> Redis-Cli-P 6380 127.0.0.1:6380> Ping Pong

There is no problem, because Docker directly operates on the objects of images and containers, while Kubernetes manages the POD and RS services of the cluster based on Deployment, and each Deployment corresponds to a Deployment in the cluster. So Deployment is the basic Kubernetes object.

First set up the Deployment object:

kubectl create deployment --image=redis redis

Here the mirroring is again the redis image that was pulled, and the deployment alias is set to redis:

PS C:\Users\Administrator\www\k8s-for-docker-desktop> kubectl create deployment --image=redis redis  
deployment.apps/redis created

After successful creation, start RS service:

kubectl expose deployment redis --port=6379 --name=redis-server

The port number here is the port of the isolation container in POD, not the primary node port:

PS C:\Users\Administrator\www\k8s-for-docker-desktop> kubectl expose deployment redis --port=6379 --name=redis-server  
service/redis-server exposed

Then get the list of PODs started:

kubectl get pods

We know that PODs are the smallest unit of cluster execution, so here we use GET PODS to see the running PODS:

PS C:\Users\Administrator\www\k8s-for-docker-desktop> kubectl get pods  
NAME                     READY   STATUS    RESTARTS   AGE  
redis-6749d7bd65-l8gnw   1/1     Running   0          64s

The POD port and the master node port need to be mapped:

kubectl port-forward redis-6749d7bd65-l8gnw 6380:6379

The parameter here is the pod name:

PS C:\Users\Administrator\www\k8s-for-docker-desktop> kubectl port-forward redis-6749d7bd65-l8gnw 6380:6379 Forwarding The from 127.0.0.1:6380 - > 6379 Forwarding the from [: : 1) : 6380 - > 6379 Handling for the connection for 6380 Handling for the connection for 6380

Then use the Redis client to link:

PS C:\Users\Administrator> Redis-Cli-P 6380 127.0.0.1:6380> Ping Pong

Here the deployment is successful, although the process is slightly more complicated than Docker direct deployment, but Kubernetes comes with good scalability and expansion as well as high availability features, once POD dead or down, you can automatically generate new POD to achieve high availability.

Next, try some more advanced actions to deploy the Gunicorn+Flask Web application. Remember the previous article: Deploying the Nginx reverse proxy Gunicorn+Flask stand-alone architecture using DockerHub in Centos7.7? It explains how to use Dockerfile to package an image. Here we pull the packaged Flask + Gunicorn image:

docker pull zcxey2911/myflask

Next, set up a Deployment based configuration file: Deployment.YAML

apiVersion: v1  
kind: Service  
metadata:  
  name: myflask  
spec:  
  selector:  
    app: myflask  
  ports:  
  - protocol: "TCP"  
    port: 5000  
    targetPort: 5000  
  type: LoadBalancer  
  
---  
apiVersion: apps/v1  
kind: Deployment  
metadata:  
  name: myflask  
spec:  
  selector:  
    matchLabels:  
      app: myflask  
  replicas: 4  
  template:  
    metadata:  
      labels:  
        app: myflask  
    spec:  
      containers:  
      - name: myflask  
        image: zcxey2911/myflask  
        imagePullPolicy: Never  
        ports:  
        - containerPort: 5000

Deploy the local MyFalsk image as the base image of Deployment. Cluster runs four PODs to maintain service guarantees for the production environment. The running protocol is of course TCP, and the primary node port number and POD port are both 5000

Then execute the command to start the service:

kubectl apply -f deployment.yaml

To view the service status:

liuyue:myflask liuyue$ kubectl get pods  
NAME                      READY   STATUS    RESTARTS   AGE  
myflask-74f646444-h7rtf   1/1     Running   0          42h  
myflask-74f646444-lfrdw   1/1     Running   0          42h  
myflask-74f646444-p96jl   1/1     Running   0          43h  
myflask-74f646444-zc7fp   1/1     Running   0          43h

You can see four PODs running at the same time. Visit http://localhost:5000

No problem, even if we manually kill a pod:

liuyue:myflask liuyue$ kubectl delete pod myflask-74f646444-h7rtf pod "myflask-74f646444-h7rtf" deleted liuyue:myflask liuyue$ liuyue:myflask liuyue$ kubectl get pods NAME READY STATUS RESTARTS AGE myflask-74f646444-dg8v7 1/1 Running 0 10s  myflask-74f646444-lfrdw 1/1 Running 0 42h myflask-74f646444-p96jl 1/1 Running 0 43h myflask-74f646444-zc7fp 1/1 Running  0 43h

You can see that the cluster immediately generates a new POD to keep the service stable, which is a highly available feature that Docker native containers don’t have.

Conclusion: The future has come and is coming. In the second decade of the 21st century, Alibaba, Bytedance, Tencent, Baidu and other giants in China’s Internet industry are stepping up their efforts in the field of containers. Kubernetes is also the core strategy of these giants in the future. With Kubernetes, we can automate and manage the entire large-scale server abstraction of computing resources from one container to another, handing the final application services back to the user without the user needing to care about the container itself. Finally, in the project to share with you: https://gitee.com/QiHanXiBei/…

The original article is reprinted from Liu Yue’s Technology Blog https://v3u.cn/a_id_185