一个pod如何访问到另外一个应用程序的svc
[root@vms61 chap9-svc]# kubectl run blog --image=hub.c.163.com/library/mysql:latest --image-pull-policy=IfNotPresent --env="MYSQL_ROOT_PASSWORD=redhat"
pod/blog created
[root@vms61 chap9-svc]# kubectl run blog --image=hub.c.163.com/library/wordpress:latest --image-pull-policy=IfNotPresent --env="WORDPRESS_DB_HOST=XXXXX" --env="WORDPRESS_DB_USER=root" --env="WORDPRESS_DB_PASSWORD=redhat" --dry-run=client -o yaml > blog.yaml
[root@vms61 chap9-svc]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db ClusterIP 10.100.102.138 <none> 3306/TCP 136m
[root@vms61 chap9-svc]# cat blog.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: blog
name: blog
spec:
containers:
- env:
- name: WORDPRESS_DB_HOST
value: 10.100.102.138
- name: WORDPRESS_DB_USER
value: root
- name: WORDPRESS_DB_PASSWORD
value: redhat
image: hub.c.163.com/library/wordpress:latest
imagePullPolicy: IfNotPresent
name: blog
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
[root@vms61 chap9-svc]# kubectl apply -f blog.yaml
pod/blog created
[root@vms61 chap9-svc]# kubectl get pods
NAME READY STATUS RESTARTS AGE
blog 1/1 Running 0 7s
[root@vms61 chap9-svc]# kubectl expose --name=blog pod blog --port=80 --type=NodePort
service/blog exposed
[root@vms61 chap9-svc]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
blog NodePort 10.98.75.125 <none> 80:32178/TCP 4s
db ClusterIP 10.100.102.138 <none> 3306/TCP 142m
如果通过变量的方式:
1.只能获取相同namespace里的变量
2.变量的获取有先后顺序,引用的变量必须要先创建
[root@vms61 chap9-svc]# kubectl get pods
NAME READY STATUS RESTARTS AGE
db 1/1 Running 0 28m
[root@vms61 chap9-svc]# cat blog.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: blog
name: blog
spec:
containers:
- env:
- name: WORDPRESS_DB_HOST
value: $(DB_SERVICE_HOST)
- name: WORDPRESS_DB_USER
value: root
- name: WORDPRESS_DB_PASSWORD
value: redhat
image: hub.c.163.com/library/wordpress:latest
imagePullPolicy: IfNotPresent
name: blog
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
[root@vms61 chap9-svc]# kubectl apply -f blog.yaml
pod/blog created
[root@vms61 chap9-svc]# kubectl get pods
NAME READY STATUS RESTARTS AGE
blog 1/1 Running 0 2s
db 1/1 Running 0 28m
[root@vms61 chap9-svc]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
blog NodePort 10.107.97.68 <none> 80:31462/TCP 2m57s
db ClusterIP 10.105.66.169 <none> 3306/TCP 27m
通过DNS的方式发现—推荐
在kube-system里有dns,可以自动发现所有命名空间里的服务的clusterIP
所以,在同一个命名空间里,一个服务访问另外一个服务的时候,可以直接通
过服务名来访问
只要创建了一个服务(不管在哪个ns里创建的),都会自动向kube-system里的
DNS注册
如果是不同的命名空间,可以通过 服务名.命名空间名来访问
服务名.命名空间.svc.cluster.local
[root@vms61 chap9-svc]# cat blog.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: blog
name: blog
spec:
containers:
- env:
- name: WORDPRESS_DB_HOST
value: db
- name: WORDPRESS_DB_USER
value: root
- name: WORDPRESS_DB_PASSWORD
value: redhat
image: hub.c.163.com/library/wordpress:latest
imagePullPolicy: IfNotPresent
name: blog
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
[root@vms61 chap9-svc]# kubectl apply -f blog.yaml
pod/blog created
[root@vms61 chap9-svc]# kubectl get pods
NAME READY STATUS RESTARTS AGE
blog 1/1 Running 0 3s
db 1/1 Running 0 64m
web1-6464d54bd7-dxz7x 1/1 Running 0 19m
web1-6464d54bd7-ktbfr 1/1 Running 0 19m
web1-6464d54bd7-m4qg5 1/1 Running 0 19m
[root@vms61 chap9-svc]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db ClusterIP 10.105.66.169 <none> 3306/TCP 63m
svc1 ClusterIP 10.108.124.206 <none> 80/TCP 10m
[root@vms61 chap9-svc]# kubectl expose --name=blog pod blog --port=80 --type=NodePort
service/blog exposed
[root@vms61 chap9-svc]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
blog NodePort 10.110.42.125 <none> 80:31672/TCP 3s
db ClusterIP 10.105.66.169 <none> 3306/TCP 65m
svc1 ClusterIP 10.108.124.206 <none> 80/TCP 11m
正文完