1. 程式人生 > >容器基礎(十): 使用kubernetes部署應用

容器基礎(十): 使用kubernetes部署應用

訪問 receive restart 文件 ip地址 pre 進入 control vip

概述

使用之前的腳本(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 

容器基礎(十): 使用kubernetes部署應用