11-常见的控制器01(rc·rs·deploy)及服务发现(service)

一、rc控制器 ReplicationController

1.什么是rc控制器

​ rc的全称为ReplicationController,简称rc,该控制器可以控制指定Pod副本数量始终存活。

​ 主要作用是保证指定数量的 Pod 副本始终运行,并提供自动恢复和滚动更新功能。

2.实战案例

2.1 编写资源清单

[root@master231 replicationcontrollers]# cat 01-rc-xiuxian.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: rc-xiuxian
  labels:
    apps: xiuxian
spec:
  # 指的Pod的副本数量
  replicas: 3
  # 标签选择器,基于标签关联Pod
  selector:
    version: v1
  # 定义Pod的模板,将来创建Pod时基于该模板进行创建
  template:
    metadata:
      labels:
        version: v1
        school: oldboyedu
        class: linux96
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
        name: xiuxian

2.2 创建资源

[root@master231 replicationcontrollers]# kubectl apply -f 01-rc-xiuxian.yaml
replicationcontroller/rc-xiuxian created

2.3 同时查看rc和pod的信息

#简略信息
[root@master231 replicationcontrollers]# kubectl get rc,pod --show-labels
NAME                               DESIRED   CURRENT   READY   AGE   LABELS
replicationcontroller/rc-xiuxian   3         3         3       67s   apps=xiuxian

NAME                   READY   STATUS    RESTARTS   AGE   LABELS
pod/rc-xiuxian-6p6l4   1/1     Running   0          67s   class=linux96,school=oldboyedu,version=v1
pod/rc-xiuxian-dhtbf   1/1     Running   0          67s   class=linux96,school=oldboyedu,version=v1
pod/rc-xiuxian-nwxmz   1/1     Running   0          67s   class=linux96,school=oldboyedu,version=v1

#详细信息
[root@master231 replicationcontrollers]# kubectl get rc,pod --show-labels -o wide
NAME                               DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                                                      SELECTOR     LABELS
replicationcontroller/rc-xiuxian   3         3         3       71s   xiuxian      registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1   version=v1   apps=xiuxian

NAME                   READY   STATUS    RESTARTS   AGE   IP               NODE        NOMINATED NODE   READINESS GATES   LABELS
pod/rc-xiuxian-6p6l4   1/1     Running   0          71s   10.100.140.92    worker233   <none>           <none>            class=linux96,school=oldboyedu,version=v1
pod/rc-xiuxian-dhtbf   1/1     Running   0          71s   10.100.203.149   worker232   <none>           <none>            class=linux96,school=oldboyedu,version=v1
pod/rc-xiuxian-nwxmz   1/1     Running   0          71s   10.100.203.148   worker232   <none>           <none>            class=linux96,school=oldboyedu,version=v1

2.4 查看Pod被哪个控制器管理

[root@master231 replicationcontrollers]# kubectl get pods -l version=v1
NAME               READY   STATUS    RESTARTS   AGE
rc-xiuxian-6p6l4   1/1     Running   0          2m36s
rc-xiuxian-dhtbf   1/1     Running   0          2m36s
rc-xiuxian-nwxmz   1/1     Running   0          2m36s


[root@master231 replicationcontrollers]# kubectl describe pod  | grep "Controlled By"
Controlled By:  ReplicationController/rc-xiuxian
Controlled By:  ReplicationController/rc-xiuxian
Controlled By:  ReplicationController/rc-xiuxian

2.5 删除资源验证Pod副本数量始终存活

[root@master231 replicationcontrollers]# kubectl get pods -l version=v1
NAME               READY   STATUS    RESTARTS   AGE
rc-xiuxian-6p6l4   1/1     Running   0          4m38s
rc-xiuxian-dhtbf   1/1     Running   0          4m38s
rc-xiuxian-nwxmz   1/1     Running   0          4m38s

[root@master231 replicationcontrollers]# kubectl delete pods -l version=v1
pod "rc-xiuxian-6p6l4" deleted
pod "rc-xiuxian-dhtbf" deleted
pod "rc-xiuxian-nwxmz" deleted

[root@master231 replicationcontrollers]# kubectl get pods -l version=v1
NAME               READY   STATUS    RESTARTS   AGE
rc-xiuxian-mvddc   1/1     Running   0          5s
rc-xiuxian-rzds4   1/1     Running   0          5s
rc-xiuxian-z24z5   1/1     Running   0          5s

