当前位置:网站首页>Introduction and basic construction of kubernetes
Introduction and basic construction of kubernetes
2022-06-11 00:42:00 【Wenxiaowu】
Introduction
Microservices are very popular now , It's on fire Docker. Because microservices are very suitable for Docker Container implementation , Each container hosts a service . A computer running multiple containers at the same time , So we can easily simulate the complex microservice architecture . So Docker The popularity of , Speed , It's jaw dropping .
It's just Docker When the technology of container is so hot , We found , If you want to Docker Apply to specific business implementation , There are difficulties —— layout 、 Management and scheduling, etc , It's not easy . therefore , There is an urgent need for a management system , Yes Docker And containers for more advanced and flexible management .
Right now ,K8S There is .
K8S, It is a container based cluster management platform , Its full name , yes kubernetes.
Kubernetes It's Google open source Docker Container cluster management system , It mainly provides resource scheduling for containerized services 、 Capacity expansion 、 Capacity reduction and deployment .
Actually from Kuberbetes You can see its purpose , The word comes from ancient Greece , It means helmsman ,Docker Of logo It's a whale , that Kubernetes Is where whales live - Ocean leader ; It was born a little late ,15 year 7 The first version only appears in January , But it was very popular at birth , Many of them include IBM、 Red hat 、 Microsoft and other giants have joined in .
Now let me show you Kubernetes, And use him to achieve revolutionary hello,world!
Kubernetes Has been the predecessor of snow
Over the past decade , Google has always had a secret weapon :Borg.Borg It has always been a large-scale cluster management system used internally by Google ,Borg It is based on container technology , It is mainly to realize the automation of resource management and improve the utilization of resources in the context of spanning multiple data centers , Google has always looked at Borg For heirloom , Even if the employee leaves the company, it cannot be disclosed to the outside world , until 2015 Google only made it public in , in other words Kubernetes It's actually Borg An open source version of .
Use Kubernetes? Give me a reason !
First of all , convenient . If our system design follows Kubernetes Design idea , Then the underlying code in the traditional system architecture has nothing to do with the business 、 We don't need to consider functional modules , We don't have to spend energy on the selection, deployment and implementation of load balancer , There is no need to consider introducing or even developing a complex service governance framework , Not to mention the headache : Service monitoring 、 Troubleshooting, etc . All in all ,Kubernetes Is to liberate our energy , So that we can focus more on the business itself to do development .
second , to open up .Kubernetes Compatible with all languages , All programming interfaces are also compatible , No matter what language you write, it can be mapped to Kubernetes Of Service And pass the standard TCP Communication protocol for interaction , The bigger benefit is , Existing systems can also be upgraded and migrated to Kubernetes On the platform ;
Third , Powerful . After more than ten years of development ,Kubernetes It has become a very complete distributed system support platform , Have extremely strong cluster management capabilities : Multi level security protection and access mechanism 、 Multi-tenant application support capability 、 Transparent service registration and service discovery mechanism 、 Built in intelligent load balancer 、 It can also find faults in time and repair itself 、 Service rolling upgrade and online capacity expansion 、 Extensible automatic resource scheduling mechanism 、 Multi granularity resource quota management …… That's too much , To name but a few !
IT We all know that technology changes faster than you change clothes ,Docker The star of containerization technology has become so popular , From single machine to cluster, this is an irreversible trend , Cloud computing is also in full swing ,Kubernetes It is the only one recognized by the industry so far Docker Distributed system solutions .
You have to know this before you start !
But don't worry , You must have some knowledge before you start again , It's like learning Java I didn't just write when I came up hello,world But first learn some and or not 、 Data types and so on .
stay Kubernetes in Service Is the core of the distributed cluster architecture , One Service It has the following key characteristics :
Have a location specific name , Just like the primary key in the database ;
Have a virtual IP And port number ;
Have the ability to provide remote services and be able to map to a set of container applications that provide such services ;
at present ,Service It's all based on Socket Communication mode or a specific service that implements a specific service TCP Server Process to provide services , Although a Service Services are usually provided by multiple related service processes , And each service process has an independent EndPoint(IP + Port) Access points , however ! however Kubernetes Can let us through the virtual Cluster IP + Service Port Connect to the specified Service On .
With Kubernetes Built-in transparent load balancing and failover mechanisms , Even if there are more service processes on the back end , Even more service processes will be redeployed to other machines due to failures , Will not affect our normal call to the service ;
Kubernetes Once created, it will not change , It also means that , stay Kubernetes In the cluster, we no longer need to IP The frequent change of address caused a headache .
One of the benefits of containers is that they have powerful isolation capabilities , Therefore, it must be regarded as Service The group of processes that provide services are put into containers for isolation ,Kubernetes Of Pod The object is to do this , Each service process is wrapped in a corresponding Pod Make it Pod The one running in Container( Containers ).
In order to establish Service and Pod Relationship between ,Kubernetes Will give each Pod A close one Label( label ), And then give them the corresponding Service Definition Label Selector( tag chooser ).
that ,Pod What kind of thing is it ?
Pod Running on the Node( node ) in , This node can be either a physical machine or a virtual machine in a public cloud or a private cloud , Usually there are hundreds running on a node Pod.
Every Pod There is a program called Pause And some business containers , In order to improve the efficiency of communication and data exchange , These business containers share Pause The container's network stack and Volume Mount the volume , We can use this feature to put a group of closely related service processes into the same Pod in .
however , There is one caveat : Not every one Pod And the container inside it can “ mapping ” To a Service On , Just a group that provides external or team services Pod can “ mapping ” Become a service .
In terms of cluster management ,Kubernetes Divide the machines in the cluster into one Master Nodes and a group of work nodes , The former runs processes related to cluster management , The work node runs the application .
stay Kubernetes In the cluster , To expand the capacity, you only need to work for Service The associated Pod Create a Replication Controller,Replication Controller The file contains at least the following information :
The goal is Pod Definition ;
The goal is Pod Number of copies to run ;
The target to monitor Pod The label of ;
Create good Replication Controller after .Kubernetes Will pass Replication Controller To filter out the corresponding Pod Instance and monitor the status and quantity in real time , If the number of instances is less than the number of copies, it will be based on Replication Controller As defined in Pod Template to create a new one Pod, Put this again. Pod Dispatch to the appropriate Node Upstart operation , until Pod The number of instances reaches the predetermined target .
Everything must be done from hello,world Start
here hello,world It's a Web Message board application , It's based on PHP + Redis Two tier distributed architecture of Web application , This is the system deployment architecture diagram of the message board :

