Kubernetes基础之service、服务发布

标签

Label:对k8s中各种资源进行分类、分组,添加一个具有特别属性的一个标签。
Selector:通过一个过滤的语法进行查找到对应标签的资源

定义Label

[root@k8s-master01 ~]# kubectl label node k8s-node02 region=subnet7
node/k8s-node02 labeled

然后,可以通过Selector对其筛选:

[root@k8s-master01 ~]# kubectl get no -l region=subnet7
NAME         STATUS   ROLES    AGE     VERSION
k8s-node02   Ready    <none>   3d17h   v1.17.3

最后,在Deployment或其他控制器中指定将Pod部署到该节点:

containers:
  ......
dnsPolicy: ClusterFirst
nodeSelector:
  region: subnet7
restartPolicy: Always
......

也可以用同样的方式对Service进行Label:

[root@k8s-master01 ~]# kubectl label svc canary-v1 -n canary-production env=canary version=v1
service/canary-v1 labeled

查看Labels:

[root@k8s-master01 ~]# kubectl get svc -n canary-production --show-labels
NAME        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE   LABELS
canary-v1   ClusterIP   10.110.253.62   <none>        8080/TCP   24h   env=canary,version=v1

还可以查看所有Version为v1的svc:

[root@k8s-master01 canary]# kubectl get svc --all-namespaces -l version=v1
NAMESPACE           NAME        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
canary-production   canary-v1   ClusterIP   10.110.253.62   <none>        8080/TCP   25h

还可以通过Selector多标签值筛选

[root@k8s-master01 ~]# kubectl get po -A --show-labels
NAMESPACE              NAME                                         READY   STATUS    RESTARTS   AGE     LABELS
default                busybox                                      1/1     Running   88         43d     <none>
default                nginx-8kjj9                                  1/1     Running   0          4h22m   app=nginx,controller-revision-hash=7c4f84f49b,pod-template-generation=3
default                nginx-9vjsm                                  1/1     Running   0          4h22m   app=nginx,controller-revision-hash=7c4f84f49b,pod-template-generation=3
kube-system            calico-kube-controllers-cdd5755b9-qztxb      1/1     Running   10         44d     k8s-app=calico-kube-controllers,pod-template-hash=cdd5755b9
kube-system            calico-node-6q95q                            1/1     Running   16         44d     controller-revision-hash=6f6fbfdcf4,k8s-app=calico-node,pod-template-generation=1
kube-system            calico-node-cgp6p                            1/1     Running   19         44d     controller-revision-hash=6f6fbfdcf4,k8s-app=calico-node,pod-template-generation=1
kube-system            calico-node-tmxtg                            1/1     Running   14         44d     controller-revision-hash=6f6fbfdcf4,k8s-app=calico-node,pod-template-generation=1
kube-system            calico-node-wc674                            1/1     Running   12         44d     controller-revision-hash=6f6fbfdcf4,k8s-app=calico-node,pod-template-generation=1
kube-system            calico-node-z8k7p                            1/1     Running   11         44d     controller-revision-hash=6f6fbfdcf4,k8s-app=calico-node,pod-template-generation=1
kube-system            coredns-684d86ff88-mtkjf                     1/1     Running   9          43d     k8s-app=kube-dns,pod-template-hash=684d86ff88
kube-system            metrics-server-64c6c494dc-gdw52              1/1     Running   11         43d     k8s-app=metrics-server,pod-template-hash=64c6c494dc
kubernetes-dashboard   dashboard-metrics-scraper-86bb69c5f6-6jrch   1/1     Running   9          43d     k8s-app=dashboard-metrics-scraper,pod-template-hash=86bb69c5f6
kubernetes-dashboard   kubernetes-dashboard-6576c84894-9f7gh        1/1     Running   19         43d     k8s-app=kubernetes-dashboard,pod-template-hash=6576c84894

[root@k8s-master01 ~]# kubectl get po -A -l 'k8s-app in (metrics-server,kubernetes-dashboard)'		# 标签多值筛选
NAMESPACE              NAME                                    READY   STATUS    RESTARTS   AGE
kube-system            metrics-server-64c6c494dc-gdw52         1/1     Running   11         43d
kubernetes-dashboard   kubernetes-dashboard-6576c84894-9f7gh   1/1     Running   19         43d
[root@k8s-master01 ~]#

在这里插入图片描述
其他资源的Label方式相同。

Selector条件匹配

