1 | initial version |
To access localhost while running an Angular application within a Kubernetes cluster, you will need to configure your Kubernetes deployment to use a port-forwarding mechanism. Here are the steps you can follow:
apiVersion: apps/v1
kind: Deployment
metadata:
name: angular-app
labels:
app: angular-app
spec:
replicas: 1
selector:
matchLabels:
app: angular-app
template:
metadata:
labels:
app: angular-app
spec:
containers:
- name: angular-app
image: your-angular-image
ports:
- containerPort: 4200
apiVersion: v1
kind: Service
metadata:
name: angular-service
spec:
selector:
app: angular-app
ports:
- name: http
port: 80
targetPort: 4200
type: NodePort
In the above YAML file, the service is configured to run on port 80 and target port 4200, which is the port on which the Angular application is running. The service is also configured to use NodePort, which maps the container port to a port on the node.
With the Kubernetes deployment and service files defined, deploy them to your Kubernetes cluster using the following kubectl commands:
kubectl apply -f deployment.yml
kubectl apply -f service.yml
kubectl get services
Find the NodePort assigned to the angular-service, and use it to access the Angular application within the Kubernetes cluster by using your Kubernetes cluster IP or hostname followed by the NodePort.
You should now be able to access the Angular application within the Kubernetes cluster through a browser or other HTTP client.