Docker Kubernetes Service 代理服务创建

Docker Kubernetes  Service 代理服务创建

创建Service需要提前创建好pod容器。再创建Service时需要指定Pod标签,它会提供一个暴露端口默会分配容器内网访问的唯一IP地址。

环境:

  • 系统:Centos 7.4 x64
  • Docker版本:18.09.0
  • Kubernetes版本:v1.8
  • 管理节点:192.168.1.79
  • 工作节点:192.168.1.78
  • 工作节点:192.168.1.77

一、通过deployment创建pod

1、创建yaml文件

vim nginx-deployment.yaml

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.12
        ports:
        - containerPort: 80
# 指定api版本
apiVersion: apps/v1beta2
# 指定需要创建的资源对象
kind: Deployment
# 源数据、可以写name,命名空间,对象标签
metadata:
# 指定对象名称
  name: nginx-deployment
# 描述资源相关信息
spec:
# 指定副本数
  replicas: 3
# 资源标签选择器
  selector:
# 匹配标签字段
    matchLabels:
# 标签名
      app: nginx
# 描述资源具体信息
  template:
# 源数据、可以写name,命名空间,对象标签
    metadata:
# 指定标签
      labels:
# 标签名
        app: nginx
# 描述资源相关信息
    spec:
# 容器管理
      containers:
# 容器名称
      - name: nginx
# 镜像名称
        image: nginx:1.12
# 端口管理
        ports:
# 指定暴露容器端口
        - containerPort: 80
文件注解

2、创建deployment

kubectl create -f nginx-deployment.yaml

二、创建Service NodePort代理服务

1、创建yaml文件

vim service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - name: http
    protocol: TCP
    port: 888
    targetPort: 80
    nodePort: 30001
  type: NodePort
  clusterIP: "10.10.10.11"
# api版本
apiVersion: v1
# 创建对象类型
kind: Service
# 保存源数据信息
metadata:
# service名称
  name: nginx-service
# 具体service内容
spec:
# 指定标签
  selector:
# 标签要与指定的pod便签相同
    app: nginx
# 开放指定端口,可写多个
  ports:
# port的指定名称
  - name: http
# 负载均衡默认协议为TCP
    protocol: TCP
# 暴露的service端口
    port: 888
# 容器端口
    targetPort: 80
# 固定容器内网唯一IP
  clusterIP: "10.10.10.11"
# node节点创建socker的暴露端口,默认30000~32767
    nodePort: 30001
# 服务类型
  type: NodePort
文件注解

2、创建service

kubectl create -f service.yaml 
命令:kubectl get service
NAME               TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
nginx-service2     NodePort    10.10.10.11    <none>        888:30001/TCP   1m
查看service
命令:kubectl describe service nginx-service

Name:                     nginx-service
Namespace:                default
Labels:                   app=nginx
Annotations:              <none>
Selector:                 app=nginx
Type:                     NodePort
IP:                       10.10.10.11
Port:                     http  888/TCP
TargetPort:               80/TCP
NodePort:                 http  30001/TCP
Endpoints:                172.17.1.2:80,172.17.1.3:80,172.17.1.4:80 + 9 more...
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
查看service详细描述信息
命令:netstat -lnpt | grep 30001

tcp6       0      0 :::30001                :::*                    LISTEN      74604/kube-proxy   
查看NODE节点暴露端口
指定代理serviceIP它会默认指定一个IP地址段
配置文件:/opt/kubernetes/cfg/kube-apiserver
# 集群分配的IP范围
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.10.10.0/24"
注:不要分配到与当前网络冲突的网段
固定集群IP地址范围

注:主配置文件内可修改默认nodeport端口范围 配置文件添加参数--service-node-port-range。 

3、测试访问

原文地址:https://www.cnblogs.com/xiangsikai/p/10019012.html