[root@k8s-master01 ~]# kubectl get svc --show-labels
NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE     LABELS
details       ClusterIP   10.99.9.178      <none>        9080/TCP   45h     app=details
kubernetes    ClusterIP   10.96.0.1        <none>        443/TCP    3d19h   component=apiserver,provider=kubernetes
nginx         ClusterIP   10.106.194.137   <none>        80/TCP     2d21h   app=productpage,version=v1
nginx-v2      ClusterIP   10.108.176.132   <none>        80/TCP     2d20h   <none>
productpage   ClusterIP   10.105.229.52    <none>        9080/TCP   45h     app=productpage,tier=frontend
ratings       ClusterIP   10.96.104.95     <none>        9080/TCP   45h     app=ratings
reviews       ClusterIP   10.102.188.143   <none>        9080/TCP   45h     app=reviews

选择app为reviews或者productpage的svc:

[root@k8s-master01 ~]# kubectl get svc -l  'app in (details, productpage)' --show-labels
NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE     LABELS
details       ClusterIP   10.99.9.178      <none>        9080/TCP   45h     app=details
nginx         ClusterIP   10.106.194.137   <none>        80/TCP     2d21h   app=productpage,version=v1
productpage   ClusterIP   10.105.229.52    <none>        9080/TCP   45h     app=productpage,tier=frontend

选择app为productpage或reviews但不包括version=v1的svc:

[root@k8s-master01 ~]# kubectl get svc -l  version!=v1,'app in (details, productpage)' --show-labels
NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE   LABELS
details       ClusterIP   10.99.9.178     <none>        9080/TCP   45h   app=details
productpage   ClusterIP   10.105.229.52   <none>        9080/TCP   45h   app=productpage,tier=frontend

选择labelkey名为app的svc:

[root@k8s-master01 ~]# kubectl get svc -l app --show-labels
NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE     LABELS
details       ClusterIP   10.99.9.178      <none>        9080/TCP   45h     app=details
nginx         ClusterIP   10.106.194.137   <none>        80/TCP     2d21h   app=productpage,version=v1
productpage   ClusterIP   10.105.229.52    <none>        9080/TCP   45h     app=productpage,tier=frontend
ratings       ClusterIP   10.96.104.95     <none>        9080/TCP   45h     app=ratings
reviews       ClusterIP   10.102.188.143   <none>        9080/TCP   45h     app=reviews

修改标签(Label)

[root@k8s-master01 canary]# kubectl get svc -n canary-production --show-labels
NAME        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE   LABELS
canary-v1   ClusterIP   10.110.253.62   <none>        8080/TCP   26h   env=canary,version=v1

[root@k8s-master01 canary]# kubectl label svc canary-v1 -n canary-production version=v2 --overwrite
service/canary-v1 labeled

[root@k8s-master01 canary]# kubectl get svc -n canary-production --show-labels
NAME        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE   LABELS
canary-v1   ClusterIP   10.110.253.62   <none>        8080/TCP   26h   env=canary,version=v2

删除标签(Label)

[root@k8s-master01 canary]# kubectl label svc canary-v1 -n canary-production version-
service/canary-v1 labeled

[root@k8s-master01 canary]# kubectl get svc -n canary-production --show-labels
NAME        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE   LABELS
canary-v1   ClusterIP   10.110.253.62   <none>        8080/TCP   26h   env=canary

Service

Service可以简单的理解为逻辑上的一组Pod。一种可以访问Pod的策略,而且其他Pod可以通过这个Service访问到这个Service代理的Pod。相对于Pod而言,它会有一个固定的名称,一旦创建就固定不变。

[root@k8s-master01 ~]# kubectl get svc -A		# 查看所有的service
NAMESPACE              NAME                        TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)                  AGE
default                kubernetes                  ClusterIP   192.168.0.1       <none>        443/TCP                  45d
default                nginx                       ClusterIP   None              <none>        80/TCP                   2d7h
kube-system            kube-dns                    ClusterIP   192.168.0.10      <none>        53/UDP,53/TCP,9153/TCP   45d
kube-system            metrics-server              ClusterIP   192.168.212.74    <none>        443/TCP                  45d
kubernetes-dashboard   dashboard-metrics-scraper   ClusterIP   192.168.216.104   <none>        8000/TCP                 45d
kubernetes-dashboard   kubernetes-dashboard        NodePort    192.168.19.209    <none>        443:31693/TCP            45d

[root@k8s-master01 ~]# kubectl get svc -n kube-system		# 查看kube-system下的service
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                  AGE
kube-dns         ClusterIP   192.168.0.10     <none>        53/UDP,53/TCP,9153/TCP   45d
metrics-server   ClusterIP   192.168.212.74   <none>        443/TCP                  45d

