Quickly get started with k8s, Kubernetes from entry to fishing series - practice

Hello everyone, I am Bittao. This article is the actual chapter of "Getting Started with k8s Quickly, Kubernetes from Getting Started to Fishing Series", aiming to get started with k8s quickly. If you have not read k8s related theories, you can read the theoretical chapters first .

1. Practice environment

The significance of k8s lies in distributed large-scale container arrangement, so if we want to maximize its value in practice, we need at least 3 machines. One of them is the master node, and the remaining two are worker nodes. Of course, it can also be practiced by creating three operating systems in a virtual machine. But this operation is still very cumbersome. In fact, there is a more convenient way to do it during learning.

  • minikube
    lets you run Kubernetes locally. minikube runs a single-node Kubernetes cluster on your local personal computer (including Windows, macOS and Linux PC), so that you can try Kubernetes or carry out daily development work.
  • Kind of
    another Kubernetes SIGs project, but very different compared to minikube. As the name suggests, it moves the cluster into Docker containers. This will significantly speed up startup compared to spawning a VM.
  • Docker Desktop
    also has a built-in k8s function in the commonly used Docker Desktop on Windows and Mac, which can be opened and used only in the settings.

It is not recommended to use kind or minikube for cluster installation and configuration on a production environment. This article uses minikube to carry out, and there is not much difference in using other environments. You can check the official website for the installation of minikube , and install it according to your own different operating systems. Whether minikube can choose to depend on the container or the local VM:
insert image description here
so we can choose to use Docker as the place where minikube runs, command:

minikube start --driver='docker’
# 如果你在Docker Desktop版本中使用,建议将 Docke Engine 切换为 Linux的版本
docker context ls
docker context use default

2. Configuration

The core idea in k8s is: declarative statelessness.
insert image description here
So when we want to communicate with the API Server in the master node in k8s through API or CLI, we must adopt a format. The CLI is implemented in YAML format. YAML is no stranger to background developers, and the configuration files in Spring are also in this format. The YAML declaration file of k8s is divided into three parts: 1) metadata; 2) specification; 3) status.
insert image description here

Careful friends will find that the third status does not seem to be declared in the file. This is because it is automatically generated and edited by k8s. Its main function is to compare the existing status of the container with the specification at all times, and if there is a difference, it will move closer to the specification.
insert image description here
For example, two Nginxes are required in the declaration file, but only one is found in the actual operation. After k8s finds out, it will actively create another one to match the two. status is actually maintained by etcd.

3. Examples

This instance is a Node application, which accesses MongoDB for data access. The deployment method is as follows: The
insert image description here
technical points involved are:

  • ConfigMap: MongoDB connection information
  • Secret: MongoDB username and password
  • Deployment & Service: application composition and service

3.1 MongoDB

First create a ConfigMap. As a tool product, k8s can actually be rewritten directly by following the examples on the official website .
insert image description here
Copy the example from the official website and rewrite it as follows:

apiVersion: v1
kind: ConfigMap
metadata:
  name: mongo-config
data:
  mongo-url: mongo-service

Then create a Secret. It should be noted here that the Secret adopts the Opaque encryption method by default. Our username and password need to be encrypted by base64 and pasted in.

apiVersion: v1
kind: Secret
metadata:
  name: mongo-secret
type: Opaque
data:
  mongo-user: bW9uZ291c2Vy
  mongo-password: bW9uZ29wYXNzd29yZA==

Finally, let's declare Deployment & Service, which can be written separately or together:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo-deployment
  labels:
    app: mongo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongo
  template:
    metadata:
      labels:
        app: mongo
    spec:
      containers:
      - name: mongodb
        image: mongo:5.0
        ports:
        - containerPort: 27017
        env:
        - name: MONGO_INITDB_ROOT_USERNAME
          valueFrom:
            secretKeyRef:
              name: mongo-secret
              key: mongo-user
        - name: MONGO_INITDB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mongo-secret
              key: mongo-password  
---
apiVersion: v1
kind: Service
metadata:
  name: mongo-service
spec:
  selector:
    app: mongo
  ports:
    - protocol: TCP
      port: 27017
      targetPort: 27017

Among them template, we saw for the first time that it is for declaring Pod, and it also has its own metadata and specification inside. The most important thing here is the containers in the declaration, which can actually be understood as the image information declared in docker-compose.
labelsIs a label, k8s can specify a label for any component, which can be used as an identifier. For example, if the name of the Pod is unpredictable, you can use labels to quickly identify and find specific components.
In the YMAL syntax, ---three dashes are used to distinguish multiple configuration files, so we declare the Service after splitting with dashes. Where targetPortis the port declared in the Pod, portyou can specify any unused port.

3.2 Application

In the same way, let's declare the deployment application part. webapp.yaml looks like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp-deployment
  labels:
    app: webapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: nanajanashia/k8s-demo-app:v1.0
        ports:
        - containerPort: 3000
        env:
        - name: USER_NAME
          valueFrom:
            secretKeyRef:
              name: mongo-secret
              key: mongo-user
        - name: USER_PWD
          valueFrom:
            secretKeyRef:
              name: mongo-secret
              key: mongo-password 
        - name: DB_URL
          valueFrom:
            configMapKeyRef:
              name: mongo-config
              key: mongo-url
---
apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  type: NodePort
  selector:
    app: webapp
  ports:
    - protocol: TCP
      port: 3000
      targetPort: 3000
      nodePort: 30100

Among them, we declared it in the Service of APP NodePort, and its function is to allow external browsers to access it. nodePort is the port bound to the ip in the k8s node, but it will have a port range limitation.
insert image description here

4. run

After writing the YAML file, you can tell the k8s cluster through Kubectl that we want these service components. First create ConfigMap, Securit:

kubectl apply -f mongo-config.yaml
kubectl apply -f mongo-secret.yaml

Then create MongoDB and APP applications:

kubectl apply -f mongo.yaml
kubectl apply -f webapp.yaml

insert image description here
Execute kubectl get allto view the current component status:
insert image description here
only our Deployment, Service, and Pod can be seen here, but the configuration file requires a separate command query:

kubectl get configmap
kubectl get secret

If you want to view the details of the component, you can use this command:

kubectl describe service webapp-service

In Docker, we often view the Log of the container, and the command to view the pod log in k8s:

kubectl logs pod名称 -f

In short, as kubectl is the most powerful interactive tool of k8s, it contains a lot of command sets, and it is difficult for us to remember them all. Just like Linux commands, make good use of it -- help~
Finally, let’s visit the application deployed by k8s and query the ip address of minikube:
insert image description here
you can see the ip of minikube 192.168.64.26, and directly access:
insert image description here

5. Summary

In this article, we have implemented the deployment and use in k8s by combining the components learned in the theoretical chapter and an example of connecting a Node.js web application to MongoDB. Although using minikube in the local environment will be more troublesome than Docker Compose, but as long as we go to the cluster, we can highlight the power of k8s.

Guess you like

Origin blog.csdn.net/u012558210/article/details/131746788