deployment 是 k8s 中的无状态服务控制器 ,定义一组 Pod 期望数量,deployment 会维持 Pod 数量与期望数量一致。一些无状态的服务可以使用 deployment 进行部署。 这里以 nmap-driver 为例,作为一个部署 deployment 服务的模板。
首先创建三个文件 cm.yaml
,nmap-driver.ymal
,svc.yaml
svc.yaml
这是 service 模板文件,用来提供外部访问服务。
apiVersion: v1
kind: Service
metadata:
name: nmap-driver
namespace: sky
labels:
app: nmap-driver
spec:
ports:
- name: grpc
targetPort: 19999
port: 19999
clusterIP: None
selector:
app: nmap-driver
---
apiVersion: v1
kind: Service
metadata:
name: nmap-driver-service
namespace: sky
labels:
app: namp-driver
spec:
externalIPs: # 暴露Service到外部IP
- 192.168.1.146 # IP
selector:
app: namp-driver
ports:
- name: grpc
targetPort: 19999
port: 19999
cm.yaml
使用 configmap 管理配置文件。
kind: ConfigMap
metadata:
name: nmap-driver-config
namespace: sky
labels:
addonmanager.kubernetes.io/mode: Reconcile
data:
config.ini: |
[Nmap-Driver]
port=19999
logging.conf: |
[loggers]
keys=root
[handlers]
keys=allHandler,errorHandler
[formatters]
keys=simpleFormatter,detailFormatter
[logger_root]
level=DEBUG
handlers=allHandler,errorHandler
[handler_allHandler]
class=logging.handlers.TimedRotatingFileHandler
args=('/root/log/all.log', 'D',1,7)
level=DEBUG
formatter=simpleFormatter
[handler_errorHandler]
class=logging.handlers.TimedRotatingFileHandler
args=('/root/log/error.log', 'D',1,7)
level=ERROR
formatter=detailFormatter
[formatter_simpleFormatter]
format=%(asctime)s - %(levelname)s - %(message)s
[formatter_detailFormatter]
format=%(asctime)s - %(levelname)s - %(filename)s[:%(lineno)d] - %(message)s
nmap-driver.yaml
deployment 文件 设置副本数,配置启动镜像 启动参数 挂载配置文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: nmap-driver
namespace: sky
spec:
replicas: 1
selector:
matchLabels:
app: nmap-driver
template:
metadata:
name: nmap-driver
labels:
app: nmap-driver
spec:
tolerations:
- key: "node.kubernetes.io/unreachable"
operator: "Exists"
effect: "NoExecute"
tolerationSeconds: 60
- key: "node.kubernetes.io/not-ready"
operator: "Exists"
effect: "NoExecute"
tolerationSeconds: 60
terminationGracePeriodSeconds: 0 # 异常立即删除
containers:
- name: nmap-driver
securityContext:
privileged: true
imagePullPolicy: IfNotPresent
image: hub.sky-cloud.net/sky/nmap-driver:master_build-4
ports:
- name: grpc
containerPort: 19999
env:
- name: TZ
value: Asia/Shanghai
volumeMounts:
- name: tz
mountPath: /etc/localtime
- name: log
mountPath: /root/log #将容器内的这个日志目录挂载到外部/srv/sky/nmap-driver/log
- name: config
mountPath: /nmap-driver/config #configmap中的配置文件 挂载到容器的这个文件夹
volumes:
- name: tz
hostPath:
path: /etc/localtime
- name: log
hostPath:
path: /srv/sky/nmap-driver/log
- name: config
configMap:
name: nmap-driver-config
items:
- key: config.ini
path: config.ini
- key: logging.conf
path: logging.conf
启动
使用 kubelet 命令启动
kubectl apply -f .
根据这个模板 只需一下几个步骤就可启动一个 deployment 服务
- 替换项目名和 namespace
- 替换 svc.yaml 里面的 ip 和端口
- 在 cm.yaml 里面放入配置文件
- 挂载日志文件目录和 config
- 替换镜像
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于