preface

Due to the needs of the company’s business development, we have gradually transferred the services on Swarm to K8S. And I finally have the opportunity to start my k8S study (Caikeng) journey. Swarm is much better than K8s. Swarm is much better than K8s. This series of tutorials is aimed at developers who want to get started on K8S but don’t have a door.

Environment set up

In this paper, the environment is not really built by hand, but by using the container service of the cloud platform to create K8S cluster. The goal is simple, let professional people do professional things. We can use K8S smoothly can, maintenance let the cloud platform to help do it. Of course, the main reason is to let everyone can get started using K8S faster. There are many cloud providers. The following will take Ali Cloud as an example to describe how to create a K8S cluster.

The preparatory work

  • Set up an ECS server. – Call him little “A” here

    It is not necessary, but it is recommended to buy one. You can choose some remote areas to buy, so that there will be discounts, such as North China 3 (Zhangjiakou).

  • Server Configuration

    Recommended minimum configuration:

    2 cores, 4G memory

    You pay by the amount you use

  • The operating system

    Suggest Centos7.7

  • Install the software

    Docker – will install, this article will not say, online tutorial a lot.

    Kubectl-k8s management tool, as described in the following tutorial.

    Nginx – this will be covered in a later tutorial, so installation is recommended

  • Get a domain name, which is also cheap

Install Kubectl

Source configuration

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
Copy the code

The installation

[root@mldong ~]# yum install -y kubectl
Copy the code

Create the cluster

Search container services

Select Create cluster

The cluster configuration

Select Kubernetes hosted version, here pay attention to the region, select and said above ECS server small A the same region, so in the same Intranet, can access each other.

Working Node Configuration

Here, use the new instance, pay by volume, and then choose a suitable configuration. Here, choose the cheaper one, ECs.t6-c1m2.large.

The operating system can use Centos7.7. You are advised to use the key for login, that is, the public key of the previous ECS server A. If you do not have one, create one by yourself. Ssh-keygen -t rsa, press Enter three times.

Component configuration

Use the Intranet. It’s cheap. Use little A for external access. Select CSI for the storage plug-in.

Confirm the configuration

Now choose this configuration is relatively cheap, more than 40 cents an hour, cheaper than the net cafe. In addition to ECS servers, there are load balancers – private network, later will teach how to shut down.

Cluster creation check, if not passed, there will be a tutorial prompt, just do as required.

Creating a Cluster

The cluster is created successfully.

Viewing Cluster Information

KubeConfig configuration

Copy KubeConfig
[root@mldong ~]# cat /root/.kube/config
Copy the code
Cluster Connection Test
[root @ mldong nginx] # kubectl get the node NAME STATUS ROLES AGE VERSION cn - zhangjiakou. 172.26.22.118 Ready < none > 83 m V1.16.9 - aliyun. 1 cn - zhangjiakou. 172.26.22.119 Ready < none > 83 m v1.16.9 - aliyun. 1Copy the code

Configuring a cluster security group

View cloud server instances

I just created two new instances

Add child A to the same security group

Verify that the security group is configured successfully

If you enter the cluster normally, it succeeds, but you do not need to enter the cluster, just to verify.

[root@mldong .kube]# ssh root@worker1 echo 666
666
Copy the code

Image Service Configuration

Therefore, you need to configure the mirroring service that can be accessed from the Intranet

Search for container mirroring services

Creating a namespace

Creating a Mirror repository

So let’s look at the region here, and make sure it matches little A’s

Access certificate password setting

View warehouse details

Remember the Intranet access address, the private network

Start Hello World

Start by creating a mirror image

Pull the latest Nginx image

[root@mldong yaml]# docker pull nginx:latest
latest: Pulling from library/nginx
8559a31e96f4: Pull complete 
8d69e59170f7: Pull complete 
3f9f1ec1d262: Pull complete 
d1f5ff4f210d: Pull complete 
1e22bfa8652e: Pull complete 
Digest: sha256:21f32f6c08406306d822a0e6e8b7dc81f53f336570e852e25fbe1e3e3d0d0133
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
Copy the code

Add a label to the image

[root@mldong yaml]# docker tag nginx:latest registry-vpc.cn-zhangjiakou.aliyuncs.com/mldong/java/nginx:latest
Copy the code

Logging In to the Image Repository

docker login [email protected] registry-vpc.cn-zhangjiakou.aliyuncs.com
Copy the code

The user name used for login is the full name of aliyun account, and the password is set when the service is opened.

You can change the certificate password on the access certificate page.

Push the image to the mirror repository

