参考文档:
接上文:ubuntu20.04下使用juju+maas环境部署k8s-8-基本操作-1-访问 kubernetes dashboard
k8s 向外提供服务有几种办法:
1 port-foward
2 node-port
3 ingress
例如,有个pod 叫做microbot-75cfc77b98-58rqh ,它通过80端口对外提供服务,那么我就可以使用kubectl port-forward pod/microbot-75cfc77b98-58rqh 8080:80
将其映射到本机的8080端口上。你可以使用kubectl port-forward -h查看更多用法。
查看服务:
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 34d
microbot ClusterIP 10.152.183.8 <none> 80/TCP 2d1h
web NodePort 10.152.183.239 <none> 8080:31961/TCP 23d
查看pod
kubectl get pod
NAME READY STATUS RESTARTS AGE
microbot-75cfc77b98-58rqh 1/1 Running 0 2d1h
microbot-75cfc77b98-h2ttn 1/1 Running 0 2d1h
microbot-75cfc77b98-zt5rx 1/1 Running 0 2d1h
端口映射:
kubectl port-forward pod/microbot-75cfc77b98-58rqh 8080:80 &
查看链接:
curl http://localhost:8080
Handling connection for 8080
<!DOCTYPE html>
<html>
<style type="text/css">
.centered
{
text-align:center;
margin-top:0px;
margin-bottom:0px;
padding:0px;
}
</style>
<body>
<p class="centered"><img src="microbot.png" alt="microbot"/></p>
<p class="centered">Container hostname: microbot-75cfc77b98-58rqh</p>
</body>
</html>
查看服务:
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 33d
microbot ClusterIP 10.152.183.8 <none> 80/TCP 22h
web NodePort 10.152.183.239 <none> 8080:31961/TCP 21d
查看服务microbot:
kubectl get svc microbot -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
microbot ClusterIP 10.152.183.8 <none> 80/TCP 22h app=microbot
查看详细的microbot服务内容:
kubectl describe svc microbot
Name: microbot
Namespace: default
Labels: app=microbot
Annotations: <none>
Selector: app=microbot
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.152.183.8
IPs: 10.152.183.8
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.1.50.8:80,10.1.89.10:80,10.1.89.9:80
Session Affinity: None
Events: <none>
在kubernetes-dashboard中查看ingress的配置:
kind: Ingress
apiVersion: networking.k8s.io/v1beta1
metadata:
name: microbot-ingress
namespace: default
uid: e8d52971-8263-4875-8b1a-2016b711e99a
resourceVersion: '4594539'
generation: 1
creationTimestamp: '2021-08-04T07:14:24Z'
annotations:
kubectl.kubernetes.io/last-applied-configuration: >
{"apiVersion":"networking.k8s.io/v1","kind":"Ingress","metadata":{"annotations":{},"name":"microbot-ingress","namespace":"default"},"spec":{"rules":[{"host":"microbot.10.0.4.18.nip.io","http":{"paths":[{"backend":{"service":{"name":"microbot","port":{"number":80}}},"path":"/","pathType":"Prefix"}]}}]}}
managedFields:
- manager: kubectl-client-side-apply
operation: Update
apiVersion: networking.k8s.io/v1
time: '2021-08-04T07:14:24Z'
fieldsType: FieldsV1
fieldsV1:
f:metadata:
f:annotations:
.: {}
f:kubectl.kubernetes.io/last-applied-configuration: {}
f:spec:
f:rules: {}
- manager: nginx-ingress-controller
operation: Update
apiVersion: networking.k8s.io/v1beta1
time: '2021-08-04T07:14:27Z'
fieldsType: FieldsV1
fieldsV1:
f:status:
f:loadBalancer:
f:ingress: {}
spec:
rules:
- host: microbot.10.0.4.18.nip.io
http:
paths:
- path: /
pathType: Prefix
backend:
serviceName: microbot
servicePort: 80
status:
loadBalancer:
ingress:
- ip: 10.0.4.18
- ip: 10.0.4.19
- ip: 10.0.4.22
命令查看ingress
kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
microbot-ingress <none> microbot.10.0.4.18.nip.io 10.0.4.18,10.0.4.19,10.0.4.22 80 22h
kubectl describe ingress microbot-ingress
Name: microbot-ingress
Namespace: default
Address: 10.0.4.18,10.0.4.19,10.0.4.22
Default backend: default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
Host Path Backends
---- ---- --------
microbot.10.0.4.18.nip.io
/ microbot:80 (10.1.50.8:80,10.1.89.10:80,10.1.89.9:80)
Annotations: <none>
Events: <none>
查看namespace
kubectl get ns
NAME STATUS AGE
default Active 33d
ingress-nginx-kubernetes-worker Active 33d
kube-node-lease Active 33d
kube-public Active 33d
kube-system Active 33d
kubernetes-dashboard Active 33d
查看namespace default 中pod
kubectl describe pod -n default
Name: microbot-75cfc77b98-58rqh
Namespace: default
Priority: 0
Node: moral-locust/10.0.4.22
Start Time: Wed, 04 Aug 2021 15:14:25 +0800
Labels: app=microbot
pod-template-hash=75cfc77b98
Annotations: kubernetes.io/psp: privileged
Status: Running
IP: 10.1.89.9
IPs:
IP: 10.1.89.9
Controlled By: ReplicaSet/microbot-75cfc77b98
Containers:
microbot:
Container ID: containerd://fee49d008623358f69f9b772b4389ded608db7d4f57c7065c029ca2a622980bf
Image: rocks.canonical.com:443/cdk/cdkbot/microbot-amd64:latest
Image ID: rocks.canonical.com:443/cdk/cdkbot/microbot-amd64@sha256:9c949ba3afdb530eaa3a6e6e67126bd7543e4a21b092384e431246dd0141d6c1
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Wed, 04 Aug 2021 15:14:30 +0800
Ready: True
Restart Count: 0
Liveness: http-get http://:80/ delay=5s timeout=30s period=10s #success=1 #failure=3
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-znxkc (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-znxkc:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>
Name: microbot-75cfc77b98-h2ttn
Namespace: default
Priority: 0
Node: moral-locust/10.0.4.22
Start Time: Wed, 04 Aug 2021 15:14:25 +0800
Labels: app=microbot
pod-template-hash=75cfc77b98
Annotations: kubernetes.io/psp: privileged
Status: Running
IP: 10.1.89.10
IPs:
IP: 10.1.89.10
Controlled By: ReplicaSet/microbot-75cfc77b98
Containers:
microbot:
Container ID: containerd://3ebdee79058a395f5adcab46085a2dfa5334de975cafff65c6f6a56af26e1d8d
Image: rocks.canonical.com:443/cdk/cdkbot/microbot-amd64:latest
Image ID: rocks.canonical.com:443/cdk/cdkbot/microbot-amd64@sha256:9c949ba3afdb530eaa3a6e6e67126bd7543e4a21b092384e431246dd0141d6c1
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Wed, 04 Aug 2021 15:14:33 +0800
Ready: True
Restart Count: 0
Liveness: http-get http://:80/ delay=5s timeout=30s period=10s #success=1 #failure=3
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-xmxln (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-xmxln:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>
Name: microbot-75cfc77b98-zt5rx
Namespace: default
Priority: 0
Node: crack-raptor/10.0.4.18
Start Time: Wed, 04 Aug 2021 15:14:25 +0800
Labels: app=microbot
pod-template-hash=75cfc77b98
Annotations: kubernetes.io/psp: privileged
Status: Running
IP: 10.1.50.8
IPs:
IP: 10.1.50.8
Controlled By: ReplicaSet/microbot-75cfc77b98
Containers:
microbot:
Container ID: containerd://6c9a3dcc859e080911a75363cee9b497f3ee7d0f9f0af190ac7bb0687bdc80f6
Image: rocks.canonical.com:443/cdk/cdkbot/microbot-amd64:latest
Image ID: rocks.canonical.com:443/cdk/cdkbot/microbot-amd64@sha256:9c949ba3afdb530eaa3a6e6e67126bd7543e4a21b092384e431246dd0141d6c1
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Wed, 04 Aug 2021 15:14:33 +0800
Ready: True
Restart Count: 0
Liveness: http-get http://:80/ delay=5s timeout=30s period=10s #success=1 #failure=3
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-zz444 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-zz444:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>
编辑本机的 /etc/hosts
vim /etc/hosts
10.0.4.18 microbot.10.0.4.18.nip.io
重启网络服务
netplan apply
测试链接:
curl microbot.10.0.4.18.nip.io
<!DOCTYPE html>
<html>
<style type="text/css">
.centered
{
text-align:center;
margin-top:0px;
margin-bottom:0px;
padding:0px;
}
</style>
<body>
<p class="centered"><img src="microbot.png" alt="microbot"/></p>
<p class="centered">Container hostname: microbot-75cfc77b98-58rqh</p>
</body>
</html>
root@vivodo-3:~# curl 10.0.4.18
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>