需要安装好k8s 集群环境,这里以1主2从示例:
【k8s教程-1】kubeadm方法来安装k8s-v1.20(Centos7.9环境):https://blog.csdn.net/weixin_41827162/article/details/117487922
【k8s教程-2】安装kubernetes-dashboard:https://blog.csdn.net/weixin_41827162/article/details/117670165
【k8s教程-3】制作dockerfile,并将go项目部署到docker里(centos7.9环境):https://blog.csdn.net/weixin_41827162/article/details/117362978
所有服务器运行示意:
本篇文章参考:https://blog.csdn.net/GiChio/article/details/106565639
1. 将docker镜像推送到镜像库里面。
在docker服务器里面,cd到项目根目录。
比如我的:
cd /home/wwwroot/go/src/ginvel
注册一个docker.com账户。
# 在终端登录docker账户(https://hub.docker.com/repository)
[root@go-docker ginvel]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: fyonecon
Password:
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
# 新建一个docker镜像
[root@go-docker ginvel]# docker build -t ginvel .
Sending build context to Docker daemon 112.5MB
Step 1/11 : FROM golang:latest as builder
---> 96129f3766cf
Step 2/11 : RUN echo "Asia/Shanghai" > /etc/timezone && rm /etc/localtime && dpkg-reconfigure -f noninteractive tzdata
---> Running in 8a0cab741bdf
Current default time zone: 'Asia/Shanghai'
Local time is now: Tue Jun 8 14:52:31 CST 2021.
Universal Time is now: Tue Jun 8 06:52:31 UTC 2021.
Removing intermediate container 8a0cab741bdf
---> 0aad4c4b88e6
Step 3/11 : MAINTAINER fyonecon "[email protected]"
---> Running in 056e2a85e4b7
Removing intermediate container 056e2a85e4b7
---> d636808ab862
Step 4/11 : WORKDIR $GOPATH/src/ginvel.com
---> Running in d44d16c5d337
Removing intermediate container d44d16c5d337
---> d76ad49ff9ec
Step 5/11 : ADD . $GOPATH/src/ginvel.com
---> e43c0520875c
Step 6/11 : RUN go env -w GO111MODULE=on
---> Running in 61c103bea3cb
Removing intermediate container 61c103bea3cb
---> 22c48434e319
Step 7/11 : RUN go env -w GOPROXY=https://goproxy.cn,direct
---> Running in 4e739ec1db49
Removing intermediate container 4e739ec1db49
---> c4376791b396
Step 8/11 : RUN export GIN_MODE=release
---> Running in fb0bf55060ff
Removing intermediate container fb0bf55060ff
---> c5b4bfe618ec
Step 9/11 : RUN go build main.go
---> Running in 4f6ce07952e6
Removing intermediate container 4f6ce07952e6
---> b373c8c85897
Step 10/11 : EXPOSE 8090
---> Running in 9037c56f2dda
Removing intermediate container 9037c56f2dda
---> 022f698f7339
Step 11/11 : ENTRYPOINT ["./main"]
---> Running in 193b02ef98a6
Removing intermediate container 193b02ef98a6
---> af65ac1420e2
Successfully built af65ac1420e2
Successfully tagged ginvel:latest
# 查看镜像列表
[root@go-docker ginvel]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ginvel latest af65ac1420e2 10 seconds ago 1.09GB
golang latest 96129f3766cf 3 weeks ago 862MB
# 更换tag,其中“ fyonecon/ginvel ”对应hub.docker.com/repository 里面的镜像repositories名,如果repositories不相同则会自动创建。
[root@go-docker ginvel]# docker tag af65ac1420e2 fyonecon/ginvel
# 查看镜像列表
[root@go-docker ginvel]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
fyonecon/ginvel latest af65ac1420e2 About a minute ago 1.09GB
ginvel latest af65ac1420e2 About a minute ago 1.09GB
golang latest 96129f3766cf 3 weeks ago 862MB
# 推送镜像到镜像库
[root@go-docker ginvel]# docker push fyonecon/ginvel
Using default tag: latest
The push refers to repository [docker.io/fyonecon/ginvel]
68c0d76df461: Pushed
ded44986a582: Pushed
a3a7ff4ade4f: Pushed
7ba7ed7c21ae: Pushed
b3523e42ddaf: Pushed
b49af619a093: Pushed
5127db6b6203: Mounted from library/golang
039d7a38d742: Mounted from library/golang
9d52e952d0a7: Mounted from library/golang
685934357c89: Mounted from library/golang
ccb9b68523fd: Mounted from library/golang
00bcea93703b: Mounted from library/golang
688e187d6c79: Mounted from library/golang
latest: digest: sha256:ea8d58cc9b7f8852bc27b89a5ed5b77989c633716801628cd2d0e6f7e5990366 size: 3050
2. 将docker镜像库镜像部署到k8s里面
进入k8s的master服务器。
# 登录docker
[root@master1 ~]# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: fyonecon
Password:
Login Succeeded
# 查看已有镜像
[root@master1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
quay.io/coreos/flannel v0.14.0 8522d622299c 3 weeks ago 67.9 MB
registry.aliyuncs.com/google_containers/kube-proxy v1.20.0 10cc881966cf 6 months ago 118 MB
registry.aliyuncs.com/google_containers/kube-apiserver v1.20.0 ca9843d3b545 6 months ago 122 MB
registry.aliyuncs.com/google_containers/kube-scheduler v1.20.0 3138b6e3d471 6 months ago 46.4 MB
registry.aliyuncs.com/google_containers/kube-controller-manager v1.20.0 b9fa1895dcaa 6 months ago 116 MB
registry.aliyuncs.com/google_containers/etcd 3.4.13-0 0369cf4303ff 9 months ago 253 MB
registry.aliyuncs.com/google_containers/coredns 1.7.0 bfe3a36ebd25 11 months ago 45.2 MB
registry.aliyuncs.com/google_containers/pause 3.2 80d28bedfe5d 15 months ago 683 kB
# 下载镜像库的镜像
[root@master1 ~]# docker pull fyonecon/ginvel
Using default tag: latest
Trying to pull repository docker.io/fyonecon/ginvel ...
latest: Pulling from docker.io/fyonecon/ginvel
d960726af2be: Pull complete
e8d62473a22d: Pull complete
8962bc0fad55: Pull complete
65d943ee54c1: Pull complete
f2253e6fbefa: Pull complete
6d7fa7c7d5d3: Pull complete
e2e442f7f89f: Pull complete
3b453a62d2aa: Pull complete
8b3f1fd2c913: Pull complete
1954f4299324: Pull complete
c4bb00dbac49: Pull complete
8093c7a2420f: Pull complete
6439037e7c1c: Pull complete
Digest: sha256:ea8d58cc9b7f8852bc27b89a5ed5b77989c633716801628cd2d0e6f7e5990366
Status: Downloaded newer image for docker.io/fyonecon/ginvel.com:latest
[root@master1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/fyonecon/ginvel latest af65ac1420e2 20 minutes ago 1.09 GB
quay.io/coreos/flannel v0.14.0 8522d622299c 3 weeks ago 67.9 MB
registry.aliyuncs.com/google_containers/kube-proxy v1.20.0 10cc881966cf 6 months ago 118 MB
registry.aliyuncs.com/google_containers/kube-scheduler v1.20.0 3138b6e3d471 6 months ago 46.4 MB
registry.aliyuncs.com/google_containers/kube-controller-manager v1.20.0 b9fa1895dcaa 6 months ago 116 MB
registry.aliyuncs.com/google_containers/kube-apiserver v1.20.0 ca9843d3b545 6 months ago 122 MB
registry.aliyuncs.com/google_containers/etcd 3.4.13-0 0369cf4303ff 9 months ago 253 MB
registry.aliyuncs.com/google_containers/coredns 1.7.0 bfe3a36ebd25 11 months ago 45.2 MB
registry.aliyuncs.com/google_containers/pause 3.2 80d28bedfe5d 15 months ago 683 kB
[root@master1 ~]# docker run -p 8090:8090 -d docker.io/fyonecon/ginvel
700f444ab2b1a65344f0e2a115cf63132ad4116362884629b246a898c23ff66e
# 测试web服务是否已经开启。“200 OK”代表成功
[root@master1 ~]# wget 127.0.0.1:8090
--2021-06-09 13:37:06-- http://127.0.0.1:8090/
Connecting to 127.0.0.1:8090... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: 'index.html'
[ <=> ] 4,367 --.-K/s in 0s
2021-06-09 13:37:06 (52.4 MB/s) - 'index.html' saved [4367]
# 测试外网是否能访问。“200 OK”代表成功
[root@master1 ~]# wget 192.168.131.30:8090
--2021-06-09 13:37:36-- http://192.168.131.30:8090/
Connecting to 192.168.131.30:8090... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: 'index.html.1'
[ <=> ] 4,367 --.-K/s in 0s
2021-06-09 13:37:36 (41.3 MB/s) - 'index.html.1' saved [4367]
3. k8s部署运行上面的docker镜像
制作yaml编排文件,自动下载镜像、启动镜像、同步给各节点镜像。
文件名:auto-build-ginvel.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: auto-build-ginvel
labels:
app: ginvel
spec:
replicas: 1 #设置pod副本数量
revisionHistoryLimit: 10 #设置保留的历史版本个数,默认是10
selector:
matchLabels:
app: ginvel
template:
metadata:
labels:
app: ginvel
spec:
containers:
- name: ginvel
image: docker.io/fyonecon/ginvel # 用docker images查看镜像的tag名
#imagePullPolicy: Never
ports:
- containerPort: 8089
resources:
requests:
memory: "200Mi"
cpu: "100m"
limits:
memory: "600Mi"
cpu: "300m"
---
kind: Service
apiVersion: v1
metadata:
labels:
app: auto-build-ginvel
name: auto-build-ginvel-service
spec:
ports:
- port: 8090
targetPort: 8090
name: http
nodePort: 31000 # dashbaord为32000已被占用,这里设置为31000
selector:
app: ginvel
type: NodePort
在master节点安装yaml文件:
# 下载自动执行文件yaml
[root@master1 ~]# wget https://makeoss.oss-cn-hangzhou.aliyuncs.com/k8s/auto-build-ginvel.yaml
--2021-06-09 13:59:27-- https://makeoss.oss-cn-hangzhou.aliyuncs.com/k8s/auto-build-ginvel.yaml
Resolving makeoss.oss-cn-hangzhou.aliyuncs.com (makeoss.oss-cn-hangzhou.aliyuncs.com)... 118.31.219.204
Connecting to makeoss.oss-cn-hangzhou.aliyuncs.com (makeoss.oss-cn-hangzhou.aliyuncs.com)|118.31.219.204|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 930 [text/yaml]
Saving to: 'auto-build-ginvel.yaml'
100%[=====================================================================>] 930 --.-K/s in 0s
2021-06-09 13:59:27 (159 MB/s) - 'auto-build-ginvel.yaml' saved [930/930]
# 执行编排
[root@master1 ~]# kubectl apply -f auto-build-ginvel.yaml
deployment.apps/auto-build-ginvel unchanged
service/auto-build-ginvel-service created
# 创建自动扩容
[root@master1 ~]# kubectl autoscale deployment auto-build-ginvel --min=1 --max=15 --cpu-percent=20
horizontalpodautoscaler.autoscaling/auto-build-ginvel autoscaled
# 安装netstat工具
[root@master1 ~]# yum -y install net-tools
# 查看所有端口占用情况,可以看到在yaml文件里面设置的31000端口已经启动
[root@master1 ~]# netstat -lnpt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:30237 0.0.0.0:* LISTEN 2472/kube-proxy
tcp 0 0 0.0.0.0:32000 0.0.0.0:* LISTEN 2472/kube-proxy
tcp 0 0 127.0.0.1:10248 0.0.0.0:* LISTEN 995/kubelet
tcp 0 0 127.0.0.1:10249 0.0.0.0:* LISTEN 2472/kube-proxy
tcp 0 0 192.168.131.30:2379 0.0.0.0:* LISTEN 2098/etcd
tcp 0 0 127.0.0.1:2379 0.0.0.0:* LISTEN 2098/etcd
tcp 0 0 192.168.131.30:2380 0.0.0.0:* LISTEN 2098/etcd
tcp 0 0 127.0.0.1:2381 0.0.0.0:* LISTEN 2098/etcd
tcp 0 0 127.0.0.1:10257 0.0.0.0:* LISTEN 2056/kube-controlle
tcp 0 0 127.0.0.1:10259 0.0.0.0:* LISTEN 2118/kube-scheduler
tcp 0 0 127.0.0.1:45334 0.0.0.0:* LISTEN 995/kubelet
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 997/sshd
tcp 0 0 0.0.0.0:31000 0.0.0.0:* LISTEN 2472/kube-proxy
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1287/master
tcp6 0 0 :::10250 :::* LISTEN 995/kubelet
tcp6 0 0 :::10251 :::* LISTEN 2118/kube-scheduler
tcp6 0 0 :::6443 :::* LISTEN 2062/kube-apiserver
tcp6 0 0 :::10252 :::* LISTEN 2056/kube-controlle
tcp6 0 0 :::10256 :::* LISTEN 2472/kube-proxy
tcp6 0 0 :::22 :::* LISTEN 997/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1287/master
tcp6 0 0 :::8090 :::* LISTEN 4701/docker-proxy-c
在浏览器查看各个节点的启动情况:
各个节点的镜像都已同步并启动。
在dashboard查看节点运行情况(参考https://blog.csdn.net/weixin_41827162/article/details/117670165 安装kubernetes-dashboard):
-