over 5 years ago
我們都知道無狀態服務比較好,可以很方便的動態去橫向擴展你的服務數量,如果你聽過 pet & cattle 就會知道不是所有服務都是無狀態,像 Mysql、Redis、Nginx 都是會有用到檔案系統來儲存,這時候你就無法調動這服務也無法擴容
結合 Ceph 提供的功能我們可以做到
1.異常狀態恢復或搬移
2.水平擴容 scale out
主機列表
先準備一台部屬用的,我們就拿 裝 Kubernetes 的那台 Ansible-Control 繼續用,
但這次是用官方部屬工具,而不是用 Ansible。
主機名 | IP | 服務 |
---|---|---|
ceph-deploy | 192.168.31.109 | 原Ansible |
mon1 | 192.168.31.108 | Ceph.Mon |
osd1 | 192.168.31.157 | Ceph.osd |
osd2 | 192.168.31.164 | Ceph.osd |
每一台都要做
關閉SELINUX
關閉方式有兩個
立即生效,重開機後恢復
setenforce 0
永久性修改(要重啟機器) 由enforcing改成disabled
vim /etc/selinux/config
SELINUX=disabled
reboot
檢查是否修改正確
getenforce
關閉防火牆
停止後 再 完全停用(重開機也不會啟動)
systemctl stop firewalld && systemctl disable firewalld
設定Host
cat << 'EOF' >> /etc/hosts
192.168.31.108 mon1
192.168.31.157 osd1
192.168.31.164 osd2
EOF
設定主機名稱
hostnamectl --static set-hostname mon1
hostnamectl --static set-hostname osd1
hostnamectl --static set-hostname osd2
時間同步
yum install -y ntp ntpdate ntp-doc
#啟動系統的NTP Daemon
systemctl start ntpd
#設定開機就啟動NTP Daemon
systemctl enable ntpd
#系統時間與pool.ntp.org的NTP server做校時
ntpdate pool.ntp.org
更新所有依賴
yum install -y yum-plugin-priorities
yum install -y epel-release
免密碼登入(ceph-deploy)
ssh-keygen
ssh-copy-id root@mon1
ssh-copy-id root@osd1
ssh-copy-id root@osd2
管理節點 安裝 ceph-deploy 管理工具(ceph-deploy)
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
cat << 'EOF' > /etc/yum.repos.d/ceph.repo
[ceph-noarch]
name=Ceph noarch packages
baseurl=https://download.ceph.com/rpm/el7/noarch
enabled=1
gpgcheck=1
type=rpm-md
gpgkey=https://download.ceph.com/keys/release.asc
EOF
yum update -y && yum install -y ceph-deploy
建立 Ceph-Mon
#創建集群目錄,用於存放配置文件,證書等信息
mkdir -p /opt/ceph-cluster && cd /opt/ceph-cluster/
#創建ceph-mon 節點
ceph-deploy new mon1
#修改 osd 的副本數,既數據保存N份
echo 'osd_pool_default_size = 2' >> ./ceph.conf
#注: 如果文件系統為 ext4 請添加
echo 'osd max object name len = 256' >> ./ceph.conf
echo 'osd max object namespace len = 64' >> ./ceph.conf
安装 Ceph
ceph-deploy install mon1 osd1 osd2
ceph --version
初始化 ceph.osd 節點 創建 存儲空間
[root@osd1 ~]# mkdir -p /opt/ceph-osd && chown ceph:ceph /opt/ceph-osd
[root@osd2 ~]# mkdir -p /opt/ceph-osd && chown ceph:ceph /opt/ceph-osd
啟動 osd
[root@ansible ceph-cluster]# ceph-deploy osd prepare osd1:/opt/ceph-osd osd2:/opt/ceph-osd
激活 所有 osd 節點
[root@ansible ceph-cluster]# ceph-deploy osd activate osd1:/opt/ceph-osd osd2:/opt/ceph-osd
把管理節點的配置文件與keyring同步至其它節點
[root@ansible ceph-cluster]# ceph-deploy admin mon1 osd1 osd2
查看狀態
ceph -s
ceph osd tree
設置所有開機啟動
systemctl enable ceph-osd.target
systemctl enable ceph-mon.target
重啟系統以後重啟 osd
#查看處於down 的 osd
ceph osd tree
#登陸所在node 啟動ceph-osd進程 [id 為 tree 查看的 id]
systemctl start ceph-osd@id
Kubernetes Volume Ceph RBD
在所有的 k8s node 裡面安裝 ceph-common
yum -y install ceph-common
拷貝 ceph.conf 與 ceph.client.admin.keyring
ssh root@master "mkdir -p /etc/ceph"
scp ./ceph.client.admin.keyring ./ceph.conf root@master:/etc/ceph
ssh root@node1 "mkdir -p /etc/ceph"
scp ./ceph.client.admin.keyring ./ceph.conf root@node1:/etc/ceph
ssh root@node2 "mkdir -p /etc/ceph"
scp ./ceph.client.admin.keyring ./ceph.conf root@node2:/etc/ceph
配置 kubelet 增加ceph參數
這個我們就不用做,安裝前記得 kubespray kubelet_load_modules: false 改成 true
重启 kubelet
systemctl restart kubelet.service
修改 ceph 配置
#修改配置
rbd image有4個 features,layering, exclusive-lock, object-map, fast-diff, deep-flatten
#因為目前內核僅支持layering,修改默認配置。
echo 'rbd_default_features = 1' >> ./ceph.conf
#驗證一下
ceph --show-config|grep rbd|grep features
rbd_default_features = 1
#把管理節點的配置文件與keyring同步至其它節點
ceph-deploy --overwrite-conf admin mon1 osd1 osd2
創建 ceph-secret
#獲取 client.admin 的值
[root@mon1 ~]# ceph auth get-key client.admin
AQBqtMlZOEwzLxAAcK8HCxK20O5p7gzl80h2Dg==
#轉換成 base64 編碼
[root@mon1 ~]# echo "AQBqtMlZOEwzLxAAcK8HCxK20O5p7gzl80h2Dg=="|base64
QVFCcXRNbFpPRXd6THhBQWNLOEhDeEsyME81cDdnemw4MGgyRGc9PQo=
#創建ceph-secret.yaml文件
cat << 'EOF' > ceph-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: ceph-secret
data:
key: QVFCcXRNbFpPRXd6THhBQWNLOEhDeEsyME81cDdnemw4MGgyRGc9PQo=
EOF
#導入 yaml 文件
kubectl apply -f ceph-secret.yaml
#察看結果
kubectl get secret
創建 ceph image
#創建一個 1G 的 image
[root@ceph-node-1 ceph-cluster]# rbd create test-image -s 1G
#查看 image
[root@ceph-node-1 ceph-cluster]# rbd list
test-image
[root@ceph-node-1 ceph-cluster]# rbd info test-image
rbd image 'test-image':
size 1024 MB in 256 objects
order 22 (4096 kB objects)
block_name_prefix: rbd_data.373b2ae8944a
format: 2
features: layering
flags:
#這裡 format 為 2 , 如果舊系統 不支持 format 2 ,可將 format 設置為 1 。
創建一個 pv
cat << 'EOF' > test-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: test-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
rbd:
monitors:
- 192.168.31.108:6789
pool: rbd
image: test-image
user: admin
secretRef:
name: ceph-secret
fsType: ext4
readOnly: false
persistentVolumeReclaimPolicy: Recycle
EOF
#monitors 依照你實際的監控節點來修改
#導入 yaml 文件
kubectl apply -f test-pv.yaml
#察看結果
kubectl get pv
創建一個 pvc
cat << 'EOF' > test-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: test-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
EOF
kubectl apply -f test-pvc.yaml
kubectl get pvc
創建一個 deployment
cat << 'EOF' > nginx-deplyment.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-dm
spec:
replicas: 1
template:
metadata:
labels:
name: nginx
spec:
containers:
- name: nginx
image: nginx:alpine
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- name: ceph-rbd-volume
mountPath: "/usr/share/nginx/html"
volumes:
- name: ceph-rbd-volume
persistentVolumeClaim:
claimName: test-claim
EOF
kubectl apply -f nginx-deplyment.yaml
kubectl get pods
#查看容器磁碟用量
kubectl exec -it nginx-dm-4095233071-7q6mg -- df -h
#在容器內建立一個檔案
kubectl exec -it nginx-dm-4095233071-7q6mg -- touch /usr/share/nginx/html/jicki.html
#檢視檔案存在
kubectl exec -it nginx-dm-4095233071-7q6mg -- ls -lt /usr/share/nginx/html
#刪除pod
kubectl delete nginx-dm-4095233071-7q6mg
#啟動一個新 pod
kubectl apply -f nginx-deplyment.yaml
#檢查是否存在
kubectl exec -it nginx-dm-4095233071-7q6mg -- ls -lt /usr/share/nginx/html
Reference:
Kubenetes Persistent Volumes
kubernetes Ceph RBD — 山脚下的胖子.
Ceph安裝
CephFS Volume Provisioner for Kubernetes