1.简介

用于大规模部署拟分布式应用的平台,管理服务容器和资源分配。

2.安装

版本一定要对应这里是1.18

2.1命令行工具Kubectl

去kubernetes官网下载Kubectl,然后将文件路径配置到环境变量中

2.2配置集群工具kind (k8s.io)

image-20220510132203188

确保在coolcar/server目录下\

1
go install sigs.k8s.io/kind@v0.11.1

在go安装目录下的bin就有了kind.exe

  1. 建立一个集群kind create cluster

image-20220510132137676

image-20220510132606482

  1. 通过设置配置文件来连接集群。
1
export KUBECONFIG=~/
1
set %KUBECONFIG%=~/

2.3容器

  1. 镜像:由docker制作,系统,配置,应用,脚本。就相当于数据
  2. 容器运行环境(CRI):
    1. 拉取镜像:去镜像仓库拉取。
    2. 分配资源
    3. 运行镜像
    4. 运行环境的实现:例如containerd,Docker,CRI-O。Docker已经弃用,由于需要维护dockershim和docker版本适配,花费太大。

1.创建coolcar/deployment文件夹,再创建nginx-deployment.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 4
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
resources:
limits:
cpu: 100m
memory: 128Mi

在当前文件目录下运行

1
kubectl apply -f .\nginx\nginx-deployment.yaml
1
kubectl get pods

image-20220510141814747

2.4工作负载

Pod:容器真正运行的地方,可以运行一个/多个Container(逻辑上的Host),这里的多表示的多个辅助模块对服务进行增强。

image-20220510143337688

Deployment:描述镜像,参数,资源。

3.制作及上传镜像

镜像不可变性:每次运行alpine都会初始化。

image-20220510151612698

想要保存的话,就需要重新制作这个镜像

1
dockre commit 镜像ID 新的名字

image-20220510152106682

3.1制作镜像步骤

选择在Docker镜像中编译。

1.拉取镜像

1
docker run -it golang:1.17

2.设置go语言官方镜像七牛云 - Goproxy.cn

1
2
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct

3.将源代码拷贝进来,进行编译

1
go install ...
Dockerfile

上面三个步骤都可以在文件中指定。创建文件夹deployment/gateway/Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
# 启动编译环境
FROM golang:1.17
# 配置编译环境
RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://goproxy.cn,direct
# 拷贝源代码的镜像中
COPY . /go/src/coolcar/server
# 编译
WORKDIR /go/src/coolcar/server
RUN go install ./gateway/...
# 设置服务入口
ENTRYPOINT [ "/go/bin/gateway" ]
1
docker build -t coolcar/gateway -f ../deployment/dateway/Dockerfile .

image-20220510155737289

注意:这里最容易出错的地方是COPY source dest对于拷贝的原目录,在执行上面这条命令是最后携带的路径path就是source的开始路径。

image-20220510155952110

运行成功就可以启动镜像。

设置暴露端口
1
2
3
4
5
6
7
8
9
10
11
12
13
# 启动编译环境
FROM golang:1.17
# 配置编译环境
RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://goproxy.cn,direct
# 拷贝源代码的镜像中
COPY . /go/src/coolcar/server
# 编译
WORKDIR /go/src/coolcar/server
RUN go install ./gateway/...
EXPOSE 8080
# 设置服务入口
ENTRYPOINT [ "/go/bin/gateway" ]

上面的8080并不是真正的暴露端口

1
docker build -t coolcar/gateway -f ../deployment/dateway/Dockerfile .

启动映射端口,暴露端口

1
docker run -p 8080:8080 coolcar/gateway

image-20220510161052077

CMDENTRYPOINT命令

ENTRYPOINT运行命令

CMD是在ENTRYPOINT后加上。

例子

1
2
3
ENTRYPOINT [ "echo","a" ]
CMD [ "b" ]
输出为a b

但是在后面指定参数的时候

1
docker run -p 8080:8080 coolcar/gateway abc

此时输出为a abc

3.2镜像瘦身

image-20220510162634759

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 启动编译环境,使用golang-alpine的环境
FROM golang:1.17-alpine as builder
# 配置编译环境
RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://goproxy.cn,direct
# 拷贝源代码的镜像中
COPY . /go/src/coolcar/server
# 编译
WORKDIR /go/src/coolcar/server
RUN go install ./gateway/...

# 二次编译
FROM alpine:3.13
# 将编译的文件放在/bin目录下
COPY --from=builder /go/bin/gateway /bin/gateway

# 申明暴露的端口
EXPOSE 8080
# 设置服务入口
ENTRYPOINT [ "/bin/gateway" ]

前后大小对比

image-20220510164926689

image-20220510165102672

3.3将main.go配置化

