【运维知识大神篇】运维界的超神器Kubernetes教程4,事已至此,来一波实战吧!(整合项目进镜像+打包项目成镜像+将Docker项目上K8s集群)

发布时间:2024年01月16日

本篇文章给大家分享实战项目,干货满满,速来学习!

目录

实战1:整合项目进镜像

实战2:打包项目成镜像,将Docker项目上K8s集群


实战1:整合项目进镜像

实战项目需求

将"斗地主","电风扇","坦克大战"游戏合并到"harbor.koten.com/koten-games/games:v0.5"中,要求如下:
1、使用configMap资源存储配置文件并挂载;
2、尽可能的将镜像压缩到300M以下,体积越小越好;
3、合并后打包为harbor.koten.com/koten-games/games:v0.6,上传至私有harbor

编写configMap资源并运行

[root@Master231 configMap]# cat 03-cm-game.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: games-conf
# 指定cm的数据
data:
  games-conf: |
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/bird/;
      server_name   game01.koten.com;
    }
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/pinshu/;
      server_name   game02.koten.com;
    }
  
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/tanke/;
      server_name   game03.koten.com;
    }
  
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/chengbao/;
      server_name   game04.koten.com;
    }
  
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/motuo/;
      server_name   game05.koten.com;
    }
  
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/liferestart/;
      server_name   game06.koten.com;
    }
  
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/huangjinkuanggong/;
      server_name   game07.koten.com;
    }  
   
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/feijidazhan/;
      server_name   game08.koten.com;
    }
  
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/zhiwudazhanjiangshi/;
      server_name   game09.koten.com;
    }
  
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/xiaobawang/;
      server_name   game10.koten.com;
    }
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/pingtai/;
      server_name   game11.koten.com;
    }
  
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/dayu/;
      server_name   game12.koten.com;
    }
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/maliao/;
      server_name   game13.koten.com;
    }
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/menghuanmonizhan/;
      server_name   game14.koten.com;
    }
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/qieshuiguo/;
      server_name   game15.koten.com;
    }
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/wangzhezhicheng/;
      server_name   game16.koten.com;
    }
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/zhiwuVSjiangshi/;
      server_name   game17.koten.com;
    }
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/killbird/;
      server_name   game18.koten.com;
    }
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/dianfengshan/;
      server_name   game19.koten.com;
    }
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/doudizhu/;
      server_name   game20.koten.com;
    }
    server {
      listen        0.0.0.0:80;
      root          /usr/local/nginx/html/tankedazhan/;
      server_name   game21.koten.com;
    }
[root@Master231 configMap]# kubectl apply -f 03-cm-game.yaml
configmap/games-conf created

编写资源清单并运行

[root@Master231 pod]# cat 19-pods-games-cm4.yaml
apiVersion: v1
kind: Pod
metadata:
  name: koten-games-004
spec:
  nodeName: worker232
  volumes:
  - name: data
    configMap: 
      name: games-conf
      items:
      - key: games-conf
        path: games.conf
  containers:
  - name: games
    image: harbor.koten.com/koten-games/games:v0.5
    volumeMounts:
    - name: data
      mountPath: /etc/nginx/conf.d
[root@Master231 pod]# kubectl apply -f 19-pods-games-cm4.yaml 
pod/koten-games-004 created
[root@Master231 pod]# kubectl get pods -o wide
NAME              READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
koten-games-004   1/1     Running   0          17s   10.100.1.58   worker232   <none>           <none>

拷贝代码目录进容器中

[root@Master231 pod]# kubectl cp doudizhu koten-games-004:/usr/local/nginx/html/ 
[root@Master231 pod]# kubectl cp tankedazhan koten-games-004:/usr/local/nginx/html/ 
[root@Master231 pod]# kubectl cp dianfengshan koten-games-004:/usr/local/nginx/html/ 

测试能否正常访问

[root@Master231 pod]# curl -sH 'host:game19.koten.com' 10.100.1.58|grep 风扇
              <div class="logo">风扇</div>

[root@Master231 pod]# curl -sH 'host:game20.koten.com' 10.100.1.58
<!DOCTYPE html><html><head><meta charset=utf-8><meta http-equiv=X-UA-Compatible content="IE=edge"><meta name=viewport content="width=device-width,initial-scale=1"><title>Vue App</title><link href=css/app.9b54c2ac.css rel=preload as=style><link href=js/app.b948232d.js rel=preload as=script><link href=js/chunk-vendors.fc173c6b.js rel=preload as=script><link href=css/app.9b54c2ac.css rel=stylesheet></head><body><div id=app></div><script src=js/chunk-vendors.fc173c6b.js></script><script src=js/app.b948232d.js></script></body></html>

[root@Master231 pod]# curl -sH 'host:game21.koten.com' 10.100.1.58|grep tank
		<script src="js/tank.js"></script>
					<canvas id="tankCanvas" ></canvas>

进入镜像中清空下下nginx日志,减少镜像体积,可做可不做,减少不了多少也

[root@Master231 configMap]# kubectl exec -it koten-games-004 -- sh
/ # > /var/log/nginx/access.log
/ # > /var/log/nginx/error.log 

打包镜像并推送至harbor,发现为295MB,也算是完成任务了

[root@Worker232 ~]# docker commit 7fc657d0608c harbor.koten.com/koten-games/games:v0.6
sha256:30ec3e6e47473e8992c92e1b86cd03191fe9c9dee7e88982bda69df623f61843
[root@Worker232 ~]# docker images |grep v0.6
harbor.koten.com/koten-games/games                   v0.6            30ec3e6e4747   2 seconds ago   295MB
[root@Worker232 ~]# docker push harbor.koten.com/koten-games/games:v0.6

