kubernetes (1) build a simple example

What are Kubernetes

It is a brand new leading solution for distributed architecture based on container technology. Although this solution is still very new, it is an important result of Google's more than ten years of experience accumulation and sublimation of relying on large-scale application of container technology. Automate resource management and maximize resource utilization across multiple data centers

Secondly, if our system design follows the design idea of ​​Kubernetes, the underlying code or functional modules that have little to do with business in the traditional system architecture can immediately disappear from our sight, and we no longer have to worry about load balancers and For deployment and implementation issues, there is no need to consider referencing or develop a complex service governance framework by yourself, and no longer have to worry about the development of service monitoring and fault handling modules. Using the solution provided by Kubernetes, we only saved no less than 30% of the development cost, and at the same time, we can focus more on the business itself, and because Kubernetes provides a powerful automation mechanism, the later operation and maintenance of the system is difficult and difficult to maintain. Costs are greatly reduced.

Kubernetes is a development platform for development. No programming interface is defined, so services written in Java, Go, C++ or Python can be mapped to Kubernetes services without any difficulty and interact through standard TCP communication protocols. In addition, since the Kubernetes platform does not have any intrusiveness to the existing programming languages, programming frameworks, and middle prices, the existing systems are very containerized and upgraded and migrated to the Kubernetes platform.

Kubernetes is a complete distributed system support platform. Kubernetes has complete cluster management capabilities, including multi-level security protection and access mechanisms, multi-tenant application support capabilities, transparent service registration and service discovery mechanisms, and built-in intelligent load balancing. Server, powerful fault discovery and self-healing capabilities, service rolling upgrade and online expansion capabilities, scalable automatic resource scheduling mechanism, and multi-granularity resource quota management capabilities. At the same time, Kubernetes provides comprehensive management tools, which cover all aspects including development, deployment testing, and operation and maintenance monitoring. Therefore, Kubernetes is a new distributed architecture solution based on container technology, and a one-stop complete distributed system development and support platform

Why use kubernetes

We can develop complex systems "lightly". After adopting the Kubernetes solution, it only takes a small and capable team to deal with it easily. In this team, an architect focuses on the refinement of system "service components", several development engineers focus on the development of business code, and a system and operation and maintenance engineer is responsible for the deployment and operation and maintenance of Kubernetes, and will never be used again." 996", not because we've done less, but because Kubernetes has already done a lot for us.

Using Kubernetes is all about embracing the microservices architecture. The core of the microservice architecture is to decompose a huge monolithic application into many small interconnected microservices. A microservice may be supported by multiple instance copies, and the number of copies may vary depending on the load of the social system. Tweaked, the inline load balancer plays a big role here. The microservice architecture enables each service to be developed by a dedicated development team, and developers can freely choose development technologies, which is very valuable for large-scale teams. In addition, each microservice is independently developed, upgraded, and expanded, so the system has High stability and fast iterative evolution capability. Google, Amazon, eBay, and other major domestic manufacturers have also adopted the microservice architecture. This time, Google has packaged the infrastructure of the microservice architecture directly into the Kubernetes solution, giving us the opportunity to directly apply the microservice architecture to solve complex business. architectural issues.

The Kubernetes system architecture has strong horizontal expansion capabilities. For Internet companies, the scale of users is equivalent to assets, and whoever has more users can win the competition. Therefore, the super horizontal expansion capability is one of the key indicators of the Internet business system. Without modifying the code, a Kubernetes cluster can smoothly expand from a small cluster containing only a few Nodes to a large-scale cluster with hundreds of Nodes. Using the tools provided by Kubernetes, we can even complete the cluster expansion online. As long as our microservices are well designed, combined with the linear increase of hardware or public cloud resources, the system can withstand the huge pressure of concurrent access by a large number of users

Simple example

A dress minikube

brew install minikube
复制代码

image.png

Dynamic minikube

minikube start
复制代码

image.pngReason: docker did not start, after starting docker, it started successfully

image.pngAnd one more docker

image.png

Check the running status of kuber

kubectl cluster-info
复制代码

image.png

connect to this node via ssh

minikube ssh
复制代码

image.png

create mysql

创建好 mysql-rc.yaml文件后,为了将它发布到kubernetes集群中,我们在Master节点执行命令:

kubectl create -f mysql-rc.yaml
复制代码

image.png

Kubernetes 通过template来生成pod,创建完后模板和pod就没有任何关系了,rc通过 labels来找对应的pod,控制副本。接下来,我们用kubectl命令查看刚刚创建的RC:

kubectl get rc
复制代码

image.png

然后,我们创建一个与之关联的 Kubernetes Service——MySQL的定义文件 mysql-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysqls
复制代码

运行 kubectl命令,创建service:

kubectl create -f mysql-svc.yaml 
复制代码

image.png 运行 kubectl 命令,可以查看到刚刚创建的service:

kubectl get svc
复制代码

image.png 注意到MySQL服务被分配了一个值为10.102.12.112的Cluster IP地址,这是一个虚地址,随后,Kubernetes集群中其他新创建的Pod就可以通过Service的Cluster IP+ 端口号3306来连接和访问它了。

在通常情况下,Cluster IP 是在Service创建后,由Kubernetes 系统自动分配的,其他Pod无法预先知道某个Service的Cluster IP地址,因此需要一个服务发现机制来知道这个服务。为此,最初时,Kubernetes巧妙地使用率Linux环境变量来解决这个问题。现在我们只需要知道,根据Service的唯一名字,容器可以从环境变量中获取到Service对应的Cluster IP 地址和端口,从而发起 TCP/IP 连接请求了。

启动tomcat应用

上面我们定义和启动了Mysql服务,接下来我们采用同样的步骤,完成Tomcat应用的启动过程。首先,创建对应的 RC 文件 myweb-rc.yaml,内容如下:

apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 2
  selector:
    app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
      - name: myweb
        image: kubeguide/tomcat-app:v1
        ports:
        - containerPort: 8080
        env:
        - name: MYSQL_SERVICE_HOST
          value: "mysql"
        - name: MYSQL_SERVICE_PORT
          value: "3306"
复制代码

注意,Tomcat容器内,应用将使用环境变量 MYSQL_SERVICE_HOST 的值连接MySQL服务。更安全可靠的用法是使用服务的名称 "mysql", 运行下面的命令,完成RC的创建和验证工作。

kubectl create -f myweb-rc.yaml 
复制代码

image.png 最后,创建对应的Service:。以下是完整的yaml定义文件:

apiVersion: v1
kind: Service
metadata: 
  name: myweb
spec:
  type: NodePort
  ports:
    - port: 8080
      nodePort: 30001
  selector:
    app: myweb
复制代码

type=NodePort 和 nodePort=30001 的两个属性,表示此 Service开启了 NodePort方式的外网访问模式,在Kubernetes集群之外,比如在本机的浏览器里,可以通过 30001 这个端口访问myweb(对应 8080 的虚端口上)。

运行 kubectl create 命令进行创建:

kubectl create -f myweb-svc.yaml 
复制代码

image.png

运行kubectl get svc

image.png 运行kubectl get rc

image.png

至此,我们的第1个Kubernetes 例子搭建完成了,通过浏览器访问网页

http://localhost:30001/
复制代码

image.png

参考

[1]Kubernetes入门
[2]mac安装启动minikube

Guess you like

Origin juejin.im/post/7084926299340275743