Kubectl:无法解析主机
Kubectl : Could not resolve host
我创建了一个节点,我是 运行 microk8s。我创建了一个 Pod 和 3 个副本集。我可以访问trought Cluster IP。但我不知道如何通过名称访问...请帮助我。我是 Kubenetes 的新手。
- 我创造
microk8s kubectl create deployment httpenv --image bretfisher/httpenv
- 规模
microk8s kubectl scale deployment/httpenv --replicas=3
- 公开
microk8s kubectl expose deployment/httpenv --port 8888
- 显示信息
microk8s kubectl get all
NAME READY STATUS RESTARTS AGE
pod/httpenv-bd844d85f-9xk9l 1/1 Running 0 18m
pod/httpenv-bd844d85f-jf85j 1/1 Running 0 18m
pod/httpenv-bd844d85f-p5kzg 1/1 Running 0 18m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/httpenv ClusterIP 10.152.183.91 <none> 8888/TCP 13m
service/kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 24h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/httpenv 3/3 3 3 18m
NAME DESIRED CURRENT READY AGE
replicaset.apps/httpenv-bd844d85f 3 3 3 18m
- 尝试访问
来自节点:
curl -i 10.152.183.91:8888
HTTP/1.1 200 OK
curl -i httpenv:8888
curl: (6) Could not resolve host: httpenv
您可以通过 3 种方式访问该服务,我只是 运行 您在我自己的 microk8s 集群中的示例。
- 端口将流量从 8888 转发到任何端口上的本地主机(例如:我已转发到 8888)。
- 如果您想从集群内访问该服务,您可以从集群中的任何 pod 运行 访问该服务(不过,安全问题可以使用策略等来处理)。
- 您可以将服务从类型:ClusterIP 转换为 NodePort
根据您上面给出的部署,我在本地运行,下面是pods 运行ning。在下面的端口转发命令中,我们将端口 8888 上来自 microk8s 集群的所有流量路由到您的本地主机 8888。然后您可以使用浏览器并导航到 http://localhost:8888,您将在那里看到输出。
k get po
httpenv-6fdc8554fb-j9q65 1/1 Running 0 14m
httpenv-6fdc8554fb-4fzr8 1/1 Running 0 14m
httpenv-6fdc8554fb-bsxgw 1/1 Running 0 14m
k port-forward httpenv-6fdc8554fb-j9q65 8888:8888 &
curl localhost:8888
Handling connection for 8888
{"HOME":"/root","HOSTNAME":"httpenv-6fdc8554fb-j9q65","KUBERNETES_PORT":"tcp://10.152.183.1:443","KUBERNETES_PORT_443_TCP":"tcp://10.152.183.1:443","KUBERNETES_PORT_443_TCP_ADDR":"10.152.183.1","KUBERNETES_PORT_443_TCP_PORT":"443","KUBERNETES_PORT_443_TCP_PROTO":"tcp","KUBERNETES_SERVICE_HOST":"10.152.183.1","KUBERNETES_SERVICE_PORT":"443","KUBERNETES_SERVICE_PORT_HTTPS":"443","PATH":"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}%
- 要访问您的 microk8s 集群中的服务,我们将 运行 一个简单的 busybox 容器。
k run -i --tty bb7 --image=busybox -- sh
# curl http://httpenv:8888
{"HOME":"/root","HOSTNAME":"httpenv-6fdc8554fb-bsxgw","KUBERNETES_PORT":"tcp://10.152.183.1:443","KUBERNETES_PORT_443_TCP":"tcp://10.152.183.1:443","KUBERNETES_PORT_443_TCP_ADDR":"10.152.183.1","KUBERNETES_PORT_443_TCP_PORT":"443","KUBERNETES_PORT_443_TCP_PROTO":"tcp","KUBERNETES_SERVICE_HOST":"10.152.183.1","KUBERNETES_SERVICE_PORT":"443","KUBERNETES_SERVICE_PORT_HTTPS":"443","PATH":"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}#
- 选项3是将服务类型从“ClusterIP”更改为“NodePort”,您可以在microk8s集群之外访问该服务。使用 ifconfig 命令获取本地 IP 并卷曲节点端口。
外汇:curl http://local-ip:31223
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2021-11-03T09:55:20Z"
labels:
app: httpenv
name: httpenv
namespace: default
resourceVersion: "2013136"
selfLink: /api/v1/namespaces/default/services/httpenv
uid: 908f4bbd-fef2-4435-b8c4-a80796e03b13
spec:
clusterIP: 10.152.183.170
clusterIPs:
- 10.152.183.170
externalTrafficPolicy: Cluster
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- nodePort: 31223
port: 8888
protocol: TCP
targetPort: 8888
selector:
app: httpenv
sessionAffinity: None
type: NodePort
k get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
httpenv NodePort 10.152.183.170 <none> 8888:31223/TCP 22m
我创建了一个节点,我是 运行 microk8s。我创建了一个 Pod 和 3 个副本集。我可以访问trought Cluster IP。但我不知道如何通过名称访问...请帮助我。我是 Kubenetes 的新手。
- 我创造
microk8s kubectl create deployment httpenv --image bretfisher/httpenv
- 规模
microk8s kubectl scale deployment/httpenv --replicas=3
- 公开
microk8s kubectl expose deployment/httpenv --port 8888
- 显示信息
microk8s kubectl get all
NAME READY STATUS RESTARTS AGE
pod/httpenv-bd844d85f-9xk9l 1/1 Running 0 18m
pod/httpenv-bd844d85f-jf85j 1/1 Running 0 18m
pod/httpenv-bd844d85f-p5kzg 1/1 Running 0 18m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/httpenv ClusterIP 10.152.183.91 <none> 8888/TCP 13m
service/kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 24h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/httpenv 3/3 3 3 18m
NAME DESIRED CURRENT READY AGE
replicaset.apps/httpenv-bd844d85f 3 3 3 18m
- 尝试访问
来自节点:
curl -i 10.152.183.91:8888
HTTP/1.1 200 OK
curl -i httpenv:8888
curl: (6) Could not resolve host: httpenv
您可以通过 3 种方式访问该服务,我只是 运行 您在我自己的 microk8s 集群中的示例。
- 端口将流量从 8888 转发到任何端口上的本地主机(例如:我已转发到 8888)。
- 如果您想从集群内访问该服务,您可以从集群中的任何 pod 运行 访问该服务(不过,安全问题可以使用策略等来处理)。
- 您可以将服务从类型:ClusterIP 转换为 NodePort
根据您上面给出的部署,我在本地运行,下面是pods 运行ning。在下面的端口转发命令中,我们将端口 8888 上来自 microk8s 集群的所有流量路由到您的本地主机 8888。然后您可以使用浏览器并导航到 http://localhost:8888,您将在那里看到输出。
k get po
httpenv-6fdc8554fb-j9q65 1/1 Running 0 14m
httpenv-6fdc8554fb-4fzr8 1/1 Running 0 14m
httpenv-6fdc8554fb-bsxgw 1/1 Running 0 14m
k port-forward httpenv-6fdc8554fb-j9q65 8888:8888 &
curl localhost:8888
Handling connection for 8888
{"HOME":"/root","HOSTNAME":"httpenv-6fdc8554fb-j9q65","KUBERNETES_PORT":"tcp://10.152.183.1:443","KUBERNETES_PORT_443_TCP":"tcp://10.152.183.1:443","KUBERNETES_PORT_443_TCP_ADDR":"10.152.183.1","KUBERNETES_PORT_443_TCP_PORT":"443","KUBERNETES_PORT_443_TCP_PROTO":"tcp","KUBERNETES_SERVICE_HOST":"10.152.183.1","KUBERNETES_SERVICE_PORT":"443","KUBERNETES_SERVICE_PORT_HTTPS":"443","PATH":"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}%
- 要访问您的 microk8s 集群中的服务,我们将 运行 一个简单的 busybox 容器。
k run -i --tty bb7 --image=busybox -- sh
# curl http://httpenv:8888
{"HOME":"/root","HOSTNAME":"httpenv-6fdc8554fb-bsxgw","KUBERNETES_PORT":"tcp://10.152.183.1:443","KUBERNETES_PORT_443_TCP":"tcp://10.152.183.1:443","KUBERNETES_PORT_443_TCP_ADDR":"10.152.183.1","KUBERNETES_PORT_443_TCP_PORT":"443","KUBERNETES_PORT_443_TCP_PROTO":"tcp","KUBERNETES_SERVICE_HOST":"10.152.183.1","KUBERNETES_SERVICE_PORT":"443","KUBERNETES_SERVICE_PORT_HTTPS":"443","PATH":"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}#
- 选项3是将服务类型从“ClusterIP”更改为“NodePort”,您可以在microk8s集群之外访问该服务。使用 ifconfig 命令获取本地 IP 并卷曲节点端口。
外汇:curl http://local-ip:31223
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2021-11-03T09:55:20Z"
labels:
app: httpenv
name: httpenv
namespace: default
resourceVersion: "2013136"
selfLink: /api/v1/namespaces/default/services/httpenv
uid: 908f4bbd-fef2-4435-b8c4-a80796e03b13
spec:
clusterIP: 10.152.183.170
clusterIPs:
- 10.152.183.170
externalTrafficPolicy: Cluster
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- nodePort: 31223
port: 8888
protocol: TCP
targetPort: 8888
selector:
app: httpenv
sessionAffinity: None
type: NodePort
k get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
httpenv NodePort 10.152.183.170 <none> 8888:31223/TCP 22m