k3sCoreDNS使用,实现内部服务互通
安装K3s后,会默认安装CoreDNS,实现集群内动态解析,在pod失效后会动态重新生成IP
## 查看CoreDNS安装运行情况> kubectl get pod,svc -n kube-system
NAME READY STATUS RESTARTS AGE
pod/coredns-d798c9dd-67mmc 1/1 Running 2 11d
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kube-dns ClusterIP 10.43.0.10 <none> 53/UDP,53/TCP,9153/TCP 11d
验证CoreDNS
安装dig用来校验域名解析情况
apiVersion: v1kind: Pod
metadata:
name: dig
namespace: default
spec:
containers:
- name: dig
image: docker.io/azukiapp/dig
command:
- sleep
- "3600"
imagePullPolicy: IfNotPresent
restartPolicy: Always
测试
>kubectl exec -it dig -- nslookup kubernetesServer: 10.43.0.10
Address: 10.43.0.10#53
Name: kubernetes.default.svc.cluster.local
Address: 10.43.0.1
# 验证kubernetes正常,接下来验证外网
> kubectl exec -it dig -- nslookup www.baidu.com
Server: 10.43.0.10
Address: 10.43.0.10#53
Non-authoritative answer:
www.baidu.com canonical name = www.a.shifen.com.
Name: www.a.shifen.com
Address: 61.135.169.125
Name: www.a.shifen.com
Address: 61.135.169.121
接下来验证内部Pod服务mysql
需要已安装mysql服务,本处需要说明Pod域名生成规则
pod域名生成规则:<svc-name>.<namespace>.svc.cluster.local
> kubectl exec -it dig -- nslookup mysql-1.dev-service.svc.cluster.localServer: 10.43.0.10
Address: 10.43.0.10#53
Name: mysql-1.dev-service.svc.cluster.local
Address: 10.43.34.109
使用nacos for mysql做测试
Tips : 测试使用nacos的镜像是1.2.0,测试时一直报错,无法连接,最后查看是因nacos的数据库链接版本较低导致的,mysql-connection 是5.1.34。如果数据库无法更改的情况下可以自己下载nacos源码打包修改驱动包版本。我自己因是测试环境,将数据库降为5.7版本后恢复正常。
apiVersion: v1kind: ConfigMap
metadata:
namespace: dev-app
name: nacos-cm
data:
mysql.host: "mysql-1.dev-service.svc.cluster.local"
mysql.db.name: "nacos_devtest"
mysql.port: "3306"
mysql.user: "nacos"
mysql.password: "nacos"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
namespace: dev-app
name: nacos-service
spec:
serviceName: nacos-service
replicas: 1
template:
metadata:
labels:
app: nacos-service
annotations:
pod.alpha.kubernetes.io/initialized: "true"
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: "app"
operator: In
values:
- nacos-service
topologyKey: "kubernetes.io/hostname"
containers:
- name: nacos-service
imagePullPolicy: Always
image: nacos/nacos-server:1.2.0
resources:
requests:
memory: "2Gi"
cpu: "500m"
ports:
- containerPort: 8848
name: client
env:
- name: NACOS_REPLICAS
value: "1"
- name: MYSQL_SERVICE_HOST
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.host
- name: MYSQL_SERVICE_DB_NAME
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.db.name
- name: MYSQL_SERVICE_PORT
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.port
- name: MYSQL_SERVICE_USER
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.user
- name: MYSQL_SERVICE_PASSWORD
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.password
- name: NACOS_SERVER_PORT
value: "8848"
- name: PREFER_HOST_MODE
value: "hostname"
- name: NACOS_SERVERS
value: "nacos-service.dev-app.svc.cluster.local:8848"
selector:
matchLabels:
app: nacos-service
以上是 k3sCoreDNS使用,实现内部服务互通 的全部内容, 来源链接: utcz.com/z/515655.html