Kubernetes怎么部署ReplicationController多副本负载均衡

这篇文章主要讲解了“Kubernetes怎么部署ReplicationController多副本负载均衡”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Kubernetes怎么部署ReplicationController多副本负载均衡”吧!

红古网站建设公司创新互联,红古网站设计制作,有大型网站制作公司丰富经验。已为红古上千余家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的红古做网站的公司定做!

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

感谢各位的阅读,以上就是“Kubernetes怎么部署ReplicationController多副本负载均衡”的内容了,经过本文的学习后,相信大家对Kubernetes怎么部署ReplicationController多副本负载均衡这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是创新互联,小编将为大家推送更多相关知识点的文章,欢迎关注!


网站题目:Kubernetes怎么部署ReplicationController多副本负载均衡
文章来源:http://scyanting.com/article/peoeop.html