k8s service 资源

k8s资源类型:

ClusterIP:

这是默认的service类型,它会创建一个虚拟的cluster ip地址,仅在集群内部可访问,这对于内部微服务之间的通信非常有用。

port:

service的端口,这个是k8s集群内部服务可访问的端口

NodePort:

这种类型会公开service在每个节点上的一个端口。这意味着service将在每个节点上都有一个公开的端口,可以通过该端口从集群外部访问service,通常用于需要从外部访问的服务,但不适合生产环境。

LoadBalancer:

这种类型通过云提供商的负载均衡服务(如AWS ELB或GCP LoadBalancer)创建一个外部负载均衡器,并将流量路由到service,这通常用于生产环境,特别是当您需要公开服务给公共互联网市。

ExternalName:

这种类型允许service充当DNS CNAME记录,将外部服务映射到集群外部的服务,它用于将内部服务映射到外部服务的情况,而不需要更改应用程序代码。

Headless:

Headless service 不会分配Cluster IP,而是为每个pod分配一个DNS条目,它通常用于Statfulsets,允许每个pod拥有唯一的DNS条目。

Ingress:

虽然Ingress不是service类型,但它是一种用于公开HTTP和HTTPS服务的资源,Ingress资源充当HTTPS路由器,允许您根据主机名、路径等将流量路由到不同的service。

service默认域名:

svc名称.svc命名空间.svc.cluster.local

ClusterIP类型案例:

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: deploy-nginx
  namespace: nginx
  labels:
    nginx: v1
spec:
  replicas: 2 #副本数
  selector:
    matchLabels:  #关联pod的标签
      nginx: v1
  template:
    metadata:    #pod的标签
      labels:
        nginx: v1
    spec:
      containers:
      - name: deploy-nginx   #容器名称
        image: nginx:latest   #镜像
        imagePullPolicy: IfNotPresent 
        ports:
        - containerPort: 80    #容器端口
        startupProbe:            #启动探测
          httpGet:               #探测返回状态码
            port: 80
            path: /
          initialDelaySeconds: 10 # 容器启动后10秒开始探测
          periodSeconds: 10  #探测的间隔时间是10秒
          timeoutSeconds: 5  #探针执行检测请求后,等待响应的超时时间
          successThreshold: 1 #成功1次就算探测成功
          failureThreshold: 3 #探测失败3次就算失败
        livenessProbe:        #存活探测
          httpGet:            #探测返回状态码
            port: 80
            path: /
          initialDelaySeconds: 10 # 容器启动后10秒开始探测
          periodSeconds: 10  #探测的间隔时间是10秒
          timeoutSeconds: 5  #探针执行检测请求后,等待响应的超时时间
          successThreshold: 1 #成功1次就算探测成功
          failureThreshold: 3 #探测失败3次就算失败
        readinessProbe:       #就绪探测
          httpGet:            #探测返回状态码
            port: 80
            path: /
          initialDelaySeconds: 10 # 容器启动后10秒开始探测
          periodSeconds: 10  #探测的间隔时间是10秒
          timeoutSeconds: 5  #探针执行检测请求后,等待响应的超时时间
          successThreshold: 1 #成功1次就算探测成功
          failureThreshold: 3 #探测失败3次就算失败
---
apiVersion: v1
kind: Service
metadata:
  name: svc-nginx
  namespace: nginx
  labels:
    deploy-svc: v1
spec:
  selector:       #关联pod标签
    nginx: v1
  ports:
  - port: 80      #端口
    protocol: TCP    #协议
    targetPort: 80
  type: ClusterIP    #svc类型

NodePort类型案例:

数据转发流程:

客户端请求http://192.168.40.180:32766->docker0虚拟网卡:172.17.0.1:32766->10.244.236.137:80,10.244.236.136:80

apiVersion: apps/v1
kind: Deployment
metadata: 
  name: deploy-nginx
  namespace: nginx
  labels:
    nginx: v1
spec:
  replicas: 2 #副本数
  selector:
    matchLabels:  #关联pod的标签
      nginx: v1
  template:
    metadata:    #pod的标签
      labels:
        nginx: v1
    spec:
      containers:
      - name: deploy-nginx   #容器名称
        image: nginx:latest   #镜像
        imagePullPolicy: IfNotPresent 
        ports:
        - containerPort: 80    #容器端口
        startupProbe:            #启动探测
          httpGet:               #探测返回状态码
            port: 80
            path: /
          initialDelaySeconds: 10 # 容器启动后10秒开始探测
          periodSeconds: 10  #探测的间隔时间是10秒
          timeoutSeconds: 5  #探针执行检测请求后,等待响应的超时时间
          successThreshold: 1 #成功1次就算探测成功
          failureThreshold: 3 #探测失败3次就算失败
        livenessProbe:        #存活探测
          httpGet:            #探测返回状态码
            port: 80
            path: /
          initialDelaySeconds: 10 # 容器启动后10秒开始探测
          periodSeconds: 10  #探测的间隔时间是10秒
          timeoutSeconds: 5  #探针执行检测请求后,等待响应的超时时间
          successThreshold: 1 #成功1次就算探测成功
          failureThreshold: 3 #探测失败3次就算失败
        readinessProbe:       #就绪探测
          httpGet:            #探测返回状态码
            port: 80
            path: /
          initialDelaySeconds: 10 # 容器启动后10秒开始探测
          periodSeconds: 10  #探测的间隔时间是10秒
          timeoutSeconds: 5  #探针执行检测请求后,等待响应的超时时间
          successThreshold: 1 #成功1次就算探测成功
          failureThreshold: 3 #探测失败3次就算失败
