pod访问mysql_9 k8s之pod之间互访

一,创建mysql的rc,svc

1) 上传mysql标签到私有仓库(也可以直接去官网pull镜像上传到私有仓库)

docker pull mysql:5.7

docker tag docker.io/mysql:5.7 192.168.31.50:5000/mysql:5.7

docker push 192.168.31.50:5000/mysql:5.7

2) 创建mysql  rc

[root@k8s-master tomcat_demo]# cat mysql-rc.yml

apiVersion: v1

kind: ReplicationController

metadata:

name: mysql

spec:

replicas: 1

selector:

app: mysql

template:

metadata:

labels:

app: mysql

spec:

containers:

- name: mysql

image: 192.168.31.50:5000/mysql:5.7

ports:

- containerPort: 3306

env:

- name: MYSQL_ROOT_PASSWORD

value: '123456'

创建

[root@k8s-master tomcat_demo]# kubectl create -f mysql-rc.yml

replicationcontroller "mysql" created

3)创建mysql svc

[root@k8s-master tomcat_demo]# vim mysql-svc.yml

apiVersion: v1

kind: Service

metadata:

name: mysql

spec:

ports:

- port: 3306

targetPort: 3306

selector:

app: mysql

创建

[root@k8s-master tomcat_demo]# kubectl create -f mysql-svc.yml

service "mysql" created

pod访问mysql_9 k8s之pod之间互访_第1张图片

二,创建tomcat的rc svc

1)创建tomcat的rc

[root@k8s-master tomcat_demo]# vim tomcat-rc.yml

apiVersion: v1

kind: ReplicationController

metadata:

name: tomcat

spec:

replicas: 1

selector:

app: tomcat

template:

metadata:

labels:

app: tomcat

spec:

containers:

- name: tomcat

image: 192.168.31.50:5000/tomcat-app:v2

ports:

- containerPort: 8080

env:

- name: MYSQL_SERVICE_HOST

value: 'mysql' # 注意,这个mysql 是之前启动mysql的svc的名称,需要dns服务解析,否则直接填写mysql的vip也是可以的,但是vip可能会变

- name: MYSQL_SERVICE_PORT

value: '3306'

下载tomcat镜像,打标签

[root@k8s-master tomcat_demo]# docker pull docker.io/kubeguide/tomcat-app:v2

[root@k8s-master tomcat_demo]# docker tag docker.io/kubeguide/tomcat-app:v2 192.168.31.50:5000/tomcat-app:v2

[root@k8s-master tomcat_demo]# docker push 192.168.31.50:5000/tomcat-app:v2

创建

[root@k8s-master tomcat_demo]# kubectl create -f tomcat-rc.yml

replicationcontroller "tomcat" created

2)创建tomcat svc

[root@k8s-master tomcat_demo]# vim tomcat-svc.yml

apiVersion: v1

kind: Service

metadata:

name: tomcat

spec:

type: NodePort

ports:

- port: 8080

nodePort: 30001

selector:

app: tomcat

创建

[root@k8s-master tomcat_demo]# kubectl create -f tomcat-svc.yml

service "tomcat" created

4865894873f477a6f5a540eb0aa33e76.png

三 测试pod之间互访

访问测试tomcat 的demo,出现如下界面说明tomcat的pod连接数据库pod成功

pod访问mysql_9 k8s之pod之间互访_第2张图片

你可能感兴趣的:(pod访问mysql)