一步步学习k8s

Posted 中國颜值的半壁江山

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一步步学习k8s相关的知识,希望对你有一定的参考价值。

一步步学习k8s(三)

将apollo交付到K8S中,apollp是携程公司开源的软件

apollp官网:https://github.com/ctripcorp/apollo

安装数据库,Mariadb,版本要高于10.1,对应mysql版本要高于5.6

10.4.7.11机器上安装

[root@hdss7-11 yum.repos.d]# vim Mariadb.repo
[mariadb]
name=MariaDB
baseurl=https://mirrors.ustc.edu.cn/mariadb/yum/10.1/centos7-amd64/
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPN-GPG-KEY-MariaDB
gpgcheck=1

# 导入 GPG-KEY
[root@hdss7-11 yum.repos.d]# rpm --import https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
[root@hdss7-11 yum.repos.d]# yum list MariaDB-server --show-duplicates
# 安装Mariadb数据库
[root@hdss7-11 yum.repos.d]# yum -y install mariadb-server

修改配置文件

[root@hdss7-11 ~]# vim /etc/my.cnf.d/server.cnf
[mysqld]
character_set_server = utf8mb4
collation_server = utf8mb4_general_ci
init_connect = "SET NAMES 'utf8mb4'"
[root@hdss7-11 ~]# vim /etc/my.cnf.d/mysql-clients.cnf
[mysql]
default-character-set = utf8mb4

启动Mariadb

[root@hdss7-11 ~]# systemctl start mariadb
[root@hdss7-11 ~]# systemctl enable mariadb
[root@hdss7-11 ~]# netstat -luntp |grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      16555/mysqld

修改数据库管理员密码

[root@hdss7-11 ~]# mysqladmin -u root password
New password: 	(123456)
Confirm new password:	(123456)

执行数据库初始化的脚本

数据库脚本地址
或者到链接:https://pan.baidu.com/s/1V1S1nXvjIzKNXY5BDT0g7A
提取码:lszs

[root@hdss7-11 ~]# wget https://raw.githubusercontent.com/ctripcorp/apollo/v1.8.1/scripts/sql/apolloconfigdb.sql -O apolloconfigdb.sql
[root@hdss7-11 ~]# mysql -uroot -p < apolloconfigdb.sql

如下:在这里插入图片描述

给数据库用户授权

grant INSERT,DELETE,UPDATE,SELECT on ApolloconfigDB.* to apolloconfig@'10.4.7.%' identified by '123456';

修改初始数据

UPDATE ServerConfig SET ServerConfig.Value = "http://config.od.com/eureka" WHERE ServerConfig.Key="eureka.service.url"

修改dns解析配置

[root@hdss7-11 ~]# vim /var/named/od.com.zone
$ORIGIN od.com.
$TTL 600  ; 10 minutes
@       IN SOA  dns.od.com. dnsadmin.od.com. (
        2021032909 ; serial
        10800      ; refresh (3 hours)
        900        ; retry (15 minutes)
        604800     ; expire (1 week)
        86400      ; minimum (1 day)
        )
        NS   dns.od.com.
$TTL 60 ; 1 minute
dns                A    10.4.7.11
harbor             A    10.4.7.200
k8s-yaml           A    10.4.7.200
traefik            A    10.4.7.10
dashboard          A    10.4.7.10
zk1                A    10.4.7.11
zk2                A    10.4.7.12
ZK3                A    10.4.7.21
jenkins            A    10.4.7.10
config             A    10.4.7.10
mysql              A    10.4.7.11
[root@hdss7-11 ~]# systemctl restart named

安装

下载地址:https://github.com/ctripcorp/apollo/releases/tag/v1.8.1
上传到运维主机10.4.7.200的 /opt/src 目录下

[root@hdss7-200 src]# mkdir /home/dockerfile/apollo-configservice
[root@hdss7-200 src]# unzip -o apollo-configservice-1.8.1-github.zip -d /home/dockerfile/apollo-configservice/
[root@hdss7-200 apollo-configservice]# rm -f apollo-configservice-1.8.1-sources.jar
[root@hdss7-200 apollo-configservice]# cd config/
[root@hdss7-200 config]# vim application-github.properties
# DataSource
spring.datasource.url = jdbc:mysql://mysql.od.com:3306/ApolloConfigDB?characterEncoding=utf8
spring.datasource.username = apolloconfig
spring.datasource.password = 123456
[root@hdss7-200 apollo-configservice]# cd ../scripts/ && ls
shutdown.sh  startup.sh
# 替换掉startup.sh,到下面的网址复制新脚本的内容