二、Service(svc)

1.什么是Service

​ 所谓的Service翻译过来表示'服务',其作用也是基于标签关联Pod,可以将Pod的端口进行暴露。从而提供统一的服务。简称svc

2.svc的四种类型

ClusterIP:
	一般情况下是K8S集群内部服务互相访问的一种类型。
		
NodePort:
	在ClusterIP基础之上,早期版本监听了所有的worker节点特定端口,新版本会有对应的iptables规则做NAT。 
	
LoadBalancer:
	一般情况下是在云厂商的K8S环境使用。
		
ExternalName:
	一般情况下用于映射K8S集群外部的某个特定服务。

3.svc拥有三个主要特征

- 1.基于标签关联后端的Pod,为Pod提供统一的访问入口;
- 2.拥有服务发现的特征,当后端关联的Pod的IP地址发生变动时,会自动更新Endpoints列表;
- 3.拥有负载均衡的能力,该能力底层是由kube-proxy组件提供

4.响应式创建Service (ClusterIP)

4.1 基于rc资源暴露,从而创建svc

service类型不指定默认为ClusterIP

[root@master231 replicationcontrollers]# kubectl expose rc rc-xiuxian --port=80
service/rc-xiuxian exposed

[root@master231 replicationcontrollers]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.200.0.1       <none>        443/TCP   28h
rc-xiuxian   ClusterIP   10.200.196.245   <none>        80/TCP    15s

4.2 svc会服务发现对应后端的Pod地址

[root@master231 replicationcontrollers]# kubectl describe svc rc-xiuxian 
Name:              rc-xiuxian
Namespace:         default
Labels:            apps=xiuxian
Annotations:       <none>
Selector:          version=v1
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
#因为是ClusterIP类型,所以只是对集群内部的统一访问入口,不对外提供访问
IP:                10.200.196.245
IPs:               10.200.196.245
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.100.140.94:80,10.100.203.152:80,10.100.203.153:80
Session Affinity:  None
Events:            <none>


#对应Endpoints
[root@master231 replicationcontrollers]# kubectl get pods -l version=v1 -o wide
NAME               READY   STATUS    RESTARTS   AGE    IP               NODE        NOMINATED NODE   READINESS GATES
rc-xiuxian-dgnqj   1/1     Running   0          8m5s   10.100.203.153   worker232   <none>           <none>
rc-xiuxian-fz4sp   1/1     Running   0          8m5s   10.100.140.94    worker233   <none>           <none>
rc-xiuxian-m6m4v   1/1     Running   0          8m5s   10.100.203.152   worker232   <none>           <none>

4.3 删除资源再次查看

#删除
[root@master231 replicationcontrollers]# kubectl delete pods -l version=v1 
pod "rc-xiuxian-dgnqj" deleted
pod "rc-xiuxian-fz4sp" deleted
pod "rc-xiuxian-m6m4v" deleted

#查看,发现podIP变了
[root@master231 replicationcontrollers]# kubectl get pods -l version=v1 -o wide
NAME               READY   STATUS    RESTARTS   AGE   IP               NODE        NOMINATED NODE   READINESS GATES
rc-xiuxian-bg7ch   1/1     Running   0          8s    10.100.140.95    worker233   <none>           <none>
rc-xiuxian-d5ng4   1/1     Running   0          8s    10.100.203.154   worker232   <none>           <none>
rc-xiuxian-mjs4q   1/1     Running   0          8s    10.100.203.155   worker232   <none>           <none>
[root@master231 replicationcontrollers]# 

#查看统一的访问入口 ,没变
[root@master231 replicationcontrollers]# kubectl describe svc rc-xiuxian 
Name:              rc-xiuxian
Namespace:         default
Labels:            apps=xiuxian
Annotations:       <none>
Selector:          version=v1
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.200.196.245
IPs:               10.200.196.245
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.100.140.95:80,10.100.203.154:80,10.100.203.155:80
Session Affinity:  None
Events:            <none>

4.4 通过svc提供统一的访问入口

[root@master231 replicationcontrollers]# curl 10.200.196.245
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <title>yinzhengjie apps v1</title>
    <style>
       div img {
          width: 900px;
          height: 600px;
          margin: 0;
       }
    </style>
  </head>

  <body>
    <h1 style="color: green">凡人修仙传 v1 </h1>
    <div>
      <img src="1.jpg">
    <div>
  </body>

</html>

5.声明式创建Service

