This tutorial provides an in-depth quick-start guide to deploying a local Kubernetes cluster with Minikube, generating and managing private certificates with Ansible, deploying the Harbor container registry with Helm, and deploying a secure demo web service. It is aimed at engineers with a basic understanding of Kubernetes, Helm, and Ansible and shows how these tools work together to build a secure development environment.
Prerequisites
Before you begin, make sure your system meets the following requirements:
- Minikube: For running a local Kubernetes cluster (Minikube installation).
- Docker: For building and pushing container images (Docker installation).
- kubectl: The Kubernetes command-line tool (kubectl installation).
- Helm: For deploying Harbor (Helm installation).
- Ansible: For automating certificate management (Ansible installation).
- A basic understanding of Kubernetes, Helm, and Ansible.
- Optional: The Kubernetes client for Python (
pip install kubernetes) for use with Ansible'sk8smodule.
Part 1: Setting Up Kubernetes with Minikube
Minikube is a lightweight tool that runs a single-node Kubernetes cluster locally, making it ideal for development and testing.
Start Minikube:
BASHminikube startThis starts a single-node Kubernetes cluster.
Enable the Ingress add-on: Minikube's Ingress add-on provides an NGINX Ingress controller to handle external HTTP/HTTPS traffic.
BASHminikube addons enable ingressVerify that the Ingress controller is running:
BASHkubectl get pods -n ingress-nginxConfigure DNS: Get Minikube's IP address:
BASHminikube ipEdit the
/etc/hostsfile (administrator privileges required) and add the following entry, replacing<minikube ip>with the actual IP address:<minikube ip> harbor.example.com demo.example.comThis lets you access the services through
harbor.example.comanddemo.example.com.
Part 2: Generating Private Certificates with Ansible
We'll use Ansible to automate the generation of self-signed certificates and store them as Kubernetes Secrets for Harbor and the web service to use. Self-signed certificates are suitable for development environments, but use certificates from a trusted CA in production.
Install the Ansible collections: Install the
community.cryptoandkubernetes.corecollections:BASHansible-galaxy collection install community.crypto ansible-galaxy collection install kubernetes.coreCreate the certificates directory:
BASHmkdir certsCreate the Ansible playbook: Create a file named
generate_certs.yamlwith the following contents:
Run the playbook:
BASHansible-playbook generate_certs.yamlThis generates self-signed certificates for
harbor.example.comanddemo.example.comand creates Kubernetes Secrets namedharbor-tlsanddemo-tls.
Part 3: Deploying Harbor
Harbor is an open-source container registry that provides role-based access control, image vulnerability scanning, and image signing. We'll deploy Harbor with Helm and configure TLS to ensure secure communication.
Add the Harbor Helm repository:
BASHhelm repo add harbor https://helm.goharbor.io helm repo updateCreate the Helm configuration file: Create a file named
values.yamlwith the following contents:
This configuration enables Ingress, uses the harbor-tls Secret to provide TLS, and sets the Harbor administrator password.
Install Harbor:
BASHhelm install harbor harbor/harbor -f values.yamlVerify the deployment: Check that the Harbor components are running:
BASHkubectl get podsGo to
https://harbor.example.com(you may need to accept the self-signed certificate warning) and sign in with the usernameadminand passwordmysecretpassword.
Part 4: Deploying the Demo Web Service
We'll create a simple Node.js web app, build a Docker image, push it to Harbor, and deploy it to Kubernetes, exposing it over HTTPS through Ingress.
Create the web app: Create a directory named
demo-appand add the following files:app.js:
package.json:
Dockerfile:
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]
Build and push the Docker image:
Log in to Harbor:
BASHdocker login harbor.example.com -u admin -p mysecretpasswordBuild the image:
BASHdocker build -t harbor.example.com/library/demo-app:latest .Push the image:
BASHdocker push harbor.example.com/library/demo-app:latest
Create the Kubernetes resources:
- Create
deployment.yaml:
- Create
- Create
service.yaml:
apiVersion: v1
kind: Service
metadata:
name: demo-service
spec:
selector:
app: demo-app
ports:
- protocol: TCP
port: 80
targetPort: 3000
- Create
ingress.yaml:
Apply the Kubernetes resources:
BASHkubectl apply -f deployment.yaml kubectl apply -f service.yaml kubectl apply -f ingress.yamlAccess the web service: Open your browser and go to
https://demo.example.com. Because the site uses a self-signed certificate, you may need to accept the security warning. You should see the message: “Hello from demo app!”