赞
踩
1 ) 环境变量概述
MY_NGINX_PORT_80_TCP_PORT=80
MY_NGINX_PORT_80_TCP_PROTO=tcp
MY_NGINX_PORT_80_TCP_ADDR=10.1.180.155
2 )环境变量实践
2.1 这里我们先反过来,先创建 deployment 再创建 service
借用之前的 development 先运行起来
apiVersion: apps/v1 kind: Deployment metadata: name: my-dep-nginx # 这个是 Deployment 对象的名称 spec: selector: matchLabels: # 匹配标签 run: my-dep-nginx # 运行匹配的 my-nginx 容器 replicas: 2 # 2个副本,会创建 2个 pod template: # 定义模板 metadata: labels: run: my-dep-nginx spec: # 模板说明 containers: - name: my-dep-nginx image: nginx resources: limits: memory: "32Mi" cpu: "100m" ports: - containerPort: 80
$ kubectl apply -f dep-my-nginx.yaml
创建pod
deployment.apps/my-dep-nginx created
$ kubectl get po -w | grep my-dep
查看pod运行状态
my-dep-nginx-7776c5d85c-gfzxd 1/1 Running 0 37s
my-dep-nginx-7776c5d85c-w8bqq 1/1 Running 0 37s
2.2 再创建 service:svc-my-nginx.yaml
创建 svc-my-nginx.yaml
apiVersion: v1
kind: Service
metadata:
name: my-svc-nginx
labels:
run: my-dep-nginx
spec:
ports:
- port: 80
protocol: TCP
selector:
run: my-dep-nginx
$ kubectl get svc
查看服务
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 4d1h
my-svc-nginx ClusterIP 10.1.237.190 <none> 80/TCP 23m
好现在服务已经运行起来了, 现在进入pod容器中
$ kubectl exec -it my-dep-nginx-7776c5d85c-w8bqq -- sh
$ printenv | grep MY_SVC
2.3 调整顺序,这时候已经有 service 了,删除 development, 重新启动 development
kubectl delete -f dep-my-nginx.yaml
先删除 developmentdeployment.apps "my-dep-nginx" deleted
kubectl apply -f dep-my-nginx.yaml
在已有Service的情况下,重建 developmentdeployment.apps/my-dep-nginx created
kubectl get po -w | grep my-dep
等待容器启动完成my-dep-nginx-7776c5d85c-fx9pt 1/1 Running 0 115s
my-dep-nginx-7776c5d85c-hktm2 1/1 Running 0 115s
kubectl exec -it my-dep-nginx-7776c5d85c-fx9pt -- sh
进入其中之一printenv | grep MY_SVC
MY_SVC_NGINX_SERVICE_HOST=10.1.237.190
MY_SVC_NGINX_SERVICE_PORT=80
MY_SVC_NGINX_PORT=tcp://10.1.237.190:80
MY_SVC_NGINX_PORT_80_TCP_ADDR=10.1.237.190
MY_SVC_NGINX_PORT_80_TCP_PORT=80
MY_SVC_NGINX_PORT_80_TCP_PROTO=tcp
MY_SVC_NGINX_PORT_80_TCP=tcp://10.1.237.190:80
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。