After we learn how to build and create k3s cluster on this article, we will try to build simple nginx application on K3s cluster with Let’s Encrypt for the SSL provider.
- install the cert-manager
kubectl create namespace cert-manager
kubectl apply --validate=false -f https://github.com/jetstack/cert-manager/releases/download/v1.0.4/cert-manager.yaml
- create cluster issuer for Let’s Encrypt
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: letsencrypt-prod
spec:
acme:
server: https://acme-v02.api.letsencrypt.org/directory
email: [email protected]
privateKeySecretRef:
name: letsencrypt-prod
solvers:
- http01:
ingress:
class: nginx
- Apply the issuer
kubectl apply -f issuer.yaml
- Create a deployment for Nginx
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.19
ports:
- containerPort: 80
- containerPort: 443
- Apply the deployment
kubectl apply -f deployment.yaml
- Create a service to expose the Nginx deployment
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- name: http
port: 80
targetPort: 80
- name: https
port: 443
targetPort: 443
type: ClusterIP
- Apply the service
kubectl apply -f service.yaml
- Create an ingress resource to route traffic to the Nginx service
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
cert-manager.io/cluster-issuer: letsencrypt-prod
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
spec:
tls:
- hosts:
- settingserver.com
secretName: settingserver-com-tls
rules:
- host: settingserver.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
name: https
- Apply the ingress
kubectl apply -f ingress.yaml
- Verify the deployment, service, and ingress
kubectl get deployments
kubectl get services
kubectl get ingresses
These are the basic steps to build a simple Nginx application with HTTPS on a K3s cluster. You need to replace the placeholder values with your own certificates and domain name. Additionally, you may need to configure the firewall rules and DNS settings to allow external traffic to reach the ingress controller.
Leave a Reply