0.环境信息
主机名 IP 角色
hadoop2 10.1.245.147 master
hadoop3 10.1.245.152 node
1.配置k8s的yum源(所有主机)
vi /etc/yum.repos.d/virt-testing.repo
[virt7-testing]
name=virt7-testing
baseurl=http://cbs.centos.org/repos/virt7-docker-common-testing/x86_64/os/
gpgcheck=0
2.安装kubernetes(所有主机)
yum -y install --enablerepo=virt7-testing kubernetes
3.安装etcd(master主机)
yum install http://cbs.centos.org/kojifiles/packages/etcd/0.4.6/7.el7.centos/x86_64/etcd-0.4.6-7.el7.centos.x86_64.rpm
4.配置hosts文件(所有主机)
more /etc/hosts
10.1.245.147 hadoop2
10.1.245.152 hadoop3
5.编辑kubernetes配置文件
vi /etc/kubernetes/config
# logging to stderr means we get it in the systemd journal
KUBE_LOGTOSTDERR="--logtostderr=true"
# journal message level, 0 is debug
KUBE_LOG_LEVEL="--v=0"
# Should this cluster be allowed to run privileged docker containers
KUBE_ALLOW_PRIV="--allow-privileged=false"
# How the controller-manager, scheduler, and proxy find the apiserver
KUBE_MASTER="--master=http://hadoop2:8080"
6.关闭防火墙(所有主机)
systemctl disable iptables-services firewalld
systemctl stop iptables-services firewalld
7.编辑apiserver的配置文件(master主机)
vi /etc/kubernetes/apiserver
# The address on the local server to listen to.
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"
# The port on the local server to listen on.
KUBE_API_PORT="--port=8080"
# Port minions listen on
KUBELET_PORT="--kubelet-port=10250"
# Comma separated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd-servers=http://hadoop2:4001"
# Address range to use for services
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"
# default admission control policies
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota"
# Add your own!
KUBE_API_ARGS=""
注:默认安装的etcd的监听端口在4001,所以etcd-servers必须指定端口为4001,否则apiserver服务会启动失败。
8.启动k8s的服务(master主机)
more start_k8s.sh
#!/bin/bash
for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
./start_k8s.sh
9.编辑kubelet配置文件(node主机)
vi /etc/kubernetes/kubelet
# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
KUBELET_ADDRESS="--address=0.0.0.0"
# The port for the info server to serve on
KUBELET_PORT="--port=10250"
# You may leave this blank to use the actual hostname
KUBELET_HOSTNAME="--hostname-override=hadoop3"
# location of the api-server
KUBELET_API_SERVER="--api-servers=http://hadoop2:8080"
# pod infrastructure container
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"
# Add your own!
KUBELET_ARGS=""
10.启动kubelet服务(node主机)
more start_kubelet.sh
#!/bin/bash
for SERVICES in kube-proxy kubelet docker; do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
./start_ kubelet .sh
11.使用kubectl命令来验证安装k8s成功(master主机)
kubectl cluster-info
kubectl get nodes
kubectl describe nodes hadoop3
12.创建一个mysql的pod
12.1 编写pod的配置文件
vi mysql.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysql
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: mysql
ports:
- containerPort: 3306
hostPort: 3307
注:这里需要指定环境变量MYSQL_ROOT_PASSWORD,否则创建pod会失败,具体错误信息如下:
Mar 7 16:33:57 localhost journal: error: database is uninitialized and password option is not specified
Mar 7 16:33:57 localhost journal: You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
12.2 创建mysql pod
kubectl create -f mysql.yaml
注:创建pod过程中遇到如下错误:
Error from server: error when creating "mysql.yaml": Pod "mysql" is forbidden: no API token found for service account default/default, retry after the token is automatically created and added to the service account
解决方法:
vi /etc/kubernetes/apiserver
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"
修改KUBE_ADMISSION_CONTROL参数,将ServiceAccount选项去掉,然后重启kube-apiserver
systemctl stop kube-apiserver
systemctl start kube-apiserver
kubectl create -f mysql.yaml
pod "mysql" created
13.测试mysql pod
[root@hadoop2 ~]# mysql -uroot -pmysql2 -hhadoop3 -P3307
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.11 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
至此Kubernetes初体验成功,哈哈!
主机名 IP 角色
hadoop2 10.1.245.147 master
hadoop3 10.1.245.152 node
1.配置k8s的yum源(所有主机)
vi /etc/yum.repos.d/virt-testing.repo
[virt7-testing]
name=virt7-testing
baseurl=http://cbs.centos.org/repos/virt7-docker-common-testing/x86_64/os/
gpgcheck=0
2.安装kubernetes(所有主机)
yum -y install --enablerepo=virt7-testing kubernetes
3.安装etcd(master主机)
yum install http://cbs.centos.org/kojifiles/packages/etcd/0.4.6/7.el7.centos/x86_64/etcd-0.4.6-7.el7.centos.x86_64.rpm
4.配置hosts文件(所有主机)
more /etc/hosts
10.1.245.147 hadoop2
10.1.245.152 hadoop3
5.编辑kubernetes配置文件
vi /etc/kubernetes/config
# logging to stderr means we get it in the systemd journal
KUBE_LOGTOSTDERR="--logtostderr=true"
# journal message level, 0 is debug
KUBE_LOG_LEVEL="--v=0"
# Should this cluster be allowed to run privileged docker containers
KUBE_ALLOW_PRIV="--allow-privileged=false"
# How the controller-manager, scheduler, and proxy find the apiserver
KUBE_MASTER="--master=http://hadoop2:8080"
6.关闭防火墙(所有主机)
systemctl disable iptables-services firewalld
systemctl stop iptables-services firewalld
7.编辑apiserver的配置文件(master主机)
vi /etc/kubernetes/apiserver
# The address on the local server to listen to.
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"
# The port on the local server to listen on.
KUBE_API_PORT="--port=8080"
# Port minions listen on
KUBELET_PORT="--kubelet-port=10250"
# Comma separated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd-servers=http://hadoop2:4001"
# Address range to use for services
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"
# default admission control policies
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota"
# Add your own!
KUBE_API_ARGS=""
注:默认安装的etcd的监听端口在4001,所以etcd-servers必须指定端口为4001,否则apiserver服务会启动失败。
8.启动k8s的服务(master主机)
more start_k8s.sh
#!/bin/bash
for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
./start_k8s.sh
9.编辑kubelet配置文件(node主机)
vi /etc/kubernetes/kubelet
# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
KUBELET_ADDRESS="--address=0.0.0.0"
# The port for the info server to serve on
KUBELET_PORT="--port=10250"
# You may leave this blank to use the actual hostname
KUBELET_HOSTNAME="--hostname-override=hadoop3"
# location of the api-server
KUBELET_API_SERVER="--api-servers=http://hadoop2:8080"
# pod infrastructure container
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"
# Add your own!
KUBELET_ARGS=""
10.启动kubelet服务(node主机)
more start_kubelet.sh
#!/bin/bash
for SERVICES in kube-proxy kubelet docker; do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
./start_ kubelet .sh
11.使用kubectl命令来验证安装k8s成功(master主机)
kubectl cluster-info
kubectl get nodes
kubectl describe nodes hadoop3
12.创建一个mysql的pod
12.1 编写pod的配置文件
vi mysql.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysql
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: mysql
ports:
- containerPort: 3306
hostPort: 3307
注:这里需要指定环境变量MYSQL_ROOT_PASSWORD,否则创建pod会失败,具体错误信息如下:
Mar 7 16:33:57 localhost journal: error: database is uninitialized and password option is not specified
Mar 7 16:33:57 localhost journal: You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD
12.2 创建mysql pod
kubectl create -f mysql.yaml
注:创建pod过程中遇到如下错误:
Error from server: error when creating "mysql.yaml": Pod "mysql" is forbidden: no API token found for service account default/default, retry after the token is automatically created and added to the service account
解决方法:
vi /etc/kubernetes/apiserver
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"
修改KUBE_ADMISSION_CONTROL参数,将ServiceAccount选项去掉,然后重启kube-apiserver
systemctl stop kube-apiserver
systemctl start kube-apiserver
kubectl create -f mysql.yaml
pod "mysql" created
13.测试mysql pod
[root@hadoop2 ~]# mysql -uroot -pmysql2 -hhadoop3 -P3307
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.11 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
至此Kubernetes初体验成功,哈哈!
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/20801486/viewspace-2050906/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/20801486/viewspace-2050906/