#搭建及使用K8s集羣 <k8s集羣搭建>
文章目錄
#1. 機器準備
host name | ip |
---|---|
master | 192.168.6.39 |
node1 | 192.168.6.163 |
node2 | 192.168.6.94 |
##2. 需要在所有機器上執行
###2.1 關閉 && 禁用 防火牆、安裝 && 啓用 ntpd
#systemctl stop firewalld
#systemctl disable firewalld
#yum -y install ntp
#systemctl start ntpd
#systemctl enable ntpd
###2.2 同步所有集羣節點host文件
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.6.39 master
192.168.6.163 node1
192.168.6.94 node2
172.18.1.10 docker.hbg.io
#mkdir batch_edit_host
#cd batch_edit_host
#touch iplist.txt
# cat iplist.txt
192.168.6.39 master
192.168.6.163 node1
192.168.6.94 node2
#touch synhost.sh
# cat synhost.sh
#!/bin/bash
user='root' //root還是少用的好,雖然都這麼說,但還是喜歡直接用它
passwd='' //你的密碼
for ip in $(awk -F' ' '{print $1}' iplist.txt); do
(
/usr/bin/expect<<EOF
set timeout -1
spawn ssh-copy-id $user@$ip
expect {
"*yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r"}
}
expect eof
EOF
)
name=`grep $ip iplist.txt| awk -F' ' '{print $2}'`
ssh $user@$ip "/usr/bin/hostnamectl set-hostname $name"
scp /etc/hosts $user@$ip:/etc/hosts
done
#chmod 777 *
./synhost.sh
##3. Kubernetes Master節點的安裝與配置
###3.1 安裝 etcd、docker和Kubernetes
yum -y install etcd docker kubernetes
###3.2 編輯配置文件/etc/etcd/etcd.conf
ETCD_NAME=default
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379";
ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379";
###**3.3 編輯配置文件/etc/kubernetes/config **
# cat /etc/kubernetes/config
KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=0"
KUBE_ALLOW_PRIV="--allow-privileged=false"
KUBE_MASTER="--master=http://master:8080"
KUBE_MASTER="–master=http://master:8080"是將Kubernetes的apiserver進程的服務地址告訴Kubernetes的controller-manager, scheduler和proxy進程。
###3.4 編輯配置文件/etc/kubernetes/apiserver
# cat /etc/kubernetes/apiserver
###
# kubernetes system config
#
# The following values are used to configure the kube-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://127.0.0.1:2379"
# 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,ResourceQuota"
# Add your own!
KUBE_API_ARGS=""
KUBE_ADMISSION_CONTROL 要去掉 ServiceAccount,
這些配置讓apiserver進程在8080端口上監聽所有網絡接口,並告訴apiserver進程etcd服務的地址。
###3.5 啓動master
現在,啓動Kubernetes Master節點上的etcd, docker, apiserver, controller-manager和scheduler進程並查看其狀態:
# for SERVICES in etcd docker kube-apiserver kube-controller-manager kube-scheduler; do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
###3.6 在etcd裏定義flannel網絡配置
# etcdctl mk /atomic.io/network/config '{"Network":"172.17.0.0/16"}'
在隨後Kubernetes的Node節點搭建和配置時,我們可以看到,etcd裏的/atomic.io/network/config節點會被Node節點上的flannel用來創建網絡的iptables
現在我們可以使用kubectl get nodes命令來查看,當然,目前還沒有Node節點加入到該Kubernetes集羣,所以命令的執行結果是空的:
# kubectl get nodes
No resources found.
##4. Kubernetes Node節點的安裝與配置
###4.1 安裝 etcd、docker和Kubernetes
# yum -y install flannel docker kubernetes
###4.2 編輯配置文件/etc/sysconfig/flanneld
# cat /etc/sysconfig/flanneld
# Flanneld configuration options
# etcd url location. Point this to the server where etcd runs
FLANNEL_ETCD_ENDPOINTS="http://master:2379"
# etcd config key. This is the configuration key that flannel queries
# For address range assignment
FLANNEL_ETCD_PREFIX="/atomic.io/network"
# Any additional options that you want to pass
#FLANNEL_OPTIONS=""
配置信息告訴flannel進程etcd服務的位置以及在etcd上網絡配置信息的節點位置
###4.2 編輯配置文件/etc/kubernetes/config
對Node節點上的Kubernetes進行配置,兩臺Node節點上的配置文件/etc/kubernetes/config內容和Master節點相同,內容如下:
# cat /etc/kubernetes/config
KUBE_LOGTOSTDERR="--logtostderr=true"
KUBE_LOG_LEVEL="--v=0"
KUBE_ALLOW_PRIV="--allow-privileged=false"
KUBE_MASTER="--master=http://master:8080"
KUBE_MASTER="–master=http://master:8080"是將Kubernetes的apiserver進程的服務地址告訴Kubernetes的controller-manager, scheduler和proxy進程。
###4.3 編輯配置文件/etc/kubernetes/kubelet
兩臺Node節點上的/etc/kubernetes/kubelet配置文件內容略微有點不同,不同之處就是
KUBELET_HOSTNAME="–hostname-override=node1"
KUBELET_HOSTNAME="–hostname-override=node2"
cat /etc/kubernetes/kubelet
###
# kubernetes kubelet (minion) config
# 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=node1"
# location of the api-server
KUBELET_API_SERVER="--api-servers=http://master: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=""
###4.4 啓動node
分別在兩個Kubernetes Node節點上啓動kube-proxy kubelet docker和flanneld進程並查看其狀態:
啓動有可能有點慢,如果一直沒反應可把 master 和 node1 node2 機器重啓試試
# for SERVICES in kube-proxy kubelet docker flanneld; do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
##5. 驗證
在master上執行以下命令
# kubectl get nodes
NAME STATUS AGE
node1 Ready 10m
node2 Ready 5m
如果看到兩個節點都是ready 證明部署成功。
##6. 快速啓動腳本
在master 可啓動node
代碼如下:
# cat start_k8s_master.sh
for SERVICES in etcd docker kube-apiserver kube-controller-manager kube-scheduler; do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
# cat start_k8s_nodes.sh
#!/bin/bash
#變量定義
ip_array=("192.168.6.148" "192.168.6.149")
user="root"
remote_cmd="/root/start_k8s_node.sh"
#本地通過ssh執行遠程服務器的腳本
for ip in ${ip_array[*]}
do
if [ $ip = "192.168.1.1" ]; then
port="7777"
else
port="22"
fi
ssh -t -p $port $user@$ip "$remote_cmd"
done
在node的/root/下創建以下
# cat start_k8s_node.sh
for SERVICES in kube-proxy kubelet docker flanneld; do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
最近自己寫了一個小程序電商,賣洗化用品,家裏人是立白代理,進貨價格低,售價自然低,大家覺着文章對自己有幫助的,正好也需要購買洗化用品的,可以去電商看看,買一些,不勝感激。
附上一張小程序二維碼