Kubernetes-服务发布

#发布服务有三种方式,第一种把端口映射到所有节点比较适合发布一些tcp/udp的服务对于应用层像http的会导致无法获取到原始客户端IP,第二种需要用到云服务才行,第三种是ingress http服务发布
#这里介绍的是NGINX: 0.9.0-beta.13版本的
#https://github.com/kubernetes/ingress/releases下载
#解压进入
cd ingress/examples/deployment/nginx/
#默认后端404服务,可自行在集群内部搭建一个404服务器然后发布并修改nginx-ingress-controller.yaml的--default-backend-service参数
kubectl apply -f default-backend.yaml
#ingress控制器,控制器可以数量为多个,这样可以用来做外部负载均衡,通过kubectl的标签限制后可以固定为指定的几台节点
kubectl apply -f nginx-ingress-controller.yaml
#创建ingress
#这里用上次搭建的Kubernetes监控工具为列子,上次搭建好后服务名为monitoring-grafana并开通了80端口,分组位于kube-system
#ingress.kubernetes.io/ssl-redirect: "false" 默认为开启tls后则关闭http,http请求自动转到https
#导入证书,这里的证书自行创建,创建好后导入,证书运营商提供的证书需要注意只保留BEGIN CERTIFICATE的内容
#证书即使导入成功了,但是访问站点还是用的默认证书代表证书里面的格式可能有问题,我这里将BEGIN CERTIFICATE REQUEST去除只保留BEGIN CERTIFICATE后才成功
kubectl create secret tls grafana-secret --cert key/grafana.crt --key key/grafana.key
#多个不同的Ingress控制器如同时有nginx和gce需要在ingress里指定使用的哪个控制器,否则直接无视kubernetes.io/ingress.class: "nginx"
#Ingress可以有多个,可以根据不同的项目来划分,这样避免更新Ingress遇到错误后导致全部项目都无法访问,Ingress是Ingress控制器的规则
cat << EOF >bbs-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: grafana-ingress
  namespace: kube-system
  annotations:
    ingress.kubernetes.io/ssl-redirect: "false"
spec:
  tls:
    - hosts:
      - grafana2.test.com
      secretName: grafana-secret
    - hosts:
      - grafana2.test.com
      secretName: grafana-secret
  rules:
  - host: grafana1.test.com
    http:
      paths:
      - backend:
          serviceName: monitoring-grafana
          servicePort: 80
  - host: grafana2.test.com
    http:
      paths:
      - backend:
          serviceName: monitoring-grafana
          servicePort: 80
EOF

kubect create -f bbs-ingress.yaml
#创建后访问,注意修改hosts或添加dns解析
http://grafana1.test.com
http://grafana2.test.com
https://grafana1.test.com
https://grafana2.test.com

#创建内部集群
#内部集群只能集群内部访问,控制节点无法访问,只能是代理节点访问,主要用于内部服务的调用
#clusterIP: 169.169.0.123可选,默认不配置clusterIP则随机IP,配置为None则不配置IP,
#配置为None主要用于利用Service监控pod ip,开发自己的负载均衡工具
apiVersion: v1
kind: Service
metadata:
  name: testapp
spec:
  ports:
  - port: 7555
    targetPort: 7523
    name: testudp
    protocol: UDP
  - port: 80
    targetPort: 80
    name: nginx
    protocol: TCP
  #添加后则为指定的IP
  #clusterIP: 169.169.0.123
  selector:
    app: webapp
#解释,根据标签找到对应的pod,然后代理指定的容器端口,外部通过对外的端口来访问
metadata:
  name: 显示的名字
- port: 对外的端口
    targetPort: 容器的端口
    name: 自定义的名字
    protocol: TCP/UDP
clusterIP: IP或None,也可不添加该参数则为随机IP
selector:
    pod的标签
    
#端口绑定,
#将绑定的端口在所有代理节点都开启占用
apiVersion: v1
kind: Service
metadata:
  name: testapp
spec:
  ports:
  - hostPort: 7555
    name: testudp
    protocol: UDP
  - containerPort: 8088
    hostPort: 80
    name: nginx
    protocol: TCP
  #添加后则为指定的IP
  #clusterIP: 169.169.0.123
  selector:
    app: webapp
#解释,根据标签找到对应的pod,然后代理指定的容器端口,外部通过对外的端口来访问
metadata:
  name: 显示的名字
- hostPort: 没有containerPort参数则代表容器端口和hostPort对外发布的端口一致
    name: 自定义名字
    protocol: TCP/UDP

- containerPort: 容器端口
  hostPort: 对外发布的端口
  name: 自定义名字
  protocol: TCP/UDP
 
clusterIP: IP或Node,也可不添加该参数则为随机IP
selector:
    pod的标签

posted @ 2018-03-16 14:26  IT菜鸟园  阅读(556)  评论(0编辑  收藏  举报