5.1 快速获取资源清单的模板

[root@master231 service]# kubectl expose rc rc-xiuxian --port=80 -o yaml --dry-run=client
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    apps: xiuxian
  name: rc-xiuxian
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    version: v1
status:
  loadBalancer: {}

5.2 编写资源清单

[root@master231 service]# cat 01-svc-xiuxian.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    apps: xiuxian
  name: svc-xiuxian
spec:
  # 配置端口映射
  ports:
    # 表示svc的端口
  - port: 90
    # 声明要用的协议,默认就是TCP,有效值为: "TCP", "UDP", and "SCTP"
    protocol: TCP
    # Pod的端口地址
    targetPort: 80
  # 关联后端的Pod
  selector:
    version: v1

5.3 创建svc并查看svc的详细信息

#创建资源
[root@master231 service]# kubectl apply -f  01-svc-xiuxian.yaml
service/svc-xiuxian created

#查看服务发现信息
[root@master231 service]# kubectl get svc
NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
kubernetes    ClusterIP   10.200.0.1       <none>        443/TCP   28h
rc-xiuxian    ClusterIP   10.200.196.245   <none>        80/TCP    13m
svc-xiuxian   ClusterIP   10.200.25.148    <none>        90/TCP    3s

[root@master231 service]# kubectl get svc svc-xiuxian 
NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
svc-xiuxian   ClusterIP   10.200.25.148   <none>        90/TCP    8s

#查看svc-xiuxian资源的svc的详细信息
[root@master231 service]# kubectl describe svc svc-xiuxian 
Name:              svc-xiuxian
Namespace:         default
Labels:            apps=xiuxian
Annotations:       <none>
Selector:          version=v1
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.200.25.148
IPs:               10.200.25.148
Port:              <unset>  90/TCP
TargetPort:        80/TCP
Endpoints:         10.100.140.95:80,10.100.203.154:80,10.100.203.155:80
Session Affinity:  None
Events:            <none>

5.4 访问测试

[root@master231 service]# curl 10.200.25.148:90
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <title>yinzhengjie apps v1</title>
    <style>
       div img {
          width: 900px;
          height: 600px;
          margin: 0;
       }
    </style>
  </head>

  <body>
    <h1 style="color: green">凡人修仙传 v1 </h1>
    <div>
      <img src="1.jpg">
    <div>
  </body>

</html>

5.5 测试验证svc拥有负载均衡的能力

#查看正在运行的容器
[root@master231 service]# kubectl get pods -o wide 
NAME               READY   STATUS    RESTARTS   AGE     IP               NODE        NOMINATED NODE   READINESS GATES
rc-xiuxian-bg7ch   1/1     Running   0          8m43s   10.100.140.95    worker233   <none>           <none>
rc-xiuxian-d5ng4   1/1     Running   0          8m43s   10.100.203.154   worker232   <none>           <none>
rc-xiuxian-mjs4q   1/1     Running   0          8m43s   10.100.203.155   worker232   <none>           <none>

#修改nginx页面
[root@master231 service]# kubectl exec -it rc-xiuxian-bg7ch -- sh
/ # echo 11111111 > /usr/share/nginx/html/index.html 
/ # 
[root@master231 service]# 
[root@master231 service]# kubectl exec -it rc-xiuxian-d5ng4 -- sh
/ # echo 222222222 > /usr/share/nginx/html/index.html 
/ # 
[root@master231 service]# 
[root@master231 service]# kubectl exec -it rc-xiuxian-mjs4q -- sh
/ # echo 3333333333 > /usr/share/nginx/html/index.html 
/ # 

#访问测试
[root@master231 service]# for i in `seq 10`;do curl 10.200.25.148:90;done
222222222
11111111
222222222
222222222
222222222
3333333333
222222222
3333333333
11111111
222222222

三、rs控制器 ReplicaSet

1.什么是rs

​ rs全称为ReplicaSet,也是K8S集群的一个副本控制器,功能和rc类似。

​ 相比于rc而言,功能更加丰富。且更加轻量级

2.实战案例

2.1 基于标签匹配matchLabels

[root@master231 replicasets]# cat 01-rs-matchLabels-xiuxian.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-xiuxian
  labels:
    apps: xiuxian
spec:
  replicas: 5
  selector:
    # 匹配Pod时基于标签匹配
    matchLabels:
      version: v1
  template:
    metadata:
      labels:
        version: v1
        school: oldboyedu
        class: linux96
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
        name: xiuxian

