k8s static cluster on-line web sites

 

Preparing the Environment

 

The deployment of a node, a master node, there are two nodes node1, node2

Intact k8s cluster environment

 

1 idea:

On node1 and node2 node between the host site and the container line static (or dynamic) and the directory mapping port mapping then maps directory made hostpath, shared by nfs out.

 

Thinking two:

On node1 node2 node storage directory and static resources into apache root containers implemented on site by copying the line static host, and by the deployment of the rear end static website static port access node nodeport.

 

 Here I demonstrate the second method:

 

Node deployment operations:

1) layout files httpd.yml

Container port is 80, the number of copies is three.

Here because I was on the line apache service httpd so use a mirror, of course, also possible to use nginx or tomcat, but it should be noted that each of the root directory is not the same.

 

2) then execute the following command to create the deployment, generate pod.

kubectl  apply -f  httpd.yml

 

View pod status

[root@dlp yml]# kubectl get pod -o wide
NAME                                READY     STATUS        RESTARTS   AGE       IP               NODE
httpd-deployment-784d567c4d-2fxb2   1/1       Running       0          10m       172.20.104.29    192.168.253.22
httpd-deployment-784d567c4d-bl4zk   1/1       Running       0          44m       172.20.166.163   192.168.253.21
httpd-deployment-784d567c4d-pdkhh   1/1       Running       0          45m       172.20.104.28    192.168.253.22

 

node node:

3), respectively, for which the container 22 and the node 21 according to the name of the pod.

 

 

 

4) Create a directory / usr / local / apache2 / htdocs, then upload a static resource package, upload a small aircraft of World War I game here. # Here we need to know is that this is the httpd root directory of the container.

[root@node1 htdocs]# ls
game.zip
[root @ node1 htdocs] # unzip game.zip 

unzip like this
[root @ node1 htdocs] # LS
Game jQuery-1.8.3.min.js readme.xls  sky_fight.html   sky.php

 

5) All the above documents are copied to the 3) step inside the container / usr / local / apache2 / htdocs directory.

[root@node2 apache2]# docker cp htdocs/ 9b20bb037dd8:/usr/local/apache2/htdocs

 


6) into the container / usr / local / apache2 / htdocs directory, view the file if the copy is successful, and the original index, html file or remove it.

The content of this file is accessed. ----- "work


[root@node2 apache2]# docker exec -it 9b20bb037dd8 /bin/bash

root@httpd-deployment-784d567c4d-2fxb2:/usr/local/apache2# ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  logs    modules
root@httpd-deployment-784d567c4d-2fxb2:/usr/local/apache2# cd htdocs/
root@httpd-deployment-784d567c4d-2fxb2:/usr/local/apache2/htdocs# ls
htdocs    index.html
tml @httpd-deployment-784d567c4d-2fxb2:/usr/local/apache2/htdocs# rm -rf index.ht
root@httpd-deployment-784d567c4d-2fxb2:/usr/local/apache2/htdocs# cd htdocs/
root@httpd-deployment-784d567c4d-2fxb2:/usr/local/apache2/htdocs/htdocs# ls
game      images           readme.xls  sky_fight.html
game.zip  jquery-1.8.3.min.js  sky.php

 

 7) 以上步骤成功后又回到部署节点编写一个service的编排文件。

 

 

 节点的静态端口设置为26055,service端口为8080.注意框内的run: httpd。要与1)步骤的编排文件的label name对应。

 

 8)启动编排文件,生成service。

kubectl  apply -f  svc.yml

 

9)查看service的状态

[root@dlp yml]# kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE       SELECTOR
httpd-svc    NodePort    10.68.90.148   <none>        8080:26055/TCP   22h       run=httpd
kubernetes   ClusterIP   10.68.0.1      <none>        443/TCP          1d        <none>
nginx-svc2   NodePort    10.68.26.208   <none>        8081:30001/TCP   21h       run=nginx

 

 10)在不属于k8s集群的任意主机上访问。格式为nodeip + nodeport  。例如:

 

因为是静态网站,所以我们去web浏览器上访问效果更好。

 

 

 

11) 但是这样只是做了一个节点的可用,我们要想将21节点加入服务端。需要将/usr/local/apache/htdocs目录当成nfs的共享目录挂载到其他节点上。并且通过将此目录下的资源拷贝到节点容器内,而不是通过目录映射,为什么呢?

因为通过目录映射的方式会重新生成一个容器,而不是1)步骤通过编排文件生成的pod容器。这样通过nodeport就无法访问静态资源而是通过宿主机映射的端口来访问。

 

12)在21节点上执行3、4、5、6步骤,然后访问浏览器,显示效果如下:

 

 

 

 可以看到集群的节点都实现了静态网站上线,如果集群内的pod down掉k8s集群会根据副本数自动生成新的pod,实现高可用。

Guess you like

Origin www.cnblogs.com/zzzynx/p/11137755.html