配置来源:

  • 命令行参数
  • 环境变量: 在k8s是最顺手的配置
  • 配置文件(ini,yaml
  • 默认值

安装一个比go语言本身flag更好的flag,可以按找上面的优先级来配置。

1
go get github.com/namsral/flag

1.gateway中定义:

1
2
3
4
5
var addr = flag.String("addr", ":8080", "address to listen")
var authAddr = flag.String("auth_addr", "localhost:8081", "address for auth service")
var tripAddr = flag.String("trip_addr", "localhost:8082", "address for trip service")
var profileAddr = flag.String("profile_addr", "localhost:8082", "address for profile service")
var carAddr = flag.String("car_addr", "localhost:8084", "address for car service")

在main函数中一点要运行

1
flag.Parse()

2.auth中配置

1
2
3
4
5
var addr = flag.String("addr", ":8081", "address to listen")
var mongoURI = flag.String("mongo_uri", "mongodb://localhost:27017", "mongo uri")
var privateKeyFile = flag.String("private_key_file", "auth/private.key", "private key file")
var wechatAppID = flag.String("wechat_app_id", "<APPID>", "wechat app id")
var wechatAppSecret = flag.String("wechat_app_secret", "<APPSECRET>", "wechat app secret")

2.blob中配置

1
2
3
4
5
var addr = flag.String("addr", ":8083", "address to listen")
var mongoURI = flag.String("mongo_uri", "mongodb://localhost:27017", "mongo uri")
var cosAddr = flag.String("cos_addr", "<URL>", "cos address")
var cosSecID = flag.String("cos_sec_id", "<SEC_ID>", "cos secret id")
var cosSecKey = flag.String("cos_sec_key", "<SEC_KEY>", "cos secret key")

3.car配置

1
2
3
4
5
6
7
var addr = flag.String("addr", ":8084", "address to listen")
var wsAddr = flag.String("ws_addr", ":9090", "websocket address to listen")
var mongoURI = flag.String("mongo_uri", "mongodb://localhost:27017", "mongo uri")
var amqpURL = flag.String("amqp_url", "amqp://guest:guest@localhost:5672/", "amqp url")
var carAddr = flag.String("car_addr", "localhost:8084", "address for car service")
var tripAddr = flag.String("trip_addr", "localhost:8082", "address for trip service")
var aiAddr = flag.String("ai_addr", "localhost:18001", "address for ai service")

4.rental配置

1
2
3
4
5
var addr = flag.String("addr", ":8082", "address to listen")
var mongoURI = flag.String("mongo_uri", "mongodb://localhost:27017", "mongo uri")
var blobAddr = flag.String("blob_addr", "localhost:8083", "address for blob service")
var carAddr = flag.String("car_addr", "localhost:8084", "address for car service")
var authPublicKeyFile = flag.String("auth_public_key_file", "shared/auth/public.key", "public key file for auth")

3.4为所有微服务制作镜像

image-20220511124632300

按照下面模板进行创建,由于POD都是独立的,所以GRPC请求都走8081端口.所有的http都走8080.在carservice中要特别配置websocket端口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 启动编译环境,使用golang-alpine的环境
FROM golang:1.17-alpine as builder
# 配置编译环境
RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://goproxy.cn,direct
# 拷贝源代码的镜像中
COPY . /go/src/coolcar/server
# 编译
WORKDIR /go/src/coolcar/server
RUN go install ./auth/...

# 二次编译
FROM alpine:3.13
# 将编译的文件放在/bin目录下
COPY --from=builder /go/bin/auth /bin/auth
ENV ADDR=:8081
# 申明暴露的端口
EXPOSE 8081
# 设置服务入口
ENTRYPOINT [ "/bin/auth" ]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 启动编译环境,使用golang-alpine的环境
FROM golang:1.17-alpine as builder
# 配置编译环境
RUN go env -w GO111MODULE=on
RUN go env -w GOPROXY=https://goproxy.cn,direct
# 拷贝源代码的镜像中
COPY . /go/src/coolcar/server
# 编译
WORKDIR /go/src/coolcar/server
RUN go install ./car/...

# 二次编译
FROM alpine:3.13
# 将编译的文件放在/bin目录下
COPY --from=builder /go/bin/car /bin/car
ENV ADDR=:8081
ENV WS_ADDR=:8080
# 申明暴露的端口
EXPOSE 8081
EXPOSE 8080
# 设置服务入口
ENTRYPOINT [ "/bin/car" ]
1
docker build -t coolcar/rental -f ..\deployment\rental\Dockerfile . 

image-20220511130032957

3.5上传镜像

在腾讯云使用个人版的镜像仓库,选择有个人版镜像仓库的区域.

image-20220511131821239

新建命名空间rental_car

本地登录实例:

1
docker login ccr.ccs.tencentyun.com --username=100018440141

上传镜像:

  1. 标签
1
docker tag [上传的镜像ID] [访问域名]/[命名空间]/[上传后的名字]:[版本号]

版本号可以用时间.

1
docker tag coolcar/gateway ccr.ccs.tencentyun.com/rental_car/gateway:20220511

image-20220511132953166

  1. 上传
1
docker push ccr.ccs.tencentyun.com/rental_car/gateway:20220511

image-20220511133348227

上传所有镜像.

image-20220511134349551