Kubernetes部署服务 - ReplicationController多副本负载均衡实战

基本流程为:

  1. 做image 并且push到private registry
  2. 创建replication controller
  3. 创建service
  4. 搭建nginx代理
  5. 测试 下面详述:

1. 制作image

java基础image:

FROM dockerimages.yinnut.com:15043/centos:7
MAINTAINER xuelun-infra morgan.wu@yinnnut.com
 
 
ADD jdk-8u60-linux-x64.tar.gz /usr/local/
ENV JAVA_HOME /usr/local/jdk1.8.0_60
ENV PATH $JAVA_HOME/bin:$PATH

Friend-Service镜像:

FROM dockerimages.yinnut.com:15043/yinnut-java:0.1
MAINTAINER xuelun-infra morgan.wu@yinnnut.com
 
ADD FriendService.war /
ADD jetty-runner.jar /
WORKDIR /
VOLUME ["/var/log"]

2. 创建Replication Controller

rc.yaml文件, 创建了2个friend的pod,设置replicas为2

apiVersion: v1
kind: ReplicationController
metadata:
  name: friend-service
spec:
  replicas: 2
  template:
    metadata:
      labels:
        yinnut-svc: "friend"
        version: "0.1"
    spec:
      containers:
      - name: friend-service
        image: dockerimages.yinnut.com:15043/friend-service:0.1
        ports:
        - containerPort: 9999
          protocol: TCP
        command: ["java"]
        args: ["-jar","jetty-runner.jar","--port","9999","--log","jetty.log","FriendService.war"]

##3. 创建Service## svc.yaml文件, 监听本地物理机的 127.0.0.1:30001端口

apiVersion: v1
kind: Service
metadata:
  name: friendsvc
  labels:
    yinnut-svc: friend
spec:
  type: NodePort
  ports:
  - port: 9999
    protocol: TCP
    targetPort: 9999
    name: http
    nodePort: 30001
  selector:
    yinnut-svc: friend

##4. 搭建Nginx反向代理## 安装:

yum install -y http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx -y

配置 /etc/nginx/conf.d/default.conf

server {
    listen       30000;
    server_name  localhost;
 
    location / {
         port_in_redirect on;
         proxy_pass http://127.0.0.1:30001;
         proxy_redirect  off;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

设置worker数量为10,并且把30000端口添加到防火墙例外。 访问 http://192.168.1.221:30000端口即可。

##5. 测试## 可以看到有两个Friend的Pod

[root@centos7-node-221 ~]$ kubectl get po
NAME                   READY     STATUS                                                      RESTARTS   AGE
busybox                1/1       Running                                                     296        12d
friend-service-38riq   1/1       Running                                                     0          6h
friend-service-nn0qt   1/1       Running                                                     0          11m

发送4个请求。其实我发送了上千个请求,怕这里写不下了。

[root@centos7-node-221 ~]$ for i in {1..4}; do time curl http://192.168.1.221:30000/user/1/friend; done
{"rc":0,"val":[{"friendUserId":2,"friendUserName":"who","close":false}]}
real 0m0.029s
user 0m0.004s
sys  0m0.003s
{"rc":0,"val":[{"friendUserId":2,"friendUserName":"who","close":false}]}
real 0m0.039s
user 0m0.001s
sys  0m0.004s
{"rc":0,"val":[{"friendUserId":2,"friendUserName":"who","close":false}]}
real 0m0.028s
user 0m0.002s
sys  0m0.004s
{"rc":0,"val":[{"friendUserId":2,"friendUserName":"who","close":false}]}
real 0m0.035s
user 0m0.003s
sys  0m0.002s

2个Pod分别2个Response,多副本的RR负载均衡工作的很棒。

# 第一个Pod
172.16.91.0 - - [11/Oct/2015:12:33:51 +0000] "GET //192.168.1.221/user/1/friend HTTP/1.0" 200 72
172.16.91.0 - - [11/Oct/2015:12:33:51 +0000] "GET //192.168.1.221/user/1/friend HTTP/1.0" 200 72
 
 
# 和第二个Pod
 
172.16.91.1 - - [11/Oct/2015:12:33:51 +0000] "GET //192.168.1.221/user/1/friend HTTP/1.0" 200 72
172.16.91.1 - - [11/Oct/2015:12:33:51 +0000] "GET //192.168.1.221/user/1/friend HTTP/1.0" 200 72

6. 总结##

Kubernetes棒棒哒!!! ;-) 负载均衡棒棒哒!

转载于:https://my.oschina.net/xue777hua/blog/516038

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值