[root@k8s-master01 ~]# kubectl get ep -n kube-system		# 查看kube-system下的endpoint
NAME             ENDPOINTS                                              AGE
kube-dns         172.27.14.224:53,172.27.14.224:53,172.27.14.224:9153   45d
metrics-server   172.25.92.77:4443                                      45d

[root@k8s-master01 ~]# kubectl get po -n kube-system -owide		# 查看kube-system下的pod的详细信息(和endpoint中的ip对应)
NAME                                      READY   STATUS    RESTARTS   AGE   IP               NODE           NOMINATED NODE   READINESS GATES
calico-kube-controllers-cdd5755b9-qztxb   1/1     Running   11         45d   10.103.236.201   k8s-master01   <none>           <none>
calico-node-6q95q                         1/1     Running   17         45d   10.103.236.205   k8s-node02     <none>           <none>
calico-node-cgp6p                         1/1     Running   21         45d   10.103.236.203   k8s-master03   <none>           <none>
calico-node-tmxtg                         1/1     Running   16         45d   10.103.236.201   k8s-master01   <none>           <none>
calico-node-wc674                         1/1     Running   14         45d   10.103.236.202   k8s-master02   <none>           <none>
calico-node-z8k7p                         1/1     Running   12         45d   10.103.236.204   k8s-node01     <none>           <none>
coredns-684d86ff88-mtkjf                  1/1     Running   10         45d   172.27.14.224    k8s-node02     <none>           <none>
metrics-server-64c6c494dc-gdw52           1/1     Running   14         45d   172.25.92.77     k8s-master02   <none>           <none>
[root@k8s-master01 ~]#

创建一个Service

[root@k8s-master01 pod]# cat nginx-deploy.yaml			# 先创建pod
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2022-10-26T06:32:24Z"
  generation: 1
  labels:
    app: nginx
  name: nginx
  namespace: default
  resourceVersion: "594863"
  uid: 81733bda-9823-49cc-b12d-caddc2e0c318
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.15.2
        imagePullPolicy: IfNotPresent
        name: nginx
        resources: {
   }
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {
   }
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2022-10-26T06:32:26Z"
    lastUpdateTime: "2022-10-26T06:32:26Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2022-10-26T06:32:24Z"
    lastUpdateTime: "2022-10-26T06:32:26Z"
    message: ReplicaSet "nginx-66bbc9fdc5" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

============================================================================

[root@k8s-master01 pod]# kubectl create -f nginx-deploy.yaml
deployment.apps/nginx created

[root@k8s-master01 pod]# kubectl get po
NAME                     READY   STATUS              RESTARTS   AGE
busybox                  1/1     Running             90         45d
nginx-66bbc9fdc5-dhrgc   1/1     Running             0          5s
nginx-66bbc9fdc5-lh7vw   0/1     ContainerCreating   0          5s
[root@k8s-master01 pod]#

[root@k8s-master01 ~]# kubectl get po -owide
NAME                     READY   STATUS    RESTARTS   AGE   IP               NODE           NOMINATED NODE   READINESS GATES
busybox                  1/1     Running   93         46d   172.25.244.222   k8s-master01   <none>           <none>
nginx-66bbc9fdc5-dhrgc   1/1     Running   1          34h   172.25.245.0     k8s-master01   <none>           <none>
nginx-66bbc9fdc5-lh7vw   1/1     Running   1          34h   172.27.14.227    k8s-node02     <none>           <none>

[root@k8s-master01 ~]# curl 172.25.245.0		#	访问pod  ip地址
<!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@k8s-master01 ~]# kubectl get po --show-labels
NAME                     READY   STATUS    RESTARTS   AGE   LABELS
busybox                  1/1     Running   93         46d   <none>
nginx-66bbc9fdc5-dhrgc   1/1     Running   1          34h   app=nginx,pod-template-hash=66bbc9fdc5
nginx-66bbc9fdc5-lh7vw   1/1     Running   1          34h   app=nginx,pod-template-hash=66bbc9fdc5

[root@k8s-master01 ~]# vim nginx-svc.yaml			# 创建service
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-svc
  name: nginx-svc
spec:
  ports:
  - name: http # Service端口的名称
    port: 80 # Service自己的端口, servicea --> serviceb http://serviceb,  http://serviceb:8080
    protocol: TCP # UDP TCP SCTP default: TCP
    targetPort: 80 # 后端应用的端口
  - name: https
    port: 443
    protocol: TCP
    targetPort: 443
  selector:
    app: nginx			# 选择上述的pod(标签)匹配
  sessionAffinity: None
  type: ClusterIP

[root@k8s-master01 ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   192.168.0.1   <none>        443/TCP   46d

[root@k8s
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值