Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Creating a Certificate Authority and TLS Certificates for Kubernetes

The various components of Kubernetes require certificates in order to authenticate with one another. Provisioning a certificate authority and using it to generate those certificates is a necessary step in bootstrapping a Kubernetes cluster from scratch. This activity will guide you through the process of provisioning a certificate authority and generating the certificates Kubernetes needs.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 0m
Published
Clock icon Sep 28, 2018

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Provision the certificate authority (CA).

    You can provision the certificate authority like so:

    {
    
    cat > ca-config.json << EOF
    {
      "signing": {
        "default": {
          "expiry": "8760h"
        },
        "profiles": {
          "kubernetes": {
            "usages": ["signing", "key encipherment", "server auth", "client auth"],
            "expiry": "8760h"
          }
        }
      }
    }
    EOF
    
    cat > ca-csr.json << EOF
    {
      "CN": "Kubernetes",
      "key": {
        "algo": "rsa",
        "size": 2048
      },
      "names": [
        {
          "C": "US",
          "L": "Portland",
          "O": "Kubernetes",
          "OU": "CA",
          "ST": "Oregon"
        }
      ]
    }
    EOF
    
    cfssl gencert -initca ca-csr.json | cfssljson -bare ca
    
    }
    
  2. Challenge

    Generate the necessary Kubernetes client certs, as well as kubelet client certs for two worker nodes.

    Use these commands to generate the client certs.

    Admin client cert:

    {
    
    cat > admin-csr.json << EOF
    {
      "CN": "admin",
      "key": {
        "algo": "rsa",
        "size": 2048
      },
      "names": [
        {
          "C": "US",
          "L": "Portland",
          "O": "system:masters",
          "OU": "Kubernetes The Hard Way",
          "ST": "Oregon"
        }
      ]
    }
    EOF
    
    cfssl gencert 
      -ca=ca.pem 
      -ca-key=ca-key.pem 
      -config=ca-config.json 
      -profile=kubernetes 
      admin-csr.json | cfssljson -bare admin
    
    }
    

    Kubelet client certs:

    {
    cat > worker0.mylabserver.com-csr.json << EOF
    {
      "CN": "system:node:worker0.mylabserver.com",
      "key": {
        "algo": "rsa",
        "size": 2048
      },
      "names": [
        {
          "C": "US",
          "L": "Portland",
          "O": "system:nodes",
          "OU": "Kubernetes The Hard Way",
          "ST": "Oregon"
        }
      ]
    }
    EOF
    
    cfssl gencert 
      -ca=ca.pem 
      -ca-key=ca-key.pem 
      -config=ca-config.json 
      -hostname=172.34.1.0,worker0.mylabserver.com 
      -profile=kubernetes 
      worker0.mylabserver.com-csr.json | cfssljson -bare worker0.mylabserver.com
    
    cat > worker1.mylabserver.com-csr.json << EOF
    {
      "CN": "system:node:worker1.mylabserver.com",
      "key": {
        "algo": "rsa",
        "size": 2048
      },
      "names": [
        {
          "C": "US",
          "L": "Portland",
          "O": "system:nodes",
          "OU": "Kubernetes The Hard Way",
          "ST": "Oregon"
        }
      ]
    }
    EOF
    
    cfssl gencert 
      -ca=ca.pem 
      -ca-key=ca-key.pem 
      -config=ca-config.json 
      -hostname=172.34.1.1,worker1.mylabserver.com 
      -profile=kubernetes 
      worker1.mylabserver.com-csr.json | cfssljson -bare worker1.mylabserver.com
    
    }
    

    Kube Controller Manager client cert:

    {
    
    cat > kube-controller-manager-csr.json << EOF
    {
      "CN": "system:kube-controller-manager",
      "key": {
        "algo": "rsa",
        "size": 2048
      },
      "names": [
        {
          "C": "US",
          "L": "Portland",
          "O": "system:kube-controller-manager",
          "OU": "Kubernetes The Hard Way",
          "ST": "Oregon"
        }
      ]
    }
    EOF
    
    cfssl gencert 
      -ca=ca.pem 
      -ca-key=ca-key.pem 
      -config=ca-config.json 
      -profile=kubernetes 
      kube-controller-manager-csr.json | cfssljson -bare kube-controller-manager
    
    }
    

    Kube Proxy client cert:

    {
    
    cat > kube-proxy-csr.json << EOF
    {
      "CN": "system:kube-proxy",
      "key": {
        "algo": "rsa",
        "size": 2048
      },
      "names": [
        {
          "C": "US",
          "L": "Portland",
          "O": "system:node-proxier",
          "OU": "Kubernetes The Hard Way",
          "ST": "Oregon"
        }
      ]
    }
    EOF
    
    cfssl gencert 
      -ca=ca.pem 
      -ca-key=ca-key.pem 
      -config=ca-config.json 
      -profile=kubernetes 
      kube-proxy-csr.json | cfssljson -bare kube-proxy
    
    }
    

    Kube Scheduler client cert:

    {
    
    cat > kube-scheduler-csr.json << EOF
    {
      "CN": "system:kube-scheduler",
      "key": {
        "algo": "rsa",
        "size": 2048
      },
      "names": [
        {
          "C": "US",
          "L": "Portland",
          "O": "system:kube-scheduler",
          "OU": "Kubernetes The Hard Way",
          "ST": "Oregon"
        }
      ]
    }
    EOF
    
    cfssl gencert 
      -ca=ca.pem 
      -ca-key=ca-key.pem 
      -config=ca-config.json 
      -profile=kubernetes 
      kube-scheduler-csr.json | cfssljson -bare kube-scheduler
    
    }
    
  3. Challenge

    Generate the Kubernetes API server certificate.

    You can generate the Kubernetes API server certificate like so:

    {
    
    cat > kubernetes-csr.json << EOF
    {
      "CN": "kubernetes",
      "key": {
        "algo": "rsa",
        "size": 2048
      },
      "names": [
        {
          "C": "US",
          "L": "Portland",
          "O": "Kubernetes",
          "OU": "Kubernetes The Hard Way",
          "ST": "Oregon"
        }
      ]
    }
    EOF
    
    cfssl gencert 
      -ca=ca.pem 
      -ca-key=ca-key.pem 
      -config=ca-config.json 
      -hostname=10.32.0.1,172.34.0.0,controller0.mylabserver.com,172.34.0.1,controller1.mylabserver.com,172.34.2.0,kubernetes.mylabserver.com,127.0.0.1,localhost,kubernetes.default 
      -profile=kubernetes 
      kubernetes-csr.json | cfssljson -bare kubernetes
    
    }
    
  4. Challenge

    Generate a Kubernetes service account key pair.

    To generate the service account key pair, do the following:

    {
    
    cat > service-account-csr.json << EOF
    {
      "CN": "service-accounts",
      "key": {
        "algo": "rsa",
        "size": 2048
      },
      "names": [
        {
          "C": "US",
          "L": "Portland",
          "O": "Kubernetes",
          "OU": "Kubernetes The Hard Way",
          "ST": "Oregon"
        }
      ]
    }
    EOF
    
    cfssl gencert 
      -ca=ca.pem 
      -ca-key=ca-key.pem 
      -config=ca-config.json 
      -profile=kubernetes 
      service-account-csr.json | cfssljson -bare service-account
    
    }
    

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans