I created one node and I'm running microk8s. I created one Pod and 3 replicasets. I can access trought Cluster IP. But I don't know how to access through the name... Please help me. I'm new in Kubenetes.
- I create
microk8s kubectl create deployment httpenv --image bretfisher/httpenv
- Scale
microk8s kubectl scale deployment/httpenv --replicas=3
- Expose
microk8s kubectl expose deployment/httpenv --port 8888
- Show Info
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
- Try to access
From node :
curl -i 10.152.183.91:8888
HTTP/1.1 200 OK
curl -i httpenv:8888
curl: (6) Could not resolve host: httpenv
CodePudding user response:
You can access the service in 3 ways, i just ran your example in my own microk8s cluster.
- Port forward traffic from 8888 to you localhost on any port(ex: i have forwarded to 8888).
- If you want to access the service from within the cluster, you can access the service from any pod running in your cluster(security issue though, can be handled using policies etc).
- You can conver the service from type: ClusterIP to NodePort
According to the deployment you have given above, which i ran on my local, below are the pods running. In the port-forward command below, we are routing all the traffic from microk8s cluster on port 8888 to your localhost 8888. You can then use your browser and navigate to http://localhost:8888, you will see the output there.
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"}%
- To access the service within your microk8s cluster, we will run a simple busybox container.
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"}#
- option 3 is to change the service type from "ClusterIP" to "NodePort" and you can access the service outside of the microk8s cluster. Get your local-ip using ifconfig command and curl the node port.
forex: 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