Preliminary practice of k8s

osc_write 2020-11-10 11:22:08
preliminary practice k8s


K8S Preliminary practice

K8S Principle introduction

Kubernetes(K8S) Is one that can help us manage microservices (microservices) The system of , It can automatically deploy and manage multiple containers on multiple machines ( Containers ). To go further ,Kubernetes The problem to be solved is :“ Manually deploying multiple containers to multiple machines and monitoring and managing the state of these containers is cumbersome .” and Kubernetes Solution to be provided : Provides a platform to automate the operation and management of containers with high-level structure abstraction .

K8S Can do

  • Deploy multiple containers to multiple machines at the same time ( Deploy )
  • When the service capacity changes , You can extend the container automatically ( The zoom )
  • Manage the state of multiple containers , Automatically detect and restart a failed container ( management )

K8S The basic components of

K8S Architecture diagram

avatar

K8S Core components

  • etcd Saves the state of the entire cluster ;
  • apiserver Provides a unique entry point for resource operations , And provide certification 、 to grant authorization 、 Access control 、API Mechanisms such as registration and discovery ;
  • controller manager Responsible for maintaining the status of the cluster , Like fault detection 、 Automatic extension 、 Rolling updates, etc. ;
  • scheduler Responsible for resource scheduling , According to the scheduled scheduling policy Pod Dispatch to the corresponding machine ;
  • kubelet Responsible for maintaining the life cycle of the container , Also responsible for Volume(CVI) And the Internet (CNI) Management of ;
  • Container runtime Responsible for mirror management and Pod And the actual running of the container (CRI);
  • kube-proxy Responsible for Service Provide cluster Internal service discovery and load balancing ;

Introduction to all components of the official website

Pod

Kubernetes The smallest unit of operation , One Pod Corresponding to an application service (Application), It means a Pod It might correspond to a API Server.

  • Every Pod They all have an ID card , It belongs to this Pod Of yaml files
  • One Pod There can be one or more inside Container, But in general, one Pod It's better to have only one Container
  • The same Pod Medium Containers Sharing the same resources and networks , Through each other local port number communicate
  • Pod, yes Kubernetes Atomic scheduling unit of the project .

The official introduction

Node

K8s The computing power in the cluster is provided by Node Provide , first Node It's called a service node Minion, Later renamed Node.K8s In the cluster Node Which is the same thing as Mesos In the cluster Slave node , It's all Pod The working host on which to run , It can be a physical machine or a virtual machine . Both physical and virtual machines , The unifying characteristic of a working host is that it is to be run from above kubelet Manages the containers running on the node .

K8S Simple deployment preparation of cluster

  • System :mac os
  • docker: 19.03.13
  • docker image
    • The output is realized hello world
    • Implementation interface /panic Abnormal termination
    • Realization /version View the current api edition

kubectl install

mac adopt brew install

brew install kubectl 

or

brew install kubernetes-cli

Verify that the installation was successful

kubectl version --client

Other installation methods

minikube install

Here we install the alicloud version

curl -Lo minikube https://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v1.13.0/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

verification

●*  minikube version
minikube version: v1.13.0
commit: 23aa1eb200a03ae5883dd9d453d4daf3e0f59668

K8S Simple deployment ( With the help of minikube)

Start a single node minikube

minikube start --cpus=2 --memory=2048mb --registry-mirror=https://t65rjofu.mirror.aliyuncs.com --driver=virtualbox

It's specified here cpu And memory resources

●*  minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
●*  kubectl get no
NAME STATUS ROLES AGE VERSION
minikube Ready master 106s v1.19.0

pod Of yaml file

apiVersion: v1
kind: Pod
metadata:
name: docker-demo
spec:
containers:
- name: docker-demo
image: sundacheng/docker_demo:latest

Execute deployment

●*  kubectl apply -f .
pod/docker-demo created
●*  kubectl get all
NAME READY STATUS RESTARTS AGE
pod/docker-demo 0/1 ContainerCreating 0 17s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3m46s

Now we don't expose the service
So we have to implement

●*  kubectl port-forward docker-demo 3000:3000
Forwarding from 127.0.0.1:3000 -> 3000
Forwarding from [::1]:3000 -> 3000
●*  curl 127.0.0.1:3000
hello docker%

Deploy a service

We just deployed pod, adopt pord-forward Expose the corresponding port to the host , This can only be used to test , We can go through k8s To implement a reverse proxy service.
Before that, let's briefly introduce Label and Label selector

Label

Label yes Kubernetes Another core concept in the series . Is a set of bindings to K8s On resource object key/value Yes . Of the same object labels Attribute key Must be unique .label Can be attached to various resource objects , Such as Node,Pod,Service,RC etc.
By binding one or more unused label To achieve multi-dimensional resource grouping management function , For flexibility , Easy resource allocation , Dispatch , To configure , Deployment and other management work .

Label selector( tag chooser )

Label selector yes Kubernetes Core grouping mechanism , adopt label selector client / Users can identify a set of resource objects with common characteristics or attributes .

Label selector Usage scenarios of

1.kube-controller Process through resource object RC Defined above Label Selector To filter what to monitor Pod Number of copies , So as to achieve Pod The number of copies is always in accordance with the fully automatic control process set out in the expectation

2.kupe-proxy Process passing Service Of Label Selector To select the corresponding Pod, Auto builder each Service Corresponding to Pod Request forwarding routing table for , So as to achieve Service Intelligent load balancing mechanism based on

3. By Node Define specific Label, And in Pod Use in definition file NodeSelector This label scheduling strategy ,Kube-scheduler Processes can implement Pod Characteristics of directional scheduling

Laber and Label selector Official documents

The type of publishing service

  • ClusterIP: Through the inside of the cluster IP Exposure Services , Choose the value , Services are only accessible within the cluster , This is also the default ServiceType.

  • NodePort: Through each Node Upper IP And static ports (NodePort) Exposure Services . NodePort The service will route to ClusterIP service , This ClusterIP The service is created automatically . By request :, You can access a... From outside the cluster NodePort service .

  • LoadBalancer: Use cloud provider's load balancer , Services can be exposed externally . External load balancers can route to NodePort Service and ClusterIP service .

  • ExternalName: By returning CNAME And its value , Services can be mapped to externalName The contents of the field ( for example , foo.bar.example.com). No proxy of any type is created .

Deploy svc

pod Of yml

apiVersion: v1
kind: Pod
metadata:
name: docker-demo
labels:
app: docker-demo # to pod Tag it
spec:
containers:
- name: docker-demo
image: sundacheng/docker_demo:v1.0.1
env:
- name: MY_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: MY_POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: MY_POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP

svc Of yml

apiVersion: v1
kind: Service
metadata:
name: docker-demo
spec:
ports:
- name: http
port: 3000
targetPort: 3000
nodePort: 31080 # Specifies that the publishing service type is nodePort And designated external ( The host machine ) Port number 
selector:
app: docker-demo #Label selector
type: NodePort

Perform deployment and validation

●*  kubectl apply -f .
pod/docker-demo created
service/docker-demo created
●*  kubectl get all
NAME READY STATUS RESTARTS AGE
pod/docker-demo 0/1 ContainerCreating 0 43s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/docker-demo NodePort 10.110.72.67 <none> 3000:31080/TCP 43s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2m52s
●*  minikube service docker-demo --url
http://192.168.99.129:31080
●*  curl http://192.168.99.129:31080
Hello World!%

Can pass svc Blue and green deployment

well, We now have a version number that is 1.0.1 Of pod, If we want to upgrade or downgrade , Can pass svc Realize blue and green deployment
First pod Of image It needs to be changed to... To be deployed image, And add new lable For version number
v1.0.0.yml

apiVersion: v1
kind: Pod
metadata:
name: docker-demo-v1.0.0
labels:
app: docker-demo
version: v1.0.0
spec:
containers:
- name: docker-demo
image: sundacheng/docker_demo:v1.0.0

v1.0.1 Just change the corresponding parameters
svc Of yml

apiVersion: v1
kind: Service
metadata:
name: docker-demo
spec:
ports:
- name: http
port: 3000
targetPort: 3000
nodePort: 31080
selector:
app: docker-demo
version: v1.0.1
type: NodePort

Execution verification

●*  ls
demo-pod-v1.0.0.yml demo-pod-v1.0.1.yml demo-service.yml
●*  kubectl apply -f .
pod/docker-demo-v1.0.0 created
pod/docker-demo-v1.0.1 created
service/docker-demo created
●*  kubectl get all
NAME READY STATUS RESTARTS AGE
pod/docker-demo-v1.0.0 1/1 Running 0 5m19s
pod/docker-demo-v1.0.1 1/1 Running 0 5m19s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/docker-demo NodePort 10.96.92.104 <none> 3000:31080/TCP 5m19s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 43m
●*  minikube service docker-demo --url
http://192.168.99.129:31080
●*  curl http://192.168.99.129:31080/version
1.0.1%
# modify svc Of selector Point to 1.0.0
●*  kubectl apply -f .
pod/docker-demo-v1.0.0 unchanged
pod/docker-demo-v1.0.1 unchanged
service/docker-demo configured
●*  curl http://192.168.99.129:31080/version
1.0.0%

svc Of rs Cluster deployment

It's about single node, single pod Deployment of , Now let's talk about multi node .
Multiple node Nodes need virtual machines , adopt token Join in ,minikube Did this for us , Just at the beginning minikube start Add parameters after --node=3

