1. 主機配置推薦
- master 16核 32GB 網卡帶寬不低于1Gb。
CPU x86_64架構,核數和主機數線性遞增,每增加一臺主機增加0.1核。5臺主機4.5核,總的核數為4+0.1 * 主機數
內存與主機數線性遞增,每增一臺主機增加200M內存,5臺8G,總的內存數為7+0.2 * 主機數 - node 40核 256GB 網卡帶寬不低于1Gb
根據應用場景估算
2. 磁盤目錄掛載
- master
磁盤格式:xfs ftype=1
/ : 10GB
/var/log :50GB
/var/lib/docker:100GB 做raid高可用
/var/lib/etcd [ssd]:20GB 做raid高可用
/var :50GB 可根據實際進行調整,主要emptyDir的存儲在/var/lib/origin目錄下 - node
磁盤格式:xfs ftype=1
/ : 10GB
/var/log :50GB
/var/lib/docker:100GB 做raid高可用
/var :50GB 可根據實際進行調整,主要emptyDir的存儲在/var/lib/origin目錄下
mkfs.xfs -n ftype=1 /path/to/your/device
說明:xfs文件格式,docker overlay2存儲設備必須設置ftype=1
。
3. 關閉swap
swapoff -a
cat /etc/fstab ## 注釋掉swap
4. 打開seLinux enabled
sed -i 's/SELINUX=disabled/SELINUX=permissive/' /etc/selinux/config
5. 設置系統參數
$ cat /etc/sysctl.conf
# 禁用整個系統所有接口的IPv6
net.ipv6.conf.all.disable_ipv6 = 1
vm.swappiness = 0
net.netfilter.nf_conntrack_max = 1000000
$ lsmod | grep conntrack || modprobe ip_conntrack
$ sysctl -w net.netfilter.nf_conntrack_max=1000000
$ sysctl -p /etc/sysctl.conf
6. 更改resolve.conf
$ cat /etc/resolv.conf
search cluster.local
nameserver 192.168.0.2
7. 時間同步
$ ansible all -m package -a 'name=chrony state=present'
## chronyd服務端配置
$ cat /etc/chrony.conf
server 55.15.226.193 iburst
allow 55.15.226.0/24
local stratum 10
強制同步時間
## chrony客戶端配置
chronyc sources -v
systemctl stop chronyd
chronyd -q 'pool 55.15.226.193 iburst'
8.創建docker 用戶組
groupadd docker
將普通用戶添加到docker用戶組
usermod -aG docker ${USER}
9. docker設置
/etc/sysconfig/docker-storage
DOCKER_STORAGE_OPTIONS="--storage-driver overlay2 "
/etc/sysconfig/docker
OPTIONS=" --log-opt max-size=1M --log-opt max-file=3 --live-restore=true "
設置docker與kubelet的cgroup driver
為systemd。OpenShift默認安裝就是設置的systemd,而社區版的kubelet默認是cgroupfs,需要注意。。
10. 網卡配置
- 配置網卡多隊列:
ethtool -l eth0
查看網卡多隊列Combined數 - NetworkManager, 是一個提供網絡檢測和配置網絡的工具,在Node節點需要使用它來自動配置節點的dnsmasq作為默認的網絡入口。
- 網絡設備的配置中/etc/sysconfig/network-scripts/ifcfg-eth*默認
NM_CONTROLLED
是被設置為yes
,如果它被設置為no
,那么NetworkManager應用將不會去自動創建dnsmasq相關的配置,所以此時需要手動配置dnsmasq。
添加文件
$ cat /etc/dnsmasq.d/origin-upstream-dns.conf
server=192.168.0.2
$ cat /etc/origin/node/resolv.conf
nameserver 192.168.0.2
參考 install-config-network-using-firewalld
11. 多網卡
管理網:集群間組件通信,Node與Master節點通信網絡
業務網:應用間網絡通信,pod間網絡通信
存儲網:與存儲設備網絡通信
還可以將與外部鏡像倉庫的網絡也考慮進去
每個網絡,使用兩張網卡做bond,提高網絡性能及可用性。
其中管理網與業務網必須互通,否則部分組件服務將不可用。
12.外部節點相關組件
- 時間同步服務(chronyd)
- DNS(dnsmasq)
- 鏡像倉庫(docker-distribution)
- 負載均衡器(Haproxy)
13. 外部鏡像倉庫授權
將私有鏡像倉庫的CA文件拷貝到鏡像倉庫所在服務器的/etc/pki/ca-trust/source/anchors/目錄下
$ ansible all -m copy -a 'src=registry.crt dest=/etc/pki/ca-trust/source/anchors/registry.crt'
$ update-ca-trust
為OpenShift節點設置默認的登錄信息
$ # 在/etc/ansible/hosts中添加認證用戶
oreg_auth_user="<用戶名>"
oreg_auth_password="<密碼>"
$ oc login <鏡像倉庫url> -u <用戶名>
$ ansible -m copy -a 'src=/root/.docker dest=/var/lib/origin' all
$ ansible -m service -a 'name=origin-node state=restarted' all
14. 內核優化(openshift安裝會自動配置)
$ ansible all -m package -a 'name=tuned state=present'
$ ansible all -m service -a 'name=tuned state=started enabled=true'
$ ansible all -m shell -a 'tuned-adm profile throughput-performance'
15. ansible設置reserved。
OpenShift官方推薦規則
通常,它需要保留5%-10%的節點資源來保護節點,越高越安全。
AWS的規則:
內存預留值(AWS):
Reserved memory = 255MiB + 11MiB * MAX_POD_PER_INSTANCE
CPU預留值(AWS):
- 6% of the first core
- 1% of the next core (up to 2 cores)
- 0.5% of the next 2 cores (up to 4 cores)
- 0.25% of any cores above 4 cores
GKE的規則:
內存預留值(GKE):
- 255 MiB of memory for machines with less than 1 GB of memory
- 25% of the first 4GB of memory
- 20% of the next 4GB of memory (up to 8GB)
- 10% of the next 8GB of memory (up to 16GB)
- 6% of the next 112GB of memory (up to 128GB)
- 2% of any memory above 128GB
CPU預留值(GKE)::
- 6% of the first core
- 1% of the next core (up to 2 cores)
- 0.5% of the next 2 cores (up to 4 cores)
- 0.25% of any cores above 4 cores
例子:2 vCPU and 7.5GB
Allocatable memory = 0.25 * 4 (first 4GB) + 0.2 * 3.5 (remaining 3.5GB)
Allocatable CPU = 0.06 * 1 (first core) + 0.01 * 1 (second core)
Azure的規則:
內存預留值(Azure):
- 255 MiB of memory for machines with less than 1 GB of memory
- 25% of the first 4GB of memory
- 20% of the next 4GB of memory (up to 8GB)
- 10% of the next 8GB of memory (up to 16GB)
- 6% of the next 112GB of memory (up to 128GB)
- 2% of any memory above 128GB
CPU預留值(Azure):
核數 core | 預留 millicores |
---|---|
1 | 60 |
2 | 100 |
4 | 140 |
8 | 180 |
16 | 260 |
32 | 420 |
64 | 740 |
另外:
Google和亞馬遜產品的hard eviction threshold 為100MB,而AKS則為750MB。
[OSEv3:vars]
# 節點配置低的話可參考
openshift_node_kubelet_args={'pods-per-core': ['10'], 'max-pods': ['250'], 'image-gc-high-threshold': ['85'], 'image-gc-low-threshold': ['80'], 'system-reserved':['cpu=200m', 'memory=1G'], 'kube-reserved':['cpu=200m','memory=1G']}
# 節點配置高的話可參考
openshift_node_kubelet_args={'pods-per-core': ['10'], 'max-pods': ['250'], 'image-gc-high-threshold': ['85'], 'image-gc-low-threshold': ['80'], 'system-reserved':['cpu=500m', 'memory=1G'], 'kube-reserved':['cpu=1','memory=2G']}
16. 配置集群對master控制臺的public域名證書及應用Route路由服務的域名證書
openshift_master_cluster_hostname=master.example.com
openshift_master_cluster_public_hostname=master_public.example.com
openshift_master_default_subdomain=apps.example.com
openshift_master_named_certificates=[{"certfile": "/data/cert/master_public.example.com.crt", "keyfile": "/data/cert/master_public.example.com.key", "names": ["master_public.example.com"], "cafile": "/data/cert/example.com_ca.crt"}]
openshift_master_overwrite_named_certificates=true
openshift_hosted_router_certificate={"certfile": "/data/cert/apps.example.com.crt", "keyfile": "/data/cert/apps.example.com.key", "cafile": "/data/cert/example.com_ca.crt"}
其中各證書的文件名不要使用與Master組件默認的名字重復,否則會覆蓋掉組件間的自簽證書。
另外可以自簽證書生成長有效期的相關證書。自簽證書步驟如下:
- 根證書創建
$ openssl genrsa -out ca.key 2048
$ openssl req -new -x509 -days 36500 -key ca.key -out ca.crt -subj "/C=CN/ST=shanxi/L=taiyuan/O=cn/OU=test/CN=example.com"
$ #或者 openssl req -new -x509 -days 36500 -key ca.key -out ca.crt 手動輸入配置
- 創建證書并使用根證書簽發
$ openssl genrsa -out app.key 2048
$ openssl req -new -key app.key -out app.csr
$ openssl x509 -req -in app.csr -CA ca.crt -CAkey ca.key -out app.crt -days 3650 -CAcreateserial
- 使用 Openssl 工具查看證書信息
$ openssl x509 -in signed.crt -noout -dates
$ openssl x509 -in signed.crt -noout -subject
$ openssl x509 -in signed.crt -noout -text
17. 添加集群自動審批證書簽發請求
OpenShift 3.11中默念Node的證書有效期為1年,滿1年后會自動更新證書。更新證書時,該節點會向集群發送證書簽發請求,批準之后才能繼續添加到集群。
[OSEv3:vars]
openshift_master_bootstrap_auto_approve=true
說明:對于已經部署好的集群可以通過執行ansible-playbook來配置
# ansible-playbook -vvv openshift-ansible/playbooks/openshift-master/enable_bootstrap.yml -e openshift_master_bootstrap_auto_approve=true
18. ansible中設置Docker存儲type及Docker與etcd額外磁盤
[OSEv3:vars]
# Docker setup for extra disks on nodes
container_runtime_docker_storage_setup_device=/dev/vdb
container_runtime_docker_storage_type=overlay2
openshift_node_local_quota_per_fsgroup=512Mi
[masters:vars]
container_runtime_extra_storage=[{'device': '/dev/vdc', 'path': '/var/lib/origin/openshift.local.volumes', 'options': 'gquota', 'filesystem': 'xfs', 'format': 'True'}, {'device': '/dev/vdd', 'path': '/var/lib/etcd', 'hosts': 'masters', 'filesystem': 'xfs', 'format': 'True'}]
[nodes:vars]
container_runtime_extra_storage=[{'device': '/dev/vdc', 'path': '/var/lib/origin/openshift.local.volumes', 'options': 'gquota', 'filesystem': 'xfs', 'format': 'True'}]
19. 設置日志自動歸檔
- journal日志歸檔
減少/var/log/journal
的日志,設置/etc/systemd/journald.conf
$ cat /etc/systemd/journald.conf
[Journal]
Storage=persistent
Compress=yes
#Seal=yes
#SplitMode=uid
SyncIntervalSec=1s
RateLimitInterval=1s
RateLimitBurst=10000
SystemMaxUse=1G
SystemKeepFree=20%
SystemMaxFileSize=10M
#RuntimeMaxUse=
#RuntimeKeepFree=
#RuntimeMaxFileSize=
MaxRetentionSec=3days
MaxFileSec=1day
ForwardToSyslog=False
#ForwardToKMsg=no
#ForwardToConsole=no
ForwardToWall=False
#TTYPath=/dev/console
#MaxLevelStore=debug
#MaxLevelSyslog=debug
#MaxLevelKMsg=notice
#MaxLevelConsole=info
#MaxLevelWall=emerg
$ systemctl restart systemd-journald
或者部署時更新以下文件內容(openshift 3.9以上)
roles/openshift_node/defaults/main.yml
...
journald_vars_to_replace:
- { var: Storage, val: persistent }
- { var: Compress, val: yes }
- { var: SyncIntervalSec, val: 1s }
- { var: RateLimitInterval, val: 1s }
- { var: RateLimitBurst, val: 10000 }
- { var: SystemMaxUse, val: 1G }
- { var: SystemKeepFree, val: 20% }
- { var: SystemMaxFileSize, val: 10M }
- { var: MaxRetentionSec, val: 3days }
- { var: MaxFileSec, val: 1day }
- { var: ForwardToSyslog, val: no }
- { var: ForwardToWall, val: no }
...
- message日志歸檔
只收集warning以上的日志/etc/rsyslog.conf
$ cat /etc/rsyslog.conf
*.warning;mail.none;authpriv.none;cron.none /var/log/messages
將message日志只保留最近三天的日志
$ cat /etc/logrotate.d/syslog
/var/log/cron
/var/log/messages
{
daily
rotate 3
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
如果要設置普通用戶可查看/var/log/messages文件,需要在/etc/rsyslog.conf配置的前面添加messages文件可讀權限
$umask 0000
20. 定時清理主機上退出的容器、未被使用的卷和未被使用的鏡像(發布非常頻繁時需要)
$ cat /usr/bin/prune_docker.sh
#!/bin/bash
docker container prune -f # 刪除所有退出狀態的容器
docker volume prune -f # 刪除未被使用的數據卷
docker image prune -f # 刪除 dangling 或所有未被使用的鏡像
做為定時任務定期作清理
$ crontab -l
0 0 * * * /usr/bin/prune_docker.sh >> /var/log/prune_docker.log 2>&1
21. 定時清理私有鏡像倉庫(發布非常頻繁時需要)
$ cat > /usr/bin/cleanregistry.sh <<EOF
#!/bin/bash
oc login -u admin -p password
oc adm prune builds --orphans --keep-complete=25 --keep-failed=5 --keep-younger-than=60m --confirm
oc adm prune deployments --orphans --keep-complete=25 --keep-failed=10 --keep-younger-than=60m --confirm
#oc rollout latest docker-registry -n default
#sleep 20
oc adm prune images --keep-younger-than=400m --confirm
EOF
$ crontab -l
0 0 * * * /usr/bin/cleanregistry.sh >> /var/log/cleanregistry.log 2>&1
22. 注釋掉origin-accounting.conf文件中的DefaultIOAccounting
$ cat /etc/systemd/system.conf.d/origin-accounting.conf
[Manager]
DefaultCPUAccounting=yes
DefaultMemoryAccounting=yes
# systemd v230 or newer
# DefaultIOAccounting=yes
# Deprecated, remove in future
DefaultBlockIOAccounting=yes
23. Pod與Service網段規劃
- 集群的Service網段
- 集群的Pod網段
- 根據主機的配置規劃好每臺主機上Pod的網段
24. Router環境變量優化
ROUTER_THREADS 設置為CPU核數
ROUTER_MAX_CONNECTIONS 默認值是20000
25. Router設置默認503頁面(服務不存在)
設置頁面HTML,覆蓋/var/lib/haproxy/conf/error-page-503.http文件
補充
:Openshift自定義Router配置
26. 計算節點優化配置
- MTU值:通常的以太網設置為1450,在巨型幀以太網中設置為8950
node的配置文件中
networkConfig:
mtu: 1450
- 開啟并行拉取鏡像,提升效率。
node的配置文件中
kubeletArguments:
serialize-image-pulls:
- "false"
- 容器清理:通過kubelet自動清理退出的容器
node的配置文件中
kubeletArguments:
minimum-container-ttl-duration:
- "10s"
maximum-dead-containers-per-container:
- "1"
maximum-dead-containers:
- "20"
minimum-container-ttl-duration
: 容器可以進行垃圾收集的最低時長。 默認值為0,表示不限制。 可以使用單位后綴來指定此設置的值,例如h表示小時,m表示分鐘,s表示秒。
maximum-dead-containers-per-container
:每個pod容器要保留的實例數。 預設值為1。
maximum-dead-containers
:節點中死容器總數的最大值。 默認值為-1,表示無限制。
27. 證書的有效期設置更長年限(100年)
核心步驟是:
- 部署時設置主要ocp組件的過期時間,/etc/ansible/hosts
- 更新部署腳本中所有生成證書的地方,設置長年限的過期時間
具體操作參考筆者之前的文章:OpenShift部署時如何延長組件證書的有效期
參考文章:
linux journalctl 命令
配置 logrotate 的終極指導
Allocatable memory and CPU in Kubernetes Nodes
OpenShift容器云平臺建設之部署前準備
企業級容器云平臺建設之功能匯總