技术/部署栈:
- Nodejs项目(Github)
- 阿里云容器镜像服务
- K3S - Lightweight Kubernetes
目录
容器镜像服务
在Github仓库中创建名为release-vX.X.X
的tag会自动触发阿里云镜像服务构建(前提是有正确的Dockerfile)。
阿里云容器镜像仓库不自动支持
latest
。
在运行K3S服务之前,创建并编辑/etc/rancher/registries.yaml
mirrors:
docker.io:
endpoint:
- "https://[阿里云容器镜像源]"
configs:
"[阿里云容器镜像仓库]":
auth:
username: [阿里云账户全名] # 建议使用子账户(容器镜像服务只读权限)
password: [阿里云镜像仓库密码]
Master Node (Server)
网络环境:公网服务器
按照官网教程一行代码安装
curl -sfL https://get.k3s.io | sh -
编辑/etc/systemd/system/k3s.service
的最后一行(启动代码)为
ExecStart=/usr/local/bin/k3s \
server \
--node-external-ip [公网IP] \
--tls-san [公网IP] \
重启服务
systemctl daemon-reload
systemctl restart k3s
Node Token
用于Worker节点连接,目录在/var/lib/rancher/k3s/server/node-token
Kubeconfig
用于远程控制集群,目录在/etc/rancher/k3s/k3s.yaml
。
在集群外部,把cluster
信息中的server
中的IP改为公网IP即可(必须是启动配置中--tls-san
指定的IP)。
Worker Node (Agent)
网络环境:内网主机
按照官网中的安装方法搭配参数:
curl -sfL https://get.k3s.io | K3S_NODE_NAME="[节点名称]" K3S_URL="https://[服务器公网IP]:6443" K3S_TOKEN="[node-token]" sh -
编辑/etc/systemd/system/k3s-agent.service
的最后一行(启动代码)为
ExecStart=/usr/local/bin/k3s \
agent \
--node-name [节点名称] \
--server [公网IP] \
--token [node-token] \
Lens
使用了Lens可视化工具来管理集群。可以直接去下载管理工具,用上面做好的Kubeconfig连接集群,同时还可以安装Metric等。
Kubernetes
若干配置
Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: [名称]-deployment
labels:
app: [名称]
spec:
replicas: 1
selector:
matchLabels:
app: [名称]
template:
metadata:
labels:
app: [名称]
spec:
containers:
- name: [名称]
image: [阿里云容器镜像仓库]/[命名空间]/[镜像名]:[版本号]
ports:
- containerPort: [服务端口]
Service
apiVersion: v1
kind: Service
metadata:
name: [名称]-service
spec:
selector:
app: [名称]
ports:
- protocol: TCP
port: 80
targetPort: [端口]
Ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: [名称]-ingress
spec:
rules:
- host: [hostname]
http:
paths:
- path: /foo
backend:
serviceName: service1
servicePort: 4200
- path: /bar
backend:
serviceName: service2
servicePort: 8080