概述
使用之前的脚本(env/server.py 得到 env/server:v0.1 镜像, env/worker.py 得到 env/worker:v0.1)得到的镜像,在部署好kubernetes的机器上进行测试。kubernetes-v1.12.2当前只部署在master上,且master的Taint已经被删除!基本的做法是:
1. 配置一个server-rc.yml, 用于创建server端,全局只配置一个replicas; 2. 配置一个server-svc.yml, 与server-rc.yml进行关联, 为server端分配VIP; 3. 配置一个worker-rc.yml, 用于创建客户端, 可配置多个replicas, 用于和server进行通信;
YAML脚本
1 ➜ k8s cat server-rc.yml 2 apiVersion: v1 3 kind: ReplicationController 4 metadata: 5 name: server 6 spec: 7 replicas: 1 8 selector: 9 app: server 10 template: 11 metadata: 12 labels: 13 app: server 14 spec: 15 containers: 16 - name: server 17 image: env/server:v0.1 18 ports: 19 - containerPort: 3000 20 env: 21 - name: APP_PORT 22 value: "3000" 23 ➜ k8s
1 ➜ k8s cat server-svc.yml 2 apiVersion: v1 3 kind: Service 4 metadata: 5 name: server 6 spec: 7 ports: 8 - port: 3000 9 selector: 10 app: server 11 ➜ k8s
1 ➜ k8s cat worker-rc.yml 2 apiVersion: v1 3 kind: ReplicationController 4 metadata: 5 name: worker 6 spec: 7 replicas: 2 8 selector: 9 app: worker 10 template: 11 metadata: 12 labels: 13 app: worker 14 spec: 15 containers: 16 - name: worker 17 image: env/worker:v0.1 18 ports: 19 - containerPort: 3000 20 env: 21 - name: APP_DOMAIN 22 value: "server" # 传入service名字,后续脚本通过该名字从k8s获取到VIP 23 - name: APP_PORT 24 value: "3000" 25 ➜ k8s
部署验证
根据yaml文件创建rc/svc
1 # create server-rc 2 ➜ k8s kubectl create -f server-rc.yml 3 replicationcontroller/server created 4 ➜ k8s kubectl get rc 5 NAME DESIRED CURRENT READY AGE 6 server 1 1 1 15s 7 8 # create service 9 ➜ k8s kubectl create -f server-svc.yml 10 service/server created 11 ➜ k8s 12 ➜ ~ kubectl get svc 13 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE 14 kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 24h 15 server ClusterIP 10.103.92.121 <none> 3000/TCP 38s 16 ➜ ~ 17 18 # create worker-rc 19 ➜ k8s kubectl create -f worker-rc.yml 20 replicationcontroller/worker created 21 ➜ k8s kubectl get rc 22 NAME DESIRED CURRENT READY AGE 23 server 1 1 1 14m 24 worker 2 2 2 10s 25 ➜ k8s kubectl get pods 26 NAME READY STATUS RESTARTS AGE 27 server-x67sw 1/1 Running 0 14m 28 worker-45qjt 1/1 Running 0 15s 29 worker-gvwlt 1/1 Running 0 15s 30 ➜ k8s
通过exec进入server/worker容器查看日志, 可以看到连接已经成功:
1 ➜ k8s docker ps | grep python | awk '{print $1, $3 $4}' 2 7b96a767637c "pythonworker.py" 3 fb45f45be871 "pythonworker.py" 4 e217df0ca52d "pythonserver.py" 5 ➜ k8s docker exec -it e217df0ca52d /bin/bash 6 root@server-x67sw:/env/server# ps -ef 7 UID PID PPID C STIME TTY TIME CMD 8 root 1 0 0 15:39 ? 00:00:00 python server.py 9 root 7 0 0 15:40 pts/0 00:00:00 /bin/bash 10 root 12 7 0 15:40 pts/0 00:00:00 ps -ef 11 root@server-x67sw:/env/server# ls 12 Dockerfile log server.py 13 root@server-x67sw:/env/server# tail -f log/server.log 14 2018-12-06 15:39:56 [INFO] Waiting for connection... 15 2018-12-06 15:54:06 [INFO] ('10.32.0.9', 51266) connected 16 2018-12-06 15:54:06 [INFO] ('10.32.0.10', 47969) connected 17 ^C 18 root@server-x67sw:/env/server# exit 19 exit 20 ➜ k8s docker exec -it 7b96a767637c /bin/bash 21 root@worker-gvwlt:/env/worker# ps -ef 22 UID PID PPID C STIME TTY TIME CMD 23 root 1 0 0 15:54 ? 00:00:00 python worker.py 24 root 6 0 0 15:57 pts/0 00:00:00 /bin/bash 25 root 11 6 0 15:57 pts/0 00:00:00 ps -ef 26 root@worker-gvwlt:/env/worker# tail -f log/worker.log 27 2018-12-06 15:56:31 [INFO] [recv] hello, docker! 28 ^C 29 root@worker-gvwlt:/env/worker# exit 30 exit 31 ➜ k8s
在宿主机直接使用VIP访问server成功(使用original/worker进行测试, ip地址通过配置文件读入):
1 ➜ worker cat ini/config.ini | grep ip 2 ip = 10.103.92.121 3 ➜ worker python worker.py 4 [+][recv] hello, docker! 5 [+][recv] hello, docker! 6 ^C 7 [-] signal(2) received, exit! 8 ➜ worker