替换脚本地址:https://github.com/ctripcorp/apollo/blob/1.8.1/scripts/apollo-on-kubernetes/apollo-config-server/scripts/startup-kubernetes.sh

[root@hdss7-200 scripts]# vim startup.sh
#!/bin/bash
SERVICE_NAME=apollo-configservice
## Adjust log dir if necessary
LOG_DIR=/opt/logs/apollo-config-server
## Adjust server port if necessary
SERVER_PORT=8080
APOLLO_CONFIG_SERVICE_NAME=$(hostname -i)
SERVER_URL="http://${APOLLO_CONFIG_SERVICE_NAME}:${SERVER_PORT}"

## Adjust memory settings if necessary
#export JAVA_OPTS="-Xms6144m -Xmx6144m -Xss256k -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=384m -XX:NewSize=4096m -XX:MaxNewSize=4096m -XX:SurvivorRatio=8"
export JAVA_OPTS="-Xms128m -Xmx128m -Xss256k -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=384m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:SurvivorRatio=8"

## Only uncomment the following when you are using server jvm
#export JAVA_OPTS="$JAVA_OPTS -server -XX:-ReduceInitialCardMarks"

########### The following is the same for configservice, adminservice, portal ###########
export JAVA_OPTS="$JAVA_OPTS -XX:ParallelGCThreads=4 -XX:MaxTenuringThreshold=9 -XX:+DisableExplicitGC -XX:+ScavengeBeforeFullGC -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+ExplicitGCInv
okesConcurrent -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Duser.timezone=Asia/Shanghai -Dclient.encoding.override=UTF-8 -Dfile.encoding=UTF-8 -Djava.security.egd=file:/dev/./urandom"export JAVA_OPTS="$JAVA_OPTS -Dserver.port=$SERVER_PORT -Dlogging.file.name=$LOG_DIR/$SERVICE_NAME.log -XX:HeapDumpPath=$LOG_DIR/HeapDumpOnOutOfMemoryError/"

# Find Java
if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then
    javaexe="$JAVA_HOME/bin/java"
elif type -p java > /dev/null 2>&1; then
    javaexe=$(type -p java)
elif [[ -x "/usr/bin/java" ]];  then
    javaexe="/usr/bin/java"
else
    echo "Unable to find Java"
    exit 1
fi

if [[ "$javaexe" ]]; then
    version=$("$javaexe" -version 2>&1 | awk -F '"' '/version/ {print $2}')
    version=$(echo "$version" | awk -F. '{printf("%03d%03d",$1,$2);}')
    # now version is of format 009003 (9.3.x)
    if [ $version -ge 011000 ]; then
        JAVA_OPTS="$JAVA_OPTS -Xlog:gc*:$LOG_DIR/gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
    elif [ $version -ge 010000 ]; then
        JAVA_OPTS="$JAVA_OPTS -Xlog:gc*:$LOG_DIR/gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
    elif [ $version -ge 009000 ]; then
        JAVA_OPTS="$JAVA_OPTS -Xlog:gc*:$LOG_DIR/gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
    else
        JAVA_OPTS="$JAVA_OPTS -XX:+UseParNewGC"
        JAVA_OPTS="$JAVA_OPTS -Xloggc:$LOG_DIR/gc.log -XX:+PrintGCDetails"
        JAVA_OPTS="$JAVA_OPTS -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=60 -XX:+CMSClassUn
loadingEnabled -XX:+CMSParallelRemarkEnabled -XX:CMSFullGCsBeforeCompaction=9 -XX:+CMSClassUnloadingEnabled  -XX:+PrintGCDateStamps -XX:+PrintGCApplicationConcurrentTime -XX:+PrintHeapAtGC -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=5M"    fi
fi

printf "$(date) ==== Starting ==== \\n"

cd `dirname $0`/..
chmod 755 $SERVICE_NAME".jar"
./$SERVICE_NAME".jar" start

rc=$?;

if [[ $rc != 0 ]];
then
    echo "$(date) Failed to start $SERVICE_NAME.jar, return code: $rc"
    exit $rc;
fi

tail -f /dev/null

创建Dockerfile

[root@hdss7-200 apollo-configservice]# vim Dockerfile
FROM stanleyws/jre8:8u112

ENV VERSION 1.8.1

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \\
    echo "Asia/Shanghai" > /etc/timezone

ADD apollo-configservice-${VERSION}.jar /apollo-configservice/apollo-configservice.jar
ADD config/ /apollo-configservice/config
ADD scripts/ /apollo-configservice/scripts

