原来Kubernetes部署如此简单,看完全明白了
杰哥的IT之旅
共 17897字,需浏览 36分钟
·
2021-03-14 20:21
公众号关注“杰哥的IT之旅”,
选择“星标”,重磅干货,第一时间送达!
来自:51CTO博客,作者:wx5c1cfd6e22842
链接:https://blog.51cto.com/14143894/2437186
制作镜像
制作镜像分为三步:
第一步基础镜像,是基于哪个操作系统,比如CentOS 7或者其他的
第二步中间件镜像,比如服务镜像,跑的像Nginx服务,Tomcat服务
第三步项目镜像,它是服务镜像之上的,将你的项目打包进去,那么这个项目就能在你这个服务镜像里面运行了
控制器管理Pod
Deployment:无状态部署
StatefulSet:有状态部署
DaemonSet:守护进程部署
Job & CronJob:批处理
Pod数据持久化
启动时需要的初始数据,可以是配置文件
启动过程中产生的临时数据,该临时数据需要多个容器间共享
启动过程中产生的持久化数据
暴露应用
Service定义了Pod的逻辑集合和访问这个集合的策略
Service的引入为解决Pod的动态变化,提供了服务发现和负载均衡
使用CoreDNS解析Service名称
对外发布应用
[root@k8s-master ~]# yum -y install java-1.8.0-openjdk.x86_64 maven
[root@k8s-master ~]# java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
[root@k8s-master tomcat-java-demo-master]# ls
db Dockerfile LICENSE pom.xml README.md src
[root@k8s-master tomcat-java-demo-master]# vim Dockerfile
FROM lizhenliang/tomcat
LABEL maintainer zhaochengcheng
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war
<mirror>
<id>central</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
[root@k8s-master tomcat-java-demo-master]# mvn clean package -D maven test.skip=true
[root@k8s-master tomcat-java-demo-master]# ls
db Dockerfile LICENSE pom.xml README.md src target
[root@k8s-master tomcat-java-demo-master]# cd target/
[root@k8s-master target]# ls
classes generated-sources ly-simple-tomcat-0.0.1-SNAPSHOT ly-simple-tomcat-0.0.1-SNAPSHOT.war maven-archiver maven-status
[root@k8s-master tomcat-java-demo-master]# cd target/
[root@k8s-master target]# ls
classes ly-simple-tomcat-0.0.1-SNAPSHOT maven-archiver
generated-sources ly-simple-tomcat-0.0.1-SNAPSHOT.war maven-status
[root@k8s-master tomcat-java-demo-master]# docker build -t 192.168.30.24/library/java-demo:latest .
[root@k8s-master tomcat-java-demo-master]# docker login 192.168.30.24
Username: admin
Password:
Error response from daemon: Get https://192.168.30.24/v2/: dial tcp 192.168.30.24:443: connect: connection refused
[root@k8s-master java-demo]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["http://f1361db2.m.daocloud.io"],
"insecure-registries": ["192.168.30.24"]
}
[root@k8s-master tomcat-java-demo-master]# docker push 192.168.30.24/library/java-demo:latest
name: tomcat-java-demo
namespace: test
project:www
app:java-demo
[root@k8s-master java-demo]# docker tag 192.168.30.24/library/java-demo 192.168.30.24/tomcat-java-demo/java-demo
[root@k8s-master java-demo]# docker push 192.168.30.24/tomcat-java-demo/java-demo:latest
imagePullSecrets:
- name: registry-pull-secret
containers:
- name: tomcat
image: 192.168.30.24/tomcat-java-demo/java-demo:latest
[root@k8s-master java-demo]# vim namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: test
[root@k8s-master java-demo]# kubectl create -f namespace.yaml
namespace/test created
[root@k8s-master java-demo]# kubectl get ns
NAME STATUS AGE
default Active 22h
kube-node-lease Active 22h
kube-public Active 22h
kube-system Active 22h
test Active 5s
[root@k8s-master java-demo]# kubectl create secret docker-registry registry-pull-secret --docker-username=admin --docker-password=Harbor12345 --docker-email=111@qq.com --docker-server=192.168.30.24 -n test
secret/registry-pull-secret created
[root@k8s-master java-demo]# kubectl get ns
NAME STATUS AGE
default Active 23h
kube-node-lease Active 23h
kube-public Active 23h
kube-system Active 23h
test Active 6m39s
[root@k8s-master java-demo]# kubectl get secret
NAME TYPE DATA AGE
default-token-2vtgm kubernetes.io/service-account-token 3 23h
registry-pull-secret kubernetes.io/dockerconfigjson 1 46s
[root@k8s-master java-demo]# vim deployment.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: tomcat-java-demo
namespace: test
spec:
replicas: 3
selector:
matchLabels:
project: www
app: java-demo
template:
metadata:
labels:
project: www
app: java-demo
spec:
imagePullSecrets:
- name: registry-pull-secret
containers:
- name: tomcat
image: 192.168.30.24/tomcat-java-demo/java-demo:latest
imagePullPolicy: Always
ports:
- containerPort: 8080
name: web
protocol: TCP
resources:
requests:
cpu: 0.5
memory: 1Gi
limits:
cpu: 1
memory: 2Gi
livenessProbe:
httpGet:
path: /
port: 8080
initialDelaySeconds: 60
timeoutSeconds: 20
readinessProbe:
httpGet:
path: /
port: 8080
initialDelaySeconds: 60
timeoutSeconds: 20
[root@k8s-master java-demo]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
tomcat-java-demo-6d798c6996-fjjvk 1/1 Running 0 2m58s
tomcat-java-demo-6d798c6996-lbklf 1/1 Running 0 2m58s
tomcat-java-demo-6d798c6996-strth 1/1 Running 0 2m58s
[root@k8s-master java-demo]# vim service.yaml
apiVersion: v1
kind: Service
metadata:
name: tomcat-java-demo
namespace: test
spec:
selector:
project: www
app: java-demo
ports:
- name: web
port: 80
targetPort: 8080
[root@k8s-master java-demo]# kubectl get pod,svc -n test
NAME READY STATUS RESTARTS AGE
pod/tomcat-java-demo-6d798c6996-fjjvk 1/1 Running 0 37m
pod/tomcat-java-demo-6d798c6996-lbklf 1/1 Running 0 37m
pod/tomcat-java-demo-6d798c6996-strth 1/1 Running 0 37m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/tomcat-java-demo ClusterIP 10.1.175.191 <none> 80/TCP 19s
[root@k8s-master java-demo]# curl 10.1.175.191
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>把美女带回家应用案例</title>
<meta name="description" content="把美女带回家应用案例">
<meta name="keywords" content="index">
[root@k8s-master java-demo]# kubectl get pod -n ingress-nginx
NAME READY STATUS RESTARTS AGE
nginx-ingress-controller-g95pp 1/1 Running 0 3m6s
nginx-ingress-controller-wq6l6 1/1 Running 0 3m6s
[root@k8s-master java-demo]# kubectl get pod,svc -n test
NAME READY STATUS RESTARTS AGE
pod/tomcat-java-demo-6d798c6996-fjjvk 1/1 Running 0 53m
pod/tomcat-java-demo-6d798c6996-lbklf 1/1 Running 0 53m
pod/tomcat-java-demo-6d798c6996-strth 1/1 Running 0 53m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/tomcat-java-demo ClusterIP 10.1.175.191 <none> 80/TCP 16m
[root@k8s-master java-demo]# vim service.yaml
[root@k8s-master java-demo]# kubectl create -f ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: tomcat-java-demo
namespace: test
spec:
rules:
- host: java.maidikebi.com
http:
paths:
- path: /
backend:
serviceName: tomcat-java-demo
servicePort: 80
推荐阅读
评论