2.2 获取rs和pod的相关信息

[root@master231 replicasets]# kubectl get rs,po
NAME                         DESIRED   CURRENT   READY   AGE
replicaset.apps/rs-xiuxian   5         5         5       84s

NAME                   READY   STATUS    RESTARTS   AGE
pod/rs-xiuxian-9jlbb   1/1     Running   0          84s
pod/rs-xiuxian-j8xlz   1/1     Running   0          83s
pod/rs-xiuxian-qj9x6   1/1     Running   0          84s
pod/rs-xiuxian-wgx4z   1/1     Running   0          84s
pod/rs-xiuxian-zsdsw   1/1     Running   0          83s


#删除所有pod
[root@master231 replicasets]# kubectl delete pods --all
pod "rs-xiuxian-9jlbb" deleted
pod "rs-xiuxian-j8xlz" deleted
pod "rs-xiuxian-qj9x6" deleted
pod "rs-xiuxian-wgx4z" deleted
pod "rs-xiuxian-zsdsw" deleted

#再次查看 
[root@master231 replicasets]# kubectl get rs,po
NAME                         DESIRED   CURRENT   READY   AGE
replicaset.apps/rs-xiuxian   5         5         5       96s

NAME                   READY   STATUS    RESTARTS   AGE
pod/rs-xiuxian-bfn8n   1/1     Running   0          4s
pod/rs-xiuxian-bzncd   1/1     Running   0          4s
pod/rs-xiuxian-czkrr   1/1     Running   0          4s
pod/rs-xiuxian-kn8hb   1/1     Running   0          4s
pod/rs-xiuxian-n2x7p   1/1     Running   0          4s

2.3 基于标签表达式

[root@master231 replicasets]# kubectl run test01 --image=registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1 -l version=v1
pod/test01 created

[root@master231 replicasets]# kubectl run test02 --image=registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1 -l version=v2
pod/test02 created
[root@master231 replicasets]# 
[root@master231 replicasets]# kubectl run test03 --image=registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1 -l version=v3
pod/test03 created
[root@master231 replicasets]# 
[root@master231 replicasets]# kubectl run test04 --image=registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1 -l version=v4
pod/test04 created
[root@master231 replicasets]# 
[root@master231 replicasets]# kubectl run test05 --image=registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1 -l version=v5
pod/test05 created


[root@master231 replicasets]# kubectl get pods --show-labels
NAME     READY   STATUS    RESTARTS   AGE   LABELS
test01   1/1     Running   0          25s   version=v1
test02   1/1     Running   0          20s   version=v2
test03   1/1     Running   0          16s   version=v3
test04   1/1     Running   0          11s   version=v4
test05   1/1     Running   0          7s    version=v5


#编写列表清单
[root@master231 replicasets]# cat 02-rs-matchExpressions-xiuxian.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs-xiuxian-matchexpressions
  labels:
    apps: xiuxian
spec:
  replicas: 5
  selector:
    # 匹配Pod时基于标签表达式
    matchExpressions:
      # 指定标签的key
    - key: version
      # 指定标签的值,可以有多个
      values:
      - v1
      - v2
      - v3
      # 声明key和value之间的关系,有效值为: In, NotIn, Exists and DoesNotExist
      # In: 
      # 	key的值必须在vlaues规定的值列表中之一。
      # NotIn:
      #     和In相反。
      # Exists:
      #    如果是存在的话, 意味着value可以不写,表示只要存在指定的key即可。values任意。
      # DoesNotExist:
      #    如果是不存在的话,意味着value可以不写,表示只要不存在指定的key即可。values任意。
      operator: In
  template:
    metadata:
      labels:
        version: v1
        school: oldboyedu
        class: linux96
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
        name: xiuxian
        
        
#创建资源
[root@master231 replicasets]# kubectl apply -f  02-rs-matchExpressions-xiuxian.yaml 
replicaset.apps/rs-xiuxian-matchexpressions created