Web System deployment architecture diagram of message board
Three Docker Mirror image :
redis-master: For the front end Web Application to write a message , One of them has been saved “hello,world”.guestbook-redis-slave: For the front end Web Application to read messages , and redis-master Keep your data in sync .guestbook-php-frontend:PHPWeb service , Display the message content on the web page , At the same time, a text input box is provided for visitors to add comments .
This is a Kubernetes Deployment frame diagram :

Kubernetes Deployment frame diagram
Now let's start hand-in-hand teaching you how to use k8s Building a hello,world application . Be careful , This article USES centos operating system .
First , We need to enable the routing and forwarding function , If you do not perform this step, the container cannot access the Internet :
echo 1 > /proc/sys/net/ipv4/ip_forward
then , We're configuring yum Source :
yum install -y epel-release
yum clean all
yum list
We'll go on with the , We installed Docker.
yum install -y docker-io
Docker After installation , We started to build Kunernetes Running environment .
## Turn off firewall
systemctl disable firewalld
suystemctl stop firewalld
## Install again etcd and Kubernetes Software
yum install -y etcd
yum install -y kubernetes
And then modify Docker Configuration file for /etc/sysconfig/docker,OPTIONS Revised as follows :
OPTIONS='--selinux-enabled=false --insecure-registry gcr.io'
Be careful ,Docker Domestic image source is recommended . The general method is to edit /etc/docker/daemon.json:
{
"registry-mirrors" : [
"http://ovfftd6p.mirror.aliyuncs.com",
"http://registry.docker-cn.com",
"http://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com"
],
"insecure-registries" : [
"registry.docker-cn.com",
"docker.mirrors.ustc.edu.cn"
],
"debug" : true,
"experimental" : true
}
And then restart Docker Of daemon that will do .
systemctl daemon-reload
systemctl restart docker
Then modify Kubernetes Of apiserver The configuration file /etc/kubernetes/apiserver:
take –admission_control In the parameter ServiceAccount Delete .
Then start all the services in sequence :
systemctl start etcd
systemctl start docker
systemctl start kube-apiserver
systemctl start kube-controller-manager
systemctl start kube-scheduler
systemctl start kubelet
systemctl start kube-proxy
Check whether the above services have been started successfully , If all have been successful kubernetes The cluster environment has been installed .
by Redis-master Service creation RC file , The grammar format is ymal.
apiVersion: v1
kind: ReplicationController
metadata:
name: redis-master
labels:
name: redis-master
spec:
replicas: 1
selector:
name: redis-master
template:
metadata:
labels:
name: redis-master
spec:
containers:
- name: master
image: kubeguide/redis-master
ports:
- containerPort: 6379
And then in Master Executing commands in nodes :Kubectl create -f<config_file>, And publish it to Kubernetes In the cluster :
$ kubectl create -f redis-master-controller.yaml
replicationcontrollers/redis-master
And then create the Service.service Corresponding file redis-master-service.yaml, The contents are as follows :
apiVersion: v1
kind: Service
metadata:
name: redis-master
labels:
name: redis-master
spec:
ports:
- port: 6379
targetPort: 6379
selector:
name: redis-master
establish redis-master service.
kubectl create -f redis-master-service.yaml
next , To create a redis-slave service , Same as the former , First create redis-slave Of RC The definition file redis-salve-controller.yaml.
apiVersion: v1
kind: ReplicationController
metadata:
name: redis-slave
labels:
name: redis-slave
spec:
replicas: 2
selector:
name: redis-slave
template:
metadata:
labels:
name: redis-slave
spec:
containers:
- name: slave
image: kubeguide/guestbook-redis-slave
env:
- name: GET_HOSTS_FROM
value: env
ports:
- containerPort: 6379
Then create redis-salve Corresponding Pod.
kubectl create -f redis-salve-controller.yaml
Pod After creation , Create the corresponding service. First create service Configuration file for redis-salve-service.yaml, The contents are as follows :
apiVersion: v1
kind: Service
metadata:
name: redis-slave
labels:
name: redis-slave
spec:
ports:
- port: 6379
selector:
name: redis-slave
Then according to the configuration file , establish service.
kubectl create -f redis-slave-service.yaml
In order to achieve Redis The master-slave data synchronization of the cluster ,redis-slave Need to know redis-master The address of , So we need to be able to redis-slave Start command of image /run.sh Add :
redis-server --slaveof ${REDIS_MASTER_SERVICE_HOST} 6379
Last , establish frontend Of RC file frontend-controller.yaml, The contents are as follows :
apiVersion: v1
kind: ReplicationController
metadata:
name: frontend
labels:
name: frontend
spec:
replicas: 3
selector:
name: frontend
template:
metadata:
labels:
name: frontend
spec:
containers:
- name: frontend
image: kubeguide/guestbook-php-frontend
env:
- name: GET_HOSTS_FROM
value: env
ports:
- containerPort: 80
And then it's running kubectl create Command to create RC:
$ kubectl create -f frontend-controller.yaml
replcationcontrollers/frontend
Then create the associated Service,frontend-service.yaml The contents are as follows :
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
name: frontend
spec:
type: NodePort
ports:
- port: 80
nodePort: 30001
selector:
name: frontend
The last step , establish service.
$ kubectl create -f frontend-service.yaml
services/frontend
Now you can open the browser to enter :http:// virtual machine IP:30001 And then there will be hello,world, Success, that's it :