[root@mldong yaml]# docker push registry-vpc.cn-zhangjiakou.aliyuncs.com/mldong/java/nginx:latest
The push refers to repository [registry-vpc.cn-zhangjiakou.aliyuncs.com/mldong/java/nginx]
f978b9ed3f26: Pushed 
9040af41bb66: Pushed 
7c7d7f446182: Pushed 
d4cf327d8ef5: Pushed 
13cb14c2acd3: Pushed 
latest: digest: sha256:0efad4d09a419dc6d574c3c3baacb804a530acd61d5eba72cb1f14e1f5ac0c8f size: 1362
Copy the code

Publish an Nginx service using Kubectl

A newns.yamlfile

[root@mldong yaml]# cat ns.yaml 
apiVersion: v1
kind: Namespace
metadata:
  name: mldong-test

Copy the code

Add anginx_deployment.yamlfile

[root@mldong nginx]# cat nginx_deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-pod
  namespace: mldong-test
spec:
  selector:
    matchLabels:
      app: nginx-pod
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
        - name: nginx
          image: registry-vpc.cn-zhangjiakou.aliyuncs.com/mldong/java/nginx:latest
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
              name: port
              protocol: TCP

Copy the code

Add anginx_service.yaml

[root@mldong nginx]# cat nginx_service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: nginx-nodeport
  namespace: mldong-test
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 32180 
  selector:
    app: nginx-pod
Copy the code

Create a namespace

[root@mldong yaml]# kubectl apply -f ns.yaml 
namespace/mldong-test created
Copy the code

View all namespaces

[root@mldong yaml]# kubectl get ns
NAME              STATUS   AGE
default           Active   44m
kube-node-lease   Active   44m
kube-public       Active   44m
kube-system       Active   44m
mldong-test       Active   87s

Copy the code

Create a POD

[root@mldong nginx]# kubectl apply -f nginx_deployment.yaml 
deployment.apps/nginx-pod created
Copy the code

View pods under the specified namespace

[root@mldong nginx]# kubectl get pods -n mldong-test NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx bbff87f9 pod - 66-0/1 VZLVS ContainerCreating 0 3 s < none > cn - zhangjiakou. 172.26.22.119 < none > < none > [root@mldong nginx]# kubectl get pods -n mldong-test NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx - pod - 66 bbff87f9 VZLVS Running 1/1-15 s 172.20.0.7 cn - zhangjiakou. 172.26.22.119 < none > < none >Copy the code

Creating a service

[root@mldong nginx]# kubectl apply -f nginx_service.yaml 
service/nginx-nodeport created
Copy the code

View services in the specified namespace

[root@mldong nginx]# kubectl get service -n mldong-test NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-nodeport NodePort 172.21.1.95 < None > 80:32180/TCP 76sCopy the code

Verify that the service is normal

Accessing any node

172.26.22.118 worker1 172.26.22.119 worker2

[root@mldong nginx]# curl worker1:32180
[root@mldong nginx]# curl worker2:32180
Copy the code

The normal return is as follows

<! DOCTYPEhtml>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Copy the code

That’s the end of Hello World, and to save money, here’s how to stop or release the service. Stop and release and then you don’t charge.

Disable Settings

Stop using instance

Instance deactivation has two ways, one is to stop without charging, the other is to release the instance completely. If you need to practice frequently, it is recommended to stop charging no fees so that you do not need to rebuild the cluster next time.

Stop charging no fees

A single stop

Instance status -> Stopped

Batch to stop

Stop in the

Has stopped

Disabling load Balancing

Search load balancing

Fully release instance resources

Modify instance release protection before setting instance release – Cancel

Instance Release Settings

Instance Status -> Release Settings

After an ECS server instance is stopped, you can restart it if you want to use it again. It will be recharged after startup.

The bill of fine

summary

This article uses ali Cloud container service to quickly create Kubernetes hosted version, the goal is to learn k8S application faster. And if you pay by the amount, the cost won’t be too high. It’s cheaper than upgrading your own computer. Of course, in addition to Ali Cloud, Tencent Cloud and Huawei cloud will also have similar services. This is just to give you an idea. Really, don’t always think about self-built, not professional personnel, really difficult. Future articles in the K8S series will also build on the current cluster. Hope to have friends to study together.

Related articles

Take you through K8S-ConfigMap and persistent storage

Take you hand in hand to play K8S – complete release of an externally accessible service

Docker-compose k8S-docker advanced Dockerfile and docker-compose

K8s – One click deployment of springboot project

Take you through k8S – One-click deployment of VUE projects

Take you hand in hand to play K8S – common object details

Walk you through k8S-Jenkins installation and assembly line

Walk you through k8S-Jenkins assembly line grammar

Take you hand in hand through the K8S-Jenkins assembly line to launch springboot project

Take you through the K8S-Jenkins assembly line to launch vUE projects

Walk you through k8S – health check survival probe and readiness probe

K8s-win10 build A K8S cluster

K8s-win10 k8S build on the development environment services