一、hostport
1、deploy.sh
#!/bin/sh
source /etc/profile
projectName=$1
namespace=$2
replicas=$3
image=$4
containerPort=$5
hostPort=$6
eval "cat <<EOF
$(< deploymentTemplate.yml)
EOF
" > "$projectName"_dp.yml
eval "cat <<EOF
$(< serviceTemplate.yml)
EOF
" > "$projectName"_ser.yml
result=$(rancher kubectl get deployment --namespace=$namespace | grep -w $projectName)
#echo "$result"
if [ -z "$result" ]; then
rancher kubectl create -f "$projectName"_dp.yml
else
rancher kubectl replace -f "$projectName"_dp.yml
fi
result=$(rancher kubectl get service --namespace=$namespace | grep -w $projectName)
#echo "$result"
if [ -z "$result" ]; then
rancher kubectl create -f "$projectName"_ser.yml
else
rancher kubectl replace --force -f "$projectName"_ser.yml
fi
2、deploymentTemplate.yml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
labels:
workload.user.cattle.io/workloadselector: deployment-${namespace}-${projectName}
name: ${projectName}
namespace: ${namespace}
spec:
replicas: ${replicas}
selector:
matchLabels:
workload.user.cattle.io/workloadselector: deployment-${namespace}-${projectName}
template:
metadata:
annotations:
field.cattle.io/ports: '[[{"containerPort":${containerPort},"dnsName":"${projectName}-hostport","hostPort":${hostPort},"kind":"HostPort","name":"${containerPort}tcp${hostPort}","protocol":"TCP","sourcePort":${hostPort}}]]'
labels:
workload.user.cattle.io/workloadselector: deployment-${namespace}-${projectName}
spec:
containers:
- name: ${projectName}
image: ${image}
imagePullPolicy: Always
ports:
- containerPort: ${containerPort}
hostPort: ${hostPort}
name: ${containerPort}tcp${hostPort}
protocol: TCP
3、serviceTemplate.yml
apiVersion: v1
kind: Service
metadata:
annotations:
field.cattle.io/targetWorkloadIds: '["deployment:${namespace}:${projectName}"]'
workload.cattle.io/targetWorkloadIdNoop: "true"
workload.cattle.io/workloadPortBased: "true"
labels:
workload.user.cattle.io/workloadselector: deployment-${namespace}-${projectName}
name: ${projectName}
namespace: ${namespace}
#ownerReferences:
#- apiVersion: apps/v1beta2
# controller: true
# kind: deployment
# name: ${projectName}
spec:
ports:
- port: ${containerPort}
protocol: TCP
targetPort: ${containerPort}
selector:
workload.user.cattle.io/workloadselector: deployment-${namespace}-${projectName}
sessionAffinity: None
type: ClusterIP
二、nodeport
1、deployNoteType.sh
#!/bin/sh
source /etc/profile
projectName=$1
namespace=$2
replicas=$3
image=$4
containerPort=$5
nodePort=$6
eval "cat <<EOF
$(< deploymentTemplateNotetype.yml)
EOF
" > "$projectName"_dp.yml
eval "cat <<EOF
$(< serviceTemplateNotetype.yml)
EOF
" > "$projectName"_ser.yml
result=$(rancher kubectl get deployment --namespace=$namespace | grep -w $projectName)
#echo "$result"
if [ -z "$result" ]; then
rancher kubectl create -f "$projectName"_dp.yml
else
rancher kubectl replace -f "$projectName"_dp.yml
fi
result=$(rancher kubectl get service --namespace=$namespace | grep -w $projectName)
#echo "$result"
if [ -z "$result" ]; then
rancher kubectl create -f "$projectName"_ser.yml
else
rancher kubectl replace --force -f "$projectName"_ser.yml
fi
2、deploymentTemplateNotetype.yml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
labels:
workload.user.cattle.io/workloadselector: deployment-${namespace}-${projectName}
name: ${projectName}
namespace: ${namespace}
spec:
replicas: ${replicas}
selector:
matchLabels:
workload.user.cattle.io/workloadselector: deployment-${namespace}-${projectName}
template:
metadata:
annotations:
field.cattle.io/ports: '[[{"containerPort":${containerPort},"dnsName":"${projectName}-noteport","kind":"NodePort","name":"${containerPort}tcp${nodePort}","protocol":"TCP","sourcePort":"${nodePort}"}]]'
labels:
workload.user.cattle.io/workloadselector: deployment-${namespace}-${projectName}
spec:
containers:
- name: ${projectName}
image: ${image}
imagePullPolicy: Always
ports:
- containerPort: ${containerPort}
name: ${containerPort}tcp${nodePort}
protocol: TCP
3、serviceTemplateNotetype.yml
apiVersion: v1
kind: Service
metadata:
annotations:
field.cattle.io/targetWorkloadIds: '["deployment:${namespace}:${projectName}"]'
workload.cattle.io/targetWorkloadIdNoop: "true"
workload.cattle.io/workloadPortBased: "true"
labels:
workload.user.cattle.io/workloadselector: deployment-${namespace}-${projectName}
name: ${projectName}
namespace: ${namespace}
#ownerReferences:
#- apiVersion: apps/v1beta2
# controller: true
# kind: deployment
# name: ${projectName}
spec:
ports:
- nodePort: ${nodePort}
port: ${containerPort}
protocol: TCP
targetPort: ${containerPort}
selector:
workload.user.cattle.io/workloadselector: deployment-${namespace}-${projectName}
sessionAffinity: None
type: NodePort
三、
3、./deploy.sh zuul platform 3 192.166.33.10/test/zuul:1.0 8080 18080
用法:
命令 服务名 命令空间 复本数量 镜像 容器端口 hostport或nodeport
4、jenkins
git拉取、mvn编译打包、docker镜像推送、ranchercli 发布