Kubernetes部署服务

橙三吉。 提交于 2020-04-07 07:24:12

基本流程为:

  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棒棒哒!!! ;-) 负载均衡棒棒哒!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!