Kubernetes deploy nginx example - unix1998/technical_notes GitHub Wiki
In Kubernetes, deploying an NGINX application typically involves several steps, each defined in a separate YAML file. However, for simplicity, you can also combine multiple configurations into a single YAML file. Here are the essential components you typically need:
- Namespace (optional): Define a namespace to isolate your application resources.
- Deployment: Define the deployment configuration for the NGINX pods.
- Service: Define a service to expose the NGINX deployment within the cluster.
- Ingress (optional): Define an ingress resource to expose the NGINX service externally.
You can either use separate YAML files for each component or combine them into a single YAML file. Here’s how you can do it in either case:
Separate YAML Files
1. Namespace (optional)
# namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: my-namespace
2. Deployment
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: my-namespace
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
3. Service
# service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: my-namespace
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
4. Ingress (optional)
# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
namespace: my-namespace
spec:
rules:
- host: my-nginx.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
Combined YAML File
You can also combine the above configurations into a single YAML file.
# combined.yaml
---
apiVersion: v1
kind: Namespace
metadata:
name: my-namespace
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: my-namespace
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: my-namespace
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
namespace: my-namespace
spec:
rules:
- host: my-nginx.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80
Applying the Configuration
To apply these configurations, you can use the kubectl apply
command.
Separate YAML Files
kubectl apply -f namespace.yaml
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f ingress.yaml
Combined YAML File
kubectl apply -f combined.yaml
By following these steps, you can deploy an NGINX application in Kubernetes with either separate or combined YAML files, depending on your preference.