#查看列表清单
[root@master231 replicasets]# kubectl get pods -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP               NODE        NOMINATED NODE   READINESS GATES
rs-xiuxian-matchexpressions-2brnv   1/1     Running   0          3s    10.100.140.103   worker233   <none>           <none>
rs-xiuxian-matchexpressions-c8tx8   1/1     Running   0          3s    10.100.203.164   worker232   <none>           <none>
test01                              1/1     Running   0          60s   10.100.203.161   worker232   <none>           <none>
test02                              1/1     Running   0          55s   10.100.203.162   worker232   <none>           <none>
test03                              1/1     Running   0          51s   10.100.140.101   worker233   <none>           <none>
test04                              1/1     Running   0          46s   10.100.203.163   worker232   <none>           <none>
test05                              1/1     Running   0          42s   10.100.140.102   worker233   <none>           <none>

#查看rs相关信息
[root@master231 replicasets]# kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
rs-xiuxian-matchexpressions   5         5         5       18s

#新添加了2个副本
[root@master231 replicasets]# kubectl get pods --show-labels
NAME                                READY   STATUS    RESTARTS   AGE   LABELS
rs-xiuxian-matchexpressions-2brnv   1/1     Running   0          28s   class=linux96,school=oldboyedu,version=v1
rs-xiuxian-matchexpressions-c8tx8   1/1     Running   0          28s   class=linux96,school=oldboyedu,version=v1
test01                              1/1     Running   0          85s   version=v1
test02                              1/1     Running   0          80s   version=v2
test03                              1/1     Running   0          76s   version=v3
test04                              1/1     Running   0          71s   version=v4
test05                              1/1     Running   0          67s   version=v5

3.理解?

​ 在上边的实战中,我们先运行了几个Pod,标签分别为v1,v2,v3,v4,v5。

​ 紧接着,我们又编写了列表清单,指定了匹配带有v1,v2,v3的Pod,并指定了key的值必须在vlaues规定的值列表中之一(In),模版中创建的标签为v1。

​ 所以在根据列表清单创建资源时,已经匹配到了3个符合条件的标签,所以只会创建两个副本

四、常见的控制器之deploy实战

1.什么是deploy

​ deploy全称为deployments,翻译为中文是'部署'的意思。

​ 该控制器不直接控制Pod,而是调用rs控制pod副本数量,且支持声明式更新。

2.实战案例

2.1 基于matchLabels实战1

[root@master231 deployments]# cat 01-deploy-matchLabels-xiuxian.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-xiuxian
  labels:
    apps: xiuxian
spec:
  replicas: 5
  selector:
    matchLabels:
      version: v1
  template:
    metadata:
      labels:
        version: v1
        school: oldboyedu
        class: linux96
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
        name: xiuxian
        
        
[root@master231 deployments]# kubectl apply -f 01-deploy-matchLabels-xiuxian.yaml
deployment.apps/deploy-xiuxian created


#获取deploy,rs,po的相关信息
[root@master231 deployments]# kubectl get deploy,rs,po -o wide
NAME                             READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                      SELECTOR
deployment.apps/deploy-xiuxian   5/5     5            5           9s    xiuxian      registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1   version=v1

NAME                                       DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                                                      SELECTOR
replicaset.apps/deploy-xiuxian-9ddcfd7db   5         5         5       9s    xiuxian      registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1   pod-template-hash=9ddcfd7db,version=v1

NAME                                 READY   STATUS    RESTARTS   AGE   IP               NODE        NOMINATED NODE   READINESS GATES
pod/deploy-xiuxian-9ddcfd7db-2np97   1/1     Running   0          9s    10.100.203.172   worker232   <none>           <none>
pod/deploy-xiuxian-9ddcfd7db-4jbl6   1/1     Running   0          9s    10.100.203.173   worker232   <none>           <none>
pod/deploy-xiuxian-9ddcfd7db-9w6v4   1/1     Running   0          9s    10.100.140.109   worker233   <none>           <none>
pod/deploy-xiuxian-9ddcfd7db-nvb9m   1/1     Running   0          9s    10.100.140.108   worker233   <none>           <none>
pod/deploy-xiuxian-9ddcfd7db-vhfs4   1/1     Running   0          9s    10.100.203.174   worker232   <none>           <none>



[root@master231 deployments]# kubectl get deployments.apps  deploy-xiuxian  -o yaml | grep "image:"
      - image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
[root@master231 deployments]# 
[root@master231 deployments]# kubectl edit deployments.apps  deploy-xiuxian  
deployment.apps/deploy-xiuxian edited
[root@master231 deployments]# 
[root@master231 deployments]# kubectl get deployments.apps  deploy-xiuxian  -o yaml | grep "image:"
      - image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2
      
      
      
      
      
