在学习了那么多k8s的知识之后,搭建一个后端系统进行复习

需求:可访问的后端系统

Namespace创建

使用命令kubectl create namespace test

[root@kubernetes container]# kubectl create namespace test
namespace/test created

部署Golang程序

1. 拉取代码

使用命令git clone https://gitee.com/chensyf/container.git拉取golang代码

[root@kubernetes ~]# git clone https://gitee.com/chensyf/container.git
Cloning into 'container'...
remote: Enumerating objects: 124, done.
remote: Counting objects: 100% (124/124), done.
remote: Compressing objects: 100% (102/102), done.
remote: Total 124 (delta 36), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (124/124), 42.09 KiB | 0 bytes/s, done.
Resolving deltas: 100% (36/36), done.

2. 修改配置文件

使用命令vi container/config/config.ini修改Mysql地址

[mysql]
Type  = mysql
User = container
Password = goodsang123
Host = 10.10.10.112:3306  
Name = container
TablePrefix = container_
MaxIdleConn = 10
MaxOpenConn = 100

3. 制作镜像并上传到镜像仓库

这里以阿里云为例,同时,若是私人仓库,请配置Secret,这里为了方便,直接将访问权限改为公开

  1. 进入container文件夹下,使用命令docker build -f Dockerfile -t container .将源码制作为镜像
  2. 根据阿里云的上传镜像步骤进行操作,以自己的为准
[root@kubernetes container]# sudo docker login --username=xxx registry.cn-hangzhou.aliyuncs.com
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded

[root@kubernetes container]# sudo docker tag f2029d95ae66 registry.cn-hangzhou.aliyuncs.com/chensyf/container:latest
[root@kubernetes container]# docker images
REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE
coantiner                                              latest              f2029d95ae66        6 minutes ago       643MB
registry.cn-hangzhou.aliyuncs.com/chensyf/container    latest              f2029d95ae66        6 minutes ago       643MB

[root@kubernetes container]# sudo docker push registry.cn-hangzhou.aliyuncs.com/chensyf/container:latest
The push refers to repository [registry.cn-hangzhou.aliyuncs.com/chensyf/container]
1de6a295ff21: Pushed 
99465447c317: Pushed 
e1fbe91254a4: Pushed 
782876bca874: Pushed 
1a9c1c6ccc89: Pushed 
7b1d18bfea11: Pushed 
f00a65231359: Pushed 
95803b58ceac: Pushed 
777b2c648970: Pushed 
1.0: digest: sha256:4f7f7a6c566f757d2aa68089b8f8ae879772dcd803066dfcf847c41006feae8d size: 2200

4. 编写Golang程序的yaml

使用命令vi conatiner_deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: container-deployment
  namespace: test
  labels:
    app: container
spec:
  replicas: 4
  selector:
    matchLabels:
      app: container
  template:
    metadata:
      labels:
        app: container
    spec:
      containers:
        - name: container
          image: registry.cn-hangzhou.aliyuncs.com/chensyf/container:latest
          ports:
            - containerPort: 9000

使用命令kubectl apply -f conatiner_deployment.yaml创建Golang程序的deployment

[root@kubernetes yaml]# kubectl apply -f container_deployment.yaml 
deployment.apps/container-deployment created
[root@kubernetes yaml]# kubectl get pods -n test -o wide
NAME                                    READY   STATUS    RESTARTS   AGE   IP               NODE                      NOMINATED NODE   READINESS GATES
container-deployment-6d859494cd-2g9mt   1/1     Running   0          20m   10.110.190.142   kubernetes.node02.local   <none>           <none>
container-deployment-6d859494cd-5zzkh   1/1     Running   0          20m   10.110.166.28    kubernetes.node01.local   <none>           <none>
container-deployment-6d859494cd-c4ccw   1/1     Running   0          20m   10.110.166.29    kubernetes.node01.local   <none>           <none>
container-deployment-6d859494cd-kjtd7   1/1     Running   0          20m   10.110.174.79    kubernetes.node03.local   <none>           <none>
mysql-deployment-85985d9f6-x9q9r        1/1     Running   0          24h   10.110.166.19    kubernetes.node01.local   <none>           <none>

5. 创建Golang程序的service

使用命令vi container_service.yaml创建服务

apiVersion: v1
kind: Service
metadata:
  name: container-service
  labels:
    app: container
  namespace: test
spec:
  selector:
    app: container
  ports:
    - name: container-port
      protocol: TCP
      port: 9000 #集群内的其他容器组可通过 9000 端口访问 Service
      nodePort: 32100 #通过任意节点的 32100 端口访问 Service
      targetPort: 9000 #将请求转发到匹配 Pod 的 9000 端口
  type: NodePort

这里一定要注意,service与deployment要处于同一个命名空间

6. 访问地址

打开浏览器,访问任意节点的端口,如http://10.10.10.118:32100/docs/index.html

查看容器内容

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