K8S之创建deployment关联NodePort服务

一、K8s编排yaml文件

1.1 如何通过Yaml文件创建deployment自动关联NodePort服务呢?

a. 命令行方式关联

参考资料:K8S拉取Django项目创建pod

文章中通过命令行方式关联,还需要手动修改svc参数

b. yaml文件编排方式

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: abc
  name: abc
  namespace: abc
spec:
  replicas: 3
  selector:
    matchLabels:
      app: iam
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: abc
    spec:
      containers:
      - image: registry.xxx.com/xxx/xxxx:1260cd6
        name: abc
        imagePullPolicy: Always
        resources: {}
        ports:
        - containerPort: 9991
          name: abcsvc
          protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: abc
  name: abcsvc
  namespace: abc
spec:
  ports:
  - port: 30333
    nodePort: 30333
    protocol: TCP
    targetPort: 9991
  selector:
    app: abc
  type: NodePort

参数说明:

kind: Deployment

  • - containerPort:容器内的服务端口
  • name: abcsvc   svc服务的名字
# kubectl get svc -n abc
NAME     TYPE       CLUSTER-IP   EXTERNAL-IP   PORT(S)           AGE
abc   NodePort   10.10.7.99   <none>        30333:30333/TCP   9m15s
# 

kind: Service

  • - port: 30333          容器暴露的端口(理解可能不对)
  • nodePort: 30333     节点服务器上暴露的端口
  • targetPort: 9991     目标容器服务的端口

 c. 创建deployment并关联svc

 # kubectl apply -f abc.deployment.yaml 

 

 

 

posted @ 2020-07-24 14:02  Pythia丶陌乐  阅读(2526)  评论(0编辑  收藏  举报