[root@master231 deployments]# kubectl get deploy,rs,po -o wide
NAME                             READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES                                                      SELECTOR
deployment.apps/deploy-xiuxian   5/5     5            5           2m23s   xiuxian      registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2   version=v1

NAME                                        DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES                                                      SELECTOR
replicaset.apps/deploy-xiuxian-67bc8cf4ff   5         5         5       32s     xiuxian      registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2   pod-template-hash=67bc8cf4ff,version=v1
replicaset.apps/deploy-xiuxian-9ddcfd7db    0         0         0       2m23s   xiuxian      registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1   pod-template-hash=9ddcfd7db,version=v1

NAME                                  READY   STATUS    RESTARTS   AGE   IP               NODE        NOMINATED NODE   READINESS GATES
pod/deploy-xiuxian-67bc8cf4ff-55zrp   1/1     Running   0          32s   10.100.203.175   worker232   <none>           <none>
pod/deploy-xiuxian-67bc8cf4ff-ctwp9   1/1     Running   0          30s   10.100.203.177   worker232   <none>           <none>
pod/deploy-xiuxian-67bc8cf4ff-j6qml   1/1     Running   0          32s   10.100.140.111   worker233   <none>           <none>
pod/deploy-xiuxian-67bc8cf4ff-m9ml9   1/1     Running   0          31s   10.100.203.176   worker232   <none>           <none>
pod/deploy-xiuxian-67bc8cf4ff-q2pf7   1/1     Running   0          32s   10.100.140.110   worker233   <none>           <none>




[root@master231 deployments]# curl  10.100.140.111 
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <title>yinzhengjie apps v2</title>
    <style>
       div img {
          width: 900px;
          height: 600px;
          margin: 0;
       }
    </style>
  </head>

  <body>
    <h1 style="color: red">凡人修仙传 v2 </h1>
    <div>
      <img src="2.jpg">
    <div>
  </body>

</html>


[root@master231 deployments]# kubectl delete -f 01-deploy-matchLabels-xiuxian.yaml 
deployment.apps "deploy-xiuxian" deleted
[root@master231 deployments]# 
[root@master231 deployments]# kubectl get deploy,rs,po -o wide
No resources found in default namespace.

2.2 基于matchLabels实战2

[root@master231 deployments]# cat 02-deploy-matchExpressions-xiuxian.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deploy-xiuxian-matchexpressions
  labels:
    apps: xiuxian
spec:
  replicas: 5
  selector:
    matchExpressions:
    - key: version
      values:
      - v1
      - v2
      - v3
      operator: In
  template:
    metadata:
      labels:
        version: v1
        school: oldboyedu
        class: linux96
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1
        name: xiuxian
[root@master231 deployments]# 
[root@master231 deployments]# kubectl apply -f  02-deploy-matchExpressions-xiuxian.yaml 
deployment.apps/deploy-xiuxian-matchexpressions created



[root@master231 deployments]# kubectl get deploy,rs,po -o wide
NAME                                              READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                      SELECTOR
deployment.apps/deploy-xiuxian-matchexpressions   5/5     5            5           2s    xiuxian      registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1   version in (v1,v2,v3)

NAME                                                        DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                                                      SELECTOR
replicaset.apps/deploy-xiuxian-matchexpressions-9ddcfd7db   5         5         5       2s    xiuxian      registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1   pod-template-hash=9ddcfd7db,version in (v1,v2,v3)

NAME                                                  READY   STATUS    RESTARTS   AGE   IP               NODE        NOMINATED NODE   READINESS GATES
pod/deploy-xiuxian-matchexpressions-9ddcfd7db-jq9t7   1/1     Running   0          2s    10.100.203.178   worker232   <none>           <none>
pod/deploy-xiuxian-matchexpressions-9ddcfd7db-n76n8   1/1     Running   0          2s    10.100.140.112   worker233   <none>           <none>
pod/deploy-xiuxian-matchexpressions-9ddcfd7db-p4jns   1/1     Running   0          2s    10.100.140.113   worker233   <none>           <none>
pod/deploy-xiuxian-matchexpressions-9ddcfd7db-swthk   1/1     Running   0          2s    10.100.203.180   worker232   <none>           <none>
pod/deploy-xiuxian-matchexpressions-9ddcfd7db-vzfjl   1/1     Running   0          2s    10.100.203.179   worker232   <none>           <none>




