搭建及使用K8s集群

k8s集群部署springcloud 单应用

  • docker配置docker私库
  • docker build 镜像
  • k8s集群部署 镜像
  • 验证部署结果

1. docker配置docker私库

1.1 编辑/etc/sysconfig/docker

cat /etc/sysconfig/docker
# /etc/sysconfig/docker

# Modify these options if you want to change the way the docker daemon runs
OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false --insecure-registry docker.hbg.io '
if [ -z "${DOCKER_CERT_PATH}" ]; then
    DOCKER_CERT_PATH=/etc/docker
fi

ADD_REGISTRY='--insecure-registry docker.hbg.io'
# Do not add registries in this file anymore. Use /etc/containers/registries.conf
# from the atomic-registries package.
#

# docker-latest daemon can be used by starting the docker-latest unitfile.
# To use docker-latest client, uncomment below lines
#DOCKERBINARY=/usr/bin/docker-latest
#DOCKERDBINARY=/usr/bin/dockerd-latest
#DOCKER_CONTAINERD_BINARY=/usr/bin/docker-containerd-latest
#DOCKER_CONTAINERD_SHIM_BINARY=/usr/bin/docker-containerd-shim-latest

OPTIONS中添加 –insecure-registry docker.hbg.io
添加一行 ADD_REGISTRY=’–insecure-registry docker.hbg.io’
重启docker
docker.hbg.io 为私库地址

登录私库

# docker login docker.hbg.io
Username: ****
Password: 
Login Succeeded

部署时 如果私库需要登录则要创建一个secret ,如下:

kubectl create secret docker-registry docker.hbg.io --docker-server=docker.hbg.io --docker-username=【你的用户名】 --docker-password=【用户名密码】 --docker-email=【邮箱】

2. docker build 镜像

2.1 maven 打jar包

mvn install

2.2 编写Dockerfile

[root@master eureka]# pwd
/root/apps/eureka
[root@master eureka]# ll
总用量 39112
-rwxrwxrwx 1 root root      118 1012 11:26 build.sh
-rwxrwxrwx 1 root root 40039685 1012 13:00 cloud-eureka-server-1.0.0.jar
-rwxrwxrwx 1 root root      182 1012 11:22 Dockerfile
[root@master eureka]# cat  Dockerfile 
FROM java:8

VOLUME /tmp

ADD *.jar app.jar

RUN sh -c 'touch /app.jar'

ENV JAVA_OPTS=""

CMD exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom  -jar /app.jar
[root@master eureka]# cat build.sh 
docker build -t docker.hbg.io/cdchen/eureka:1.0.0 /root/apps/eureka && docker push docker.hbg.io/cdchen/eureka:1.0.0 

开始build

[root@master eureka]# ./build.sh 
Sending build context to Docker daemon 40.04 MB
Step 1 : FROM java:8
Trying to pull repository docker.io/library/java ... 
8: Pulling from docker.io/library/java
5040bd298390: Pull complete 
fce5728aad85: Pull complete 
76610ec20bf5: Pull complete 
60170fec2151: Pull complete 
e98f73de8f0d: Pull complete 
11f7af24ed9c: Pull complete 
49e2d6393f32: Pull complete 
bb9cdec9c7f3: Pull complete 
Digest: sha256:c1ff613e8ba25833d2e1940da0940c3824f03f802c449f3d1815a66b7f8c0e9d
 ---> d23bdf5b1b1b
Step 2 : VOLUME /tmp
 ---> Running in c922ea759ecd
 ---> 90e6bfb979be
Removing intermediate container c922ea759ecd
Step 3 : ADD *.jar app.jar
 ---> 22693545d892
Removing intermediate container 1eae1ee6e02e
Step 4 : RUN sh -c 'touch /app.jar'
 ---> Running in 4a3f242329a7
 ---> 7655007ab702
Removing intermediate container 4a3f242329a7
Step 5 : ENV JAVA_OPTS ""
 ---> Running in f2e6a37c62e9
 ---> 463cb7c6074d
Removing intermediate container f2e6a37c62e9
Step 6 : CMD exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom  -jar /app.jar
 ---> Running in 299051efb456
 ---> 54c5b40ddedf
Removing intermediate container 299051efb456
Successfully built 54c5b40ddedf
The push refers to a repository [docker.hbg.io/cdchen/eureka]
de6598ca7626: Pushed 
818fc746fd69: Pushed 
35c20f26d188: Pushed 
c3fe59dd9556: Pushed 
6ed1a81ba5b6: Pushed 
a3483ce177ce: Pushed 
ce6c8756685b: Pushed 
30339f20ced0: Pushed 
0eb22bfb707d: Pushed 
a2ae92ffcd29: Pushed 
1.0.0: digest: sha256:430e3db388ecc253acd64c97eae3b169732dc1c98e79e2c2f29c1aba679bbaec size: 2424

3. k8s部署镜像

3.1 部署

使用 k8s webUI部署 如下图
搭建及使用K8s集群_第1张图片
点击 SHOW ADVANCED OPTIONS选择拉取镜像的秘钥
找到image pull secret
搭建及使用K8s集群_第2张图片

点击DEPLOY

4. 验证部署结果

查看nodePort

# kubectl describe service/cloud-eureka-server
Name:                   cloud-eureka-server
Namespace:              default
Labels:                 app=cloud-eureka-server
                        version=1.0.0
Selector:               app=cloud-eureka-server,version=1.0.0
Type:                   LoadBalancer
IP:                     10.254.247.31
Port:                   tcp-8888-8888-c73x7     8888/TCP
NodePort:               tcp-8888-8888-c73x7     31427/TCP
Endpoints:              172.17.26.2:8888
Session Affinity:       None
No events.

在 node1 和 node2 中执行docker ps 看pod运行在哪一台上。

浏览器访问:
http://node2:31427/
如下图,证明启动成功
搭建及使用K8s集群_第3张图片

你可能感兴趣的:(kubernetes)