How to deploy your blog into k8s
What we can learn from this post?
- Build the blog image;
- Deploy your blog image into the local K8s cluster.
Steps
- Update the Dockefile copy the statics files
into NGINX
1 2 3 4 5 6 7 8 9 10 11
| FROM node_base AS node_build RUN npm install
RUN npm run clean && npm run build
FROM nginx_base
COPY --from=node_build ./public /data/sites/blog COPY .docker/nginx/conf/nginx.conf /etc/nginx/nginx.conf COPY .docker/nginx/conf/app.conf.template /etc/nginx/conf.d/app.conf.template
|
- Build your docker images into the k8s cluster
1 2 3
| eval $(minikube docker-env) docker build -t node-base -f .docker/node/Dockerfile . docker build -t nginx-base -f .docker/nginx/Dockerfile ./.docker/nginx
|
1
| docker build -t blog-nginx -f .docker/nginx/Dockerfile.k8s .
|
1
| minikube image ls|grep blog
|
The output should be:
1
| docker.io/library/blog-nginx:latest
|
- Create the env for k8s
1 2 3 4 5 6 7
| apiVersion: v1 kind: ConfigMap metadata: name: blog-configmap namespace: default data: SITE: blog.example.com
|
- Create the deployment configures for k8s
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| apiVersion: v1 kind: Service metadata: name: blog labels: name: blog spec: ports: - name: http port: 80 targetPort: 80 type: NodePort selector: name: blog
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| apiVersion: apps/v1 kind: Deployment metadata: labels: name: blog name: blog spec: replicas: 1 selector: matchLabels: name: blog strategy: type: Recreate template: metadata: labels: name: blog spec: containers: - envFrom: - configMapRef: name: blog-configmap image: blog-nginx:latest imagePullPolicy: IfNotPresent name: blog ports: - containerPort: 80 restartPolicy: Always
|
- Create TLS secret for HTTPS
1 2 3 4
| openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout .docker/nginx/letsencrypt/blog.key \ -out .docker/nginx/letsencrypt/blog.crt -subj "/CN=blog.example.com/O=example.com" kubectl create secret tls blog-tls --cert=.docker/nginx/letsencrypt/blog.crt \ --key=.docker/nginx/letsencrypt/blog.key
|
- Add the ingress configure to expose the service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: blog-ingress namespace: default annotations: nginx.ingress.kubernetes.io/rewrite-target: / spec: tls: - hosts: - blog.example.com secretName: blog-tls ingressClassName: nginx rules: - host: blog.example.com http: paths: - path: / pathType: Prefix backend: service: name: blog port: number: 80
|
- Deploy the blog into minikube
- Verify the blog service and expose ports
1
| kubectl get service blog
|
The output is similar as:
1 2
| NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE blog NodePort 10.105.72.160 <none> 80:30500/TCP,443:30211/TCP 5m
|
- Get blog url
1
| minikube service blog --url
|
The output is similar to:
1 2
| http://192.168.49.2:30500 http://192.168.49.2:30211
|
- Modifier the hosts
1
| sudo -- sh -c "echo '$(minikube ip) blog.example.com' >> /etc/hosts"
|
- Check it in browser and enjoy your blog
1
| https://blog.example.com/
|