Web Effect drawing of message board interface operation
summary
This is it. Kubernetes, Realized hello,world Even if a foot came in ,Kubernetesde There are so many advantages to the market , The most direct benefit is that you can develop complex systems with light equipment , Architects focus on “ Service components ”, The rest are responsible for the development of business code , and Kubernetes It is also a powerful tool for implementing microservices , Microservices are actually the process of breaking a big Mac into small units ,Kubernetes To do this is undoubtedly the best person !
边栏推荐
猜你喜欢
![[database] MySQL index interview questions](/img/ff/8713465293f728f57840237242e227.png)
[database] MySQL index interview questions
![[network planning] 1.5 seven layer network model and five layer network model](/img/a8/74a1b44ce4d8b0b1a85043a091a91d.jpg)
[network planning] 1.5 seven layer network model and five layer network model

MP framework basic operation (self use)

图的最短路径问题 详细分解版

系统应用安装时,签名校验失败问题

Unable to return to the default page after page Jump

Kubernetes入门介绍与基础搭建

如何保证消息的顺序性、消息不丢失、不被重复消费

动态规划经典题目三角形最短路径

Word在目录里插入引导符(页码前的小点点)的方法
随机推荐
[untitled] test
SQL查询,子查询作为结果字段
VTK example -- three intersecting planes
LeetCode 1673. 找出最具竞争力的子序列**
【无标题】4555
Word在目录里插入引导符(页码前的小点点)的方法
Docking request mode
对象作为点(Objects as Points) 个人总结
Kubeflow 1.2.0 installation
JVM garbage collection mechanism and common garbage collectors
Installation of phpstudy
teterttet
mybaits merge into
Detailed decomposition of the shortest path problem in Figure
动态规划经典题目三角形最短路径
[JVM] garbage collection mechanism
unity 网格面片生成抛物线,折线
快手处置超过5.4万个违规账号:如何打击平台上的违规账号
452. detonate the balloon with the minimum number of arrows
[network planning] 2.1.2 transport layer services that can be selected by the application