1. 程式人生 > 實用技巧 >CentOS下的安裝測試K8S

CentOS下的安裝測試K8S

master: 192.168.0.100

node1: 192.168.0.105
node2: 192.168.0.106

一,k8s基本操作命令:

k8s全名:kubernetes,
容器編排工具,說白了就是自動化管理容器的工具。包括:

  • 服務自動註冊和發現
  • 負載均衡
  • 健康監測
  • 自動申縮
  • 冗餘容災

基本操作命令:

建立:kubectl create -f xxx.yaml

查詢:
kubectl get pod yourPodName

kubectl describe pod yourPodName //這個我一般用來排查機器無故沒有Running

刪除:kubectl delete pod yourPodName
強制刪除: kubectl delete pod PODNAME 
--force --grace-period=0 更新:kubectl replace /path/to/yourNewYaml.yaml

二,k8s_master安裝配置:

2.1,關閉selinux,關閉防火牆,升級yum

setenforce 0
systemctl stop firewalld.service
yum update

2.2,安裝並配置k8s

yum -y install epel-release
yum -y install etcd kubernetes-master

#配置
vi /etc/etcd/etcd.conf vi /etc/kubernetes/apiserver

2.3,啟動etcd、kube-apiserver、kube-controller-manager、kube-scheduler等服務,並設定開機啟動

for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; 
do systemctl restart $SERVICES;
systemctl enable $SERVICES;
systemctl status $SERVICES ;
done

2.4,定義flannel網路
etcdctl mk /atomic.io/network/config '{"Network":"172.17.0.0/16"}'


三,k8s_node安裝配置:

3.1,關閉selinux,關閉防火牆,升級yum

setenforce 0
systemctl stop firewalld.service
yum update

3.2,安裝docker,並配置阿里雲加速

yum install docker
docker -v ##檢視版本
service docker start ##開啟docker服務才能用其他命令
chkconfig docker on
docker images

配置vim /etc/docker/daemon.json

{
"registry-mirrors": [ "https://cd6xo91e.mirror.aliyuncs.com"]
}

3.3,安裝並配置k8s,,並啟動

yum -y install epel-release
yum -y install flannel kubernetes-node

#配置
vi /etc/sysconfig/flanneld
vi /etc/kubernetes/config
vi /etc/kubernetes/kubelet

3.4,node節點機上啟動kube-proxy,kubelet,docker,flanneld等服務,並設定開機啟動

for SERVICES in kube-proxy kubelet docker flanneld;
do systemctl restart $SERVICES;
systemctl enable $SERVICES;
systemctl status $SERVICES;
done

四,編排

建立/root/k8s/tomcat01.yaml檔案

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: myweb
spec:
  replicas: 3
  template:
    metadata:
      name: test-namespace
      labels:
        app: myweb
    spec:
      containers:
      - name: myweb
        image: docker.io/nginx:1.18.0-alpine
        ports:
        - containerPort: 80

---
apiVersion: v1
kind: Service
metadata:
  name: myweb
spec:
   ports:
   - port: 80
     targetPort: 80
     nodePort: 31111
   selector:
     app: myweb
   type: NodePort

nginx開放的80,如果是tomcat開放的8080,檔案裡面80都改成8080即可

kubectl create -f tomcat01.yaml

#如果配置錯了,建立了一個錯誤的service和pod,可以用刪除命令,配置正確後再重新建立一遍
kubectl delete -f tomcat01.yaml

檢查一下pod和service:

kubectl get pods -o wide

kubectl get svc

#如果兩個pods的狀態都是ContainerCreating,而不是running,說明kubectl create -f xxx操作是失敗的,察看日誌
kubectl describe pods myweb-3889544643-3sxrb

五、排障,測試

5.1 錯誤:redhat-ca.crt
解決方法:

yum install *rhsm* -y

5.2 錯誤:kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Fail
解決方法:

master:
通過環境變數,配置引數
[root@k8s-master dns]# vi ~/.bash_profile #新增下面三行

export DNS_SERVER_IP="10.254.10.2"
export DNS_DOMAIN="cluster.local"
export DNS_REPLICAS=1

設定 Cluster DNS Service的IP為 10.254.10.2(不能和已分配的IP重複,如10.0.10.0),Cluster DNS的本地域為 cluster.local。

node:
在vi /etc/kubernetes/kubelet 配置檔案中新增如下內容即可

KUBE_ARGS="--cluster-dns=10.254.10.2 --cluster-domain=cluster.local"
重啟
systemctl daemon
-reload; systemctl restart kubelet