# [GUIDE] Running MicroK8s   & Nginx-ingress on CentOS 7

The goal of this guide is to help you get a single-machine Kubernetes cluster up and running. The cluster will be exposed to outside connections through Nginx-ingress. An Ingress and a Service will be created to send traffic to a Deployment running a Hello World NodeJS application. So, if you ever wanted to run your personal website on k8s without a hitch this guide is your go-to.

The guide is meant to be hands-on and no-nonsense. I won’t go into too much, if any, detail about individual components and I will assume you’re familiar with the following:

*   [CentOS 7](https://www.centos.org/)
*   [Snap](https://snapcraft.io)
*   [Docker](https://www.docker.com/)
*   [Kubernetes](https://kubernetes.io/)
*   [MicroK8s](https://microk8s.io/)
*   [Nginx](https://www.nginx.com/)
*   [Nginx-ingress](https://kubernetes.github.io/ingress-nginx/)
*   [NodeJS](https://nodejs.org/)
*   [DNS](https://en.wikipedia.org/wiki/List_of_DNS_record_types)

NOTE: You can follow along on a different Linux distro or even Windows. I‘ve added references to installation guides at the bottom of each section where applicable. Windows users should skip to the Kubernetes section. I have not attempted installation on a OS other than CentOS 7 & 8. The attempt on CentOS 8 was unsuccessful due to a failure to install `snap` .

### Pre-requistes

You have:

*   Access to a machine running CentOS 7.
*   Root access to that machine.
*   A Fully Qualified Domain Name at your disposal.

### CentOS 7

#### Snap

We’ll need snap to install MicroK8s. So lets install it.

*   Login to your CentOS machine as `root`
*   Run these commands to install snap and enable backwards compatibility with classic snaps:

yum install snapd  
systemctl enable --now snapd.socket  
ln -s /var/lib/snapd/snap /snap

*   Restart your CentOS instance or start a new terminal session. This allows the `snapd` binaries to be loaded.
*   IMPORTANT: Do not prepend `sudo` to the `snapd`installation commands. Follow [this link](https://forum.snapcraft.io/t/how-to-fix-snap-binaries-not-found/9469/43?u=robertdiebels) to find out why.

Linux distros: Follow the instructions on [this page](https://snapcraft.io/docs/installing-snapd) to install snap for your specific distro.

### Kubernetes

#### MicroK8s

We’ll need MicroK8s so we can run your personal website.

*   Run these commands to install MicroK8s:

snap install microk8s --classic --channel=1.16/stable  
microk8s.status --wait-ready

Windows: You will need `multipass` to install MicroK8s. Go to [this page](https://multipass.run/#install), follow the instructions and install `multipass` on Windows. When you’re done installing `multipass`, go to [this page](https://microk8s.io/), scroll down until you see the Windows logo, click the logo and follow the instructions.

#### Nginx Ingress

To expose your Kubernetes cluster to the big, bad outside world we’ll need another piece of software. Kubernetes has a concept called an [Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/index.html) to do this. A standalone installation of [Nginx](https://www.nginx.com/) could perform the same job in a non-Kubernetes system. So, lets have a divine union and use [Nginx-ingress](https://github.com/kubernetes/ingress-nginx) in your cluster.

*   Run these commands to, alias microk8s.kubectl, enable install Nginx-ingress

sudo snap alias microk8s.kubectl kubectl  
microk8s.enable ingress  
kubectl apply -f [https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml](https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml)

Windows: If-and-only-if you’ve installed `multipass` you can follow the commands above.

#### Creating the necessary objects

Now that your Kubernetes cluster is ready, lets deploy your personal website. First we’ll deploy the NodeJS server hosting the website.

IMPORTANT: All of the files below contain a `example.com` string-value. Replace that string with the actual domain-name of your own `personal-website.com`. You should also replace the image `heroku/nodejs-hello-world` with an image containing your web-application.

*   Create a file named `deployment.yml` and edit its content to equal the following:

apiVersion: apps/v1  
kind: Deployment  
metadata:  
  name: personal-website-deployment  
  labels:  
    uri: example.com  
spec:  
  selector:  
    matchLabels:  
      uri: example.com  
  template:  
    metadata:  
      labels:  
        uri: example.com  
    spec:  
      containers:  
        - name: personal-website  
          image: heroku/nodejs-hello-world  
          ports:  
            - containerPort: 5000

*   Create a file named service.yml and edit its content to equal the following:

apiVersion: v1  
kind: Service  
metadata:  
  name: personal-website-service  
spec:  
  selector:  
    uri: example.com  
  type: ClusterIP  
  ports:  
    - name: http  
      protocol: TCP  
      port: 80  
      targetPort: 5000

*   Create a file named ingress.yml and edit its content to equal the following:

apiVersion: networking.k8s.io/v1beta1  
kind: Ingress  
metadata:  
  name: personal-website-ingress  
  annotations:  
    nginx.ingress.kubernetes.io/rewrite-target: /  
spec:  
  rules:  
    - host: example.com  
      http:  
        paths:  
          - backend:  
              serviceName: personal-website-service  
              servicePort: 80

*   Now run the following command to create the Kubernetes Objects necessary for your application.

kubectl create -f deployment.yml -f service.yml -f ingress.yml

### Networking

#### DNS

This guide assumes following DNS prerequisites.

*   You own a domain-name.
*   You’ve setup an A- or AAAA-record that refers to the IP-adress of your CentOS-machine.

#### Localhost

If you do not want to proxy traffic from `example.com` and you just want to fiddle around locally. Removing the `host`key from the `rules` map in the `ingress.yml` file should do the trick. I’ve only tested the DNS settings mentioned above so I cannot ensure it will.

Aaaaand boom goes the dynamite! Liked this post? Clap. Disliked this post? Comment. Have something interesting to add or have I missed something? Comment! I’ll always consider any of your suggestions and/or improvements.
