一.k8s對(duì)接glusterfs存儲(chǔ)
1.創(chuàng)建endpoint
#查看
kubectl describe svc myweb
kubectl get endpoints myweb
kubectl describe endpoints myweb
#創(chuàng)建
[root@k8s-master ~]# cd k8s_yaml/
[root@k8s-master k8s_yaml]# mkdir gfs
[root@k8s-master k8s_yaml]# cd gfs/
#添加文件
[root@k8s-master gfs]# vim glusterfs-ep.yaml
apiVersion: v1
kind: Endpoints
metadata:
name: glusterfs
namespace: default
subsets:
- addresses:
- ip: 10.0.0.11
- ip: 10.0.0.12
- ip: 10.0.0.13
ports:
- port: 49152
protocol: TCP
端口和協(xié)議可以通過(guò)netstat -lntp查看得到
#創(chuàng)建并查看
[root@k8s-master gfs]# kubectl create -f glusterfs-ep.yaml
endpoints "glusterfs" created
[root@k8s-master gfs]# kubectl get endpoints
NAME ENDPOINTS AGE
glusterfs 10.0.0.11:49152,10.0.0.12:49152,10.0.0.13:49152 9s
kubernetes 10.0.0.11:6443 6d
mysql <none> 1d
myweb 172.18.13.7:8080 1d
nginx 172.18.13.7:80 5d
nginx-deployment 172.18.13.5:80 1d
2.創(chuàng)建glusterfs-svc.yaml
[root@k8s-master gfs]# vim glusterfs-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: glusterfs
namespace: default
spec:
ports:
- port: 49152
protocol: TCP
targetPort: 49152
sessionAffinity: None
type: ClusterIP
[root@k8s-master gfs]# kubectl create -f glusterfs-svc.yaml
service "glusterfs" created
3.創(chuàng)建gluster類型pv
#配置文件中的glusterfs名是用昨天創(chuàng)建好的qiangge
[root@k8s-master gfs]# vim glusterfs-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: gluster
labels:
type: glusterfs
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteMany
glusterfs:
endpoints: "glusterfs"
path: "qiangge"
readOnly: false
[root@k8s-master gfs]# kubectl create -f glusterfs-pv.yaml
persistentvolume "gluster" created
[root@k8s-master gfs]# kubectl get pv
NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM REASON AGE
gluster 20Gi RWX Retain Available 5s
4.創(chuàng)建k8s_pvc.yaml
[root@k8s-master gfs]# vim k8s_pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: tomcat-mysql
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 20Gi
[root@k8s-master gfs]# kubectl create -f k8s_pvc.yaml
persistentvolumeclaim "tomcat-mysql" created
[root@k8s-master gfs]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESSMODES AGE
tomcat-mysql Bound gluster 20Gi RWX 9s
[root@k8s-master gfs]# kubectl get pvc -n default
NAME STATUS VOLUME CAPACITY ACCESSMODES AGE
tomcat-mysql Bound gluster 20Gi RWX 19s
5.改變?cè)瓉?lái)tomcat項(xiàng)目里面額mysql-rc.yaml
[root@k8s-master tomcat_demo]# cat mysql-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: mysql
spec:
replicas: 1
selector:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
volumes:
- name: mysql
persistentVolumeClaim:
claimName: tomcat-mysql
containers:
- name: mysql
volumeMounts:
- mountPath: /var/lib/mysql
name: mysql
image: 10.0.0.11:5000/mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: '123456'
[root@k8s-master tomcat_demo]# kubectl delete -f mysql-rc.yaml
replicationcontroller "mysql" deleted
[root@k8s-master tomcat_demo]# kubectl create -f mysql-rc.yaml
replicationcontroller "mysql" created
[root@k8s-master tomcat_demo]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
mysql-p2xkq 1/1 Running 0 10m 172.18.81.4 k8s-node1
myweb-41l9f 1/1 Running 1 16h
6.瀏覽器訪問(wèn)添加數(shù)據(jù)后刪除pod
#創(chuàng)建新數(shù)據(jù)后刪除mysql的pod會(huì)自動(dòng)生成新的pod
kubectl delete pod mysql-m3zm9
瀏覽器訪問(wèn)數(shù)據(jù)還在
二、與jenkins集成實(shí)現(xiàn)ci/cd
1.安裝gitlab并上傳代碼
#a:安裝
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.9.11-ce.0.el7.x86_64.rpm
yum localinstall gitlab-ce-11.9.11-ce.0.el7.x86_64.rpm -y
#b:配置
vim /etc/gitlab/gitlab.rb
external_url 'http://10.0.0.13'
prometheus_monitoring['enable'] = false
#c:應(yīng)用并啟動(dòng)服務(wù)
gitlab-ctl reconfigure
?
#使用瀏覽器訪問(wèn)http://10.0.0.13,修改root用戶密碼,創(chuàng)建project
?
#上傳代碼到git倉(cāng)庫(kù)
cd /srv/
rz -E
unzip xiaoniaofeifei.zip
rm -fr xiaoniaofeifei.zip
?
git config --global user.name "Administrator"
git config --global user.email "admin@example.com"
git init
git remote add origin http://10.0.0.13/root/xiaoniao.git
git add .
git commit -m "Initial commit"
git push -u origin master
2.安裝jenkins坐榆,并自動(dòng)構(gòu)建docker鏡像
1.安裝jenkins
先上車這些安裝包
cd /opt/
rz -E
rpm -ivh jdk-8u102-linux-x64.rpm
mkdir /app
tar xf apache-tomcat-8.0.27.tar.gz -C /app
rm -fr /app/apache-tomcat-8.0.27/webapps/*
mv jenkins.war /app/apache-tomcat-8.0.27/webapps/ROOT.war
tar xf jenkin-data.tar.gz -C /root
/app/apache-tomcat-8.0.27/bin/startup.sh
netstat -lntup
2.訪問(wèn)jenkins
訪問(wèn)http://10.0.0.12:8080/,默認(rèn)賬號(hào)密碼admin:123456
3.配置jenkins拉去gitlab代碼憑據(jù)
a:在jenkins上生成秘鑰對(duì)
ssh-keygen -t rsa
b:復(fù)制公鑰粘貼gitlab上
c.jenkins上創(chuàng)建全局憑據(jù)
添加私鑰
d.拉去代碼
e.編寫dockerfile并測(cè)試
#vim dockerfile
FROM 10.0.0.11:5000/nginx:1.13
add . /usr/share/nginx/html
測(cè)試dockerfile是否好用
docker build -t xiaoniao:v1 .
docker run -d -p 88:80 xiaoniao:v1
打開(kāi)瀏覽器測(cè)試訪問(wèn)xiaoniaofeifei的項(xiàng)目
上傳dockerfile到gitlab倉(cāng)庫(kù)
f.點(diǎn)擊jenkins立即構(gòu)建,自動(dòng)構(gòu)建docker鏡像并上傳到私有倉(cāng)庫(kù)
這里主節(jié)點(diǎn)的registry私有倉(cāng)庫(kù)必須是啟動(dòng)的
修改jenkins工程配置
變量是jenkins的內(nèi)置變量旧巾,根據(jù)拉去代碼生成版本
去私有倉(cāng)庫(kù)查看一下有沒(méi)有xiaoniao的鏡像,并查看版本
[root@k8s-master ~]# cd /opt/myregistry/docker/registry/v2/repositories/
[root@k8s-master repositories]# ls
busybox mysql nginx rhel7 tomcat-app wordpress xiaoniao
[root@k8s-master repositories]# ls xiaoniao/_manifests/tags/
v1
jenkins自動(dòng)部署應(yīng)用到k8s
kubectl -s 10.0.0.11:8080 get nodes可以遠(yuǎn)程執(zhí)行命令
if [ -f /tmp/xiaoniao.lock ];then
docker build -t 10.0.0.11:5000/xiaoniao:v$BUILD_ID .
docker push 10.0.0.11:5000/xiaoniao:v$BUILD_ID
kubectl -s 10.0.0.11:8080 set image -n xiaoniao deploy xiaoniao xiaoniao=10.0.0.11:5000/xiaoniao:v$BUILD_ID
echo "更新成功"
else
docker build -t 10.0.0.11:5000/xiaoniao:v$BUILD_ID .
docker push 10.0.0.11:5000/xiaoniao:v$BUILD_ID
kubectl -s 10.0.0.11:8080 create namespace xiaoniao
kubectl -s 10.0.0.11:8080 run xiaoniao -n xiaoniao --image=10.0.0.11:5000/xiaoniao:v$BUILD_ID --replicas=3 --record
kubectl -s 10.0.0.11:8080 expose -n xiaoniao deployment xiaoniao --port=80 --type=NodePort
port=`kubectl -s 10.0.0.11:8080 get svc -n xiaoniao|grep -oP '(?<=80:)\d+'`
echo "你的項(xiàng)目地址訪問(wèn)是http://10.0.0.13:$port"
touch /tmp/xiaoniao.lock
fi
把這個(gè)腳本添加到j(luò)enkins的工程配置敞映,更改代碼可以直接拉去并上線
jenkins一鍵回滾
kubectl -s 10.0.0.11:8080 rollout undo -n xiaoniao deployment xiaoniao