Skip to main content

Kubernetes ETCD Data Store Cluster with TLS Certificate

This article is all about configuration of ETCD kubernetes data store with high security  measurement. We will use TLS/SSL certificate so that all communication within cluster will secure and private.

Environment:
3 RHEL 7 / Centos 7 Server with minimal installation.
etcd and etcdctl binary which we can download from https://github.com/coreos/etcd/releases/
openssl.conf will provide in tutorial.
Inventory:
Node1: etcd-01.syshunt.com 192.168.43.45
Node2: etcd-02.syshunt.com 192.168.43.46
Node3: etcd-03.syshunt.com 192.168.43.47
Certificate Generation:
We will use openssl tool for certificate generation as it required for secure communication.
Create Certificate Authority (CA):
Create “etcd-certificate” directory and switch in etcd-certificate directory, will use this directory in our tutorial for all certificate generation,
[root@etcd-01]# mkdir /root/etcd-certificate
[root@etcd-01]# cd /root/etcd-certificate
[root@etcd-01 etcd-certificate]# openssl genrsa -out ca-key.pem 2048
[root@etcd-01 etcd-certificate]# openssl req -x509 -new -nodes -key ca-key.pem -days 10000 -out ca.pem -subj "/CN=etcd-ca"
ETCD node-1 certificate generation:
Create openssl.conf file with below given configuration,
[root@etcd-01 etcd-certificate]# vi openssl.conf
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name

[req_distinguished_name]

