当前位置:网站首页>Kubernetes - Introduction to actual combat
Kubernetes - Introduction to actual combat
2022-04-23 02:47:00 【XL's Princess】
6、 ... and 、 Practical introduction
1、Namespace
Namespace yes kubernetes A very important resource in the system , Its main function is to realize resource isolation of multiple environments or multi tenant resource isolation .
The default is ,kubernetes All in the cluster pod They are all accessible to each other . But in practice , Maybe I don't want two pod Mutual visits between , Then you can put two pod Divided into different namespace Next ,kubernetes By allocating resources within the cluster to different locations Nameapace in , Can form logical groups , In order to facilitate the isolation of different group resources, how to use and manage .
Can pass kubernetes The authorization mechanism of enterprise , Will be different namespace
Leave it to different tenants for management , This enables multi tenant resource isolation , It can also be combined at this time kubernetes Resource allocation mechanism , Limit the resources that different tenants can occupy , Columns such as cpu Usage quantity 、 Memory usage, etc , To manage the resources available to tenants .
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 24d # All not specified Namespace All objects will be assigned to default Namespace
dev Active 2d22h # Custom created Namespace
kube-node-lease Active 24d # Heartbeat maintenance between cluster nodes ,v1,v3 Version introduction
kube-public Active 24d # Resources under this namespace can be accessed by everyone ( Include unauthenticated users )
kube-system Active 24d # All by bunerntes All resources checked by the system are in this namespace
# View a space
[root@master ~]# kubectl get ns dev
NAME STATUS AGE
dev Active 2d22h
# View the details of a space
[root@master ~]# kubectl describe ns dev
Name: dev
Labels:
Annotations: kubectl.kubernetes.io/last-applied-configuration: {“apiVersion”:“v1”,“kind”:“Namespace”,“metadata”:{“annotations”:{},“name”:“dev”}}
Status: Active #active Namespace in use Terminating Deleting namespace
No resource quota. # Struggle for namespace Do resource constraints
No LimitRange resource. # Struggle for namespace Resource constraints for each component in
Namespace creation 、 Delete
[root@master ~]# kubectl create ns dev1
namespace/dev1 created
[root@master ~]# kubectl get ns dev1
NAME STATUS AGE
dev1 Active 9s
[root@master ~]# kubectl delete ns dev1
namespace "dev1" deleted
use yaml Create a configuration file 、 Delete namespace
[root@master ~]# cat ns-dev.yaml
apiVersion: v1
kind: Namespace
metadata:
name: dev
[root@master ~]# kubectl create -f ns-dev.yaml
namespace/dev created
[root@master ~]# kubectl get ns dev
NAME STATUS AGE
dev Active 5s
[root@master ~]# kubectl delete -f ns-dev.yaml
namespace "dev" deleted
2、pod
pod yes kuberntes The smallest unit for cluster management , The program must be deployed in a container to run , And the container must exist in pod in .
pod It can be considered as the encapsulation of the container , One pod There can be one or more containers in the .
Create and run
kubernetes No separate operation is provided pod The order of , It's all through pod Controller
# Command format :kubectl run (pod Controller name )[ Parameters ]
#–image Appoint pod Mirror image
#–port Designated port
#–namespace Appoint namespace
[root@master ~]# kubectl create ns dev
namespace/dev created
[root@master ~]# kubectl run nginx --image=nginx:1.17.1 --port=90 --namespace=dev
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created
see pod Information
[root@master ~]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
nginx-5f8884d5fc-dzn8w 1/1 Running 0 41s
[root@master ~]# kubectl get pods -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-5f8884d5fc-dzn8w 1/1 Running 0 50s 10.244.1.14 node1 <none> <none>
[root@master ~]# kubectl describe pod nginx-5f8884d5fc-dzn8w -n dev
Name: nginx-5f8884d5fc-dzn8w
Namespace: dev
Priority: 0
Node: node1/192.168.29.137
Start Time: Mon, 07 Mar 2022 14:52:15 +0800
Labels: pod-template-hash=5f8884d5fc
run=nginx
Annotations: <none>
Status: Running
IP: 10.244.1.14
IPs:
IP: 10.244.1.14
Controlled By: ReplicaSet/nginx-5f8884d5fc
Containers:
nginx:
Container ID: docker://04fb5968a164787f8af6c6560ce22745a420be50fb2cd228dee7dfb2311b34d5
Image: nginx:1.17.1
Image ID: docker-pullable://nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
Port: 90/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 07 Mar 2022 14:52:17 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-4dskd (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-4dskd:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-4dskd
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m21s default-scheduler Successfully assigned dev/nginx-5f8884d5fc-dzn8w to node1
Normal Pulled 3m19s kubelet, node1 Container image "nginx:1.17.1" already present on machine
Normal Created 3m19s kubelet, node1 Created container nginx
Normal Started 3m18s kubelet, node1 Started container nginx
visit pod service
[root@master ~]# curl 10.244.2.5:80
<!DOCTYPE html>
<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>
Delete pod
Delete pod You need to remove pod The controller
[root@master ~]# kubectl delete pod nginx-5f8884d5fc-dzn8w -n dev
pod "nginx-5f8884d5fc-dzn8w" deleted
# here , Show delete pod success , But in the query , Found a new one
[root@master ~]# kubectl get pods -o wide -n dev
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-5f8884d5fc-897hv 1/1 Running 0 2m3s 10.244.2.7 node2 <none> <none>
# This is because of the current pod By pod Created by controller , The controller back monitors pod condition , Once found pod Death , Will be rebuilt immediately
# At this point, you want to delete pod, Must delete pod controller
# Query the current namespace Under the pod controller
[root@master ~]# kubectl get deployment -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 1/1 1 1 25m
# Delete controller
[root@master ~]# kubectl delete deployment nginx -n dev
deployment.apps "nginx" deleted
use yaml Configuration creation 、 Delete pod
[root@master kuberntes]# cat pod_nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: dev
spec:
containers:
- image: nginx:1.17.1
imagePullPolicy: IfNotPresent
name: pod
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
[root@master kuberntes]# kubectl create -f pod_nginx.yaml
pod/nginx created
[root@master kuberntes]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 5s
[root@master kuberntes]# kubectl delete -f pod_nginx.yaml
pod "nginx" deleted
3、Label
Label yes kubernetes An important concept in the system , Its function is to add identification on resources , Used to partition and select them
Lade Characteristics :
- One Label Will key/value The form of key-value pairs is attached to various objects , Such as Node、Pod、Service wait
- A resource object can be defined in any number of Label, The same Label It can also be added to any number of resource objects
- Label It is usually determined when the resource is defined at will . Of course, you can also add or delete objects dynamically after they are created
Can pass Label Realize multi-dimensional grouping of resources , One side is flexible 、 Convenient resource allocation 、 Dispatch 、 To configure 、 Deployment and other management work
After the label is defined , Also consider the choice of labels 、 This is going to use Label Selector, namely :
- Label Used to define the identity of a resource object
- Label Selector Used to query and filter resource objects with certain tags
At present, there are two Label Selector:
- Based on equality Label Selector
name = slave: Choose all that contain Label in key=“name” And value=“slave” The object of
env!=production: Select all, including Label Medium key=“env” And value It's not equal to “production” The object of - Set based Label Selector
name in (master slave): All choices... Ah Bohan Label Medium key=“name” And value=“master” perhaps “slave” The object of
name not in (frontend): Choose all that contain Label Medium key=“name” And value It's not equal to “frontend” The object of - The selection criteria of the tag can use multiple , There will be multiple Label Selector Are combined , Good use “,” Just separate , Columns such as :
name=slave,env!=production
name not in(frontend),env!=production
Command mode
# View tab
[root@master kuberntes]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 55s <none>
# new label
[root@master kuberntes]# kubectl label pod nginx -n dev version=1.0
pod/nginx labeled
[root@master kuberntes]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 95s version=1.0
[root@master kuberntes]# kubectl label pod nginx -n dev tier=back
pod/nginx labeled
[root@master kuberntes]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 2m19s tier=back,version=1.0
# Modify the label
[root@master kuberntes]# kubectl label pod nginx -n dev version=2.0 --overwrite
pod/nginx labeled
[root@master kuberntes]# kubectl get pod -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 3m41s tier=back,version=2.0
# Screening tags
[root@master kuberntes]# kubectl get pods -l "version=2.0" -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 13m tier=back,version=2.0
[root@master kuberntes]# kubectl get pods -l "version!=2.0" -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx1 1/1 Running 0 2m32s version=1.0
# Remove the label
[root@master kuberntes]# kubectl label pod nginx -n dev tier-
pod/nginx labeled
[root@master kuberntes]# kubectl get pods -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 14m version=2.0
nginx1 1/1 Running 0 3m56s version=1.0
Use yaml Configuration creation label
[root@master kuberntes]# cat pod_nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx1
namespace: dev
labels:
version: "3.0"
env: "test"
spec:
containers:
- image: nginx:1.17.1
imagePullPolicy: IfNotPresent
name: pod
ports:
- name: nginx-port
containerPort: 80
protocol: TCP
[root@master kuberntes]# kubectl create -f pod_nginx.yaml
pod/nginx1 created
[root@master kuberntes]# kubectl get pods -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx 1/1 Running 0 21m version=2.0
nginx1 1/1 Running 0 4s env=test,version=3.0
4、Deployment
stay kubernetes in ,pod The smallest control unit , however kubernete Little direct control pod, It's usually passed pod Controller to complete .pod The controller is used for pod Management of , Make sure pod The resource is in the expected state , When pod When a resource fails , Will try to restart or rebuild pod.
Command format :kubectl run deployment name [ Parameters ]
–image Appoint pod Mirror image
–port Designated port
–replicas Specify create pod Number
namespace Appoint namespace
Command mode
# Create an image as nginx, Port is 80,pod The number of 3, The namespace is dev Of Deployment
[root@master kuberntes]# kubectl run nginx --image=nginx:1.17.1 --port=80 --replicas=3 --na mespace=dev
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future ve rsion. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created
# View the creation results
[root@master kuberntes]# kubectl get deployment,pods -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 3/3 3 3 64s
NAME READY STATUS RESTARTS AGE
pod/nginx-64777cd554-2dthg 1/1 Running 0 64s
pod/nginx-64777cd554-l4h9q 1/1 Running 0 64s
pod/nginx-64777cd554-vxw8n 1/1 Running 0 64s
# see pod Manager deployment Details of
[root@master kuberntes]# kubectl describe deploy nginx -n dev
Name: nginx
Namespace: dev
CreationTimestamp: Thu, 10 Mar 2022 16:02:00 +0800
Labels: run=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: run=nginx
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: run=nginx
Containers:
nginx:
Image: nginx:1.17.1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-64777cd554 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 2m1s deployment-controller Scaled up replica set nginx-64777 cd554 to 3
# see pod The label of
[root@master kuberntes]# kubectl get pods -n dev --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-64777cd554-2dthg 1/1 Running 0 3m pod-template-hash=64777cd554,run=nginx
nginx-64777cd554-l4h9q 1/1 Running 0 3m pod-template-hash=64777cd554,run=nginx
nginx-64777cd554-vxw8n 1/1 Running 0 3m pod-template-hash=64777cd554,run=nginx
# Delete deployment
[root@master kuberntes]# kubectl delete deploy nginx -n dev
deployment.apps "nginx" deleted
yaml How to profile
[root@master kuberntes]# cat deploy-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
run: nginx
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx:1.17.1
name: nginx
ports:
- containerPort: 80
protocol: TCP
[root@master kuberntes]# kubectl create -f deploy-nginx.yaml
deployment.apps/nginx created
[root@master kuberntes]# kubectl get deploy,pods -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 3/3 3 3 41s
NAME READY STATUS RESTARTS AGE
pod/nginx-64777cd554-4t64k 1/1 Running 0 41s
pod/nginx-64777cd554-tn9sx 1/1 Running 0 41s
pod/nginx-64777cd554-xjcbw 1/1 Running 0 41s
[root@master kuberntes]# kubectl delete -f deploy-nginx.yaml
deployment.apps "nginx" deleted
5、service
Although each of them pod There will be a separate pod ip, However, there are the following problems :
- pod ip Will follow pod The reconstruction of changes
- pod ip It's just the visible virtual within the cluster ip, External inaccessible
service It can be regarded as a group of the same kind pod External access interface , With the help of service, The application can easily realize the load balancing of service discovery
Create a cluster that is accessible within the cluster Service
- deploy nginx Referring to nginx Named deployment
- –name=svc-nginx1 finger service For the name of the :svc-nginx1
[root@master kuberntes]# kubectl expose deploy nginx --name=svc-nginx1 --type=ClusterIP --port=80 --target-port=80 -n dev
service/svc-nginx1 exposed
[root@master kuberntes]# kubectl get service -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc-nginx1 ClusterIP 10.98.249.163 <none> 80/TCP 21s
[root@master kuberntes]# curl 10.98.249.163:80
<!DOCTYPE html>
<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>
[root@master kuberntes]#
Create a cluster that can also be accessed outside the cluster service
Created above service Of type The type is ClusterIP, This IP Addresses can only be accessed within the cluster
If you need to create a that can also be accessed externally service, Need modification type by NodePort
[root@master kuberntes]# kubectl expose deploy nginx --name=svc-nginx2 --type=NodePort --port=80 --target-port=80 -n dev
service/svc-nginx2 exposed
[root@master kuberntes]# kubectl get service -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc-nginx1 ClusterIP 10.98.249.163 <none> 80/TCP 7m13s
svc-nginx2 NodePort 10.104.39.233 <none> 80:30999/TCP 6s
# Delete service
[root@master kuberntes]# kubectl delete svc svc-nginx1 -n dev
service "svc-nginx1" deleted
Use yaml How to profile
[root@master kuberntes]# cat svc-nginx.yaml
apiVersion: v1
kind: Service
metadata:
name: svc-nginx
namespace: dev
spec:
clusterIP:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: nginx
type: ClusterIP
[root@master kuberntes]# kubectl create -f svc-nginx.yaml
service/svc-nginx created
[root@master kuberntes]# kubectl get svc -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc-nginx ClusterIP 10.107.160.43 <none> 80/TCP 5s
svc-nginx2 NodePort 10.104.39.233 <none> 80:30999/TCP 14m
[root@master kuberntes]# kubectl delete -f svc-nginx.yaml
service "svc-nginx" deleted
版权声明
本文为[XL's Princess]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220741454665.html
边栏推荐
- JDBC JDBC
- Day 3 of learning rhcsa
- The problem of removing spaces from strings
- 魔王冷饭||#078 魔王答上海、南京行情;沟通指导;得国和打杀筛选;赚钱的目的;改变别人看法
- Six very 6 computer driver managers: what software is good for driver upgrade? Recommended by the best computer driver management software abroad
- Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment
- Modification du contenu de la recherche dans la boîte déroulante par PHP + MySQL
- Using go language to build web server
- Probabilistic model of machine learning
- leangoo脑图-共享式多人协作思维导图工具分享
猜你喜欢
Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment
Linux Redis——Redis 数据库缓存服务
First knowledge of C language ~ branch statements
Linux Redis ——Redis HA Sentinel 集群搭建详解 & Redis主从部署
Android high-level interview must ask: overall business and project architecture design and reconstruction
国产轻量级看板式Scrum敏捷项目管理工具
基于多态的职工管理系统源码与一些理解
接口请求时间太长,jstack观察锁持有情况
Flink stream processing engine system learning (I)
Solve the problem that PowerShell mining occupies 100% of cpu7 in win7
随机推荐
If MySQL / SQL server judges that the table or temporary table exists, it will be deleted
Android 高阶面试必问:全局业务和项目的架构设计与重构
Learn regular expression options, assertions
[unity3d] rolling barrage effect in live broadcasting room
Flink stream processing engine system learning (III)
Linux Redis ——Redis HA Sentinel 集群搭建详解 & Redis主从部署
打靶narak
The second day of learning rhcsa
Specific field information of MySQL export table (detailed operation of Navicat client)
First knowledge of C language ~ branch statements
Yes, from today on, our fans can participate in Netease data analysis training camp for free!
期中汇总(概论+应用层+运输层)
Practical combat of industrial defect detection project (II) -- steel surface defect detection based on deep learning framework yolov5
When using art template inheritance, compileerror: invalid or unexpected token generated
PIP install shutil reports an error
The express project changes the jade template to art template
Rhcsa day 4 operation
解决 注册谷歌邮箱 gmail 手机号无法用于验证
Mosaic Routing: implement / home / news
JZ35 replication of complex linked list