修改为3个pod
root@master:/opt/k8s-data/ymal/test# vim nginx-test-v1.yaml
replicas: 3 # tells deployment to run 2 pods matching the template
root@master:/opt/k8s-data/ymal/test# kubectl apply -f nginx-test-v1.yaml
查看增加ip地址的信息
root@master:/opt/k8s-data/ymal/test# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 192.168.200.197:6443,192.168.200.198:6443 5d6h
nginx-service 172.31.104.149:80,172.31.167.81:80,172.31.180.105:80 109m
root@master:/opt/k8s-data/dockerfile# docker pull centos
打标签
root@master:/opt/k8s-data/dockerfile# docker tag centos harbor.wyh.net/baseimages/centos:7.6.1810
上传镜像
root@master:/opt/k8s-data/dockerfile# docker push harbor.wyh.net/baseimages/centos:7.6.1810
进入到这个容器里
root@master:/opt/k8s-data/dockerfile# docker run -it --rm harbor.wyh.net/baseimages/centos:7.6.1810
查看系统版本
[root@5a595eedee14 /]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core)
重新打标签
root@master:/opt/k8s-data/dockerfile# docker tag centos harbor.wyh.net/baseimages/centos:8.0.1905
上传系统
root@master:/opt/k8s-data/dockerfile# docker push harbor.wyh.net/baseimages/centos:8.0.1905
编辑dockerfiel
root@master:/opt/k8s-data/dockerfile/system/centos# cat Dockerfile | grep ^[^#]
FROM harbor.wyh.net/baseimages/centos:7.6.1810
MAINTAINER Jack.Zhang 2973707860@qq.com
ADD filebeat-6.8.3-x86_64.rpm /tmp
RUN yum install -y /tmp/filebeat-6.8.3-x86_64.rpm vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop && rm -rf /etc/localtime /tmp/filebeat-6.8.3-x86_64.rpm && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && useradd nginx -u 2019 && useradd www -u 2020
root@master:/opt/k8s-data/dockerfile# docker pull centos:7.6.1810
打标签
root@master:/opt/k8s-data/ymal/test# docker tag 0f3e07c0138f harbor.wyh.net/baseimages/centos:7.6.1810
上传镜像
root@master:/opt/k8s-data/ymal/test# docker push harbor.wyh.net/baseimages/centos:7.6.1810
添加build脚本信息
root@master:/opt/k8s-data/dockerfile/system/centos# cat build-command.sh
#!/bin/bash
#docker build -t linux37-centos-base:7.6.1810 .
docker build -t harbor.wyh.net/baseimages/centos:7.6.1810 .
docker push harbor.wyh.net/baseimages/centos:7.6.1810
把镜像上传上去
进入到容器里
root@master:/opt/k8s-data/dockerfile/system/centos# docker run -it --rm harbor.wyh.net/baseimages/centos:7.6.1810 bash
修改java的镜像,添加javajdk
root@master:/opt/k8s-data/dockerfile/web/linux37/jdk-8u-212# vim Dockerfile
FROM harbor.wyh.net/baseimages/centos:7.6.1810
maintainer zhangshijie 2973707860@qq.com
ADD jdk-8u212-linux-x64.tar.gz /usr/local/src/
RUN ln -sv /usr/local/src/jdk1.8.0_212 /usr/local/jdk
ADD profile /etc/profile
ENV JAVA_HOME /usr/local/jdk
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/lib/
ENV PATH $PATH:$JAVA_HOME/bin
root@master:/opt/k8s-data/dockerfile/web/linux37/jdk-8u-212# vim build-command.sh
#!/bin/bash
docker build -t harbor.wyh.net/baseimages/linux37-centos7.6-jdk:8u212 .
docker push harbor.wyh.net/baseimages/linux37-centos7.6-jdk:8u212
root@master:/opt/k8s-data/dockerfile/web/linux37/jdk-8u-212# bash build-command.sh
添加tomcat的信息
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-8.5.45-base# cat Dockerfile | grep ^[^#]
FROM harbor.wyh.net/baseimages/linux37-centos7.6-jdk:8u212
ENV TZ "Asia/Shanghai"
ENV LANG en_US.UTF-8
ENV TERM xterm
ENV TOMCAT_MAJOR_VERSION 8
ENV TOMCAT_MINOR_VERSION 8.5.45
ENV CATALINA_HOME /apps/tomcat
ENV APP_DIR ${CATALINA_HOME}/webapps
ADD apache-tomcat-8.5.45.tar.gz /apps
RUN ln -sv /apps/apache-tomcat-8.5.45 /apps/tomcat
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-8.5.45-base# cat build-command.sh
#!/bin/bash
docker build -t harbor.wyh.net/linux37/linux37-centos7.6-tomcat:8.5.45 .
docker push harbor.wyh.net/linux37/linux37-centos7.6-tomcat:8.5.45
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-8.5.45-base# bash build-command.sh
配置tomcat的app1的信息
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app1# cat Dockerfile | grep ^[^#]
FROM harbor.wyh.net/linux37/linux37-centos7.6-tomcat:8.5.45
maintainer zhangshijie "2973707860@qq.com"
ADD app.tar.gz /data/tomcat/webapps
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD server.xml /apps/tomcat/conf/server.xml
ADD catalina.sh /apps/tomcat/bin/catalina.sh
RUN chown www.www /apps/tomcat /data/tomcat /apps/apache-tomcat-8.5.45 -R
EXPOSE 8080 8443
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app1# cat Dockerfile
FROM harbor.wyh.net/linux37/linux37-centos7.6-tomcat:8.5.45
maintainer zhangshijie "2973707860@qq.com"
ADD app.tar.gz /data/tomcat/webapps
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD server.xml /apps/tomcat/conf/server.xml
ADD catalina.sh /apps/tomcat/bin/catalina.sh
RUN chown www.www /apps/tomcat /data/tomcat /apps/apache-tomcat-8.5.45 -R
EXPOSE 8080 8443
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app1# cat build-command.sh
#/bin/bash
docker build -t harbor.wyh.net/linux37/linux37-tomcat:app1 .
docker push harbor.wyh.net/linux37/linux37-tomcat:app1
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app1# bash build-command.sh
运行可以访问
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app1# docker run -it --rm -p 8081:8080 harbor.wyh.net/linux37/linux37-tomcat:app1
Using CATALINA_BASE: /apps/tomcat
Using CATALINA_HOME: /apps/tomcat
Using CATALINA_TMPDIR: /apps/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar
Tomcat started.
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 eaedfa9c6317
image.png
修改APP2
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app2# vim build-command.sh
#!/bin/bash
docker build -t harbor.wyh.net/linux37/linux37-tomcat:app2 .
docker push harbor.wyh.net/linux37/linux37-tomcat:app2
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app2# cat Dockerfile | grep ^[^#]
FROM harbor.wyh.net/linux37/linux37-centos7.6-tomcat:8.5.45
maintainer zhangshijie "2973707860@qq.com"
ADD app.tar.gz /data/tomcat/webapps
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD server.xml /apps/tomcat/conf/server.xml
ADD catalina.sh /apps/tomcat/bin/catalina.sh
RUN chown www.www /apps/tomcat /data/tomcat /apps/apache-tomcat-8.5.45 -R
EXPOSE 8080 8443
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app2# bash build-command.sh
创建目录
root@master:/opt/k8s-data/ymal# mkdir web/linux37/tomcat-app2 -p
root@master:/opt/k8s-data/ymal# mkdir web/linux37/tomcat-app1
root@master:/opt/k8s-data/ymal# mkdir namespaces
编辑yaml文件
root@master:/opt/k8s-data/ymal/namespaces# cat linux37.yaml
apiVersion: v1 #API版本
kind: Namespace #类型为namespac
metadata:
name: linux37
root@master:/opt/k8s-data/ymal/namespaces# kubectl apply -f linux37.yaml
查看已经有一个命名空间
创建pod的文件
root@master:/opt/k8s-data/yaml/web/linux37/tomcat-app1# cat tomcat-app1.yaml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
labels:
app: linux37-tomcat-app1-deployment-label
name: linux37-tomcat-app1-deployment
namespace: linux37
spec:
replicas: 1
selector:
matchLabels:
app: linux37-tomcat-app1-selector
template:
metadata:
labels:
app: linux37-tomcat-app1-selector
spec:
containers:
- name: linux37-tomcat-app1-container
image: harbor.wyh.net/linux37/linux37-tomcat:app1
#command: ["/apps/tomcat/bin/run_tomcat.sh"]
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always
ports:
- containerPort: 8080
protocol: TCP
name: http
env:
- name: "password"
value: "123456"
- name: "age"
value: "18"
resources:
limits:
cpu: 2
memory: "2048Mi"
requests:
cpu: 0.5
memory: "1024Mi"
---
kind: Service
apiVersion: v1
metadata:
labels:
app: linux37-tomcat-app1-service-label
name: linux37-tomcat-app1-service
namespace: linux37
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
nodePort: 30002
selector:
app: linux37-tomcat-app1-selector
---
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
namespace: linux37
name: linux37-tomcat-app1-podautoscaler
labels:
app: linux37-tomcat-app1
version: v2beta1
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: linux37-tomcat-app1-deployment
minReplicas: 1
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 1
- type: Resource
resource:
name: memory
targetAverageValue: 1024Mi
root@master:/opt/k8s-data/yaml/web/linux37/tomcat-app1# kubectl apply -f tomcat-app1.yaml
deployment.extensions/linux37-tomcat-app1-deployment created
service/linux37-tomcat-app1-service created
horizontalpodautoscaler.autoscaling/linux37-tomcat-app1-podautoscaler created
查看ep的ip
root@master:/opt/k8s-data/yaml/web/linux37/tomcat-app1# kubectl get ep -n linux37
NAME ENDPOINTS AGE
linux37-tomcat-app1-service 172.31.104.152:8080 11h
查看pod 的ip地址
root@master:/opt/k8s-data/yaml/web/linux37/tomcat-app1# kubectl get pod -n linux37 -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
linux37-tomcat-app1-deployment-7d54894fdb-82xdl 1/1 Running 0 4m30s 172.31.104.152 192.168.200.207 <none> <none>
访问宿主机的30002地址
修改app2
root@master:/opt/k8s-data/yaml/web/linux37/tomcat-app2# cat tomcat-app2.yaml | grep ^[^#]
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
labels:
app: linux37-tomcat-app2-deployment-label
name: linux37-tomcat-app2-deployment
namespace: linux37
spec:
replicas: 1
selector:
matchLabels:
app: linux37-tomcat-app2-selector
template:
metadata:
labels:
app: linux37-tomcat-app2-selector
修改app2的名称
spec:
containers:
- name: linux37-tomcat-app2-container
image: harbor.wyh.net/linux37/linux37-tomcat:app2
#command: ["/apps/tomcat/bin/run_tomcat.sh"]
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always
ports:
- containerPort: 8080
protocol: TCP
name: http
env:
- name: "password"
value: "123456"
- name: "age"
value: "18"
resources:
limits:
#cpu: 2
#memory: "2048Mi"
requests:
#cpu: 0.5
#memory: "1024Mi"
---
kind: Service
apiVersion: v1
metadata:
labels:
app: linux37-tomcat-app2-service-label
name: linux37-tomcat-app2-service
namespace: linux37
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
nodePort: 30003
修改端口30003
不能同一个宿主机端口
selector:
app: linux37-tomcat-app2-selector
root@master:/opt/k8s-data/yaml/web/linux37/tomcat-app2# kubectl apply -f tomcat-app2.yaml
创建app2
root@master:/opt/k8s-data/yaml/web/linux37/tomcat-app2# kubectl get pod -n linux37 -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
linux37-tomcat-app1-deployment-7d54894fdb-82xdl 1/1 Running 0 27m 172.31.104.152 192.168.200.207 <none> <none>
linux37-tomcat-app2-deployment-6cfff645c-rsd8k 1/1 Running 0 17s 172.31.104.154 192.168.200.207 <none> <none>
修改nginx主页
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app2# vim app/index.html
linux37 tomcat app2
查看服务信息
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app2# kubectl get service -n linux37
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
linux37-tomcat-app1-service NodePort 10.20.188.42 <none> 80:30002/TCP 12h
linux37-tomcat-app2-service NodePort 10.20.127.235 <none> 80:30003/TCP 19m
查看deployment
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app2# kubectl get deployment -n linux37
NAME READY UP-TO-DATE AVAILABLE AGE
linux37-tomcat-app1-deployment 1/1 1 1 39m
linux37-tomcat-app2-deployment 1/1 1 1 12m
解析服务名称
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app2# kubectl exec busybox nslookup linux37-tomcat-app1-service.linux37.svc.linux37.local
Server: 10.20.254.254
Address 1: 10.20.254.254 kube-dns.kube-system.svc.linux37.local
Name: linux37-tomcat-app1-service.linux37.svc.linux37.local
Address 1: 10.20.188.42 linux37-tomcat-app1-service.linux37.svc.linux37.local
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app2# kubectl exec busybox nslookup linux37-tomcat-app2-service.linux37.svc.linux37.local
Server: 10.20.254.254
Address 1: 10.20.254.254 kube-dns.kube-system.svc.linux37.local
Name: linux37-tomcat-app2-service.linux37.svc.linux37.local
Address 1: 10.20.127.235 linux37-tomcat-app2-service.linux37.svc.linux37.local
查看 serviec信息
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app2# kubectl get service -n linux37
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
linux37-tomcat-app1-service NodePort 10.20.188.42 <none> 80:30002/TCP 12h
linux37-tomcat-app2-service NodePort 10.20.127.235 <none> 80:30003/TCP 19m
创建nginx目录
root@master:/opt/k8s-data/dockerfile/web/linux37# mkdir nginx
修改dockerfile
root@master:/opt/k8s-data/dockerfile/web/linux37/nginx# cat Dockerfile | grep ^[^#]
FROM harbor.wyh.net/baseimages/centos:7.6.18102
MAINTAINER Jack.Zhang 2973707860@qq.com
RUN yum install vim iotop bc gcc gcc-c++ glibc glibc-devel pcre \
pcre-devel openssl openssl-devel zip unzip zlib-devel net-tools \
lrzsz tree ntpdate telnet lsof tcpdump wget libevent libevent-devel \
bc systemd-devel bash-completion traceroute -y
ADD nginx-1.16.1.tar.gz /usr/local/src
RUN cd /usr/local/src/nginx-1.16.1 && ./configure --prefix=/apps/nginx && make && make install && ln -sv /apps/nginx/sbin/nginx /usr/bin/
RUN mkdir /data/nginx/html/linux37 -pv
ADD nginx.conf /apps/nginx/conf/nginx.conf
ADD app.tar.gz /data/nginx/html/linux37
RUN useradd nginx -s /sbin/nologin -M
RUN chown nginx.nginx -R /data/nginx/ /apps/nginx
EXPOSE 80 443
CMD ["nginx"]
修改nginx配置文件
root@master:/opt/k8s-data/dockerfile/web/linux37/nginx# cat nginx.conf | grep -v "#" | grep ^[^#]
user www;
worker_processes auto;
daemon off;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /linux37 {
root /data/nginx/html;
index index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
编辑build
root@master:/opt/k8s-data/dockerfile/web/linux37/nginx# vim build-command.sh
#!/bin/bash
#TAG=$1
docker build -t harbor.wyh.net/linux37/nginx:v1 .
docker push harbor.wyh.net/linux37/nginx:v1
root@master:/opt/k8s-data/dockerfile/web/linux37/nginx# bash build-command.sh
运行容器
root@master:/opt/k8s-data/dockerfile/web/linux37/nginx# docker run -it --rm -p 80:80 harbor.wyh.net/linux37/nginx:v1
image.png
访问页面
增加内容
root@master:/opt/k8s-data/dockerfile/web/linux37/nginx/app# echo "zengjia" >> index.html
压缩成tar.gz格式的,这样的就没有目录了
root@master:/opt/k8s-data/dockerfile/web/linux37/nginx/app# tar czvf app.tar.gz ./*
./1.jpg
./index.html
修改nginx的yaml信息
root@master:/opt/k8s-data/yaml/web/linux37/nginx# cat nginx.yaml | grep ^[^#]
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
labels:
app: linux37-nginx-deployment-label
name: linux37-nginx-deployment
namespace: linux37
spec:
replicas: 1
selector:
matchLabels:
app: linux37-nginx-selector
template:
metadata:
labels:
app: linux37-nginx-selector
spec:
containers:
- name: linux37-nginx-container
image: harbor.wyh.net/linux37/nginx:v1
imagePullPolicy: Always
ports:
- containerPort: 80
protocol: TCP
name: http
env:
- name: "password"
value: "123456"
- name: "age"
value: "18"
resources:
limits:
#cpu: 2
#memory: "2048Mi"
requests:
#cpu: 0.5
#memory: "1024Mi"
---
kind: Service
apiVersion: v1
metadata:
labels:
app: linux37-nginx-service-label
name: linux37-nginx-service
namespace: linux37
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
nodePort: 30004
selector:
app: linux37-nginx-selector
创建nginx的pod
root@master:/opt/k8s-data/yaml/web/linux37/nginx# kubectl apply -f nginx.yaml
deployment.extensions/linux37-nginx-deployment created
service/linux37-nginx-service created
访问宿主机的30004端口,然后转发到pod的80端口.这个就表示是nginx的pod在k8s里跑起来了
修改haprox的信息
root@haproxy1:~# vim /etc/haproxy/haproxy.cfg
listen linux37-nginx-80
bind 192.168.200.248:80
mode tcp
server 192.168.200.198 192.168.200.198:30004 check fall 3 rise 3 inter 3s
server 192.168.200.197 192.168.200.197:30004 check fall 3 rise 3 inter 3s
查看service的名称
root@master:/opt/k8s-data/yaml/web/linux37/nginx# kubectl get service -n linux37
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
linux37-nginx-service NodePort 10.20.239.180 <none> 80:30004/TCP 21m
linux37-tomcat-app1-service NodePort 10.20.188.42 <none> 80:30002/TCP 16h
linux37-tomcat-app2-service NodePort 10.20.127.235 <none> 80:30003/TCP 4h37m
root@master:/opt/k8s-data/dockerfile/web/linux37/nginx# cat nginx.conf | grep ^[^#] | grep -v "#"
user www;
worker_processes auto;
daemon off;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream tomcat_webserver {
server linux37-tomcat-app1-service.linux37.svc.linux37.local:80;
server linux37-tomcat-app2-service.linux37.svc.linux37.local:80;
这个是server的dns 解析,端口是容器的端口
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /linux37 {
root /data/nginx/html;
index index.html;
}
location /app {
proxy_pass http://tomcat_webserver;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
打v2的标签
root@master:/opt/k8s-data/dockerfile/web/linux37/nginx# cat build-command.sh
#!/bin/bash
TAG=$1
docker build -t harbor.wyh.net/linux37/nginx:${TAG} .
docker push harbor.wyh.net/linux37/nginx:${TAG}
传递参数v2上传镜像
root@master:/opt/k8s-data/dockerfile/web/linux37/nginx# bash build-command.sh v2
root@master:/opt/k8s-data/yaml/web/linux37/nginx# vim nginx.yaml
20 image: harbor.wyh.net/linux37/nginx:v2
更换镜像
更新pod
root@master:/opt/k8s-data/yaml/web/linux37/nginx# kubectl apply -f nginx.yaml
deployment.extensions/linux37-nginx-deployment configured
service/linux37-nginx-service unchanged
pod在哪个节点创建的就到哪个节点,看他的日志报错信息
修改文件之后改成v3
root@master:/opt/k8s-data/dockerfile/web/linux37/nginx# bash build-command.sh v3
修改为v3
root@master:/opt/k8s-data/yaml/web/linux37/nginx# vim nginx.yaml
20 image: harbor.wyh.net/linux37/nginx:v3
查看已经可以轮训调度到tomcat的8080端口了
image.png
网友评论