[root@master231 deployments]# curl 10.100.203.180 
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <title>yinzhengjie apps v1</title>
    <style>
       div img {
          width: 900px;
          height: 600px;
          margin: 0;
       }
    </style>
  </head>

  <body>
    <h1 style="color: green">凡人修仙传 v1 </h1>
    <div>
      <img src="1.jpg">
    <div>
  </body>

</html>




[root@master231 deployments]# vim 02-deploy-matchExpressions-xiuxian.yaml 
[root@master231 deployments]# 
[root@master231 deployments]# kubectl apply -f  02-deploy-matchExpressions-xiuxian.yaml 
deployment.apps/deploy-xiuxian-matchexpressions configured
[root@master231 deployments]# 
[root@master231 deployments]# kubectl get deployments.apps  deploy-xiuxian-matchexpressions  -o yaml | grep "image:"
      - image: registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2
      
      
      
      
[root@master231 deployments]# kubectl get deploy,rs,po -o wide
NAME                                              READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                      SELECTOR
deployment.apps/deploy-xiuxian-matchexpressions   5/5     5            5           66s   xiuxian      registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2   version in (v1,v2,v3)

NAME                                                         DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                                                      SELECTOR
replicaset.apps/deploy-xiuxian-matchexpressions-67bc8cf4ff   5         5         5       36s   xiuxian      registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v2   pod-template-hash=67bc8cf4ff,version in (v1,v2,v3)
replicaset.apps/deploy-xiuxian-matchexpressions-9ddcfd7db    0         0         0       66s   xiuxian      registry.cn-hangzhou.aliyuncs.com/yinzhengjie-k8s/apps:v1   pod-template-hash=9ddcfd7db,version in (v1,v2,v3)

NAME                                                   READY   STATUS    RESTARTS   AGE   IP               NODE        NOMINATED NODE   READINESS GATES
pod/deploy-xiuxian-matchexpressions-67bc8cf4ff-2ddwv   1/1     Running   0          36s   10.100.140.114   worker233   <none>           <none>
pod/deploy-xiuxian-matchexpressions-67bc8cf4ff-cfzvk   1/1     Running   0          35s   10.100.203.182   worker232   <none>           <none>
pod/deploy-xiuxian-matchexpressions-67bc8cf4ff-krvzm   1/1     Running   0          36s   10.100.140.115   worker233   <none>           <none>
pod/deploy-xiuxian-matchexpressions-67bc8cf4ff-p2wfs   1/1     Running   0          36s   10.100.203.181   worker232   <none>           <none>
pod/deploy-xiuxian-matchexpressions-67bc8cf4ff-xnrpl   1/1     Running   0          35s   10.100.203.183   worker232   <none>           <none>




[root@master231 deployments]# curl   10.100.203.183 
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <title>yinzhengjie apps v2</title>
    <style>
       div img {
          width: 900px;
          height: 600px;
          margin: 0;
       }
    </style>
  </head>

  <body>
    <h1 style="color: red">凡人修仙传 v2 </h1>
    <div>
      <img src="2.jpg">
    <div>
  </body>

</html>

五、Pod的服务发现Service之NodePort类型

1.编写资源清单

[root@master231 service]# cat 02-svc-NodePort-xiuxian.yaml 
apiVersion: v1
kind: Service
metadata:
  labels:
    apps: xiuxian
  name: svc-xiuxian-nodeport
spec:
  type: NodePort
  ports:
  - port: 90
    protocol: TCP
    targetPort: 80
    # 声明worker节点的转发的端口,默认的有效范围是:  30000-32767
    nodePort: 30080
  selector:
    version: v1
    
[root@master231 service]# kubectl apply -f 02-svc-NodePort-xiuxian.yaml 
service/svc-xiuxian-nodeport created


[root@master231 service]# kubectl get svc 
NAME                   TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes             ClusterIP   10.200.0.1       <none>        443/TCP        30h
rc-xiuxian             ClusterIP   10.200.196.245   <none>        80/TCP         116m
svc-xiuxian            ClusterIP   10.200.25.148    <none>        90/TCP         102m
svc-xiuxian-nodeport   NodePort    10.200.21.38     <none>        90:30080/TCP   6s

2.访问测试

http://10.0.0.231:30080/
http://10.0.0.232:30080/
http://10.0.0.233:30080/

六、deploy,rs,svc,Pod之间的关系

posted @ 2025-04-09 09:21  丁志岩  阅读(52)  评论(0)    收藏  举报