Tuesday, November 20, 2018

Highly Available Clusters with kubeadm

In CentOs 7, we´ll install Kubernetes with the following command:

yum install - and kubernetes etcd

For it to work, we must have the Centos-Extras repository enabled.

Once the packages are installed we can start booting services.

Booting services in the Master

systemctl start etcd
systemctl start kube-controller-manager
systemctl start kube-scheduler
systemctl start kube-apiserver

Start-up of the services in each of the nodes

systemctl start docker
systemctl start kube-proxy
systemctl start kubelet

you will notice that a new network interface called docker0 has been created.



POD Configuration

We will create a file in JSON format like the one below. If you remember, in the article Installation and configuration of Dockers (Containers) in Centos 7 we already downloaded the Apache container, so we will use it to configure the POD.

This time, I will start it in the local port 9090, since for the 8080 I have another service listening:

root@Centos7 kubernetes]# docker run -dit -name apachetest -p 9090:80 -v /tmp/ws/:/usr/local/apache2/htdocs/ httpd
7983a74eee23fa59abd434ad5107896e2b2a1a5b9539c5770e6d1c8549eeb060
[root@Centos7 kubernetes]#
[root@Centos7 kubernetes]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7983a74eee23 httpd "httpd-foreground" About a minute ago Up About a minute 0.0.0.0:9090->80/tcp apachetest
[root@Centos7 kubernetes]#
[root@Centos7 kubernetes]# curl -s http://localhost:9090
<html>
<body>
I'm a container
</body>
</html>
[root@Centos7 kubernetes]#
The JSON file would be as follows:
[root@Centos7 kubernetes]# cat container-httpd-rc.json
{ "kind": "ReplicationController",
"apiVersion": "v1″,
"metadata":{ "name": "apachepod-controller" },
"spec":{
"replicas":3,
"selector":{ "name": "apachepod" },
"template:{
"metadata":{
"labels":{ "name": "apachepod" }
},
"spec":{
"containers:[ {
"name": "apachepod",
"image": "docker.io/httpd",
"ports:[ {
"containerPort":80,
"protocol": "TCP"
} ]
} ]
}
}
}
}
[root@Centos7 kubernetes]#
Next, we apply the settings:
- If we haven't previously created a key, we have to create it for the first time:
[root@Centos7 kubernetes]# openssl genrsa -out /tmp/serviceaccount.key 2048
Generating RSA private key, 2048 bit long modulus
……………………….+++
……………………………………………………+++
e is 65537 (0x10001)

We edit the file /etc/kubernetes/apiserver, adding:

KUBE_API_ARGS="-service_account_key_file=/tmp/serviceaccount.key"
We edit the file /etc/kubernetes/controller-manager, adding:
KUBE_CONTROLLER_MANAGER_ARGS="-service_account_private_key_file=/tmp/serviceaccount.key"

We restart the Kubernetes service:

root@Centos7 kubernetes]# systemctl restart etcd
[root@Centos7 kubernetes]# systemctl restart kube-controller-manager
[root@Centos7 kubernetes]# systemctl restart kube-scheduler
[root@Centos7 kubernetes]# systemctl restart kube-apiserver
[root@Centos7 kubernetes]#

- Once the key is generated, we can finally create our POD from the previously created JSON file:

[root@Centos7 kubernetes]# kubectl create -f container-httpd-rc.json
replicationcontroller "apachepod-controller" created
[root@Centos7 kubernetes]#
[root@Centos7 kubernetes]# kubectl get pod
NAME READY STATUS RESTARTS AGE
apachepod-controller-10xv7 0/1 Pending 0 50s
apachepod-controller-dx8nr 0/1 Pending 0 50s
apachepod-controller-nm5k4 0/1 Pending 0 50s
[root@Centos7 kubernetes]#
[root@Centos7 kubernetes]# kubectl get replicationcontrollers
NAME DESIRED CURRENT READY AGE
apachepod-controller 3 3 0 1m
[root@Centos7 kubernetes]#
If you wish, you can scale the number of PODs in real time:
root@Centos7 kubernetes]# kubectl scale rc apachepod-controller -replicas=4
replicationcontroller "apachepod-controller" scaled
[root@Centos7 kubernetes]# kubectl get replicationcontrollers
NAME DESIRED CURRENT READY AGE
apachepod-controller 4 4 0 2m
[root@Centos7 kubernetes]# kubectl get pod
NAME READY STATUS RESTARTS AGE
apachepod-controller-10xv7 0/1 Pending 0 3m
apachepod-controller-dx8nr 0/1 Pending 0 3m
apachepod-controller-ksrdc 0/1 Pending 0 14s
apachepod-controller-nm5k4 0/1 Pending 0 3m
[root@Centos7 kubernetes]#

Creation of the nodes that will form part of the kubernetes cluster:

[root@Centos7 kubernetes]# cat nodes.json
{
"kind": "Node",
"apiVersion": "v1",
"metadata": {
"name": "10.0.0.2",
"labels: {
"environment: production,
"name": "kubernete1"
}
}
}
{
"kind": "Node",
"apiVersion": "v1",
"metadata": {
"name": "10.0.0.3",
"labels: {
"environment: production,
"name": "kubernete2"
}
}
}
[root@Centos7 kubernetes]#
[root@Centos7 kubernetes]# kubectl create -f nodes.json
node "10.0.0.2" created
node "10.0.0.3" created
[root@Centos7 kubernetes]#