OpenShift 本地開發環境配置(基於 Minishift)

東北小狐狸發表於2021-11-21

本文要做什麼?

很多為了驗證應用在 OpenShift 平臺的行為是否正常,或者組成一個簡單的開發環境,直接搭建一個 OpenShift/Origin 環境可能太重了,而且執行在本機可能佔用記憶體也太多。此時使用 MiniShift 來組成自己的開發環境就比較合適了。

本文就簡單介紹下如何配置 MiniShift 環境,無論是 Windows/Linux/MacOS都是可以安裝的,前提是你需要安裝個虛擬機器軟體 virtualbox。此處將以筆者 Linux 開發環境進行演示,操作大同小異可供參考。

安裝 Minishift

官方 Github Relese Page https://github.com/minishift/minishift/releases,

cd ~/Downloads
#下載v1.34.3 minishift
wget https://github.com/minishift/minishift/releases/download/v1.34.3/minishift-1.34.3-linux-amd64.tgz
#解壓到/usr/bin目錄下
sudo tar zxvf minishift-1.34.3-linux-amd64.tgz -C /usr/bin
#驗證是否能執行,有版本輸出即正常
minishift version
#新增自動補全,重開shell自動提示即正常
sudo minishift completion bash > /etc/bash_completion.d/minishift

對於 Win 環境,將下載的 zip 包解壓到任意目錄,然後將minishift.exe新增環境變數Path中即可,Win下自動補全功能好像沒有。

安裝 virtualbox

官方下載地址 https://www.virtualbox.org/wiki/Downloads

根據自己的平臺下載安裝即可。

簡易搭建docker registry

注意,請提前規劃好docker registry埠號,配置到docker上再重啟docker服務。

docker run --name myRegistry --restart=always -d -p 5000:5000 registry

webUI 可以參考 CSDN 胡了了 的部落格 https://blog.csdn.net/huqigang/article/details/76655854

搭建 Gitea

基於 docker-compose 搭建 Gitea 用於 Minishift 上傳程式碼構建映象。

gitea/docker-compose.yml

version: "3"

networks:
  gitea:
    external: false

services:
  server:
    image: gitea/gitea:1.15.6
    container_name: gitea
    environment:
      - USER_UID=1000
      - USER_GID=1000
    restart: always
    networks:
      - gitea
    volumes:
      - ./gitea:/data
      - /etc/timezone:/etc/timezone:ro
      - /etc/localtime:/etc/localtime:ro
    ports:
      - "3000:3000"
      - "222:22"

啟動 Gitea

cd gitea
docker-compose up -d

啟動完成後,訪問 3000 新建使用者即可使用。

配置與啟動 Minishift

以下命令依次執行,記憶體、CPU、硬碟、非安全docker倉庫按需配置即可。

#設定虛擬機器驅動
minishift config set vm-driver virtualbox
#設定openshift版本
minishift config set openshift-version 3.11.0
#設定虛擬機器cpu核數
minishift config set cpus 2
#設定虛擬機器記憶體
minishift config set memory 9GB
#設定虛擬機器磁碟大小
minishift config set disk-size 20GB
#設定docker registry倉庫地址,剛才建立的那個
minishift config set insecure-registry 192.168.99.1:5000
#設定映象快取,方便刪除叢集后再建立,不需要再重新拉映象(不開以後再拉較慢)
minishift config set image-caching true
#啟用管理員使用者外掛
minishift addon enable admin-user
#啟用任意使用者uid,不設定會導致自定義映象部署時使用者設定不正常
minishift addon enable anyuid
#設定靜態ip,也可以不設定
minishift ip --set-static

啟動 Minishift

#啟動minishift,等待初始化完成,首次初始化較慢
hellxz@debian:~/WorkSpace/openshift$ minishift start
-- Starting profile 'minishift'
-- Check if deprecated options are used ... OK
-- Checking if https://github.com is reachable ... FAIL
-- Checking if requested OpenShift version 'v3.11.0' is valid ... SKIP
-- Checking if requested OpenShift version 'v3.11.0' is supported ... OK
-- Checking if requested hypervisor 'virtualbox' is supported on this platform ... OK
-- Checking if VirtualBox is installed ... OK
-- Checking the ISO URL ... OK
-- Checking if provided oc flags are supported ... OK
-- Starting the OpenShift cluster using 'virtualbox' hypervisor ...
-- Starting Minishift VM ..................... OK
-- Checking for IP address ... OK
-- Checking for nameservers ... OK
-- Checking if external host is reachable from the Minishift VM ... 
   Pinging 8.8.8.8 ... OK
