自定義endpoint實(shí)現(xiàn)內(nèi)部pod訪問(wèn)外部應(yīng)用
endpoint除了可以暴露pod的IP和端口還可以代理到外部的ip和端口
使用場(chǎng)景
-
公司業(yè)務(wù)還還沒(méi)有完成上云, 一部分云原生的,一部分是實(shí)體的
-
業(yè)務(wù)上云期間逐步實(shí)現(xiàn)上云,保證各個(gè)模塊之間的解耦性
比如使用云數(shù)據(jù)庫(kù)或者實(shí)體數(shù)據(jù)庫(kù)服務(wù)器啥的,因?yàn)橄駭?shù)據(jù)庫(kù)實(shí)現(xiàn)容器化的話在實(shí)際生產(chǎn)環(huán)境中是不推薦的
所以一些靜態(tài)服務(wù)上云以后pod還是需要訪問(wèn)外部應(yīng)用服務(wù)的
k8s的Endpoint自定義實(shí)驗(yàn)
還是用tomcat+mysql的zrlog來(lái)實(shí)驗(yàn)
首先準(zhǔn)備好tomcat的zrlog代碼,我直接用上個(gè)博文實(shí)驗(yàn)用的yaml文件,因?yàn)楝F(xiàn)在主要探討的是pod通過(guò)service與外部網(wǎng)絡(luò)通信
[root@server153 test]# cat tomcat-deploy.yaml
apiVersion: v1
kind: Service # 聲明版本為Service
metadata:
name: tomcat-service # 定義Service的名字
labels:
name: show-tomcat-pod # 定義Service的標(biāo)簽
spec:
type: NodePort # 定義Service的類型,自動(dòng)分配一個(gè)集群serviceip
selector:
app: tomcat-deploy #定義標(biāo)簽選擇器,會(huì)代理后端app=tomcat-deploy的Pod
ports:
- port: 80 #內(nèi)部暴露的端口
targetPort: 8080 #代理的pod的端口
nodePort: 31111 #暴露給主機(jī)外部訪問(wèn)的端口(default: 30000-32767)
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: tomcat-deploy
name: tomcat-deploy
namespace: default
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: tomcat-deploy
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
app: tomcat-deploy
spec:
#創(chuàng)建init容器
initContainers:
#代碼鏡像
- image: www.test.com/mytest/zrlog:v1
#init容器名字
name: init
#將代碼復(fù)制到匿名數(shù)據(jù)卷
command: ["cp","-r","/tmp/ROOT.war","/www"]
#將匿名數(shù)據(jù)卷掛載到容器中的/www目錄下
volumeMounts:
- mountPath: /www
name: tomcat-volume
#創(chuàng)建tomcat容器
containers:
- image: oxnme/tomcat
imagePullPolicy: Always
name: tomcat
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
#將數(shù)據(jù)卷掛載到tomcat的代碼目錄下
volumeMounts:
- mountPath: /usr/local/tomcat/webapps/
name: tomcat-volume
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
terminationGracePeriodSeconds: 10
#創(chuàng)建匿名數(shù)據(jù)卷
volumes:
- name: tomcat-volume
emptyDir: {}
tomcat的文件yaml文件這樣就可以了,還是暴露主機(jī)的31111端口
然后就去配置我們的mysql數(shù)據(jù)庫(kù),創(chuàng)建數(shù)據(jù)庫(kù)并創(chuàng)建一個(gè)連接數(shù)據(jù)庫(kù)的用戶,給與權(quán)限
[root@server160 ~]# mysql -uroot -pMySQL@666
mysql> CREATE USER 'zrtest'@'%' IDENTIFIED BY 'MySQL@666';
Query OK, 0 rows affected (0.02 sec)
mysql> CREATE DATABASE Zrlog;
Query OK, 1 row affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON `Zrlog`.* TO 'zrtest'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| Zrlog |
| mysql |
| performance_schema |
| sys |
| zabbix |
+--------------------+
6 rows in set (0.00 sec)
數(shù)據(jù)庫(kù)這樣就配置好了
然后去配置我們自定義的Endpoint 和service
[root@server153 test]# cat endpoint.yaml
apiVersion: v1
kind: Endpoints
metadata:
name: mysql
namespace: default
#指定自定義的point的目標(biāo)地址
subsets:
- addresses:
#外部的reids ip
- ip: 192.168.121.160
# 外部redis的真實(shí)的工作端口
ports:
- port: 3306
# 定義端口的名稱,必須與 service 中的 ports.name 一致
name: mysqlport
---
#這里的service配置大家都熟悉了,主要就是上面的endpoint而已
kind: Service
apiVersion: v1
metadata:
name: mysql
namespace: default
spec:
ports:
- port: 3306
protocol: TCP
name: mysqlport
targetPort: 3306
type: ClusterIP
這樣配置就可以了,然后執(zhí)行配置文件
[root@server153 test]# kubectl apply -f tomcat-deploy.yaml
[root@server153 test]# kubectl apply -f endpoint.yaml
然后去查看mysql endpoint的詳細(xì)信息
[root@server153 test]# kubectl describe endpoints mysql
Name: mysql
Namespace: default
Labels: <none>
Annotations: <none>
Subsets:
Addresses: 192.168.121.160
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
mysqlport 3306 TCP
Events: <none>
還有service的信息
[root@server153 test]# kubectl describe services mysql
Name: mysql
Namespace: default
Labels: <none>
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.1.30.160
IPs: 10.1.30.160
Port: mysqlport 3306/TCP
TargetPort: 3306/TCP
Endpoints: 192.168.121.160:3306
Session Affinity: None
Events: <none>
可以看到service是代理到了160主機(jī),然后去瀏覽器訪問(wèn)31111端口安裝測(cè)試
查看數(shù)據(jù)庫(kù)內(nèi)容
mysql> use Zrlog;
mysql> show tables;
+-----------------+
| Tables_in_Zrlog |
+-----------------+
| comment |
| link |
| log |
| lognav |
| plugin |
| tag |
| type |
| user |
| website |
+-----------------+
9 rows in set (0.00 sec)
可以看到安裝完畢就是這樣的,只靠service的自動(dòng)發(fā)現(xiàn)服務(wù)是沒(méi)辦法訪問(wèn)到外部網(wǎng)絡(luò)的
所以自定義的Endpoint作用就體現(xiàn)出來(lái)了,這個(gè)還是比較有必要了解的
因?yàn)閿?shù)據(jù)庫(kù)數(shù)據(jù)的特殊性,一般是不容器化的文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-822068.html
希望對(duì)大家有幫助文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-822068.html
到了這里,關(guān)于k8s自定義Endpoint實(shí)現(xiàn)內(nèi)部pod訪問(wèn)外部應(yīng)用的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!