yaml格式的service定义文件完整内容

    1. apiVersion: v1
    2. kind: Service
    3. matadata: #元数据
    4. name: string #service的名称
    5. namespace: string #命名空间
    6. labels: #自定义标签属性列表
    7. - name: string
    8. annotations: #自定义注解属性列表
    9. - name: string
    10. spec: #详细描述
    11. selector: [] #label selector配置,将选择具有label标签的Pod作为管理 范围
    12. type: string #service的类型,指定service的访问方式,默认为clusterIp
    13. clusterIP: string #虚拟服务地址
    14. sessionAffinity: string #是否支持session
    15. ports: #service需要暴露的端口列表
    16. - name: string #端口名称
    17. protocol: string #端口协议,支持TCP和UDP,默认TCP
    18. port: int #服务监听的端口号
    19. targetPort: int #需要转发到后端Pod的端口号
    20. nodePort: int #当type = NodePort时,指定映射到物理机的端口号
    21. status: #当spce.type=LoadBalancer时,设置外部负载均衡器的地址
    22. loadBalancer: #外部负载均衡器
    23. ingress: #外部负载均衡器
    24. ip: string #外部负载均衡器的Ip地址值
    25. hostname: string #外部负载均衡器的主机名

    外部服务service
    将一个外部服务(数据库等)作为后端服务进行连接

    1. ---
    2. kind: Service
    3. apiVersion: v1
    4. metadata:
    5. name: mysql-test
    6. spec:
    7. ports:
    8. - protocol: TCP
    9. port: 3306
    10. targetPort: 3306
    11. ---
    12. kind: Endpoints
    13. apiVersion: v1
    14. metadata:
    15. name: mysql-test
    16. subsets:
    17. - addresses:
    18. - ip: 10.24.2.191 //只能IP,不能是域名
    19. ports:
    20. - port: 3306

    验证,在pod里面telnet 域名和端口
    Image.png

    1. ---
    2. apiVersion: extensions/v1beta1
    3. kind: Ingress
    4. metadata:
    5. name: xintuo-jenkins-1070941479
    6. annotations:
    7. nginx.ingress.kubernetes.io/ssl-redirect: "false"
    8. nginx.ingress.kubernetes.io/rewrite-target: /xintuo
    9. spec:
    10. rules:
    11. - host: ci.hundsun.com
    12. http:
    13. paths:
    14. - path: /xintuo
    15. backend:
    16. serviceName: xintuo-jenkins-1070941479-web
    17. servicePort: 8080
    18. ---
    19. apiVersion: v1
    20. kind: PersistentVolume
    21. metadata:
    22. name: xintuo-jenkins-1070941479
    23. labels:
    24. pv: xintuo-jenkins-1070941479
    25. spec:
    26. capacity:
    27. storage: 200Gi
    28. accessModes:
    29. - ReadWriteMany
    30. nfs:
    31. path: /data/JENKINS_HOME/xintuo
    32. server: 192.168.102.234
    33. ---
    34. kind: PersistentVolumeClaim
    35. apiVersion: v1
    36. metadata:
    37. name: xintuo-jenkins-1070941479
    38. labels:
    39. pv: xintuo-jenkins-1070941479
    40. spec:
    41. accessModes:
    42. - ReadWriteMany
    43. resources:
    44. requests:
    45. storage: 200Gi
    46. selector:
    47. matchLabels:
    48. pv: xintuo-jenkins-1070941479
    49. ---
    50. #Jenkins的web服务和slave服务
    51. apiVersion: v1
    52. kind: Service
    53. metadata:
    54. name: xintuo-jenkins-1070941479-web
    55. labels:
    56. name: jenkins-1070941479
    57. spec:
    58. ports:
    59. - name: web
    60. port: 8080
    61. selector:
    62. name: jenkins-1070941479
    63. ---
    64. apiVersion: v1
    65. kind: Service
    66. metadata:
    67. name: xintuo-jenkins-1070941479-slave
    68. labels:
    69. name: jenkins-1070941479
    70. spec:
    71. type: NodePort
    72. ports:
    73. - name: slave
    74. port: 38830
    75. nodePort: 38830
    76. selector:
    77. name: jenkins-1070941479
    78. ---
    79. #Jenkins的容器
    80. apiVersion: apps/v1
    81. kind: Deployment
    82. metadata:
    83. name: xintuo-jenkins-1070941479
    84. spec:
    85. replicas: 1
    86. selector:
    87. matchLabels:
    88. name: "jenkins-1070941479"
    89. template:
    90. metadata:
    91. labels:
    92. "name": "jenkins-1070941479"
    93. spec:
    94. affinity:
    95. podAntiAffinity:
    96. preferredDuringSchedulingIgnoredDuringExecution:
    97. - weight: 100
    98. podAffinityTerm:
    99. labelSelector:
    100. matchExpressions:
    101. - key: "name"
    102. operator: In
    103. values:
    104. - "jenkins-1070941479"
    105. topologyKey: kubernetes.io/hostname
    106. nodeAffinity:
    107. requiredDuringSchedulingIgnoredDuringExecution:
    108. nodeSelectorTerms:
    109. - matchExpressions:
    110. - key: kubernetes.io/hostname
    111. operator: In
    112. values:
    113. - ci-k8s-worker04
    114. containers:
    115. - name: jenkins
    116. image: registry.hundsun.com/jenkins/jenkins-common:centos7.6_ver2.153
    117. imagePullPolicy: IfNotPresent
    118. lifecycle:
    119. postStart:
    120. exec:
    121. command: ["/bin/sh", "-c", "mv /opt/apache-tomcat-9.0.0.M9/webapps/jenkins.war /opt/apache-tomcat-9.0.0.M9/webapps/xintuo.war"]
    122. resources:
    123. requests:
    124. cpu: 0
    125. memory: 0
    126. limits:
    127. cpu: 0
    128. memory: 0
    129. ports:
    130. - containerPort: 8080
    131. name: web
    132. - containerPort: 38830
    133. name: slave
    134. volumeMounts:
    135. - name: data
    136. mountPath: /var/jenkins
    137. - name: timezone
    138. mountPath: /etc/localtime
    139. readOnly: true
    140. volumes:
    141. - name: data
    142. persistentVolumeClaim:
    143. claimName: xintuo-jenkins-1070941479
    144. - name: timezone
    145. hostPath:
    146. path: /etc/localtime