CMD ["/apollo-config-server/scripts/startup.sh"

生成镜像并上传到harbor仓库

[root@hdss7-200 apollo-configservice]# docker build . -t harbor.od.com/infra/apollo-configservice:v1.8.1
Sending build context to Docker daemon  71.74MB
Step 1/7 : FROM stanleyws/jre8:8u112
 ---> fa3a085d6ef1
Step 2/7 : ENV VERSION 1.8.1
 ---> Using cache
 ---> e60a14359025
Step 3/7 : RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&     echo "Asia/Shanghai" > /etc/timezone
 ---> Using cache
 ---> c67ca99f3db8
Step 4/7 : ADD apollo-configservice-${VERSION}.jar /apollo-configservice/apollo-configservice.jar
 ---> 3cc47a5a1b39
Step 5/7 : ADD config/ /apollo-configservice/config
 ---> cf2782caf3a9
Step 6/7 : ADD scripts/ /apollo-configservice/scripts
 ---> 69800ca9fadf
Step 7/7 : CMD ["/apollo-config-server/scripts/startup.sh"]
 ---> Running in c41bc1b34036
Removing intermediate container c41bc1b34036
 ---> de14004c60e8
Successfully built de14004c60e8
Successfully tagged harbor.od.com/infra/apollo-configservice:v1.8.1
[root@hdss7-200 apollo-configservice]# docker push harbor.od.com/infra/apollo-configservice:v1.8.1

创建资源配置清单

[root@hdss7-200 apollo-configservice]# cd /home/k8s-yaml/
[root@hdss7-200 k8s-yaml]# mkdir apollo-configservice

1、Deployment

[root@hdss7-200 apollo-configservice]# vim dp.yaml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: apollo-configservice
  namespace: infra
  labels:
    name: apollo-configservice
spec:
  replicas: 1
  selector:
    matchLabels :
      name: apollo-configservice
  template:
    metadata:
      labels:
        app: apollo-configservice
        name: apollo-configservice
    spec:
      volumes:
      - name: configmap-volume
        configMap:
          name: apollo-configservice-cm
      containers:
      - name: apollo-configservice
        image: harbor.od.com/infra/apollo-configservice:v1.8.1
        ports:
        - containerPort: 8080
          protocol: TCP
        volumeMounts:
        - name: configmap-volume
          mountPath: /apollo-configservice/config
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        imagePullPolicy: IfNotPresent
      imagePullSecrets:
      - name: harbor
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      securityContext:
        runAsUser: 0
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  revisionHistoryLimit: 7
  progressDeadlineSeconds: 600    

2、ConfigMap

[root@hdss7-200 apollo-configservice]# vim cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: apollo-configservice-cm
  namespace: infra
data:
  application-github.properties: |
    # DataSource
    spring.datasource.url = jdbc:mysql://mysql.od.com:3306/ApolloConfigDB?characterEncoding=utf8
    spring.datasource.username = apolloconfig
    spring.datasource.password = 123456
    eureka.service.url = http://config.od.com/eureka
  app.properties: |
    appId=100003171

3、Service

[root@hdss7-200 apollo-configservice]# vim svc.yaml
kind: Service
apiVersion: v1
metadata:
  name: apollo-configservice
  namespace: infra
spec:
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 8080
  selector :
    app: apollo-configservice

4、Ingress

[root@hdss7-200 apollo-configservice]# vim ingress.yaml
kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  name: apollo-configservice
  namespace: infra
spec:
  rules:
  - host: config.od.com
    http:
      paths:
      - path: /
        backend:
          serviceName: apollo-configservice
          servicePort: 8080

应用资源配置清单

[root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.od.com/apollo-configservice/cm.yaml
configmap/apollo-configservice-cm created
[root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.od.com/apollo-configservice/dp.yaml
deployment.extensions/apollo-configservice created
[root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.od.com/apollo-configservice/svc.yaml
service/apollo-configservice created
[root@hdss7-21 ~]# kubectl apply -f http://k8s-yaml.od.com/apollo-configservice/ingress.yaml
ingress.extensions/apollo-configservice created

以上是关于一步步学习k8s的主要内容,如果未能解决你的问题,请参考以下文章

Docker&K8s---跟我一步步部署K8s(二进制安装部署)

Docker&K8s---跟我一步步部署K8s(二进制安装部署)

k8s-高可用架构设计

SSH之旅一步步学习Hibernate框架:关于持久化

安卓一步步从基础到精通自学教程,纯实战,纯干货

No.5一步步学习vuejs之事件监听和组件