今天來記錄下K8s部署,由於咱們這是測試環境,所以只部署一臺master和一臺node,後續我會記錄多master高可用部署,使用kubeadm部署k8s 1.9.0。
一、安裝之前準備工作
1、設置主機名,分別為master,node
hostnamectl set-hostname ${hostname}
2、禁用自帶的firewall
systemctl stop firewalld && systemctl disable firewalld
3、關selinux
sed -i -re '/^\s*SELINUX=/s/^/#/' -e '$i\\SELINUX=disabled' /etc/selinux/config
4、開啟IP和iptables的bridge的轉發功能
echo "
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
" >> /etc/sysctl.conf
sysctl -p
5、若之前安裝過,可以清理一下(正常安裝請忽略)
kubeadm reset
二、安裝所需要軟件包
1、安裝docker
yum install docker -y
2、安裝k8s軟件包,因為被deny網絡原因,可以下載離線包,私信k8s即可獲取。
socat-1.7.3.2-2.el7.x86_64.rpm
kubectl-1.9.0-0.x86_64.rpm kubeadm-1.9.0-0.x86_64.rpm
kubelet-1.9.0-0.x86_64.rpm kubernetes-cni-0.6.0-0.x86_64.rpm
3、設為自啟動,並啟動docker,kubelet服務
4、因為deny國外一些網絡的原因以下tar是離線下載的,私信k8s即可獲取。
導入到docker中
for x in `ls *.tar`;do docker load
三、master上配置
1、以下操作在master上進行初始化,初始化完畢後生成一串token,務必記下,這是node加入master使用的。
kubeadm init --kubernetes-version=v1.9.0 --token-ttl 0 --pod-network-cidr=10.244.0.0/16
--kubernetes-version=v1.8.4 :不指定會去google獲取版本信息,所以你懂的~~~
--token-ttl 0 :token永不過期,不指定默認24h後過期
--pod-network-cidr=10.244.0.0/16 :如果要正常使用Flannel,則確保使用此配置
完成後按提示設置變量如下
vi ~/.bashrc
export KUBECONFIG=/etc/kubernetes/admin.conf
source ~/.bashrc
編輯配置文件 /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
新增:
Environment="KUBELET_MY_ARGS=--runtime-cgroups=/systemd/system.slice --kubelet-cgroups=/systemd/system.slice"
修改ExecStart: 在末尾新增 $KUBELET_MY_ARGS
systemctl daemon-reload &&systemctl restart kubelet
3、安裝網絡flannel
https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml
4、設置所有master參見調度
kubectl taint nodes --all node-role.kubernetes.io/master-
5、安裝監控
5.1、安裝儀表板
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.8.1/src/deploy/recommended/kubernetes-dashboard.yaml
cat <
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: kubernetes-dashboard
labels:
k8s-app: kubernetes-dashboard
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: kubernetes-dashboard
namespace: kube-system
EOF
kubectl create -f kubernetes-dashboard-admin.yaml
5.2、安裝負責收集pod各項指標, 比如cpu, 內存, 網絡等
kubectl create -f heapster-rbac.yaml
kubectl create -f heapster.yaml
安裝這個是存儲pod信息相關的數據庫, heapster獲取數據之後, 可以指定存儲在InfluxDB
kubectl create -f influxdb.yaml
這個主要是用於顯示InfluxDB裡面的數據情況, 可以讓我們很直觀看到數據變化
kubectl create -f grafana.yaml
默認情況下, grafana是不需要密碼的,
wget https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/grafana.yaml
wget https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/rbac/heapster-rbac.yaml
wget https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/heapster.yaml
wget https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/influxdb.yaml
5.3、啟動UI和監控面板
kubectl proxy --address=0.0.0.0 -p 8001 --accept-hosts='^.*' &
獲取kubernetes-dashboard用戶的token
kubectl -n kube-system describe secret kubernetes-dashboard-token
用IP 127.0.0.1/ui 和獲取的token即可進入UI管理面板。
儀表板頁面訪問:masterIP或nodeIP:8001
監控頁面訪問:masterIP或nodeIP:32667這是使用的nodeport
四、node配置
在master上執行 kubeadm token create --print-join-command,獲取認證內容,並在每臺node上運行,即可受權。或者直接將master初始化生成的token執行即可。
kubeadm join --token xxxxxxxx 192.168.x.x:6443 --discovery-token-ca-cert-hash xxxxxxxxx
kubectl get node 查看集群狀態
五、測試部署應用
新建一臺tomcat的pod副本為2,以下為yaml配置文件。tomcat-rc.yaml tomcat-service.yaml,我使用nodeport直接暴露pod的端口,後續使用traefik代理進行訪問。
kubectl create -f tomcat-rc.yaml -f tomcat-service.yaml
cat tomcat-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: tomcat
labels:
app: tomcat
spec:
replicas: 2
template:
metadata:
labels:
app: tomcat
spec:
containers:
- name: tomcat
image: docker.io/tomcat
ports:
- containerPort: 8080
cat tomcat-service.yaml
apiVersion: v1
kind: Service
metadata:
name: tomcat
spec:
type: NodePort
ports:
- port: 8080
nodePort: 30001
selector:
app: tomcat
查看pod運行狀態
kubectl get pods -o wide
kubectl get svc -o wide
因為上邊使用nodeport方式來訪問tomcat的我們來測試下。
訪問master:30001
訪問node:30001
訪問正常,搭建完畢。如果需要離線包私信索取即可。
閱讀更多 運維小筆記 的文章