本文主要内容:
- Pod
- Service
- Label
- Annotation
- 以 Pod 为例动手写配置
Pod
什么是 Pod
管理、创建、计划的最小单元
特点
部署同一个主机下,共享磁盘、网络地址、主机名称等
Service
什么是 Service
联通 Pod 的服务
类型
ClusterIP(只对k8s内提供服务,k8s以外不能使用)、NodePort(将服务暴露给外部)、LoadBalancer(有特殊的云服务提供商才能访问)
Label
apiVersion: v1
Annotation
apiVersion: v1
// 岛上的矿,以及各种资源——Pod;Service ——海上的船;label —— 标签;Annotation——
创建Pod
- 创建名为 test 的 namespace:
kubectl create namespace test
- 创建 Pod:
apiVersion: v1
kind: Pod
metadata:
name: hello-world
labels:
name: hello
spec:
containers:
- name: hello-world
image: kataqlee/hello-world-of-node:1.1
ports:
- containerPort: 8080
env:
- name: CONTAINER_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
创建 Service
apiVersion: v1
kind: Service
metadata:
namespace: test
name: hello-world-service
spec:
type: NodePort
ports:
- protocol: TCP
port: 80
targetPort: 8080
nodePort: 30998
selector:
name: hello
selector 中的 name 和 Pod 中 label 的 name 保持一致
在命令行创建:
kubectl apply -f xxx.yml -n <namespace name>
with env:
apiVersion: v1
kind: Pod
metadata:
namespace: test
name: hello-test
labels:
name: hello
spec:
containers:
- name: hello-test
image: kataqlee/hello-world-of-node:1.1
ports:
- containerPort: 8080
env:
- name: THE_NAME
value: "ENV NAME"
- name: CONTAINER_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
因为在 Kubernetes 中 hello-world 和 hello-test 的label 都是一样的,访问的时候,无法确定会路由到哪里
内部消费的 comsumer:
apiVersion: v1
kind: Pod
metadata:
namespace: test
name: hello-world-comsumer
labels:
name: hello-consumer
spec:
containers:
- name: hello-world-comsumer
image: kataqlee/get-hello-world:1.3
restartPolicy: OnFailure