运行镜像,windows进行hosts解析,浏览器访问是否正常

[root@Master231 pod]# cat 20-pods-games-cm5.yaml
apiVersion: v1
kind: Pod
metadata:
  name: koten-games-005
spec:
  nodeName: worker232
  volumes:
  - name: data
    configMap: 
      name: games-conf
      items:
      - key: games-conf
        path: games.conf
  containers:
  - name: games
    image: harbor.koten.com/koten-games/games:v0.6
    ports:
    - containerPort: 80
      hostPort: 80
    volumeMounts:
    - name: data
      mountPath: /etc/nginx/conf.d
[root@Master231 pod]# kubectl apply -f 20-pods-games-cm5.yaml
pod/koten-games-005 created

实战2:打包项目成镜像,将Docker项目上K8s集群

实战项目需求

项目地址:https://github.com/luozhihao/vue-puzzle/tree/master

1、将github的拼图游戏下载到Linux,打包成docker镜像
2、将docker镜像部署到k8s集群
3、要求用到Pod和configMap,映射到本机的88端口

打包项目成镜像

[root@Worker232 ~]# docker run -id harbor.koten.com/koten-web/nginx:1.24.0-alpine
9d3f2ad7395a211cf155b54f190a95a7c2ae3018b1bfa17deb58ad613bdb9d86
[root@Worker232 ~]# docker ps -l
CONTAINER ID   IMAGE                                            COMMAND                  CREATED         STATUS         PORTS     NAMES
9d3f2ad7395a   harbor.koten.com/koten-web/nginx:1.24.0-alpine   "/docker-entrypoint.…"   4 seconds ago   Up 3 seconds   80/tcp    affectionate_ellis

[root@Worker232 ~]# docker cp vue-puzzle-master 9d3f2ad7395:/usr/share/nginx/html
[root@Worker232 ~]# docker exec -it 9d3f2ad7395 sh
......    # 将代码目录整理成如下所示
/usr/share/nginx/html # ls
README.md          index.html         src
dist               package.json       webpack.config.js

# 打包成镜像推送至harbor仓库
[root@Worker232 ~]# docker commit 9d3f2ad7395 harbor.koten.com/koten-games/game_number:v1.0
[root@Worker232 ~]# docker push harbor.koten.com/koten-games/game_number:v1.0

将docker项目上k8s集群,编写资源清单

[root@Master231 pod]# cat 23-pods-game.yaml
apiVersion: v1
kind: Pod
metadata:
  name: game-number-001
spec:
  nodeName: worker232
  hostNetwork: true
  containers:
  - name: web
    image: harbor.koten.com/koten-games/game_number:v1.0

---

apiVersion: v1
kind: Pod
metadata:
  name: game-number-002
spec:
  nodeName: worker233
  hostNetwork: true
  containers:
  - name: web
    image: harbor.koten.com/koten-games/game_number:v1.0

运行资源清单并查看效果

[root@Master231 pod]# kubectl apply -f 23-pods-game.yaml
pod/game-number-001 created
pod/game-number-002 created
[root@Master231 pod]# kubectl get pods -o wide
NAME              READY   STATUS    RESTARTS   AGE   IP           NODE        NOMINATED NODE   READINESS GATES
game-number-001   1/1     Running   0          62s   10.0.0.232   worker232   <none>           <none>
game-number-002   1/1     Running   0          62s   10.0.0.233   worker233   <none>           <none>

当然这样起不到集群效果,因客户访问只能在一个IP上去访问,所以我们还需要做负载均衡去进行流量的分发处理,后面文章会介绍。

还需要使用configMap进行端口映射

[root@Master231 configMap]# cat 04-cm-game.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: default.conf
# 指定cm的数据
data:
  game-conf: |
    server {
      listen        0.0.0.0:88;
      root          /usr/share/nginx/html;
      server_name   game.koten.com;
    }
[root@Master231 configMap]# kubectl apply -f 04-cm-game.yaml
configmap/default.conf created

修改资源清单

[root@Master231 pod]# cat 23-pods-game.yaml
apiVersion: v1
kind: Pod
metadata:
  name: game-number-001
spec:
  volumes:
  - name: data
    configMap: 
      name: default.conf
      items:
      - key: game-conf
        path: game.conf
  nodeName: worker232
  hostNetwork: true
  containers:
  - name: web
    image: harbor.koten.com/koten-games/game_number:v1.0
    volumeMounts:
    - name: data
      mountPath: /etc/nginx/conf.d
---
apiVersion: v1
kind: Pod
metadata:
  name: game-number-002
spec:
  volumes:
  - name: data
    configMap: 
      name: default.conf
      items:
      - key: game-conf
        path: game.conf
  nodeName: worker233
  hostNetwork: true
  containers:
  - name: web
    image: harbor.koten.com/koten-games/game_number:v1.0
    volumeMounts:
    - name: data
      mountPath: /etc/nginx/conf.d

运行资源清单并查看效果

[root@Master231 pod]# kubectl apply -f 23-pods-game.yaml 
pod/game-number-001 created
pod/game-number-002 created
[root@Master231 pod]# kubectl get pods
NAME              READY   STATUS    RESTARTS   AGE
game-number-001   1/1     Running   0          4s
game-number-002   1/1     Running   0          4s

端口成功修改,访问没有问题。


我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!

文章来源:https://blog.csdn.net/qq_37510195/article/details/131262880
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。