k8s中不同类型pod之间的访问
一、web服务访问db服务
1.启动mysql服务
为mysql服务创建一个RC定义文件:mysql-rc.yaml,创建好之后,将其发布到k8s集群中
[root@kub_master k8s]# mkdir tomcat_demo [root@kub_master k8s]# cd tomcat_demo/
[root@kub_master tomcat_demo]# vim mysql-rc.yaml [root@kub_master tomcat_demo]# cat mysql-rc.yaml 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.0.212:5000/mysql:5.7 ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: '123456'
[root@kub_master tomcat_demo]# kubectl create -f mysql-rc.yaml replicationcontroller "mysql" created [root@kub_master tomcat_demo]# kubectl get rc NAME DESIRED CURRENT READY AGE mysql 1 1 0 9s [root@kub_master tomcat_demo]# kubectl get rc NAME DESIRED CURRENT READY AGE mysql 1 1 1 32s [root@kub_master tomcat_demo]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE mysql-x6ql5 1/1 Running 0 1m 172.16.46.2 192.168.0.184
可以看到一个名为mysql-x6ql5的pod实例,这就是k8s根据mysql这个rc定义自动创建的pod。可以通过docker指令查看正在运行的容器。
[root@kub_node1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7e68b0d7186c 192.168.0.212:5000/mysql:5.7 "docker-entrypoint..." 2 minutes ago Up 2 minutes k8s_mysql.ce4b222f_mysql-x6ql5_default_2e2061a8-ffd6-11ea-8a8e-fa163e38ad0d_9b303ade ab8349676815 192.168.0.212:5000/pod-infrastructure:latest "/pod" 2 minutes ago Up 2 minutes k8s_POD.4e302f1_mysql-x6ql5_default_2e2061a8-ffd6-11ea-8a8e-fa163e38ad0d_8015ffff
此时发现mysql pod对应的容器还多创建一个pod容器,这就是pod的根容器。
再创建一个与之关联的k8s service-mysql的定义文件,文件名为:mysql-svc.yaml
[root@kub_master tomcat_demo]# vim mysql-svc.yaml [root@kub_master tomcat_demo]# cat mysql-svc.yaml apiVersion: v1 kind: Service metadata: name: mysql spec: ports: - port: 3306 targetPort: 3306 selector: app: mysql
其中:metadata.name 是service的服务名;port定义了service的虚端口;spec.selector确定了哪些pod副本对应的服务到本服务。
创建service对象
[root@kub_master tomcat_demo]# kubectl create -f mysql-svc.yaml service "mysql" created [root@kub_master tomcat_demo]# kubectl get svc NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 192.168.0.1 <none> 443/TCP 5d mysql 192.168.41.34 <none> 3306/TCP 14s
mysql服务被分配到一个职位192.168.137.225的cluster ip地址上,这是一个虚地址。随后k8s集群中其他新创建的pod的就可以通过service 的cluster ip+端口3306来连接和访问它。
通常情况下,cluster ip是在service创建后由k8s系统自动分配的,其他pod是无法预先知道某个service的cluster ip地址,因此需要一个服务发现机制来找到这个服务。为此,k8s使用了Linux环境变量来解决这个问题。
2. 启动tomcat服务
以同样的步骤,完成tomcat应用的启动过程。首先,创建tomcat对应的RC文件tomcat-rc.yaml
[root@kub_master tomcat_demo]# vim tomcat-rc.yaml [root@kub_master tomcat_demo]# cat tomcat-rc.yaml apiVersion: v1 kind: ReplicationController metadata: name: myweb spec: replicas: 2 selector: app: myweb template: metadata: labels: app: myweb spec: containers: - name: myweb image: 192.168.0.212:5000/tomcat-app:v1 ports: - containerPort: 8080 env: - name: MYSQL_SERVICE_HOST value: '192.168.41.34' - name: MYSQL_SERVICE_PORT value: '3306'
Tomcat容器内,应用将使用的环境变量MYSQL_SERVICE_HOST的值连接mysql服务。更安全可靠的用法是使用服务的名称“mysql”。
[root@kub_master tomcat_demo]# kubectl create -f tomcat-rc.yaml replicationcontroller "myweb" created [root@kub_master tomcat_demo]# kubectl get rc -o wide NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR mysql 1 1 1 6m mysql 192.168.0.212:5000/mysql:5.7 app=mysql myweb 2 2 1 13s myweb 192.168.0.212:5000/tomcat-app:v1 app=myweb [root@kub_master tomcat_demo]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE mysql-x6ql5 1/1 Running 0 6m 172.16.46.2 192.168.0.184 myweb-03lkh 1/1 Running 0 21s 172.16.81.3 192.168.0.212 myweb-d5h4z 1/1 Running 0 21s 172.16.66.2 192.168.0.208
最后,创建对应的service
[root@kub_master tomcat_demo]# vim tomcat-svc.yaml [root@kub_master tomcat_demo]# cat tomcat-svc.yaml apiVersion: v1 kind: Service metadata: name: myweb spec: type: NodePort ports: - port: 8080 nodePort: 30001 selector: app: myweb
type=NodePort和nodePort=30001这两个属性,表明service开启了NodePort方式的外网访问模式,在k8s集群之外,在本机的浏览器里,可以通过30001这个端口访问myweb(对应到8080的虚端口上)。
[root@kub_master tomcat_demo]# kubectl create -f tomcat-svc.yaml service "myweb" created [root@kub_master tomcat_demo]# kubectl get svc -o wide NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR kubernetes 192.168.0.1 <none> 443/TCP 5d <none> mysql 192.168.41.34 <none> 3306/TCP 3m app=mysql myweb 192.168.7.143 <nodes> 8080:30001/TCP 11s app=myweb
3. 测试访问
进入数据库查看
[root@kub_master tomcat_demo]# kubectl exec -it mysql-x6ql5 bash root@mysql-x6ql5:/# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.7.15 MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | HPE_APP | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> use HPE_APP; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +-------------------+ | Tables_in_HPE_APP | +-------------------+ | T_USERS | +-------------------+ 1 row in set (0.00 sec) mysql> select * from T_USERS; +----+-----------+-------+ | ID | USER_NAME | LEVEL | +----+-----------+-------+ | 1 | me | 100 | | 2 | our team | 100 | | 3 | HPE | 100 | | 4 | teacher | 100 | | 5 | docker | 100 | | 6 | google | 100 | | 7 | haha | 100 | +----+-----------+-------+ 7 rows in set (0.00 sec)
二、wordpress + mysql服务
1.启动mysql服务
为mysql创建一个deployment文件,mysql-wp-deployment.yaml
[root@kub_master k8s]# mkdir wordpress_deployment [root@kub_master k8s]# cd wordpress_deployment/ [root@kub_master wordpress_deployment]# vim mysql-deployment.yaml [root@kub_master wordpress_deployment]# cat mysql-deployment.yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: mysql-wp spec: replicas: 1 template: metadata: labels: app: mysql-wp spec: containers: - name: mysql-wp image: 192.168.0.212:5000/mysql:5.7 ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: '123456' - name: MYSQL_DATABASE value: 'wordpress' - name: MYSQL_USER value: 'wordpress' - name: MYSQL_PASSWORD value: 'wordpress'
[root@kub_master wordpress_deployment]# kubectl create -f mysql-deployment.yaml deployment "mysql-wp" created [root@kub_master wordpress_deployment]# kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE mysql-wp 1 1 1 1 9s
为其创建一个与之关联的Service-Mysql定义文件,文件名为:mysql-wp-svc.yaml
[root@kub_master wordpress_deployment]# vim mysql-wp-svc.yaml [root@kub_master wordpress_deployment]# cat mysql-wp-svc.yaml apiVersion: v1 kind: Service metadata: name: mysql-wp spec: ports: - port: 3306 targetPort: 3306 selector: app: mysql-wp
[root@kub_master wordpress_deployment]# kubectl create -f mysql-wp-svc.yaml service "mysql-wp" created [root@kub_master wordpress_deployment]# kubectl get svc -o wide NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR kubernetes 192.168.0.1 <none> 443/TCP 5d <none> mysql 192.168.41.34 <none> 3306/TCP 1h app=mysql mysql-wp 192.168.179.3 <none> 3307/TCP 12s app=mysql-wp myweb 192.168.7.143 <nodes> 8080:30001/TCP 56m app=myweb [root@kub_master wordpress_deployment]# kubectl get pods -o wide |grep mysql-wp mysql-wp-3745398300-msm4j 1/1 Running 0 2m 172.16.46.3 192.168.0.184
可以看到mysql-wp被分配到的cluster ip地址为192.168.179.3。这是一个虚地址。
2. 启动wordpress服务
首先,创建wordpress对应的deployment文件wp-deployment.yaml
[root@kub_master wordpress_deployment]# vim wp-deployment.yaml [root@kub_master wordpress_deployment]# cat wp-deployment.yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: wp-deployment spec: replicas: 1 template: metadata: labels: app: wp spec: containers: - name: wp image: 192.168.0.212:5000/wordpress:latest ports: - containerPort: 80 env: - name: WORDPRESS_DB_HOST value: '192.168.179.3' - name: WORDPRESS_DB_USER value: 'wordpress' - name: WORDPRESS_DB_PASSWORD value: 'wordpress' resources: limits: cpu: 100m requests: cpu: 100m
[root@kub_master wordpress_deployment]# kubectl create -f wp-deployment.yaml deployment "wordpress-deployment" created [root@kub_master wordpress_deployment]# kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE mysql-wp 1 1 1 1 8m wp-deployment 1 1 1 1 10s [root@kub_master wordpress_deployment]# kubectl get pods -o wide |grep wordpress wp-deployment-2881001018-lqpsf 1/1 Running 0 28s 172.16.81.4 192.168.0.212
wordpress容器内,使用环境变量WORDPRESS_DB_HOST的值连接mysql服务。
创建与其对应的Service。以下是完整的yaml定义文件(wp-svc.yaml)
[root@kub_master wordpress_deployment]# vim wp-svc.yaml [root@kub_master wordpress_deployment]# cat wp-svc.yaml apiVersion: v1 kind: Service metadata: name: wp spec: type: NodePort ports: - port: 80 nodePort: 30002 selector: app: wp
在k8s集群外,可以通过30002端口访问wp(对应到80的虚端口上)
运行命令进行创建
[root@kub_master wordpress_deployment]# kubectl create -f wp-svc.yaml service "wp" created [root@kub_master wordpress_deployment]# kubectl get svc -o wide NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR kubernetes 192.168.0.1 <none> 443/TCP 5d <none> mysql 192.168.41.34 <none> 3306/TCP 1h app=mysql mysql-wp 192.168.179.3 <none> 3307/TCP 12m app=mysql-wp myweb 192.168.7.143 <nodes> 8080:30001/TCP 1h app=myweb wp 192.168.184.38 <nodes> 80:30002/TCP 21s app=wp
3. 测试访问
进入数据库查看数据
[root@kub_master wordpress_deployment]# kubectl exec -it mysql-wp-3651026459-v31gc bash root@mysql-wp-3651026459-v31gc:/# mysql -uroot -p123456 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 26 Server version: 5.7.31 MySQL Community Server (GPL) Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | wordpress | +--------------------+ 5 rows in set (0.00 sec) mysql> use wordpress; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +-----------------------+ | Tables_in_wordpress | +-----------------------+ | wp_commentmeta | | wp_comments | | wp_links | | wp_options | | wp_postmeta | | wp_posts | | wp_term_relationships | | wp_term_taxonomy | | wp_termmeta | | wp_terms | | wp_usermeta | | wp_users | +-----------------------+ 12 rows in set (0.00 sec)