minikube start --cpus=2 --memory=2048mb --registry-mirror=https://t65rjofu.mirror.aliyuncs.com --driver=virtualbox --nodes=3

meanwhile pod Of yml The document also needs to be modified

apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: docker-demo
spec:
replicas: 3 # Appoint rs Number 
selector:
matchLabels:
app: docker-demo
template: # Define each pod The kind of capacity 
metadata:
labels:
app: docker-demo
spec:
containers:
- name: docker-demo
image: sundacheng/docker_demo:v1.0.1
env:
- name: MY_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: MY_POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: MY_POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP

Perform deployment validation

●*  kubectl get no
NAME STATUS ROLES AGE VERSION
minikube Ready master 8m28s v1.19.0
minikube-m02 Ready <none> 7m33s v1.19.0
minikube-m03 Ready <none> 6m39s v1.19.0
# see node node 
●*  kubectl get all
NAME READY STATUS RESTARTS AGE
pod/docker-demo-5mcx4 1/1 Running 0 6m32s
pod/docker-demo-qgt9r 1/1 Running 0 6m32s
pod/docker-demo-rgdzq 1/1 Running 0 6m32s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/docker-demo NodePort 10.103.56.181 <none> 3000:31080/TCP 6m32s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15m
NAME DESIRED CURRENT READY AGE
replicaset.apps/docker-demo 3 3 3 6m32s
# Deployment success 
●*  kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
docker-demo-5mcx4 1/1 Running 0 7m10s 172.17.0.2 minikube-m03 <none> <none>
docker-demo-qgt9r 1/1 Running 0 7m10s 172.17.0.2 minikube-m02 <none> <none>
docker-demo-rgdzq 1/1 Running 0 7m10s 172.17.0.3 minikube <none> <none>
# see pod and node Binding relationship of 

deployment Deploy

K8S Provides a simpler update RC and Pod The mechanism of , be called Deployment. By means of Deployment Describe the desired cluster state in ,Deployment Controller It will update the current cluster state to the desired cluster state at a controllable speed .Deployment The main responsibility is also to ensure that pod Quantity and health ,90% With the function of Replication Controller Exactly the same as , Can be seen as a new generation of Replication Controller.

function :

  • Event and status view : You can see Deployment The detailed progress and status of the upgrade .
  • Roll back : When upgrading pod Problems are found when mirroring or related parameters , You can use the rollback operation to rollback to the last stable version or the specified version .
  • Version record : Every time Deployment The operation of , Can be preserved , It is used for possible rollback in the future .
  • Pause and start : For every upgrade , Can pause and start at any time .
  • Multiple upgrade options :Recreate---- Delete all existing pod, Re create a new ; RollingUpdate---- Rolling upgrade , Step by step replacement strategy , While scrolling through upgrades , Support more additional parameters , For example, setting the maximum unavailable pod Number , Minimum upgrade interval, etc .
# Create command :kubectl create -f deployment.yaml --record
# Use rollout history command , see Deployment Historical information of :kubectl rollout history deployment docker-demo
# Use rollout undo Roll back to the previous version : kubectl rollout undo deployment docker-demo
# Use –to-revision You can roll back to the specified version : kubectl rollout undo deployment docker-deployment --to-revision=2

config map

ConfigMap Allows you to separate the configuration file from the mirror file , To make containerized Applications Portable . This page provides a series of usage examples , These examples show how to create ConfigMap As well as the configuration Pod Use store in ConfigMap Data in .

config map yml

apiVersion: v1
kind: ConfigMap
metadata:
name: docker-config-v2
data:
SPRING_PROFILES_ACTIVE: mysql
DATASOURCE_URL: jdbc:mysql://mysql/demo/update
DATASOURCE_USERNAME: root
DATASOURCE_PASSWORD: k8s_test
DATASOURCE_INIT_MODE: always
TEST_CONFIG: test_config_v2

Corresponding pod yml modify

apiVersion: apps/v1
kind: Deployment
metadata:
name: docker-demo
spec:
selector:
matchLabels:
app: docker-demo
minReadySeconds: 06
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1 #
maxSurge: 2 # The upgrade process can be compared to the preset pod The number of more than , The default value is 25%
replicas: 3
template:
metadata:
labels:
app: docker-demo
spec:
containers:
- name: docker-demo
image: sundacheng/docker_demo:v1.0.1
envFrom: # Specify read config
- configMapRef:
name: docker-config-v2
env:
- name: MY_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: MY_POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: MY_POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP

We update config map After the configuration , We need to restart deployment To reload the configuration .
restart deployment

kubectl replace --force -f demo-deployment.yml

Horizontal expansion HPA

Horizontal expansion HPA You can refer to Alibaba cloud's article .
【 From entry to abandonment -Kubernetes】Kubernetes Advanced -pod Horizontal auto retraction (hpa)

版权声明
本文为[osc_write]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云