zl程序教程

您现在的位置是:首页 >  系统

当前栏目

Centos7 k8s 容器的网络访问service

centos7k8s网络容器 访问 Service
2023-09-14 09:01:13 时间

一、k8s Service基本情况
  1、概念:为了适应快速的业务需求,微服务架构已经逐渐成为主流,微服务架构的应用需要有非常好的服务编排支持。Kubernetes中的核心要素service便提供了一套简化的服务代理和发现机制,天然适应服务架构。

  2、原理:在kubernetes中,在受到RC调控的时候,Pod副本是变化的,对于的虚拟IP也是变化的,比如发生迁移或者伸缩的时候。这对于Pod的访问者来说是不可接受的。Kubrnetes中的Service是一种抽象概念,它定义了一个Pod逻辑集合以及访问他们的策略,Service同Pod的关联同样是基于Label来完成的。Service的目标是提供一种桥梁,它会为访问者提供一个固定访问地址,用于在访问时重定向到相应的后端,这使得非Kubernetes原生应用程序,在无须为Kubernetes编写特定代码的前提下,轻松访问后端。

 二、配置案例

  1、编写配置文件

[root@k8s-master ~]# vim nginx-sv.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30001
  selector:
    app: myweb

  2、创建

[root@k8s-master ~]# kubectl create -f nginx-sv.yaml 
service "nginx" created
[root@k8s-master ~]# kubectl get svc 
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 4d
nginx 10.254.101.134 <nodes> 80:30001/TCP 21m
[root@k8s-master ~]# kubectl describe svc  nginx
Name:            nginx
Namespace:        default
Labels:            <none>
Selector:        app=myweb
Type:            NodePort
IP:            10.254.101.134
Port:            <unset>    80/TCP
NodePort:        <unset>    30001/TCP
Endpoints:        <none>
Session Affinity:    None
No events.
[root@k8s-master ~]# 

   3、修改svc配置文件

[root@k8s-master ~]# kubectl edit svc

 

  4、 访问容器

 

 

   5、自动发现功能

[root@k8s-master ~]# kubectl edit deployment nginx

 将副本修改为5

 自动把新创建的容器加入。

  6、进入容器

[root@k8s-master ~]# kubectl exec -it nginx-deployment-3637952344-9hjtw /bin/bash