[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names

[ ssl_client ]
extendedKeyUsage = clientAuth, serverAuth
basicConstraints = CA:FALSE
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid,issuer
subjectAltName = @alt_names

[ v3_ca ]
basicConstraints = CA:TRUE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names
authorityKeyIdentifier=keyid:always,issuer

[alt_names]
DNS.1 = localhost
DNS.2 = etcd-01
IP.1 = 192.168.43.45
IP.2 = 127.0.0.1
Set openssl.conf location variable in your current shell,
[root@etcd-01 etcd-certificate]# CONFIG=`echo $PWD/openssl.conf`
Generate Certificates:
[root@etcd-01 etcd-certificate]# openssl genrsa -out member-etcd-01-key.pem 2048
[root@etcd-01 etcd-certificate]# openssl req -new -key member-etcd-01-key.pem -out member-etcd-01.csr -subj "/CN=etcd-01" -config ${CONFIG}
[root@etcd-01 etcd-certificate]# openssl x509 -req -in member-etcd-01.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out member-etcd-01.pem -days 3650 -extensions ssl_client -extfile ${CONFIG}
ETCD node-2 certificate generation:
Create openssl.conf file with below given configuration,
[root@etcd-01 etcd-certificate]# vi openssl.conf
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name

[req_distinguished_name]

[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names

[ ssl_client ]
extendedKeyUsage = clientAuth, serverAuth
basicConstraints = CA:FALSE
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid,issuer
subjectAltName = @alt_names

[ v3_ca ]
basicConstraints = CA:TRUE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names
authorityKeyIdentifier=keyid:always,issuer

[alt_names]
DNS.1 = localhost
DNS.2 = etcd-02
IP.1 = 192.168.43.46
IP.2 = 127.0.0.1
Generate Certificates:
[root@etcd-01 etcd-certificate]# openssl genrsa -out member-etcd-02-key.pem 2048
[root@etcd-01 etcd-certificate]# openssl req -new -key member-etcd-02-key.pem -out member-etcd-02.csr -subj "/CN=etcd-02" -config ${CONFIG}
[root@etcd-01 etcd-certificate]# openssl x509 -req -in member-etcd-02.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out member-etcd-02.pem -days 3650 -extensions ssl_client -extfile ${CONFIG}
ETCD node-3 certificate generation:
Create openssl.conf file with below given configuration,
[root@etcd-02 etcd-certificate]# vi openssl.conf
[req]
req_extensions = v3_req
distinguished_name = req_distinguished_name

[req_distinguished_name]

[ v3_req ]
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names

[ ssl_client ]
extendedKeyUsage = clientAuth, serverAuth
basicConstraints = CA:FALSE
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid,issuer
subjectAltName = @alt_names

[ v3_ca ]
basicConstraints = CA:TRUE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
subjectAltName = @alt_names
authorityKeyIdentifier=keyid:always,issuer

[alt_names]
DNS.1 = localhost
DNS.2 = etcd-03
IP.1 = 192.168.43.47
IP.2 = 127.0.0.1
Generate Certificates:
[root@etcd-01 etcd-certificate]# openssl genrsa -out member-etcd-03-key.pem 2048
[root@etcd-01 etcd-certificate]# openssl req -new -key member-etcd-03-key.pem -out member-etcd-03.csr -subj "/CN=etcd-03" -config ${CONFIG}
[root@etcd-01 etcd-certificate]# openssl x509 -req -in member-etcd-03.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out member-etcd-03.pem -days 3650 -extensions ssl_client -extfile ${CONFIG}
Copy all created certificates in every etcd nodes from “/etc/ssl/etcd-certificate” directory to “/etc/ssl/etcd/ssl/”:
[root@etcd-* etcd]# cp -rvp /root/etcd-certificate/*.pem /etc/ssl/etcd/ssl/
Install ETCD binary on all the 3 etcd Members:
Copy etcd and etcdctl binary on all 3 etcd members which you have downloaded form etcd github project page ( https://github.com/coreos/etcd/releases/ ),
[root@etcd-* etcd]# cp -vp etcd etcdctl /usr/bin
Create Etcd data directory (on all 3 members):
Etcd data directory is very important as etcd utility saves all cluster data in data directory,
[root@etcd-* etcd]# mkdir /var/lib/etcd
Create etcd user on all 3 nodes:
[root@etcd-* etcd]# useradd etcd -s /sbin/nologin -r -d /var/lib/etcd
Fixing permissions on all etcd nodes at required directory and files:
[root@etcd-* etcd]# chmod -Rv 550 /etc/ssl/etcd/
[root@etcd-* etcd]# chmod 440 /etc/ssl/etcd/ssl/*.pem
[root@etcd-* etcd]# chown -Rv etcd:etcd /etc/ssl/etcd/
[root@etcd-* etcd]# chown -Rv etcd:etcd /etc/ssl/etcd/*
[root@etcd-* etcd]# chown etcd:etcd /var/lib/etcd/
Setting Up etcd service daemon files on all etcd nodes:
It is required to create etcd service files on every etcd nodes so that we can start and enable etcd as service daemon.
[root@etcd-* etcd]# vi /usr/lib/systemd/system/docker.service
[Unit]
Description=etcd
After=network.target

[Service]
Type=notify
User=etcd
EnvironmentFile=/etc/etcd.env
ExecStart=/usr/bin/etcd
NotifyAccess=all
Restart=always
RestartSec=10s
LimitNOFILE=40000

[Install]
WantedBy=multi-user.target
ETCD Configuration File on every etcd node:
For configuring etcd with our custom configuration it required to create configuration file in “/etc/etcd.env” location because we are calling this environment file in etcd service file.
Etcd configuration file for etcd node1:
[root@etcd-1 etcd]# vi /etc/etcd.env
ETCD_DATA_DIR=/var/lib/etcd
ETCD_ADVERTISE_CLIENT_URLS=https://192.168.43.45:2379
ETCD_INITIAL_ADVERTISE_PEER_URLS=https://192.168.43.45:2380
ETCD_INITIAL_CLUSTER_STATE=new
ETCD_LISTEN_CLIENT_URLS=https://192.168.43.45:2379
ETCD_ELECTION_TIMEOUT=5000
ETCD_HEARTBEAT_INTERVAL=250
ETCD_LISTEN_PEER_URLS=https://192.168.43.45:2380
ETCD_NAME=etcd1
ETCD_PROXY=off
ETCD_INITIAL_CLUSTER=etcd1=https://192.168.43.45:2380,etcd2=https://192.168.43.46:2380,etcd3=https://192.168.43.47:2380
#ETCD_INITIAL_CLUSTER=etcd1=https://192.168.43.45:2380

# TLS settings
ETCD_TRUSTED_CA_FILE=/etc/ssl/etcd/ssl/ca.pem
ETCD_CERT_FILE=/etc/ssl/etcd/ssl/member-etcd-01.pem
ETCD_KEY_FILE=/etc/ssl/etcd/ssl/member-etcd-01-key.pem
ETCD_PEER_TRUSTED_CA_FILE=/etc/ssl/etcd/ssl/ca.pem
ETCD_PEER_CERT_FILE=/etc/ssl/etcd/ssl/member-etcd-01.pem
ETCD_PEER_KEY_FILE=/etc/ssl/etcd/ssl/member-etcd-01-key.pem
ETCD_PEER_CLIENT_CERT_AUTH=true
Etcd configuration file for etcd node2:
[root@etcd-2 etcd]# vi /etc/etcd.env
ETCD_DATA_DIR=/var/lib/etcd
ETCD_ADVERTISE_CLIENT_URLS=https://192.168.43.46:2379
ETCD_INITIAL_ADVERTISE_PEER_URLS=https://192.168.43.46:2380
ETCD_INITIAL_CLUSTER_STATE=new
ETCD_LISTEN_CLIENT_URLS=https://192.168.43.46:2379
ETCD_ELECTION_TIMEOUT=5000
ETCD_HEARTBEAT_INTERVAL=250
ETCD_LISTEN_PEER_URLS=https://192.168.43.46:2380
ETCD_NAME=etcd2
ETCD_PROXY=off
ETCD_INITIAL_CLUSTER=etcd1=https://192.168.43.45:2380,etcd2=https://192.168.43.46:2380,etcd3=https://192.168.43.47:2380
#ETCD_INITIAL_CLUSTER=etcd1=https://192.168.43.46:2380

# TLS settings
ETCD_TRUSTED_CA_FILE=/etc/ssl/etcd/ssl/ca.pem
ETCD_CERT_FILE=/etc/ssl/etcd/ssl/member-etcd-02.pem
ETCD_KEY_FILE=/etc/ssl/etcd/ssl/member-etcd-02-key.pem
ETCD_PEER_TRUSTED_CA_FILE=/etc/ssl/etcd/ssl/ca.pem
ETCD_PEER_CERT_FILE=/etc/ssl/etcd/ssl/member-etcd-02.pem
ETCD_PEER_KEY_FILE=/etc/ssl/etcd/ssl/member-etcd-02-key.pem
ETCD_PEER_CLIENT_CERT_AUTH=true
Etcd configuration file for etcd node3:
[root@etcd-* etcd]# vi /etc/etcd.env
ETCD_DATA_DIR=/var/lib/etcd
ETCD_ADVERTISE_CLIENT_URLS=https://192.168.43.47:2379
ETCD_INITIAL_ADVERTISE_PEER_URLS=https://192.168.43.47:2380
ETCD_INITIAL_CLUSTER_STATE=new
ETCD_LISTEN_CLIENT_URLS=https://192.168.43.47:2379
ETCD_ELECTION_TIMEOUT=5000
ETCD_HEARTBEAT_INTERVAL=250
ETCD_LISTEN_PEER_URLS=https://192.168.43.47:2380
ETCD_NAME=etcd3
ETCD_PROXY=off
ETCD_INITIAL_CLUSTER=etcd1=https://192.168.43.45:2380,etcd2=https://192.168.43.46:2380,etcd3=https://192.168.43.47:2380

# TLS settings
ETCD_TRUSTED_CA_FILE=/etc/ssl/etcd/ssl/ca.pem
ETCD_CERT_FILE=/etc/ssl/etcd/ssl/member-etcd-03.pem
ETCD_KEY_FILE=/etc/ssl/etcd/ssl/member-etcd-03-key.pem
ETCD_PEER_TRUSTED_CA_FILE=/etc/ssl/etcd/ssl/ca.pem
ETCD_PEER_CERT_FILE=/etc/ssl/etcd/ssl/member-etcd-03.pem
ETCD_PEER_KEY_FILE=/etc/ssl/etcd/ssl/member-etcd-03-key.pem
ETCD_PEER_CLIENT_CERT_AUTH=true
Applying ETCD configuration on systemd for every etcd nodes:
In systemd based system it is required to reload daemon on every etcd nodes after service file change.
[root@etcd-* etcd]# systemctl daemon-reload
Start and enable ETCD service on every:
Now we can start our etcd service by systemd command, It is also required to enable daemon on system boot time so we will need not to start service every time manually on system startup.
[root@etcd-* etcd]# systemctl start etcd
[root@etcd-* etcd]# systemctl enable etcd
Verifying ETCD cluster status:
For verifying etcd cluster status we can run below command on any etcd nodes.
[root@etcd-01 ~]# etcdctl -C https://192.168.43.45:2379 --ca-file /etc/ssl/etcd/ssl/ca.pem cluster-health
member 649628565489a99c is healthy: got healthy result from https://192.168.43.45:2379
member caa56683e6af0137 is healthy: got healthy result from https://192.168.43.46:2379
member dc4795c6ff3e6627 is healthy: got healthy result from https://192.168.43.47:2379
cluster is healthy
If cluster is showing healthy that indicated we have configured everything properly so it’s time to use etcd based data store in our kubernetes cluster . In our future tutorials we will demonstrate how can we setup highly secure kubernetes cluster.

Comments

Popular posts from this blog

Tomcat - Basic Clustering

Tomcat - Basic Clustering Till now we have 1 tomcat instance running and now we'll learn how to loadbalance multiple tomcat instances. For this let us have another tomcat instance configured first. Go to the  /opt  directory and copy  tomcat8  to  tomcat8-n2 # cp -prv tomcat8 tomcat8-n2 Now we know that we are using some ports for tomcat say 8080, 8005, 8009 For our additional tomcat instance we need to change the ports. If we don't then we might need to experience port conflict I am going to edit  server.xml  config file in the new node and increment the port numbers by 1 # cd /opt/tomcat8-n2/conf/ # vi server.xml 8005 -> 8006 8080 -> 8081 8443 -> 8444 8009 -> 8010 Now you need to add your new Catalina home, base and temp directories First you need to change the  catalina.sh  script and redirect the above 3 variables to the new variables Edit the  catalina.sh  script and add the following lines # vi...

CI-CD_004 - Git with Jenkins

Integrating Git with Jenkins If we are dealing with some specific code based builds where the code doesn't need any modification can be directly used for a build. But in some cases the code need to be changed several times depending on the need. In such case the periodic builds need to have those modifications. We use SCM (Source Code Management) to overcome this situation. And instead of manually copying the code to the build we can integrate the repositories from the SCM tools with our Jenkins builds. Configuration Global Settings In the Configure System page, the Git Plugin provides the following options: Global Config user.name Value: if provided git config user.name is called before builds. This can be overridden by individual projects. Global Config user.email Value: if provided git config user.email is called before builds. This can be overridden by individual projects. Create new accounts base on author/committer's email: if checked, upon parsing of git c...

Tomcat 8 Environment Setup

Tomcat 8 Environment Setup We need to add the Environment setups to the universal profile file. Let us edit  /etc/profile  and add the below lines CATALINA_BASE=/opt/tomcat8 CATALINA_HOME=/opt/tomcat8 CATALINA_TMPDIR=/opt/tomcat8/temp JAVA_HOME=/opt/jdk8 JRE_HOME=/opt/jdk8