openshift 4.3 build config & hpa
video for build config & scale up
- https://youtu.be/O0TjPBisMVo
- https://www.bilibili.com/video/BV1rT4y137QJ/
- https://www.ixigua.com/i6824464593977344525/
video for scale up & service
- https://youtu.be/6fMe7T4RlCI
- https://www.bilibili.com/video/BV1Xt4y1y7xG/
- https://www.ixigua.com/i6824739572237206023/
php build config
# 准备一个php的测试镜像
cat << EOF > php.dockerfile
FROM php:apache
COPY . /var/www/html/
EOF
cat <<EOF > index.php
<?php
ECHO "Hello!<br>";
echo "Welcome to RedHat Developer<br>";
EcHo "Enjoy all of the ad-free articles<br>";
?>
EOF
buildah build-using-dockerfile -t docker.io/wangzheng422/php:demo -f php.dockerfile .
podman run -it --rm -p 18080:80 --name my-running-app docker.io/wangzheng422/php:demo
# 创建一个git服务器,用gogs,启动以后要做一些配置。
# 配置 resolve.conf
# 配置 app.ini
# [webhook]
# SKIP_TLS_VERIFY = true
mkdir -p /data/ocp4/gogs
podman run -d --name=gogs -p 10022:22 -p 10080:3000 -v /data/ocp4/gogs:/data:Z registry.redhat.ren:5443/docker.io/gogs/gogs
podman stop gogs
podman start gogs
# http://registry.redhat.ren:10080
# 在demo项目中,创建编译配置
oc project demo
oc import-image php:apache-wzh --from=registry.redhat.ren:5443/docker.io/library/php:apache-wzh --confirm
# oc import-image php:apache-wzh --from=registry.redhat.ren:5443/docker.io/wangzheng422/php:apache --confirm
oc create is php-sample -n demo
cat << EOF > bc.is.yaml
kind: BuildConfig
apiVersion: build.openshift.io/v1
metadata:
name: "php-sample-build"
spec:
runPolicy: "Serial"
triggers:
- type: "Generic"
generic:
secret: "secret101"
-
type: "ImageChange"
source:
git:
uri: "http://registry.redhat.ren:10080/root/php"
dockerfile: "FROM php:apache\nCOPY . /var/www/html/"
strategy:
dockerStrategy:
from:
kind: "ImageStreamTag"
name: "php:apache-wzh"
output:
to:
kind: "ImageStreamTag"
name: "php-sample:demo"
EOF
oc apply -f bc.is.yaml
# 在界面上操作,通过镜像创建应用,并通过代码更改,触发应用的重新部署。
hpa
我们在这里展示openshift如何根据cpu的负载,自动扩缩pod的数量。
video
- https://youtu.be/_UTncz3StXE
- https://www.bilibili.com/video/BV1Tk4y1r7Be/
# oc autoscale dc/php-sample \
# --min 1 \
# --max 3 \
# --cpu-percent=50
# 根据已经创建的deployment,创建HPA
cat << 'EOF' > demo.hpa.yaml
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2beta1
metadata:
name: php-sample
namespace: demo
spec:
scaleTargetRef:
kind: Deployment
name: php-sample
apiVersion: apps/v1
minReplicas: 1
maxReplicas: 3
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 50
EOF
oc apply -n demo -f demo.hpa.yaml
# 为了不影响其他测试,我们把php pod定点到同一个交换机主机上
cat << 'EOF'
nodeSelector:
kubernetes.io/hostname: 'infra1.hsc.redhat.ren'
EOF
# 为了更好的展示效果,我们限制cpu使用量
cat << 'EOF'
resources:
requests:
cpu: '100m'
memory: "1G"
limits:
cpu: '100m'
memory: "1G"
EOF
# 开始压力
ab -c 100 -n 99999 http://php-sample-demo.apps.ocpsc.redhat.ren/
# 查看当前hpa的状态
oc describe hpa/php-sample
弯路
skopeo copy docker://docker.io/php:apache docker-archive:///root/tmp/php.tar
gzip php.tar
skopeo copy docker-archive:///data/ocp4/tmp/php.tar.gz docker://registry.redhat.ren:5443/docker.io/library/php:apache
skopeo copy docker://docker.io/wangzheng422/php:apache docker://registry.redhat.ren:5443/docker.io/wangzheng422/php:apache
cat << EOF > docker.php.sh
#!/usr/bin/env bash
set -e
set -x
buildah from --name onbuild-container docker.io/php:apache
buildah run onbuild-container sed -i "s/80/8080/g" /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf
buildah umount onbuild-container
buildah config -p 8080 onbuild-container
buildah commit --squash --rm --format=docker onbuild-container docker.io/wangzheng422/php:apache
buildah push docker.io/wangzheng422/php:apache
EOF
bash docker.php.sh
cat << EOF > docker.php.sh
#!/usr/bin/env bash
set -e
set -x
buildah from --name onbuild-container registry.redhat.ren:5443/docker.io/library/php:apache
buildah run onbuild-container sed -i "s/80/8080/g" /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf
buildah umount onbuild-container
buildah config -p 8080 onbuild-container
buildah commit --squash --rm --format=docker onbuild-container registry.redhat.ren:5443/docker.io/library/php:apache-wzh
buildah push registry.redhat.ren:5443/docker.io/library/php:apache-wzh
EOF
bash docker.php.sh
# 我们不需要复杂的 template
oc get template -n openshift | grep php
# 用 source to image 功能就可以,所有找一下image stream
oc get is -A | grep php
# 我们把sample operator的状态改一下
oc get configs.samples.operator.openshift.io/cluster -o yaml
oc patch configs.samples.operator.openshift.io/cluster -p '{"spec":{"managementState": "Unmanaged"}}' --type=merge
export LOCAL_REG='registry.redhat.ren:5443'
var_is_name='php'
var_json=$(oc get is ${var_is_name} -n openshift -o json)
var_j=0
for var_is_tag in $(echo $var_json | jq -r ".spec.tags[].name"); do
var_is_image_name=$(echo $var_json | jq -r ".spec.tags[${var_j}].from.name")
var_is_image_kind=$(echo $var_json | jq -r ".spec.tags[${var_j}].from.kind")
if [[ $var_is_image_kind =~ 'DockerImage' ]]; then
var_new_is_image_name="${LOCAL_REG}/$var_is_image_name"
echo "###############################"
echo $var_is_image_name
echo $var_is_image_kind
echo $var_new_is_image_name
echo $var_is_tag
oc patch -n openshift is ${var_is_name} --type='json' -p="[{\"op\": \"replace\", \"path\": \"/spec/tags/${var_j}/from/name\", \"value\":\"${var_new_is_image_name}\"}]"
fi
var_j=$((var_j+1))
done