Native-cloud-k8s-book

来自linuxsa wiki
跳转到导航 跳转到搜索

chapter 6

6.1

6.1.2 labels
 kubectl get  service --show-labels 
NAME                  TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE    LABELS
hello-web-svc         ClusterIP      10.43.179.173   <none>        80/TCP         107d   <none>
kubernetes            ClusterIP      10.43.0.1       <none>        443/TCP        143d   component=apiserver,provider=kubernetes
mysql-proxy           ClusterIP      10.43.255.171   <none>        3306/TCP       22h    <none>
ngint-1               LoadBalancer   10.43.62.220    <pending>     80:30771/TCP   70d    k8s-app=ngint-1
nginx                 ClusterIP      10.43.57.74     <none>        80/TCP         68d    <none>
nginx-2               NodePort       10.43.6.192     <none>        80:30388/TCP   70d    app=nginx-2
nginx-3               NodePort       10.43.171.2     <none>        80:31393/TCP   70d    <none>
nginx-alb-style-svc   ClusterIP      10.43.134.238   <none>        80/TCP         106d   <none>
nginx-demo-svc        ClusterIP      10.43.54.218    <none>        80/TCP         106d   <none>
nginx-lb              LoadBalancer   10.43.27.174    <pending>     80:32693/TCP   107d   <none>
nginx-test            NodePort       10.43.162.71    <none>        80:30080/TCP   141d   <none>
pod-readiness         ClusterIP      10.43.233.102   <none>        80/TCP         67d    app=pod-readiness
portal                NodePort       10.43.87.94     <none>        80:30003/TCP   68d    <none>
➜  ~ kubectl get  svc -l 'app in (nginx-2, pod-readiness)' --show-labels
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   LABELS
nginx-2         NodePort    10.43.6.192     <none>        80:30388/TCP   70d   app=nginx-2
pod-readiness   ClusterIP   10.43.233.102   <none>        80/TCP         67d   app=pod-readiness

 kubectl get  svc  -l app   --show-labels 
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE   LABELS
nginx-2         NodePort    10.43.6.192     <none>        80:30388/TCP   70d   app=nginx-2
pod-readiness   ClusterIP   10.43.233.102   <none>        80/TCP         67d   app=pod-readiness

6.2

  chap06 cat 6.2.2.yaml              
kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  selector:
    app: myapp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376


➜  chap06 kubectl apply -f  6.2.2.yaml
service/my-service created

 kubectl get svc  | grep my-service 
my-service            ClusterIP      10.43.19.218    <none>        80/TCP         89s


ap06 kubectl get svc -n kube-system metrics-server -oyaml
apiVersion: v1
kind: Service
metadata:
  annotations:


  ports:
  - name: https
    port: 443
    protocol: TCP
    targetPort: https
  selector:
    k8s-app: metrics-server
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

创建带有选择器的service后, 集群会在该service所在的namespace 下自动创建一个同名的endpoint,记录选择器match到的pod ip and port eg 
➜  chap06 kubectl get po -n kube-system -l k8s-app=metrics-server -owide 
NAME                              READY   STATUS    RESTARTS   AGE   IP           NODE           NOMINATED NODE   READINESS GATES
metrics-server-7c86f97b8d-tjsv5   1/1     Running   0          18h   10.42.2.51   k3s-agent-01   <none>           <none>
➜  chap06 kubectl get ep -n kube-system metrics-server 
Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice
NAME             ENDPOINTS          AGE
metrics-server   10.42.2.51:10250   143d