-- Checking HTTP connectivity from the VM ... 
   Retrieving http://minishift.io/index.html ... OK
-- Checking if persistent storage volume is mounted ... OK
-- Checking available disk space ... 19% used OK
-- Writing current configuration for static assignment of IP address ... OK
-- OpenShift cluster will be configured with ...
   Version: v3.11.0
-- Copying oc binary from the OpenShift container image to VM ... OK
-- Starting OpenShift cluster ...............................
Getting a Docker client ...
Checking if image openshift/origin-control-plane:v3.11.0 is available ...
Checking type of volume mount ...
Determining server IP ...
Using public hostname IP 192.168.99.115 as the host IP
Checking if OpenShift is already running ...
Checking for supported Docker version (=>1.22) ...
Checking if insecured registry is configured properly in Docker ...
Checking if required ports are available ...
Checking if OpenShift client is configured properly ...
Checking if image openshift/origin-control-plane:v3.11.0 is available ...
Starting OpenShift using openshift/origin-control-plane:v3.11.0 ...
I1121 10:47:29.719658    2437 flags.go:30] Running "create-kubelet-flags"
I1121 10:47:30.136043    2437 run_kubelet.go:49] Running "start-kubelet"
I1121 10:47:30.373865    2437 run_self_hosted.go:181] Waiting for the kube-apiserver to be ready ...
I1121 10:48:00.400822    2437 interface.go:26] Installing "kube-proxy" ...
I1121 10:48:00.401522    2437 interface.go:26] Installing "kube-dns" ...
I1121 10:48:00.401530    2437 interface.go:26] Installing "openshift-service-cert-signer-operator" ...
I1121 10:48:00.401535    2437 interface.go:26] Installing "openshift-apiserver" ...
I1121 10:48:00.401559    2437 apply_template.go:81] Installing "openshift-apiserver"
I1121 10:48:00.402465    2437 apply_template.go:81] Installing "kube-proxy"
I1121 10:48:00.403006    2437 apply_template.go:81] Installing "kube-dns"
I1121 10:48:00.403118    2437 apply_template.go:81] Installing "openshift-service-cert-signer-operator"
I1121 10:48:35.202811    2437 interface.go:41] Finished installing "kube-proxy" "kube-dns" "openshift-service-cert-signer-operator" "openshift-apiserver"
I1121 10:48:45.237092    2437 run_self_hosted.go:242] openshift-apiserver available
I1121 10:48:45.237114    2437 interface.go:26] Installing "openshift-controller-manager" ...
I1121 10:48:45.237133    2437 apply_template.go:81] Installing "openshift-controller-manager"
I1121 10:48:47.120838    2437 interface.go:41] Finished installing "openshift-controller-manager"
Adding default OAuthClient redirect URIs ...
Adding centos-imagestreams ...
Adding registry ...
Adding router ...
Adding persistent-volumes ...
Adding web-console ...
Adding sample-templates ...
I1121 10:48:47.161522    2437 interface.go:26] Installing "centos-imagestreams" ...
I1121 10:48:47.161533    2437 interface.go:26] Installing "openshift-image-registry" ...
I1121 10:48:47.161541    2437 interface.go:26] Installing "openshift-router" ...
I1121 10:48:47.161546    2437 interface.go:26] Installing "persistent-volumes" ...
I1121 10:48:47.161554    2437 interface.go:26] Installing "openshift-web-console-operator" ...
I1121 10:48:47.161560    2437 interface.go:26] Installing "sample-templates" ...
I1121 10:48:47.161607    2437 interface.go:26] Installing "sample-templates/postgresql" ...
I1121 10:48:47.161614    2437 interface.go:26] Installing "sample-templates/nodejs quickstart" ...
I1121 10:48:47.161620    2437 interface.go:26] Installing "sample-templates/rails quickstart" ...
I1121 10:48:47.161626    2437 interface.go:26] Installing "sample-templates/jenkins pipeline ephemeral" ...
I1121 10:48:47.161632    2437 interface.go:26] Installing "sample-templates/mongodb" ...
I1121 10:48:47.161637    2437 interface.go:26] Installing "sample-templates/mysql" ...
I1121 10:48:47.161647    2437 interface.go:26] Installing "sample-templates/cakephp quickstart" ...
I1121 10:48:47.161653    2437 interface.go:26] Installing "sample-templates/dancer quickstart" ...
I1121 10:48:47.161662    2437 interface.go:26] Installing "sample-templates/django quickstart" ...
I1121 10:48:47.161684    2437 interface.go:26] Installing "sample-templates/sample pipeline" ...
I1121 10:48:47.161724    2437 interface.go:26] Installing "sample-templates/mariadb" ...
I1121 10:48:47.161772    2437 apply_list.go:67] Installing "sample-templates/mariadb"
W1121 10:48:47.162234    2437 create_secret.go:78] Error reading $HOME/.docker/config.json: open /home/docker/.docker/config.json: no such file or directory, imagestream import credentials will not be setup
I1121 10:48:47.162270    2437 apply_list.go:67] Installing "centos-imagestreams"
I1121 10:48:47.162982    2437 apply_list.go:67] Installing "sample-templates/mongodb"
I1121 10:48:47.163194    2437 apply_list.go:67] Installing "sample-templates/mysql"
I1121 10:48:47.163346    2437 apply_list.go:67] Installing "sample-templates/cakephp quickstart"
I1121 10:48:47.163459    2437 apply_list.go:67] Installing "sample-templates/dancer quickstart"
I1121 10:48:47.163564    2437 apply_list.go:67] Installing "sample-templates/django quickstart"
I1121 10:48:47.163670    2437 apply_list.go:67] Installing "sample-templates/sample pipeline"
I1121 10:48:47.163706    2437 apply_template.go:81] Installing "openshift-web-console-operator"
I1121 10:48:47.163904    2437 apply_list.go:67] Installing "sample-templates/postgresql"
I1121 10:48:47.163958    2437 apply_list.go:67] Installing "sample-templates/nodejs quickstart"
I1121 10:48:47.164070    2437 apply_list.go:67] Installing "sample-templates/rails quickstart"
I1121 10:48:47.164091    2437 apply_list.go:67] Installing "sample-templates/jenkins pipeline ephemeral"
I1121 10:48:55.790359    2437 interface.go:41] Finished installing "sample-templates/postgresql" "sample-templates/nodejs quickstart" "sample-templates/rails quickstart" "sample-templates/jenkins pipeline ephemeral" "sample-templates/mongodb" "sample-templates/mysql" "sample-templates/cakephp quickstart" "sample-templates/dancer quickstart" "sample-templates/django quickstart" "sample-templates/sample pipeline" "sample-templates/mariadb"
I1121 10:48:59.606507    2437 interface.go:41] Finished installing "centos-imagestreams" "openshift-image-registry" "openshift-router" "persistent-volumes" "openshift-web-console-operator" "sample-templates"
Server Information ...
OpenShift server started.

The server is accessible via web console at:
    https://192.168.99.115:8443/console

此時直接訪問輸出的地址就能訪問平臺了,最好先執行下邊的命令再進平臺。

啟動後配置命令:

#啟用admin使用者
minishift addons apply admin-user
#啟用任意使用者uid,允許pod使用root使用者執行(便於除錯)
minishift addons apply anyuid

訪問 Minishift WebUI,那個 console 地址(本例中為https://192.168.99.115:8443/console),使用 developer 和任意密碼即可登入。

可以使用預設的 My Project 也可以自己新建,這裡使用預設的就行。

到這個熟悉的 OpenShift 介面就可以了。除了 logo 不大一樣,其餘功能都差不多。

馬上更新自定義OpenShift映象,靜請期待!

本文同步釋出以下兩個地址,未經許可禁止轉載。

相關文章