---
apiVersion: v1
kind: Service
metadata:
  name: svc-nginx
  namespace: nginx
  labels:
    deploy-svc: v1
spec:
  selector:       #关联pod标签
    nginx: v1
  ports:
  - port: 80      #端口
    protocol: TCP    #协议
    targetPort: 80
  type: NodePort    #svc类型

ExternalName类型案例:

服务端:
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: deploy-nginx
  namespace: nginx
  labels:
    nginx: v1
spec:
  replicas: 2 #副本数
  selector:
    matchLabels:  #关联pod的标签
      nginx: v1
  template:
    metadata:    #pod的标签
      labels:
        nginx: v1
    spec:
      containers:
      - name: deploy-nginx   #容器名称
        image: nginx:latest   #镜像
        imagePullPolicy: IfNotPresent 
        ports:
        - containerPort: 80    #容器端口
        startupProbe:            #启动探测
          httpGet:               #探测返回状态码
            port: 80
            path: /
          initialDelaySeconds: 10 # 容器启动后10秒开始探测
          periodSeconds: 10  #探测的间隔时间是10秒
          timeoutSeconds: 5  #探针执行检测请求后,等待响应的超时时间
          successThreshold: 1 #成功1次就算探测成功
          failureThreshold: 3 #探测失败3次就算失败
        livenessProbe:        #存活探测
          httpGet:            #探测返回状态码
            port: 80
            path: /
          initialDelaySeconds: 10 # 容器启动后10秒开始探测
          periodSeconds: 10  #探测的间隔时间是10秒
          timeoutSeconds: 5  #探针执行检测请求后,等待响应的超时时间
          successThreshold: 1 #成功1次就算探测成功
          failureThreshold: 3 #探测失败3次就算失败
        readinessProbe:       #就绪探测
          httpGet:            #探测返回状态码
            port: 80
            path: /
          initialDelaySeconds: 10 # 容器启动后10秒开始探测
          periodSeconds: 10  #探测的间隔时间是10秒
          timeoutSeconds: 5  #探针执行检测请求后,等待响应的超时时间
          successThreshold: 1 #成功1次就算探测成功
          failureThreshold: 3 #探测失败3次就算失败
---
apiVersion: v1
kind: Service
metadata:
  name: svc-nginx
  namespace: nginx
  labels:
    deploy-svc: v1
spec:
  selector:       #关联pod标签
    nginx: v1
  ports:
  - port: 80      #端口
    protocol: TCP    #协议
    targetPort: 80

客户端:
apiVersion: v1
kind: Pod
metadata:
  name: centos-pod
  labels:
    nginx: v1
spec:
  containers:
  - name: centos-container
    image: centos:latest
    command: ["/bin/sleep", "3600"]
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-client
  labels:
spec:
  selector:       #关联pod标签
    nginx: v1
  ports:
  - port: 80      #端口
    protocol: TCP    #协议
    targetPort: 80
  type: ExternalName
  externalName: svc-nginx.nginx.svc.cluster.local

测试:
root@k8s01:/k8s/nginx# kubectl get svc
NAME            TYPE           CLUSTER-IP    EXTERNAL-IP                         PORT(S)        AGE
nginx-client    ExternalName           svc-nginx.nginx.svc.cluster.local   80/TCP         13m
root@k8s01:/k8s/nginx# kubectl exec -it centos-pod -- /bin/bash
[root@centos-pod /]# 
[root@centos-pod /]# 
[root@centos-pod /]# 
[root@centos-pod /]# curl svc-nginx.nginx.svc.cluster.local



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

k8s的最佳实践,映射外部服务案例分享:

场景1:k8s集群引用外部Mysql数据库
主机192.168.40.181安装数据库:
root@k8s02:~# yum install mariadb* -y
root@k8s02:~# systemctl start mariadb
root@k8s02:~# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
在k8s集群中创建Mysql的svc:
piVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306      #端口
查看mysql svc详细信息
root@k8s01:/k8s/mysql# kubectl describe svc mysql 
Name:              mysql
Namespace:         default
Labels:            
Annotations:       
Selector:          
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.97.247.132
IPs:               10.97.247.132
Port:                3306/TCP
TargetPort:        3306/TCP
Endpoints:         
Session Affinity:  None
Events:            
在k8s集群中创建Mysql的endpoints:
apiVersion: v1
kind: Endpoints
metadata:
  name: mysql
subsets:
- addresses:
  - ip: 192.168.40.181
  ports:
  - port: 3306  
查看mysql svc详细信息
root@k8s01:/k8s/mysql# kubectl get endpoints
NAME            ENDPOINTS             AGE
kubernetes      192.168.40.180:6443   5d4h
mysql           192.168.40.181:3306   8s
nginx-service                   5d4h
root@k8s01:/k8s/mysql# kubectl describe svc mysql 
Name:              mysql
Namespace:         default
Labels:            
Annotations:       
Selector:          
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.97.247.132
IPs:               10.97.247.132
Port:                3306/TCP
TargetPort:        3306/TCP
Endpoints:         192.168.40.181:3306
Session Affinity:  None
Events:            

你可能感兴趣的:(k8s service 资源)