小丸子學Kubernetes系列之——Kubernetes安裝與使用

wxjzqym發表於2016-03-07
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=
gpgcheck=0


2.安裝kubernetes(所有主機)
yum -y install --enablerepo=virt7-testing kubernetes


3.安裝etcd(master主機)
yum install


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="


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="

# 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="

# 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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章